{"id":184,"date":"2025-06-26T14:17:54","date_gmt":"2025-06-26T06:17:54","guid":{"rendered":"https:\/\/mianka.xyz\/?p=184"},"modified":"2025-06-26T14:20:41","modified_gmt":"2025-06-26T06:20:41","slug":"pytorch","status":"publish","type":"post","link":"https:\/\/www.mianka.xyz\/?p=184","title":{"rendered":"PyTorch\u57fa\u7840\u5165\u95e8\u6559\u7a0b"},"content":{"rendered":"<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e00\u3001PyTorch\u7b80\u4ecb<\/h2>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">PyTorch\u662f\u4e00\u4e2a\u57fa\u4e8ePython\u7684\u79d1\u5b66\u8ba1\u7b97\u5305\uff0c\u4e3b\u8981\u7528\u9014\u5305\u62ecNumPy\u7684\u66ff\u4ee3\u54c1\uff08\u4ee5\u4f7f\u7528GPU\u548c\u5176\u4ed6\u52a0\u901f\u5668\u7684\u5f3a\u5927\u529f\u80fd\uff09\u4ee5\u53ca\u4e00\u4e2a\u7528\u4e8e\u5b9e\u73b0\u795e\u7ecf\u7f51\u7edc\u7684\u81ea\u52a8\u5fae\u5206\u5e93\u3002\u5b83\u5177\u6709\u52a8\u6001\u8ba1\u7b97\u56fe\u7684\u7279\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u8ba1\u7b97\u9700\u8981\u5b9e\u65f6\u6539\u53d8\u8ba1\u7b97\u56fe\uff0c\u4e0eTensorFlow\u7684\u9759\u6001\u8ba1\u7b97\u56fe\u4e0d\u540c\u3002\u7531\u4e8e\u5176\u7b80\u6d01\u6027\u548c\u7b26\u5408Python\u98ce\u683c\u7684\u7279\u70b9\uff0cPyTorch\u5728\u673a\u5668\u5b66\u4e60\u9886\u57df\u5f97\u5230\u4e86\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e8c\u3001\u5b89\u88c5PyTorch<\/h2>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u5728\u5f00\u59cb\u4f7f\u7528PyTorch\u4e4b\u524d\uff0c\u9700\u8981\u5148\u8fdb\u884c\u5b89\u88c5\u3002\u4ee5\u4e0b\u662f\u5b89\u88c5PyTorch\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<ol style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u5b89\u88c5Conda<\/span>\uff1a\u8bbf\u95eeConda\u5b98\u7f51\uff0c\u6839\u636e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u9009\u62e9\u5408\u9002\u7684\u5b89\u88c5\u5305\u8fdb\u884c\u4e0b\u8f7d\u548c\u5b89\u88c5\u3002<\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u521b\u5efaConda\u73af\u5883<\/span>\uff1a\u5728Anaconda Prompt\u4e2d\u8f93\u5165\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Conda\u73af\u5883\uff0c\u4f8b\u5982<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">conda create -n pytorch python=3.9<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u6fc0\u6d3b\u73af\u5883<\/span>\uff1a\u4f7f\u7528\u547d\u4ee4<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">conda activate pytorch<\/code>\u6fc0\u6d3b\u65b0\u521b\u5efa\u7684\u73af\u5883\u3002<\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u5b89\u88c5PyTorch<\/span>\uff1a\u5728\u6fc0\u6d3b\u7684Conda\u73af\u5883\u4e2d\uff0c\u6839\u636ePyTorch\u5b98\u7f51\u7684\u6307\u5bfc\uff0c\u9009\u62e9\u5408\u9002\u7684\u5b89\u88c5\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff0c\u4f8b\u5982<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">conda install pytorch torchvision torchaudio pytorch-cuda=12.4 -c pytorch -c nvidia<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u68c0\u9a8c\u5b89\u88c5<\/span>\uff1a\u8fdb\u5165Python\u73af\u5883\uff0c\u5bfc\u5165PyTorch\u5e76\u68c0\u9a8cCUDA\u53ef\u7528\u6027\uff0c\u4f8b\u5982<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">import torch<\/code>\u548c<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">torch.cuda.is_available()<\/code>\u3002<\/p>\n<\/li>\n<\/ol>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e09\u3001Tensor\u57fa\u7840\u64cd\u4f5c<\/h2>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">Tensor\uff08\u5f20\u91cf\uff09\u662fPyTorch\u4e2d\u7684\u57fa\u672c\u64cd\u4f5c\u5bf9\u8c61\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3aNumPy\u4e2d\u7684ndarray\u3002\u4ee5\u4e0b\u662fTensor\u7684\u4e00\u4e9b\u57fa\u7840\u64cd\u4f5c\uff1a<\/p>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">1. \u521b\u5efaTensor<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">Tensor\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u521b\u5efa\uff0c\u5305\u62ec\u76f4\u63a5\u4ece\u6570\u636e\u521b\u5efa\u3001\u4eceNumPy\u6570\u7ec4\u521b\u5efa\u3001\u4ece\u53e6\u4e00\u4e2aTensor\u521b\u5efa\uff0c\u4ee5\u53ca\u6307\u5b9a\u7279\u5b9a\u503c\u6216\u968f\u673a\u521b\u5efa\u3002<\/p>\n<pre class=\"prism-highlight prism-language-python\">import&nbsp;torch\nimport&nbsp;numpy&nbsp;as&nbsp;np\n\n#&nbsp;\u76f4\u63a5\u4ece\u6570\u636e\u521b\u5efa\ndata&nbsp;=&nbsp;[[1,&nbsp;2],&nbsp;[3,&nbsp;4]]\nx_data&nbsp;=&nbsp;torch.tensor(data)\n\n#&nbsp;\u4eceNumPy\u6570\u7ec4\u521b\u5efa\nnp_array&nbsp;=&nbsp;np.array(data)\nx_np&nbsp;=&nbsp;torch.from_numpy(np_array)\n\n#&nbsp;\u4ece\u53e6\u4e00\u4e2aTensor\u521b\u5efa\nx_ones&nbsp;=&nbsp;torch.ones_like(x_data)\nx_rand&nbsp;=&nbsp;torch.rand_like(x_data,&nbsp;dtype=torch.float)\n\n#&nbsp;\u6307\u5b9a\u7279\u5b9a\u503c\u6216\u968f\u673a\u521b\u5efa\nshape&nbsp;=&nbsp;(2,&nbsp;3)\nrand_tensor&nbsp;=&nbsp;torch.rand(shape)\nones_tensor&nbsp;=&nbsp;torch.ones(shape)\nzeros_tensor&nbsp;=&nbsp;torch.zeros(shape)<\/pre>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">2. Tensor\u7684\u5c5e\u6027<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u53ef\u4ee5\u901a\u8fc7<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">.shape()<\/code>\u6216<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">.size()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6Tensor\u7684\u5f62\u72b6\uff1b\u901a\u8fc7<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">.dtype()<\/code>\u6765\u67e5\u770b\u5176\u4e2d\u5b58\u653e\u7684\u6570\u636e\u7c7b\u578b\uff1b\u901a\u8fc7<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">.device()<\/code>\u65b9\u6cd5\u6765\u67e5\u770b\u8fd0\u7b97\u6240\u5904\u4e8e\u7684\u8bbe\u5907\u3002<\/p>\n<pre style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin-top: 8px; margin-bottom: 8px; scrollbar-width: none; font-size: 15px; overflow: auto; position: relative; color: rgb(6, 7, 31); background-color: rgb(253, 253, 254);\">pythontensor&nbsp;=&nbsp;torch.rand(3,&nbsp;4)print(f&quot;Shape&nbsp;of&nbsp;tensor:&nbsp;{tensor.shape}&quot;)print(f&quot;Datatype&nbsp;of&nbsp;tensor:&nbsp;{tensor.dtype}&quot;)print(f&quot;Device&nbsp;tensor&nbsp;is&nbsp;stored&nbsp;on:&nbsp;{tensor.device}&quot;)<\/pre>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">3. Tensor\u7684\u8fd0\u7b97<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">Tensor\u652f\u6301\u591a\u79cd\u8fd0\u7b97\uff0c\u5305\u62ec\u7d22\u5f15\u548c\u5207\u7247\u3001\u5f20\u91cf\u7684\u8fde\u63a5\u3001\u7b97\u6570\u64cd\u4f5c\uff08\u5982\u52a0\u6cd5\uff09\u3001\u77e9\u9635\u4e58\u6cd5\u4ee5\u53ca\u5143\u7d20\u7ea7\u4e58\u6cd5\u7b49\u3002<\/p>\n<pre class=\"prism-highlight prism-language-python\">tensor&nbsp;=&nbsp;torch.rand(3,&nbsp;4)\nprint(f&quot;Shape&nbsp;of&nbsp;tensor:&nbsp;{tensor.shape}&quot;)\nprint(f&quot;Datatype&nbsp;of&nbsp;tensor:&nbsp;{tensor.dtype}&quot;)\nprint(f&quot;Device&nbsp;tensor&nbsp;is&nbsp;stored&nbsp;on:&nbsp;{tensor.device}&quot;)<\/pre>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u56db\u3001\u81ea\u52a8\u5fae\u5206\uff08Autograd\uff09<\/h2>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">PyTorch\u7684\u81ea\u52a8\u5fae\u5206\u673a\u5236\u4f7f\u5f97\u53cd\u5411\u4f20\u64ad\u6c42\u68af\u5ea6\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002\u901a\u8fc7\u8bbe\u7f6e<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">requires_grad=True<\/code>\uff0c\u53ef\u4ee5\u4f7f\u5f97Tensor\u5728\u53c2\u4e0e\u8ba1\u7b97\u65f6\u81ea\u52a8\u8bb0\u5f55\u68af\u5ea6\u4fe1\u606f\u3002<\/p>\n<pre style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin-top: 8px; margin-bottom: 8px; scrollbar-width: none; font-size: 15px; overflow: auto; position: relative; color: rgb(6, 7, 31); background-color: rgb(253, 253, 254);\">pythonx&nbsp;=&nbsp;torch.tensor(2.0,&nbsp;requires_grad=True)y&nbsp;=&nbsp;x&nbsp;**&nbsp;2y.backward()&nbsp;&nbsp;#&nbsp;\u53cd\u5411\u4f20\u64ad\u6c42\u68af\u5ea6print(x.grad)&nbsp;&nbsp;#&nbsp;\u8f93\u51fa\u68af\u5ea6\u503c<\/pre>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e94\u3001\u6570\u636e\u52a0\u8f7d\uff08DataLoader\uff09<\/h2>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">PyTorch\u901a\u8fc7Dataset\u548cDataLoader\u8fdb\u884c\u6784\u5efa\u6570\u636e\u7ba1\u9053\u3002Dataset\u7528\u4e8e\u5c01\u88c5\u6570\u636e\u96c6\uff0c\u800cDataLoader\u5219\u7528\u4e8e\u5c06\u6570\u636e\u96c6\u5206\u6279\u52a0\u8f7d\u5230\u6a21\u578b\u4e2d\u8fdb\u884c\u8bad\u7ec3\u3002<\/p>\n<pre class=\"prism-highlight prism-language-python\">#&nbsp;\u7d22\u5f15\u548c\u5207\u7247\ntensor&nbsp;=&nbsp;torch.ones(4,&nbsp;4)\nprint(f&quot;First&nbsp;row:&nbsp;{tensor[0]}&quot;)\nprint(f&quot;First&nbsp;column:&nbsp;{tensor[:,&nbsp;0]}&quot;)\nprint(f&quot;Last&nbsp;column:&nbsp;{tensor[...,&nbsp;-1]}&quot;)\ntensor[:,&nbsp;1]&nbsp;=&nbsp;0\nprint(tensor)\n\n#&nbsp;\u5f20\u91cf\u7684\u8fde\u63a5\nt1&nbsp;=&nbsp;torch.cat([tensor,&nbsp;tensor,&nbsp;tensor],&nbsp;dim=1)\nprint(t1)\n\n#&nbsp;\u7b97\u6570\u64cd\u4f5c\uff08\u4ee5\u52a0\u6cd5\u4e3a\u4f8b\uff09\nt&nbsp;=&nbsp;torch.randn(4,&nbsp;4)\nprint(tensor)\nprint(t)\nprint(tensor&nbsp;+&nbsp;t)&nbsp;&nbsp;#&nbsp;\u76f4\u63a5\u76f8\u52a0\nprint(torch.add(tensor,&nbsp;t))&nbsp;&nbsp;#&nbsp;\u4f7f\u7528add\nresult&nbsp;=&nbsp;torch.empty(4,&nbsp;4)\ntorch.add(tensor,&nbsp;t,&nbsp;out=result)&nbsp;&nbsp;#&nbsp;\u6307\u5b9a\u8f93\u51fa\u5230result\u4e2d\nprint(result)\nt.add_(tensor)&nbsp;&nbsp;#&nbsp;inplace\u64cd\u4f5c\nprint(t)\n\n#&nbsp;\u77e9\u9635\u4e58\u6cd5\ny1&nbsp;=&nbsp;tensor&nbsp;@&nbsp;tensor.T&nbsp;&nbsp;#&nbsp;tensor.T&nbsp;\u662f&nbsp;tensor&nbsp;\u7684\u8f6c\u7f6e\ny3&nbsp;=&nbsp;torch.rand_like(y1)\ntorch.matmul(tensor,&nbsp;tensor.T,&nbsp;out=y3)\n\n#&nbsp;\u5143\u7d20\u7ea7\u4e58\u6cd5\n#&nbsp;\u8ba1\u7b97&nbsp;tensor&nbsp;\u7684\u5143\u7d20\u7ea7\u4e58\u79ef\uff0c\u5373\u6bcf\u4e2a\u4f4d\u7f6e\u4e0a\u7684\u5143\u7d20\u76f8\u4e58<\/pre>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u516d\u3001\u6a21\u578b\u6784\u5efa\u4e0e\u8bad\u7ec3<\/h2>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u5728PyTorch\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">Module<\/code>\u7c7b\u6765\u6784\u9020\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6a21\u578b\u6784\u5efa\u548c\u8bad\u7ec3\u793a\u4f8b\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">import&nbsp;torch.nn&nbsp;as&nbsp;nn\nimport&nbsp;torch.optim&nbsp;as&nbsp;optim\n\n#&nbsp;\u5b9a\u4e49\u6a21\u578b\nclass&nbsp;SimpleModel(nn.Module):\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__init__(self):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(SimpleModel,&nbsp;self).__init__()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.fc&nbsp;=&nbsp;nn.Linear(3,&nbsp;1)&nbsp;&nbsp;#&nbsp;\u8f93\u5165\u7279\u5f813\uff0c\u8f93\u51fa\u7279\u5f811\n\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;forward(self,&nbsp;x):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;self.fc(x)\n\n#&nbsp;\u521d\u59cb\u5316\u6a21\u578b\u3001\u635f\u5931\u51fd\u6570\u548c\u4f18\u5316\u5668\nmodel&nbsp;=&nbsp;SimpleModel()\ncriterion&nbsp;=&nbsp;nn.MSELoss()&nbsp;&nbsp;#&nbsp;\u5747\u65b9\u8bef\u5dee\u635f\u5931\u51fd\u6570\noptimizer&nbsp;=&nbsp;optim.SGD(model.parameters(),&nbsp;lr=0.01)&nbsp;&nbsp;#&nbsp;\u968f\u673a\u68af\u5ea6\u4e0b\u964d\u4f18\u5316\u5668\n\n#&nbsp;\u8bad\u7ec3\u6a21\u578b\nfor&nbsp;epoch&nbsp;in&nbsp;range(100):\n&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;batch_data,&nbsp;batch_labels&nbsp;in&nbsp;dataloader:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u524d\u5411\u4f20\u64ad\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;outputs&nbsp;=&nbsp;model(batch_data)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loss&nbsp;=&nbsp;criterion(outputs,&nbsp;batch_labels.float())&nbsp;&nbsp;#&nbsp;\u5047\u8bbe\u6807\u7b7e\u662f\u6d6e\u70b9\u6570\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u53cd\u5411\u4f20\u64ad\u548c\u4f18\u5316\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optimizer.zero_grad()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loss.backward()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;optimizer.step()\n\n&nbsp;&nbsp;&nbsp;&nbsp;print(f&#39;Epoch&nbsp;{epoch+1},&nbsp;Loss:&nbsp;{loss.item()}&#39;)<\/pre>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e03\u3001\u7ed3\u8bed<\/h2>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86PyTorch\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u5305\u62ecTensor\u7684\u521b\u5efa\u548c\u64cd\u4f5c\u3001\u81ea\u52a8\u5fae\u5206\u673a\u5236\u3001\u6570\u636e\u52a0\u8f7d\u4ee5\u53ca\u6a21\u578b\u6784\u5efa\u548c\u8bad\u7ec3\u7b49\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u4f60\u5feb\u901f\u5165\u95e8PyTorch\uff0c\u5e76\u5f00\u542f\u4f60\u7684\u6df1\u5ea6\u5b66\u4e60\u4e4b\u65c5\u3002<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"><!--autointro--><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-184","post","type-post","status-publish","format-standard","hentry","category-pythonbiji"],"_links":{"self":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=184"}],"version-history":[{"count":0,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/184\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}