{"id":950407,"date":"2024-12-27T00:34:53","date_gmt":"2024-12-26T16:34:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/950407.html"},"modified":"2024-12-27T00:34:54","modified_gmt":"2024-12-26T16:34:54","slug":"python-%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e5%bc%a0%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/950407.html","title":{"rendered":"python \u5982\u4f55\u8f93\u51fa\u5f20\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085014\/9ae8b8ce-0a49-4cc0-8676-566f893d155d.webp\" alt=\"python \u5982\u4f55\u8f93\u51fa\u5f20\u91cf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8f93\u51fa\u5f20\u91cf\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528print\u51fd\u6570\u3001\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3001\u4f7f\u7528PyTorch\u81ea\u5e26\u7684\u663e\u793a\u51fd\u6570\u7b49\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7print\u51fd\u6570\u76f4\u63a5\u8f93\u51fa\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u5f20\u91cf\u8f93\u51fa\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001PRINT\u51fd\u6570\u8f93\u51fa\u5f20\u91cf<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u5c31\u662f\u901a\u8fc7print\u51fd\u6570\u8f93\u51fa\u5f20\u91cf\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u51e0\u4e4e\u6240\u6709\u7684\u5f20\u91cf\u683c\u5f0f\uff0c\u4e0d\u7ba1\u662fTensorFlow\u8fd8\u662fPyTorch\u7684\u5f20\u91cf\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>PyTorch\u5f20\u91cf\u8f93\u51fa<\/strong><\/p>\n<\/p>\n<p><p>\u5728PyTorch\u4e2d\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528print\u51fd\u6570\u6765\u8f93\u51fa\u5f20\u91cf\u7684\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import torch<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a2x2\u7684\u5f20\u91cf<\/strong><\/h2>\n<p>tensor = torch.tensor([[1, 2], [3, 4]])<\/p>\n<p>print(tensor)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\uff0c\u9002\u5408\u5feb\u901f\u67e5\u770b\u5f20\u91cf\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>TensorFlow\u5f20\u91cf\u8f93\u51fa<\/strong><\/p>\n<\/p>\n<p><p>\u5728TensorFlow\u4e2d\uff0c\u7531\u4e8e\u8ba1\u7b97\u56fe\u7684\u7279\u6027\uff0c\u76f4\u63a5print\u4e00\u4e2a\u5f20\u91cf\u4f1a\u5f97\u5230\u5b83\u7684\u63cf\u8ff0\uff0c\u800c\u4e0d\u662f\u5177\u4f53\u7684\u503c\u3002\u56e0\u6b64\uff0c\u9700\u8981\u5728\u4f1a\u8bdd\u4e2d\u8fd0\u884c\u5f20\u91cf\u624d\u80fd\u8f93\u51fa\u5176\u503c\uff08\u5728TensorFlow 1.x\u4e2d\uff09\u3002\u5728TensorFlow 2.x\u4e2d\uff0c\u7531\u4e8eEager Execution\u662f\u9ed8\u8ba4\u542f\u7528\u7684\uff0c\u5f20\u91cf\u7684\u8f93\u51fa\u4e0ePyTorch\u7c7b\u4f3c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a2x2\u7684\u5f20\u91cf<\/strong><\/h2>\n<p>tensor = tf.constant([[1, 2], [3, 4]])<\/p>\n<p>print(tensor)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728TensorFlow 2.x\u4e2d\uff0c\u5f20\u91cf\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7print\u51fd\u6570\u8f93\u51fa\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u8f6c\u6362\u4e3aNUMPY\u6570\u7ec4<\/p>\n<\/p>\n<p><p>\u5c06\u5f20\u91cf\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u4e5f\u662f\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5229\u7528NumPy\u7684\u51fd\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u65f6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>PyTorch\u5f20\u91cf\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/p>\n<\/p>\n<p><p>\u5728PyTorch\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>.numpy()<\/code>\u65b9\u6cd5\u5c06\u5f20\u91cf\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f20\u91cf\u9700\u8981\u5728CPU\u4e0a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import torch<\/p>\n<p>tensor = torch.tensor([[1, 2], [3, 4]])<\/p>\n<p>numpy_array = tensor.numpy()<\/p>\n<p>print(numpy_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>TensorFlow\u5f20\u91cf\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/p>\n<\/p>\n<p><p>\u5728TensorFlow 2.x\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>.numpy()<\/code>\u65b9\u6cd5\u5c06\u5f20\u91cf\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>tensor = tf.constant([[1, 2], [3, 4]])<\/p>\n<p>numpy_array = tensor.numpy()<\/p>\n<p>print(numpy_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u65b9\u4fbf\uff0c\u56e0\u4e3aNumPy\u6570\u7ec4\u5728Python\u6570\u636e\u79d1\u5b66\u751f\u6001\u7cfb\u7edf\u4e2d\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5f20\u91cf\u5e93\u81ea\u5e26\u7684\u663e\u793a\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u4e00\u4e9b\u6df1\u5ea6\u5b66\u4e60\u5e93\u63d0\u4f9b\u4e86\u4e13\u95e8\u7684\u51fd\u6570\u6765\u683c\u5f0f\u5316\u548c\u663e\u793a\u5f20\u91cf\uff0c\u8fd9\u4e9b\u51fd\u6570\u901a\u5e38\u53ef\u4ee5\u63d0\u4f9b\u6bd4print\u66f4\u597d\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>PyTorch\u7684format\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>PyTorch\u4e2d\u6ca1\u6709\u4e13\u95e8\u7684\u683c\u5f0f\u5316\u663e\u793a\u51fd\u6570\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7<code>torch.set_printoptions<\/code>\u8bbe\u7f6e\u6253\u5370\u9009\u9879\u4ee5\u8c03\u6574\u8f93\u51fa\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import torch<\/p>\n<h2><strong>\u8bbe\u7f6e\u6d6e\u70b9\u6570\u7cbe\u5ea6<\/strong><\/h2>\n<p>torch.set_printoptions(precision=2)<\/p>\n<p>tensor = torch.tensor([[1.12345, 2.12345], [3.12345, 4.12345]])<\/p>\n<p>print(tensor)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>TensorFlow\u7684tf.print\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5728TensorFlow\u4e2d\uff0c<code>tf.print<\/code>\u53ef\u4ee5\u7528\u4e8e\u66f4\u7075\u6d3b\u7684\u5f20\u91cf\u8f93\u51fa\uff0c\u800c\u4e0d\u4f1a\u4e2d\u65ad\u8ba1\u7b97\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>tensor = tf.constant([[1, 2], [3, 4]])<\/p>\n<p>tf.print(tensor)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>tf.print<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u683c\u5f0f\u5316\u9009\u9879\uff0c\u5e76\u4e14\u4e0d\u4f1a\u5e72\u6270\u8ba1\u7b97\u56fe\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528\u53ef\u89c6\u5316\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u5f20\u91cf\uff0c\u7279\u522b\u662f\u5728\u8fdb\u884c\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u8bad\u7ec3\u65f6\uff0c\u53ef\u89c6\u5316\u5de5\u5177\u53ef\u4ee5\u63d0\u4f9b\u66f4\u6df1\u5165\u7684\u6d1e\u5bdf\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>TensorBoard<\/strong><\/p>\n<\/p>\n<p><p>TensorBoard\u662fTensorFlow\u63d0\u4f9b\u7684\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\uff0c\u7528\u4e8e\u53ef\u89c6\u5316\u5f20\u91cf\u548c\u6a21\u578b\u8bad\u7ec3\u8fc7\u7a0b\u3002\u5b83\u53ef\u4ee5\u663e\u793a\u6807\u91cf\u3001\u56fe\u50cf\u3001\u76f4\u65b9\u56fe\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>from tensorflow import summary<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65e5\u5fd7\u76ee\u5f55<\/strong><\/h2>\n<p>logdir = &quot;logs\/&quot;<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u7f16\u5199\u5668<\/strong><\/h2>\n<p>writer = summary.create_file_writer(logdir)<\/p>\n<p>tensor = tf.constant([[1, 2], [3, 4]])<\/p>\n<h2><strong>\u4f7f\u7528TensorBoard\u8bb0\u5f55\u5f20\u91cf<\/strong><\/h2>\n<p>with writer.as_default():<\/p>\n<p>    summary.scalar(&quot;my_scalar&quot;, tf.reduce_sum(tensor), step=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>Matplotlib\u548cSeaborn<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e8c\u7ef4\u5f20\u91cf\uff08\u77e9\u9635\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u548cSeaborn\u8fdb\u884c\u53ef\u89c6\u5316\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u56fe\u50cf\u3001\u70ed\u56fe\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import torch<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import seaborn as sns<\/p>\n<p>tensor = torch.tensor([[1, 2], [3, 4]])<\/p>\n<p>sns.heatmap(tensor.numpy(), annot=True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u53ef\u89c6\u5316\u77e9\u9635\u5f62\u5f0f\u7684\u5f20\u91cf\uff0c\u5e2e\u52a9\u7406\u89e3\u6570\u636e\u7684\u5206\u5e03\u548c\u53d8\u5316\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8f93\u51fa\u5f20\u91cf\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<strong>print\u51fd\u6570<\/strong>\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u9002\u5408\u5feb\u901f\u68c0\u67e5\uff1b<strong>NumPy\u8f6c\u6362<\/strong>\u63d0\u4f9b\u4e86\u4e0ePython\u751f\u6001\u7cfb\u7edf\u7684\u826f\u597d\u517c\u5bb9\u6027\uff1b<strong>\u5e93\u81ea\u5e26\u7684\u663e\u793a\u51fd\u6570<\/strong>\u53ef\u4ee5\u63d0\u4f9b\u66f4\u597d\u7684\u683c\u5f0f\u5316\u9009\u9879\uff0c\u800c<strong>\u53ef\u89c6\u5316\u5de5\u5177<\/strong>\u5219\u5728\u5206\u6790\u590d\u6742\u5f20\u91cf\u65f6\u975e\u5e38\u6709\u7528\u3002\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u548c\u5229\u7528\u5f20\u91cf\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u548c\u8f93\u51fa\u5f20\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528NumPy\u6216TensorFlow\u7b49\u5e93\u6765\u521b\u5efa\u548c\u8f93\u51fa\u5f20\u91cf\u3002\u4f7f\u7528NumPy\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>np.array()<\/code>\u51fd\u6570\u521b\u5efa\u5f20\u91cf\uff0c\u7136\u540e\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u8f93\u51fa\u3002\u5bf9\u4e8eTensorFlow\uff0c\u53ef\u4ee5\u901a\u8fc7<code>tf.constant()<\/code>\u521b\u5efa\u5f20\u91cf\uff0c\u5e76\u4f7f\u7528<code>tf.print()<\/code>\u8fdb\u884c\u8f93\u51fa\u3002\u786e\u4fdd\u5728\u5f00\u59cb\u4e4b\u524d\u5b89\u88c5\u76f8\u5173\u5e93\u3002<\/p>\n<p><strong>\u5f20\u91cf\u7684\u7ef4\u5ea6\u548c\u5f62\u72b6\u5982\u4f55\u5f71\u54cd\u8f93\u51fa\u7ed3\u679c\uff1f<\/strong><br \/>\u5f20\u91cf\u7684\u7ef4\u5ea6\u548c\u5f62\u72b6\u76f4\u63a5\u5f71\u54cd\u5176\u8f93\u51fa\u683c\u5f0f\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>shape<\/code>\u5c5e\u6027\u6765\u67e5\u770b\u5f20\u91cf\u7684\u5f62\u72b6\u3002\u4f8b\u5982\uff0c\u4e8c\u7ef4\u5f20\u91cf\u7684\u8f93\u51fa\u5c06\u5c55\u793a\u4e3a\u77e9\u9635\u5f62\u5f0f\uff0c\u800c\u4e09\u7ef4\u5f20\u91cf\u4f1a\u4ee5\u591a\u5c42\u7ed3\u6784\u663e\u793a\u3002\u8fd9\u79cd\u7ed3\u6784\u5316\u7684\u8f93\u51fa\u65b9\u5f0f\u5728\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\u975e\u5e38\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u6570\u636e\u7684\u5206\u5e03\u548c\u7279\u5f81\u3002<\/p>\n<p><strong>\u5728\u8f93\u51fa\u5f20\u91cf\u65f6\uff0c\u5982\u4f55\u8c03\u6574\u683c\u5f0f\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\uff1f<\/strong><br \/>\u5728\u8f93\u51fa\u5f20\u91cf\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.set_printoptions()<\/code>\u6765\u8c03\u6574\u8f93\u51fa\u683c\u5f0f\uff0c\u4f8b\u5982\u8bbe\u7f6e\u7cbe\u5ea6\u6216\u9650\u5236\u8f93\u51fa\u5143\u7d20\u7684\u6570\u91cf\u3002\u5728TensorFlow\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>tf.print()<\/code>\u7684\u53c2\u6570\u6765\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u5305\u62ec\u63a7\u5236\u8f93\u51fa\u7684\u6700\u5c0f\u548c\u6700\u5927\u503c\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u8f93\u51fa\u66f4\u52a0\u6e05\u6670\uff0c\u4fbf\u4e8e\u5206\u6790\u548c\u8c03\u8bd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8f93\u51fa\u5f20\u91cf\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528print\u51fd\u6570\u3001\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u3001\u4f7f\u7528PyTorch\u81ea\u5e26 [&hellip;]","protected":false},"author":3,"featured_media":950408,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/950407"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=950407"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/950407\/revisions"}],"predecessor-version":[{"id":950409,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/950407\/revisions\/950409"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/950408"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=950407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=950407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=950407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}