{"id":6750,"date":"2025-09-29T14:06:05","date_gmt":"2025-09-29T06:06:05","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/?p=6750"},"modified":"2025-09-29T14:06:17","modified_gmt":"2025-09-29T06:06:17","slug":"yolo%e5%85%a5%e9%97%a8%e6%95%99%e7%a8%8b%ef%bc%88%e7%95%aa%e5%a4%96%ef%bc%89%ef%bc%9a%e4%b8%80%e6%96%87%e6%90%9e%e5%ae%9apytorch","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/python\/yolo%e5%85%a5%e9%97%a8%e6%95%99%e7%a8%8b%ef%bc%88%e7%95%aa%e5%a4%96%ef%bc%89%ef%bc%9a%e4%b8%80%e6%96%87%e6%90%9e%e5%ae%9apytorch\/","title":{"rendered":"YOLO\u5165\u95e8\u6559\u7a0b\uff08\u756a\u5916\uff09\uff1a\u4e00\u6587\u641e\u5b9aPyTorch"},"content":{"rendered":"\n<p>\u5728\u8fdb\u884c\u4e0b\u4e00\u6b65\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981Python\u548cPytorch\u7684\u57fa\u7840\u3002\u4e8e\u662f\u5728\u7f51\u4e0a\u627e\u4e86\u4e00\u7bc7\u5f88\u5b8c\u6574\u7684\u535a\u6587\uff0c\u5185\u5bb9\u7cfb\u7edf\u4e14\u4e30\u5bcc\u3002<\/p>\n\n\n\n<p>\u6211\u57fa\u4e8e\u6b64\u505a\u4e86\u4e00\u6b21\u6ce8\u91ca\uff0c\u54b1\u4eec\u5e94\u8be5\u4f9d\u9760\u8be5\u5927\u4f6c\u7684\u4f18\u79c0\u6587\u7ae0\u7b80\u5355\u5165\u4e2a\u95e8\u3002\u81f3\u4e8e\u540e\u9762\u7684\u54b1\u4eec\u53ef\u4ee5\u540e\u9762\u7528\u7684\u65f6\u5019\u8fb9\u7528\u8fb9\u8865\u5145\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Python\u90e8\u5206\u5c31\u4e0d\u5c55\u5f00\u4e86\uff0c\u82b1\u4e00\u4e2a\u5c0f\u65f6\u719f\u6089\u4e00\u4e0b\uff0c\u5e94\u8be5\u5c31\u80fd\u4f7f\u7528\u4e86\u89e3\u4e86\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u63a8\u8350\u8d44\u6599\uff1ahttps:\/\/liaoxuefeng.com\/books\/python\/introduction\/index.html<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6587\u7ae0\u6807\u9898\uff1aPytorch \u6700\u5168\u5165\u95e8\u4ecb\u7ecd\uff0cPytorch\u5165\u95e8\u770b\u8fd9\u4e00\u7bc7\u5c31\u591f\u4e86<\/li>\n\n\n\n<li>\u5b66\u4e60\u6559\u6848\uff1ahttps:\/\/www.cnblogs.com\/xfuture\/p\/17593872.html<\/li>\n\n\n\n<li>\u4f5c\u8005\u59d3\u540d\uff1atechlead_krischang<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">1. Pytorch\u7b80\u4ecb<\/h1>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"474\" height=\"94\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060505968.png\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" alt=\"\" class=\"wp-image-6751 lazyload\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060505968.png 474w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060505968-300x59.png 300w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><noscript><img loading=\"lazy\" decoding=\"async\" width=\"474\" height=\"94\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060505968.png\" alt=\"\" class=\"wp-image-6751\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060505968.png 474w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060505968-300x59.png 300w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/noscript><\/figure>\n\n\n\n<p>\u5728\u8fd9\u4e00\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u4f1a\u5bf9Pytorch\u505a\u4e00\u4e2a\u7b80\u5355\u7684\u4ecb\u7ecd\uff0c\u5305\u62ec\u5b83\u7684\u5386\u53f2\u3001\u4f18\u70b9\u4ee5\u53ca\u4f7f\u7528\u573a\u666f\u7b49\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.1 Pytorch\u7684\u5386\u53f2<\/h2>\n\n\n\n<p>PyTorch\u662f\u4e00\u4e2a\u7531Facebook\u7684\u4eba\u5de5\u667a\u80fd\u7814\u7a76\u56e2\u961f\u5f00\u53d1\u7684\u5f00\u6e90\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u3002\u57282016\u5e74\u53d1\u5e03\u540e\uff0cPyTorch\u5f88\u5feb\u5c31\u56e0\u5176\u6613\u7528\u6027\u3001\u7075\u6d3b\u6027\u548c\u5f3a\u5927\u7684\u529f\u80fd\u800c\u5728\u79d1\u7814\u793e\u533a\u4e2d\u5e7f\u53d7\u6b22\u8fce\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecdPyTorch\u7684\u53d1\u5c55\u5386\u7a0b\u3002<\/p>\n\n\n\n<p>\u57282016\u5e74\uff0cFacebook\u7684AI\u7814\u7a76\u56e2\u961f\uff08FAIR\uff09\u516c\u5f00\u4e86PyTorch\uff0c\u5176\u65e8\u5728\u63d0\u4f9b\u4e00\u4e2a\u5feb\u901f\uff0c\u7075\u6d3b\u4e14\u52a8\u6001\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u3002PyTorch\u7684\u8bbe\u8ba1\u54f2\u5b66\u4e0ePython\u7684\u8bbe\u8ba1\u54f2\u5b66\u975e\u5e38\u76f8\u4f3c\uff1a\u6613\u8bfb\u6027\u548c\u7b80\u6d01\u6027\u4f18\u4e8e\u9690\u5f0f\u7684\u590d\u6742\u6027\u3002PyTorch\u7528Python\u8bed\u8a00\u7f16\u5199\uff0c\u662fPython\u7684\u4e00\u79cd\u6269\u5c55\uff0c\u8fd9\u4f7f\u5f97\u5176\u66f4\u6613\u4e8e\u5b66\u4e60\u548c\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>PyTorch\u5728\u8bbe\u8ba1\u4e0a\u53d6\u4e86\u4e00\u4e9b\u5927\u80c6\u7684\u51b3\u5b9a\uff0c\u5176\u4e2d\u6700\u91cd\u8981\u7684\u4e00\u9879\u5c31\u662f\u9009\u62e9\u52a8\u6001\u8ba1\u7b97\u56fe\uff08Dynamic Computation Graph\uff09\u4f5c\u4e3a\u5176\u6838\u5fc3\u3002\u52a8\u6001\u8ba1\u7b97\u56fe\u4e0e\u5176\u4ed6\u6846\u67b6\uff08\u4f8b\u5982TensorFlow\u548cTheano\uff09\u4e2d\u7684\u9759\u6001\u8ba1\u7b97\u56fe\u6709\u7740\u672c\u8d28\u7684\u533a\u522b\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u8fd0\u884c\u65f6\u6539\u53d8\u8ba1\u7b97\u56fe\u3002\u8fd9\u4f7f\u5f97PyTorch\u5728\u5904\u7406\u590d\u6742\u6a21\u578b\u65f6\u66f4\u5177\u7075\u6d3b\u6027\uff0c\u5e76\u4e14\u5bf9\u4e8e\u7814\u7a76\u4eba\u5458\u6765\u8bf4\uff0c\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u8c03\u8bd5\u3002<\/p>\n\n\n\n<p>\u5728\u53d1\u5e03\u540e\u7684\u51e0\u5e74\u91cc\uff0cPyTorch\u8fc5\u901f\u5728\u79d1\u7814\u793e\u533a\u4e2d\u53d6\u5f97\u4e86\u5e7f\u6cdb\u7684\u8ba4\u53ef\u3002\u57282019\u5e74\uff0cPyTorch\u53d1\u5e03\u4e861.0\u7248\u672c\uff0c\u5f15\u5165\u4e86\u4e00\u4e9b\u91cd\u8981\u7684\u65b0\u529f\u80fd\uff0c\u5305\u62ec\u652f\u6301ONNX\u3001\u4e00\u4e2a\u65b0\u7684\u5206\u5e03\u5f0f\u5305\u4ee5\u53ca\u5bf9C++\u7684\u524d\u7aef\u652f\u6301\u7b49\u3002\u8fd9\u4e9b\u529f\u80fd\u4f7f\u5f97PyTorch\u5728\u5de5\u4e1a\u754c\u7684\u5e94\u7528\u66f4\u52a0\u5e7f\u6cdb\uff0c\u540c\u65f6\u4e5f\u4fdd\u6301\u4e86\u5176\u5728\u79d1\u7814\u9886\u57df\u7684\u5f3a\u52b2\u52bf\u5934\u3002<\/p>\n\n\n\n<p>\u5230\u4e86\u8fd1\u4e24\u5e74\uff0cPyTorch\u5df2\u7ecf\u6210\u4e3a\u5168\u7403\u6700\u6d41\u884c\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u4e4b\u4e00\u3002\u5176\u5728GitHub\u4e0a\u7684\u661f\u6807\u6570\u91cf\u8d85\u8fc7\u4e8650k\uff0c\u88ab\u7528\u5728\u4e86\u5404\u79cd\u5404\u6837\u7684\u9879\u76ee\u4e2d\uff0c\u4ece\u6700\u65b0\u7684\u7814\u7a76\u8bba\u6587\u5230\u5927\u89c4\u6a21\u7684\u5de5\u4e1a\u5e94\u7528\u3002<\/p>\n\n\n\n<p>\u7efc\u4e0a\uff0cPyTorch\u7684\u53d1\u5c55\u5386\u7a0b\u662f\u4e00\u90e8\u5145\u6ee1\u521b\u65b0\u548c\u6311\u6218\u7684\u5386\u53f2\uff0c\u5b83\u4ece\u4e00\u4e2a\u79d1\u7814\u9879\u76ee\u53d1\u5c55\u6210\u4e3a\u4e86\u5168\u7403\u6700\u6d41\u884c\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u4e4b\u4e00\u3002\u5728\u672a\u6765\uff0c\u6211\u4eec\u6709\u7406\u7531\u76f8\u4fe1\uff0cPyTorch\u5c06\u4f1a\u5728\u6df1\u5ea6\u5b66\u4e60\u9886\u57df\u7ee7\u7eed\u53d1\u6325\u91cd\u8981\u7684\u4f5c\u7528\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.2 Pytorch\u7684\u4f18\u70b9<\/h2>\n\n\n\n<p>PyTorch\u4e0d\u4ec5\u662f\u6700\u53d7\u6b22\u8fce\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u4e4b\u4e00\uff0c\u800c\u4e14\u4e5f\u662f\u6700\u5f3a\u5927\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u4e4b\u4e00\u3002\u5b83\u6709\u8bb8\u591a\u72ec\u7279\u7684\u4f18\u70b9\uff0c\u4f7f\u5176\u5728\u5b66\u672f\u754c\u548c\u5de5\u4e1a\u754c\u90fd\u53d7\u5230\u5e7f\u6cdb\u7684\u5173\u6ce8\u548c\u4f7f\u7528\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u6765\u8be6\u7ec6\u5730\u63a2\u8ba8\u4e00\u4e0bPyTorch\u7684\u4f18\u70b9\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u52a8\u6001\u8ba1\u7b97\u56fe<\/h3>\n\n\n\n<p>PyTorch\u6700\u7a81\u51fa\u7684\u4f18\u70b9\u4e4b\u4e00\u5c31\u662f\u5b83\u4f7f\u7528\u4e86\u52a8\u6001\u8ba1\u7b97\u56fe\uff08Dynamic Computation Graphs\uff0cDCGs\uff09\uff0c\u4e0eTensorFlow\u548c\u5176\u4ed6\u6846\u67b6\u4f7f\u7528\u7684\u9759\u6001\u8ba1\u7b97\u56fe\u4e0d\u540c\u3002\u52a8\u6001\u8ba1\u7b97\u56fe\u5141\u8bb8\u4f60\u5728\u8fd0\u884c\u65f6\u66f4\u6539\u56fe\u7684\u884c\u4e3a\u3002\u8fd9\u4f7f\u5f97PyTorch\u975e\u5e38\u7075\u6d3b\uff0c\u5728\u5904\u7406\u4e0d\u786e\u5b9a\u6027\u6216\u590d\u6742\u6027\u65f6\u5177\u6709\u4f18\u52bf\uff0c\u56e0\u6b64\u975e\u5e38\u9002\u5408\u7814\u7a76\u548c\u539f\u578b\u8bbe\u8ba1\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u767d\u8bdd\u4e00\u4e0b<\/p>\n<\/blockquote>\n\n\n\n<p>PyTorch \u4e2d\u7684\u52a8\u6001\u8ba1\u7b97\u56fe\uff08Dynamic Computation Graph\uff09\u662f\u5176\u6838\u5fc3\u7279\u6027\uff0c\u5b83\u8ba9PyTorch\u7528\u8d77\u6765\u7279\u522b\u7075\u6d3b\u548c\u76f4\u89c2\u3002\u522b\u88ab\u8fd9\u4e2a\u540d\u5b57\u5413\u5230\uff0c\u5176\u5b9e\u5b83\u7684\u6982\u5ff5\u5f88\u76f4\u63a5\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udd0d \u52a8\u6001\u8ba1\u7b97\u56fe\uff1aPyTorch\u7684\u201c\u5373\u5174\u6d41\u7a0b\u56fe\u201d<\/p>\n<\/blockquote>\n\n\n\n<p>\u53ef\u4ee5\u628a\u52a8\u6001\u8ba1\u7b97\u56fe\u60f3\u8c61\u6210\u4e00\u4efd<strong>\u5373\u65f6\u8bb0\u5f55\u7684\u201c\u83dc\u8c31\u201d<\/strong>&nbsp;\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5047\u8bbe\u4f60\u5728\u505a\u4e00\u9053\u65b0\u83dc\uff0c\u4e0d\u9700\u8981\u4e25\u683c\u6309\u7167\u9884\u5148\u5199\u597d\u7684\u56fa\u5b9a\u83dc\u8c31\uff0c\u800c\u662f<strong>\u4e00\u8fb9\u505a\u4e00\u8fb9\u8bb0\u5f55\u6b65\u9aa4<\/strong>\u3002\u5148\u5207\u83dc\u3001\u7136\u540e\u5f00\u706b\u3001\u518d\u6839\u636e\u9505\u91cc\u60c5\u51b5\u51b3\u5b9a\u4e0b\u4e00\u6b65\u52a0\u4ec0\u4e48\u8c03\u6599\u3002<\/li>\n\n\n\n<li>\u8fd9\u91cc\u7684\u6bcf\u4e00\u4e2a\u6b65\u9aa4\uff08\u5207\u83dc\u3001\u7ffb\u7092\u3001\u8c03\u5473\uff09\u5c31\u50cf\u662f\u8ba1\u7b97\u56fe\u4e2d\u7684\u4e00\u4e2a<strong>\u8282\u70b9<\/strong>\uff0c\u6b65\u9aa4\u4e4b\u95f4\u7684\u987a\u5e8f\u548c\u4f9d\u8d56\u5173\u7cfb\u5c31\u662f\u8fde\u63a5\u8fd9\u4e9b\u8282\u70b9\u7684<strong>\u8fb9<\/strong>\u3002<\/li>\n\n\n\n<li>\u5728PyTorch\u4e2d\uff0c\u4f60\u7684<strong>\u6bcf\u4e00\u6b21\u8ba1\u7b97<\/strong>\uff08\u6bd4\u5982\u5f20\u91cf\u7684\u52a0\u6cd5\u3001\u4e58\u6cd5\uff09\u90fd\u4f1a\u5728\u8fd9\u4e2a\u201c\u83dc\u8c31\u201d\u4e0a<strong>\u5b9e\u65f6\u6dfb\u52a0\u4e00\u4e2a\u6b65\u9aa4<\/strong>\uff0c\u8ba1\u7b97\u56fe\u5c31\u5728\u4ee3\u7801\u8fd0\u884c\u65f6\u52a8\u6001\u6784\u5efa\u8d77\u6765 \u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e0eTensorFlow 1.x\u7b49\u6846\u67b6\u91c7\u7528\u7684<strong>\u9759\u6001\u8ba1\u7b97\u56fe<\/strong>\u4e0d\u540c\u3002\u9759\u6001\u56fe\u9700\u8981\u4f60<strong>\u63d0\u524d\u5b9a\u4e49\u597d\u6574\u4e2a\u8ba1\u7b97\u6d41\u7a0b<\/strong>\uff08\u5c31\u50cf\u5fc5\u987b\u4e25\u683c\u6309\u7167\u4e00\u4efd\u56fa\u5b9a\u4e0d\u53d8\u7684\u83dc\u8c31\u505a\u83dc\uff09\uff0c\u7136\u540e\u624d\u6267\u884c \u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2699\ufe0f \u52a8\u6001\u8ba1\u7b97\u56fe\u5982\u4f55\u5de5\u4f5c<\/p>\n<\/blockquote>\n\n\n\n<p>PyTorch\u901a\u8fc7&nbsp;<code>torch.autograd<\/code>&nbsp;\u6a21\u5757\u5b9e\u73b0\u81ea\u52a8\u5fae\u5206\u548c\u52a8\u6001\u8ba1\u7b97\u56fe\u3002\u5f53\u4f60\u8fdb\u884c\u524d\u5411\u8ba1\u7b97\u65f6\uff0c\u7cfb\u7edf\u4f1a\u52a8\u6001\u8ddf\u8e2a\u6240\u6709\u64cd\u4f5c\uff1b\u800c\u5728\u53cd\u5411\u4f20\u64ad\u65f6\uff0c\u5219\u4f9d\u636e\u8fd9\u4e2a\u52a8\u6001\u751f\u6210\u7684\u8ba1\u7b97\u56fe\u8fdb\u884c\u68af\u5ea6\u8ba1\u7b97\u3002\u5176\u5173\u952e\u7ec4\u4ef6\u548c\u6d41\u7a0b\u53ef\u4ee5\u6982\u62ec\u4e3a\u4e0b\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"274\" height=\"708\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060510154.png\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" alt=\"\" class=\"wp-image-6752 lazyload\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060510154.png 274w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060510154-116x300.png 116w\" sizes=\"auto, (max-width: 274px) 100vw, 274px\" \/><noscript><img loading=\"lazy\" decoding=\"async\" width=\"274\" height=\"708\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060510154.png\" alt=\"\" class=\"wp-image-6752\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060510154.png 274w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060510154-116x300.png 116w\" sizes=\"auto, (max-width: 274px) 100vw, 274px\" \/><\/noscript><\/figure>\n\n\n\n<p>\ud83d\udd11 \u5173\u952e\u7ec4\u4ef6\u4e0e\u6d41\u7a0b<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5f20\u91cf (Tensor) \u4e0e\u00a0<code>requires_grad=True<\/code><\/strong>\uff1a\u5f53\u4f60\u521b\u5efa\u4e00\u4e2a\u5f20\u91cf\u5e76\u8bbe\u7f6e\u00a0<code>requires_grad=True<\/code>\u00a0\u65f6\uff0cPyTorch \u5c31\u4f1a\u5f00\u59cb\u8ddf\u8e2a\u6240\u6709\u65bd\u52a0\u4e8e\u8be5\u5f20\u91cf\u4e4b\u4e0a\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<code>x = torch.tensor([2.0], requires_grad=True)<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u8fd0\u7b97 (Operations) \u4e0e Function \u8282\u70b9<\/strong>\uff1a\u6bcf\u4e00\u4e2a\u5bf9\u5f20\u91cf\u7684\u6570\u5b66\u8fd0\u7b97\uff08\u5982\u52a0\u6cd5\u3001\u4e58\u6cd5\uff09\u90fd\u4f1a\u5728\u8ba1\u7b97\u56fe\u4e2d\u521b\u5efa\u4e00\u4e2a\u00a0<strong><code>Function<\/code>\u00a0\u8282\u70b9<\/strong>\uff08\u4e5f\u79f0\u4e3a\u68af\u5ea6\u51fd\u6570\u00a0<code>grad_fn<\/code>\uff09\u3002\u8fd9\u4e2a\u8282\u70b9\u8bb0\u5f55\u4e86\u8fdb\u884c\u7684\u5177\u4f53\u64cd\u4f5c\uff0c\u5e76\u5728\u53cd\u5411\u4f20\u64ad\u65f6\u77e5\u9053\u5982\u4f55\u8ba1\u7b97\u8be5\u64cd\u4f5c\u7684\u5bfc\u6570\u3002<\/li>\n\n\n\n<li><strong>\u524d\u5411\u4f20\u64ad (Forward Pass)<\/strong>\uff1a\u524d\u5411\u4f20\u64ad\u8fc7\u7a0b\u5c31\u662f<strong>\u5373\u65f6<\/strong>\u6267\u884c\u8fd9\u4e9b\u8fd0\u7b97\u7684\u8fc7\u7a0b\u3002\u56fe\u4e5f\u968f\u4e4b\u6784\u5efa\u8d77\u6765\u3002\u4f8b\u5982\uff1a<code>y = x ** 2 # PyTorch \u4f1a\u8bb0\u5f55\u8fd9\u4e2a\u5e42\u8fd0\u7b97\u64cd\u4f5c\uff0c\u5e76\u521b\u5efa\u5bf9\u5e94\u7684\u8ba1\u7b97\u56fe\u8282\u70b9<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u53cd\u5411\u4f20\u64ad (Backward Pass)<\/strong>\uff1a\u5f53\u4f60\u5bf9\u6700\u7ec8\u7684\u8f93\u51fa\uff08\u901a\u5e38\u662f\u4e00\u4e2a\u635f\u5931\u503c\uff09\u8c03\u7528\u00a0<code>.backward()<\/code>\u00a0\u65b9\u6cd5\u65f6\uff0cPyTorch \u4f1a<strong>\u4ece\u8be5\u8f93\u51fa\u5f00\u59cb\uff0c\u6cbf\u7740\u52a8\u6001\u6784\u5efa\u7684\u8ba1\u7b97\u56fe\u53cd\u5411\u56de\u6eaf<\/strong>\uff0c\u5229\u7528\u94fe\u5f0f\u6cd5\u5219\u81ea\u52a8\u8ba1\u7b97\u6240\u6709\u00a0<code>requires_grad=True<\/code>\u00a0\u7684\u5f20\u91cf\u7684\u68af\u5ea6\u3002\u8fd9\u4e9b\u68af\u5ea6\u4f1a\u88ab\u7d2f\u79ef\u5230\u5404\u4e2a\u5f20\u91cf\u7684\u00a0<code>.grad<\/code>\u00a0\u5c5e\u6027\u4e2d\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u6765\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u76f4\u89c2\u611f\u53d7\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;torch<br><br><em># 1. \u521b\u5efa\u9700\u8981\u8ba1\u7b97\u68af\u5ea6\u7684\u5f20\u91cf<\/em><br>x = torch.tensor(3.0, requires_grad=True)<br><br><em># 2. \u6267\u884c\u4e00\u4e9b\u8ba1\u7b97\uff08\u52a8\u6001\u56fe\u5728\u6b64\u8fc7\u7a0b\u4e2d\u6784\u5efa\uff09<\/em><br>y = x *&nbsp;2&nbsp; &nbsp;<em># \u8fd9\u4e2a\u4e58\u6cd5\u64cd\u4f5c\u6210\u4e3a\u4e00\u4e2a\u8ba1\u7b97\u56fe\u8282\u70b9<\/em><br>z = y **&nbsp;2<em># \u8fd9\u4e2a\u5e73\u65b9\u64cd\u4f5c\u6210\u4e3a\u53e6\u4e00\u4e2a\u8282\u70b9<\/em><br><br><em># 3. \u8ba1\u7b97\u68af\u5ea6<\/em><br>z.backward()&nbsp;<br><br><em># 4. \u67e5\u770b\u68af\u5ea6<\/em><br>print(x.grad)&nbsp;<em># \u8f93\u51fa\uff1atensor(24.)&nbsp;<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em># \u56e0\u4e3a dz\/dx = dz\/dy * dy\/dx = 2*y * 2 = 2*(x*2)*2 = 4*x, \u5f53x=3\u65f6\u662f12<\/em><\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2728 \u4e3b\u8981\u4f18\u52bf\uff1a\u7075\u6d3b\u3001\u76f4\u89c2\u3001\u6613\u8c03\u8bd5<\/p>\n<\/blockquote>\n\n\n\n<p>\u52a8\u6001\u8ba1\u7b97\u56fe\u7ed9PyTorch\u5e26\u6765\u4e86\u8bb8\u591a\u663e\u8457\u4f18\u52bf\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6781\u9ad8\u7684\u7075\u6d3b\u6027<\/strong>\uff1a\u56e0\u4e3a\u56fe\u662f\u8fd0\u884c\u65f6\u52a8\u6001\u751f\u6210\u7684\uff0c\u6240\u4ee5\u4f60\u53ef\u4ee5<strong>\u8f7b\u677e\u5730\u4f7f\u7528\u6807\u51c6\u7684Python\u63a7\u5236\u6d41\u8bed\u53e5<\/strong>\uff08\u5982\u00a0<code>if-else<\/code>\u3001<code>for<\/code>\u00a0\u5faa\u73af\u3001<code>while<\/code>\u00a0\u5faa\u73af\uff09\u6765\u6539\u53d8\u6a21\u578b\u7684\u884c\u4e3a\u3002\u8fd9\u5bf9\u4e8e\u5904\u7406\u53d8\u957f\u5e8f\u5217\uff08\u5982\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4e2d\u4e0d\u540c\u957f\u5ea6\u7684\u53e5\u5b50\uff09\u6216\u6a21\u578b\u7ed3\u6784\u4e0d\u786e\u5b9a\u7684\u573a\u666f\u975e\u5e38\u6709\u7528\u3002<code><em># \u793a\u4f8b\uff1a\u52a8\u6001\u6539\u53d8\u8ba1\u7b97\u8def\u5f84<\/em><br>def\u00a0dynamic_flow(x):<br>\u00a0 \u00a0\u00a0if\u00a0x.sum() >\u00a00:<br>\u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0x *\u00a02<br>\u00a0 \u00a0\u00a0else:<br>\u00a0 \u00a0 \u00a0 \u00a0\u00a0return\u00a0x \/\u00a02<\/code><\/li>\n\n\n\n<li><strong>\u76f4\u89c2\u6613\u7528\uff0c\u8c03\u8bd5\u65b9\u4fbf<\/strong>\uff1a\u7531\u4e8e\u8ba1\u7b97\u56fe\u7684\u6784\u5efa\u4e0e\u4f60\u7f16\u5199\u4ee3\u7801\u7684\u987a\u5e8f\u4e00\u81f4\uff0c<strong>\u8c03\u8bd5\u8d77\u6765\u975e\u5e38\u76f4\u89c2<\/strong>\u3002\u4f60\u53ef\u4ee5\u50cf\u8c03\u8bd5\u666e\u901aPython\u7a0b\u5e8f\u4e00\u6837\u4f7f\u7528\u00a0<code>print<\/code>\u00a0\u8bed\u53e5\u6216\u8c03\u8bd5\u5668\uff08\u5982PDB\uff09\u5728\u4efb\u4f55\u5730\u65b9\u68c0\u67e5\u4e2d\u95f4\u53d8\u91cf\u7684\u503c\uff0c\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u66f4\u9002\u5408\u7814\u7a76\u5b9e\u9a8c<\/strong>\uff1a\u8fd9\u79cd\u7075\u6d3b\u6027\u548c\u6613\u8c03\u8bd5\u6027\u4f7f\u5f97\u7814\u7a76\u8005\u80fd\u591f\u5feb\u901f\u5c1d\u8bd5\u5404\u79cd\u65b0\u5947\u7684\u60f3\u6cd5\u548c\u6a21\u578b\u7ed3\u6784\uff0c\u65e0\u9700\u53d7\u9650\u4e8e\u56fa\u5b9a\u7684\u56fe\u7ed3\u6784\uff0c\u4ece\u800c\u52a0\u901f\u8fed\u4ee3\u548c\u5b9e\u9a8c\u8fc7\u7a0b\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u26a0\ufe0f \u4f7f\u7528\u6ce8\u610f\uff1a\u751f\u547d\u5468\u671f\u4e0e\u68af\u5ea6\u7ba1\u7406<\/p>\n<\/blockquote>\n\n\n\n<p>\u4f7f\u7528\u52a8\u6001\u8ba1\u7b97\u56fe\u65f6\uff0c\u6709\u51e0\u70b9\u9700\u8981\u4e86\u89e3\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8ba1\u7b97\u56fe\u7684\u751f\u547d\u5468\u671f<\/strong>\uff1a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<strong>\u6bcf\u6b21\u8c03\u7528\u00a0<code>.backward()<\/code>\u00a0\u540e\uff0c\u4e3a\u8282\u7701\u5185\u5b58\uff0c\u52a8\u6001\u8ba1\u7b97\u56fe\u4f1a\u81ea\u52a8\u91ca\u653e\u9500\u6bc1<\/strong>\u3002\u8fd9\u610f\u5473\u7740\u901a\u5e38\u4f60\u4e0d\u80fd\u5bf9\u540c\u4e00\u4e2a\u8ba1\u7b97\u56fe\u518d\u6b21\u8c03\u7528\u00a0<code>.backward()<\/code>\uff08\u9664\u975e\u5728\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\u8bbe\u7f6e\u4e86\u00a0<code>retain_graph=True<\/code>\u00a0\u53c2\u6570\uff09\u3002<\/li>\n\n\n\n<li><strong>\u53f6\u5b50\u8282\u70b9 (Leaf Tensors)\u00a0\uff1a\u7531\u7528\u6237\u76f4\u63a5\u521b\u5efa\u3001<code>requires_grad=True<\/code>\u00a0\u7684\u5f20\u91cf\u79f0\u4e3a\u53f6\u5b50\u8282\u70b9<\/strong>\uff08\u5982\u6a21\u578b\u53c2\u6570\u00a0<code>w<\/code>\u00a0\u548c\u00a0<code>b<\/code>\uff09\u3002\u901a\u8fc7\u8ba1\u7b97\u5f97\u5230\u7684\u5f20\u91cf\u79f0\u4e3a<strong>\u975e\u53f6\u5b50\u8282\u70b9<\/strong>\uff08\u5982\u4e2d\u95f4\u7279\u5f81\u00a0<code>y<\/code>\u00a0\u548c\u635f\u5931\u503c\u00a0<code>loss<\/code>\uff09\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<strong>\u53ea\u6709\u53f6\u5b50\u8282\u70b9\u7684\u68af\u5ea6\u4f1a\u88ab\u4fdd\u7559<\/strong>\u5728\u5b83\u4eec\u7684\u00a0<code>.grad<\/code>\u00a0\u5c5e\u6027\u4e2d\u3002<\/li>\n\n\n\n<li><strong>\u68af\u5ea6\u7d2f\u79ef<\/strong>\uff1a\u8c03\u7528\u00a0<code>.backward()<\/code>\u00a0\u65f6\uff0c\u8ba1\u7b97\u51fa\u7684\u68af\u5ea6\u4f1a<strong>\u7d2f\u52a0<\/strong>\u5230\u53f6\u5b50\u8282\u70b9\u7684\u00a0<code>.grad<\/code>\u00a0\u5c5e\u6027\u4e2d\uff08\u800c\u4e0d\u662f\u8986\u76d6\uff09\u3002\u8fd9\u610f\u5473\u7740\u5728\u6bcf\u6b21\u53cd\u5411\u4f20\u64ad\u524d\uff0c\u901a\u5e38\u9700\u8981<strong>\u624b\u52a8\u5c06\u68af\u5ea6\u6e05\u96f6<\/strong>\uff08\u4f7f\u7528\u00a0<code>optimizer.zero_grad()<\/code>\uff09\uff0c\u5426\u5219\u68af\u5ea6\u4f1a\u4e0d\u65ad\u7d2f\u52a0\uff0c\u5bfc\u81f4\u9519\u8bef\u7684\u66f4\u65b0\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udcca \u52a8\u6001\u56fe vs. \u9759\u6001\u56fe<\/p>\n<\/blockquote>\n\n\n\n<p>\u4e3a\u4e86\u66f4\u6e05\u6670\u5730\u7406\u89e3\u52a8\u6001\u8ba1\u7b97\u56fe\u7684\u7279\u70b9\uff0c\u4e0b\u8868\u5bf9\u6bd4\u4e86\u5176\u4e3b\u8981\u7279\u6027\u4e0e\u4f20\u7edf\u9759\u6001\u8ba1\u7b97\u56fe\u7684\u533a\u522b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>\u52a8\u6001\u8ba1\u7b97\u56fe (PyTorch)<\/th><th>\u9759\u6001\u8ba1\u7b97\u56fe (TensorFlow 1.x)<\/th><\/tr><\/thead><tbody><tr><td><strong>\u6784\u5efa\u65b9\u5f0f<\/strong><\/td><td>\u8fd0\u884c\u65f6\u52a8\u6001\u521b\u5efa<\/td><td>\u5148\u9884\u5148\u5b9a\u4e49\u5b8c\u6574\u8ba1\u7b97\u56fe\uff0c\u518d\u6267\u884c<\/td><\/tr><tr><td><strong>\u7075\u6d3b\u6027<\/strong><\/td><td>\u2b50\u2b50\u2b50\u2b50\u2b50 \u975e\u5e38\u9ad8\uff0c\u53ef\u968f\u65f6\u6539\u53d8\u7ed3\u6784\uff0c\u652f\u6301\u539f\u751f\u63a7\u5236\u6d41<\/td><td>\u2b50\u2b50 \u8f83\u4f4e\uff0c\u56fe\u7ed3\u6784\u56fa\u5b9a\u540e\u96be\u4ee5\u4fee\u6539<\/td><\/tr><tr><td><strong>\u8c03\u8bd5\u96be\u5ea6<\/strong><\/td><td>\u2b50\u2b50\u2b50\u2b50\u2b50 \u975e\u5e38\u5bb9\u6613\uff0c\u53ef\u4f7f\u7528\u6807\u51c6\u8c03\u8bd5\u5de5\u5177<\/td><td>\u2b50\u2b50 \u8f83\u96be\uff0c\u56fe\u9884\u5148\u5b9a\u4e49\uff0c\u8c03\u8bd5\u4e2d\u95f4\u7ed3\u679c\u4e0d\u4fbf<\/td><\/tr><tr><td><strong>\u6027\u80fd\u8868\u73b0<\/strong><\/td><td>\u2b50\u2b50\u2b50\u2b50 \u901a\u5e38\u4e0d\u9519\uff0c\u4f46\u6bcf\u6b21\u8fd0\u884c\u9700\u91cd\u5efa\u56fe<\/td><td>\u2b50\u2b50\u2b50\u2b50\u2b50 \u4f18\u5316\u540e\u6267\u884c\u6548\u7387\u53ef\u80fd\u66f4\u9ad8\uff0c\u5c24\u5176\u9002\u5408\u90e8\u7f72<\/td><\/tr><tr><td><strong>\u5178\u578b\u573a\u666f<\/strong><\/td><td>\u7814\u7a76\u3001\u539f\u578b\u8bbe\u8ba1\u3001\u9700\u8981\u7075\u6d3b\u6027\u7684\u4efb\u52a1<\/td><td>\u5927\u89c4\u6a21\u751f\u4ea7\u73af\u5883\u90e8\u7f72\u3001\u5bf9\u6027\u80fd\u6709\u6781\u81f4\u8981\u6c42\u7684\u56fa\u5b9a\u6a21\u578b<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udc8e \u603b\u7ed3<\/p>\n<\/blockquote>\n\n\n\n<p>\u603b\u800c\u8a00\u4e4b\uff0cPyTorch\u7684<strong>\u52a8\u6001\u8ba1\u7b97\u56fe\u662f\u4e00\u79cd\u5728\u8fd0\u884c\u65f6\u5373\u65f6\u6784\u5efa\u7684\u8ba1\u7b97\u56fe<\/strong>\u3002\u5b83\u5c31\u50cf\u4e00\u4efd<strong>\u5373\u65f6\u8bb0\u5f55\u7684\u201c\u83dc\u8c31\u201d<\/strong>\uff0c\u8ddf\u8e2a\u4f60\u7684\u6bcf\u4e00\u6b65\u8ba1\u7b97\u64cd\u4f5c\uff0c\u5e76\u5728\u9700\u8981\u8ba1\u7b97\u68af\u5ea6\u65f6\uff0c\u6cbf\u7740\u8fd9\u4efd\u201c\u83dc\u8c31\u201d\u53cd\u5411\u81ea\u52a8\u6c42\u5bfc\u3002<\/p>\n\n\n\n<p>\u5176<strong>\u7075\u6d3b\u6027<\/strong>\u548c<strong>\u76f4\u89c2\u6027<\/strong>\u4f7f\u5176\u6210\u4e3a\u7814\u7a76\u548c\u539f\u578b\u8bbe\u8ba1\u7684\u5f3a\u5927\u5de5\u5177\u3002\u7406\u89e3\u52a8\u6001\u8ba1\u7b97\u56fe\u662f\u7406\u89e3PyTorch\u81ea\u52a8\u5fae\u5206\u5982\u4f55\u5de5\u4f5c\u7684\u57fa\u7840\u3002\u968f\u7740\u4f60\u66f4\u6df1\u5165\u5730\u4f7f\u7528PyTorch\uff0c\u4f60\u4f1a\u8d8a\u6765\u8d8a\u4f53\u4f1a\u5230\u8fd9\u79cd\u201c\u5b9a\u4e49-by-run\u201d\u6a21\u5f0f\u5e26\u6765\u7684\u4fbf\u5229\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u6613\u7528\u6027<\/h3>\n\n\n\n<p>PyTorch\u88ab\u8bbe\u8ba1\u6210\u6613\u4e8e\u7406\u89e3\u548c\u4f7f\u7528\u3002\u5176API\u8bbe\u8ba1\u7684\u76f4\u89c2\u6027\u4f7f\u5f97\u5b66\u4e60\u548c\u4f7f\u7528PyTorch\u6210\u4e3a\u4e00\u4ef6\u975e\u5e38\u6109\u5feb\u7684\u4e8b\u60c5\u3002\u6b64\u5916\uff0c\u7531\u4e8ePyTorch\u4e0ePython\u7684\u6df1\u5ea6\u96c6\u6210\uff0c\u5b83\u5728Python\u7a0b\u5e8f\u5458\u4e2d\u975e\u5e38\u6d41\u884c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u6613\u4e8e\u8c03\u8bd5<\/h3>\n\n\n\n<p>\u7531\u4e8ePyTorch\u7684\u52a8\u6001\u6027\u548cPython\u6027\u8d28\uff0c\u8c03\u8bd5PyTorch\u7a0b\u5e8f\u53d8\u5f97\u76f8\u5f53\u76f4\u63a5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684\u6807\u51c6\u8c03\u8bd5\u5de5\u5177\uff0c\u5982PDB\u6216PyCharm\uff0c\u76f4\u63a5\u67e5\u770b\u6bcf\u4e2a\u64cd\u4f5c\u7684\u7ed3\u679c\u548c\u4e2d\u95f4\u53d8\u91cf\u7684\u72b6\u6001\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u5f3a\u5927\u7684\u793e\u533a\u652f\u6301<\/h3>\n\n\n\n<p>PyTorch\u7684\u793e\u533a\u975e\u5e38\u6d3b\u8dc3\u548c\u652f\u6301\u3002\u5b98\u65b9\u8bba\u575b\u3001GitHub\u3001Stack Overflow\u7b49\u5e73\u53f0\u4e0a\u6709\u5927\u91cf\u7684PyTorch\u7528\u6237\u548c\u5f00\u53d1\u8005\uff0c\u4f60\u53ef\u4ee5\u4ece\u4e2d\u627e\u5230\u5927\u91cf\u7684\u8d44\u6e90\u548c\u5e2e\u52a9\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u5e7f\u6cdb\u7684\u9884\u8bad\u7ec3\u6a21\u578b<\/h3>\n\n\n\n<p>PyTorch\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u9884\u8bad\u7ec3\u6a21\u578b\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8eResNet\uff0cVGG\uff0cInception\uff0cSqueezeNet\uff0cEfficientNet\u7b49\u7b49\u3002\u8fd9\u4e9b\u9884\u8bad\u7ec3\u6a21\u578b\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u5f00\u59cb\u65b0\u7684\u9879\u76ee\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u9ad8\u6548\u7684GPU\u5229\u7528<\/h3>\n\n\n\n<p>PyTorch\u53ef\u4ee5\u975e\u5e38\u9ad8\u6548\u5730\u5229\u7528NVIDIA\u7684CUDA\u5e93\u6765\u8fdb\u884cGPU\u8ba1\u7b97\u3002\u540c\u65f6\uff0c\u5b83\u8fd8\u652f\u6301\u5206\u5e03\u5f0f\u8ba1\u7b97\uff0c\u8ba9\u4f60\u53ef\u4ee5\u5728\u591a\u4e2aGPU\u6216\u670d\u52a1\u5668\u4e0a\u8bad\u7ec3\u6a21\u578b\u3002<\/p>\n\n\n\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0cPyTorch\u56e0\u5176\u6613\u7528\u6027\u3001\u7075\u6d3b\u6027\u3001\u4e30\u5bcc\u7684\u529f\u80fd\u4ee5\u53ca\u5f3a\u5927\u7684\u793e\u533a\u652f\u6301\uff0c\u5728\u6df1\u5ea6\u5b66\u4e60\u9886\u57df\u4e2d\u5907\u53d7\u6b22\u8fce\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.3 Pytorch\u7684\u4f7f\u7528\u573a\u666f<\/h2>\n\n\n\n<p>PyTorch\u7684\u5f3a\u5927\u529f\u80fd\u548c\u7075\u6d3b\u6027\u4f7f\u5176\u5728\u8bb8\u591a\u6df1\u5ea6\u5b66\u4e60\u5e94\u7528\u573a\u666f\u4e2d\u90fd\u80fd\u591f\u53d1\u6325\u91cd\u8981\u4f5c\u7528\u3002\u4ee5\u4e0b\u662fPyTorch\u5728\u5404\u79cd\u5e94\u7528\u4e2d\u7684\u4e00\u4e9b\u5178\u578b\u7528\u4f8b\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol class=\"wp-block-list\">\n<li>\u8ba1\u7b97\u673a\u89c6\u89c9<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<p>\u5728\u8ba1\u7b97\u673a\u89c6\u89c9\u65b9\u9762\uff0cPyTorch\u63d0\u4f9b\u4e86\u8bb8\u591a\u9884\u8bad\u7ec3\u6a21\u578b\uff08\u5982ResNet\uff0cVGG\uff0cInception\u7b49\uff09\u548c\u5de5\u5177\uff08\u5982TorchVision\uff09\uff0c\u53ef\u4ee5\u7528\u4e8e\u56fe\u50cf\u5206\u7c7b\u3001\u7269\u4f53\u68c0\u6d4b\u3001\u8bed\u4e49\u5206\u5272\u548c\u56fe\u50cf\u751f\u6210\u7b49\u4efb\u52a1\u3002\u8fd9\u4e9b\u9884\u8bad\u7ec3\u6a21\u578b\u548c\u5de5\u5177\u5927\u5927\u7b80\u5316\u4e86\u5f00\u53d1\u8ba1\u7b97\u673a\u89c6\u89c9\u5e94\u7528\u7684\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u81ea\u7136\u8bed\u8a00\u5904\u7406<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<p>\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u9886\u57df\uff0cPyTorch\u7684\u52a8\u6001\u8ba1\u7b97\u56fe\u7279\u6027\u4f7f\u5f97\u5176\u975e\u5e38\u9002\u5408\u5904\u7406\u53d8\u957f\u8f93\u5165\uff0c\u8fd9\u5bf9\u4e8e\u8bb8\u591aNLP\u4efb\u52a1\u6765\u8bf4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u540c\u65f6\uff0cPyTorch\u4e5f\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684NLP\u5de5\u5177\u548c\u9884\u8bad\u7ec3\u6a21\u578b\uff08\u5982Transformer\uff0cBERT\u7b49\uff09\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5904\u7406\u6587\u672c\u5206\u7c7b\u3001\u60c5\u611f\u5206\u6790\u3001\u547d\u540d\u5b9e\u4f53\u8bc6\u522b\u3001\u673a\u5668\u7ffb\u8bd1\u548c\u95ee\u7b54\u7cfb\u7edf\u7b49\u4efb\u52a1\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\u751f\u6210\u5bf9\u6297\u7f51\u7edc<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<p>\u751f\u6210\u5bf9\u6297\u7f51\u7edc\uff08GANs\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\uff0c\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u56fe\u50cf\u751f\u6210\u3001\u56fe\u50cf\u5230\u56fe\u50cf\u7684\u8f6c\u6362\u3001\u6837\u5f0f\u8fc1\u79fb\u548c\u6570\u636e\u589e\u5f3a\u7b49\u4efb\u52a1\u3002PyTorch\u7684\u7075\u6d3b\u6027\u4f7f\u5f97\u5176\u975e\u5e38\u9002\u5408\u5f00\u53d1\u548c\u8bad\u7ec3GAN\u6a21\u578b\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"4\" class=\"wp-block-list\">\n<li>\u5f3a\u5316\u5b66\u4e60<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<p>\u5f3a\u5316\u5b66\u4e60\u662f\u4e00\u79cd\u5b66\u4e60\u65b9\u6cd5\uff0c\u5176\u4e2d\u667a\u80fd\u4f53\u901a\u8fc7\u4e0e\u73af\u5883\u7684\u4ea4\u4e92\u6765\u5b66\u4e60\u5982\u4f55\u6267\u884c\u4efb\u52a1\u3002PyTorch\u7684\u52a8\u6001\u8ba1\u7b97\u56fe\u548c\u6613\u4e8e\u4f7f\u7528\u7684API\u4f7f\u5f97\u5176\u5728\u5b9e\u73b0\u5f3a\u5316\u5b66\u4e60\u7b97\u6cd5\u65f6\u8868\u73b0\u51fa\u6781\u9ad8\u7684\u6548\u7387\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"5\" class=\"wp-block-list\">\n<li>\u65f6\u5e8f\u6570\u636e\u5206\u6790<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<p>\u5728\u5904\u7406\u65f6\u5e8f\u6570\u636e\u7684\u4efb\u52a1\u4e2d\uff0c\u5982\u8bed\u97f3\u8bc6\u522b\u3001\u65f6\u95f4\u5e8f\u5217\u9884\u6d4b\u7b49\uff0cPyTorch\u7684\u52a8\u6001\u8ba1\u7b97\u56fe\u4e3a\u5904\u7406\u53ef\u53d8\u957f\u5ea6\u7684\u5e8f\u5217\u6570\u636e\u63d0\u4f9b\u4e86\u4fbf\u5229\u3002\u540c\u65f6\uff0cPyTorch\u63d0\u4f9b\u4e86\u5305\u62ecRNN\u3001LSTM\u3001GRU\u5728\u5185\u7684\u5404\u79cd\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u3002<\/p>\n\n\n\n<p>\u603b\u7684\u6765\u8bf4\uff0cPyTorch\u51ed\u501f\u5176\u5f3a\u5927\u7684\u529f\u80fd\u548c\u6781\u9ad8\u7684\u7075\u6d3b\u6027\uff0c\u5728\u8bb8\u591a\u6df1\u5ea6\u5b66\u4e60\u7684\u5e94\u7528\u573a\u666f\u4e2d\u90fd\u80fd\u591f\u53d1\u6325\u91cd\u8981\u4f5c\u7528\u3002\u65e0\u8bba\u4f60\u662f\u5728\u7814\u7a76\u65b0\u7684\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\uff0c\u8fd8\u662f\u5728\u5f00\u53d1\u5b9e\u9645\u7684\u6df1\u5ea6\u5b66\u4e60\u5e94\u7528\uff0cPyTorch\u90fd\u80fd\u591f\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">2. Pytorch\u57fa\u7840<\/h1>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"641\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060516262-1024x641.png\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" alt=\"\" class=\"wp-image-6753 lazyload\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060516262-1024x641.png 1024w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060516262-300x188.png 300w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060516262-768x481.png 768w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060516262.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><noscript><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"641\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060516262-1024x641.png\" alt=\"\" class=\"wp-image-6753\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060516262-1024x641.png 1024w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060516262-300x188.png 300w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060516262-768x481.png 768w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060516262.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/noscript><\/figure>\n\n\n\n<p>\u5728\u6211\u4eec\u5f00\u59cb\u6df1\u5165\u4f7f\u7528PyTorch\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u5148\u4e86\u89e3\u4e00\u4e9b\u57fa\u7840\u6982\u5ff5\u548c\u64cd\u4f5c\u3002\u8fd9\u4e00\u90e8\u5206\u5c06\u6db5\u76d6PyTorch\u7684\u57fa\u7840\uff0c\u5305\u62ectensor\u64cd\u4f5c\u3001GPU\u52a0\u901f\u4ee5\u53ca\u81ea\u52a8\u6c42\u5bfc\u673a\u5236\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.1 Tensor\u64cd\u4f5c<\/h2>\n\n\n\n<p>Tensor\u662fPyTorch\u4e2d\u6700\u57fa\u672c\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f60\u53ef\u4ee5\u5c06\u5176\u89c6\u4e3a\u591a\u7ef4\u6570\u7ec4\u6216\u8005\u77e9\u9635\u3002PyTorch tensor\u548cNumPy array\u975e\u5e38\u76f8\u4f3c\uff0c\u4f46\u662ftensor\u8fd8\u53ef\u4ee5\u5728GPU\u4e0a\u8fd0\u7b97\uff0c\u800cNumPy array\u5219\u53ea\u80fd\u5728CPU\u4e0a\u8fd0\u7b97\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u57fa\u672c\u7684tensor\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165PyTorch\u5e93\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684tensor\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u521b\u5efatensor\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u521b\u5efa\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u76845x3\u77e9\u9635<br>x = torch.empty(5, 3)<br>print(x)<br><br># \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u521d\u59cb\u5316\u76845x3\u77e9\u9635<br>x = torch.rand(5, 3)<br>print(x)<br><br># \u521b\u5efa\u4e00\u4e2a5x3\u7684\u96f6\u77e9\u9635\uff0c\u7c7b\u578b\u4e3along<br>x = torch.zeros(5, 3, dtype=torch.long)<br>print(x)<br><br># \u76f4\u63a5\u4ece\u6570\u636e\u521b\u5efatensor<br>x = torch.tensor(&#91;5.5, 3])<br>print(x)<br>\u6211\u4eec\u8fd8\u53ef\u4ee5\u5bf9\u5df2\u6709\u7684tensor\u8fdb\u884c\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u64cd\u4f5c\uff1a<br><br># \u521b\u5efa\u4e00\u4e2atensor\uff0c\u5e76\u8bbe\u7f6erequires_grad=True\u4ee5\u8ddf\u8e2a\u8ba1\u7b97\u5386\u53f2<br>x = torch.ones(2, 2, requires_grad=True)<br>print(x)<br><br># \u5bf9tensor\u8fdb\u884c\u64cd\u4f5c<br>y = x + 2<br>print(y)<br><br># y\u662f\u64cd\u4f5c\u7684\u7ed3\u679c\uff0c\u6240\u4ee5\u5b83\u6709grad_fn\u5c5e\u6027<br>print(y.grad_fn)<br><br># \u5bf9y\u8fdb\u884c\u66f4\u591a\u64cd\u4f5c<br>z = y * y * 3<br>out = z.mean()<br><br>print(z, out)<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u64cd\u4f5c\u7684\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tensor(&#91;&#91;1., 1.],<br>&nbsp; &nbsp; &nbsp; &nbsp; &#91;1., 1.]], requires_grad=True)<br>tensor(&#91;&#91;3., 3.],<br>&nbsp; &nbsp; &nbsp; &nbsp; &#91;3., 3.]], grad_fn=&lt;AddBackward0&gt;)<br>&lt;AddBackward0 object at 0x7f36c0a7f1d0&gt;<br>tensor(&#91;&#91;27., 27.],<br>&nbsp; &nbsp; &nbsp; &nbsp; &#91;27., 27.]], grad_fn=&lt;MulBackward0&gt;) tensor(27., grad_fn=&lt;MeanBackward0&gt;)<\/code><\/pre>\n\n\n\n<p>\u5728PyTorch\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528.backward()\u65b9\u6cd5\u6765\u8ba1\u7b97\u68af\u5ea6\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u56e0\u4e3aout\u5305\u542b\u4e00\u4e2a\u6807\u91cf\uff0cout.backward()\u7b49\u4ef7\u4e8eout.backward(torch.tensor(1.))<br>out.backward()<br><br># \u6253\u5370\u68af\u5ea6 d(out)\/dx<br>print(x.grad)<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u662fPyTorch tensor\u7684\u57fa\u672c\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230PyTorch tensor\u64cd\u4f5c\u975e\u5e38\u7b80\u5355\u548c\u76f4\u89c2\u3002\u5728\u540e\u7eed\u7684\u5b66\u4e60\u4e2d\uff0c\u6211\u4eec\u5c06\u4f1a\u4f7f\u7528\u5230\u66f4\u591a\u7684tensor\u64cd\u4f5c\uff0c\u4f8b\u5982\u7d22\u5f15\u3001\u5207\u7247\u3001\u6570\u5b66\u8fd0\u7b97\u3001\u7ebf\u6027\u4ee3\u6570\u3001\u968f\u673a\u6570\u7b49\u7b49\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.2 GPU\u52a0\u901f<\/h2>\n\n\n\n<p>\u5728\u6df1\u5ea6\u5b66\u4e60\u8bad\u7ec3\u4e2d\uff0cGPU\uff08\u56fe\u5f62\u5904\u7406\u5668\uff09\u52a0\u901f\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002GPU\u7684\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\u4f7f\u5f97\u5176\u6bd4CPU\u5728\u5927\u89c4\u6a21\u77e9\u9635\u8fd0\u7b97\u4e0a\u66f4\u5177\u4f18\u52bf\u3002PyTorch\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684API\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5728CPU\u548cGPU\u4e4b\u95f4\u5207\u6362\u8ba1\u7b97\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u68c0\u67e5\u7cfb\u7edf\u4e2d\u662f\u5426\u5b58\u5728\u53ef\u7528\u7684GPU\u3002\u5728PyTorch\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528torch.cuda.is_available()\u6765\u68c0\u67e5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch<br><br># \u68c0\u67e5\u662f\u5426\u6709\u53ef\u7528\u7684GPU<br>if torch.cuda.is_available():<br>&nbsp; &nbsp; print(\"There is a GPU available.\")<br>else:<br>&nbsp; &nbsp; print(\"There is no GPU available.\")<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u5b58\u5728\u53ef\u7528\u7684GPU\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528.to()\u65b9\u6cd5\u5c06tensor\u79fb\u52a8\u5230GPU\u4e0a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u521b\u5efa\u4e00\u4e2atensor<br>x = torch.tensor(&#91;1.0, 2.0])<br><br># \u79fb\u52a8tensor\u5230GPU\u4e0a<br>if torch.cuda.is_available():<br>&nbsp; &nbsp; x = x.to('cuda')<br><\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5728\u521b\u5efatensor\u7684\u65f6\u5019\u5c31\u6307\u5b9a\u5176\u8bbe\u5907\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u76f4\u63a5\u5728GPU\u4e0a\u521b\u5efatensor<br>if torch.cuda.is_available():<br>&nbsp; &nbsp; x = torch.tensor(&#91;1.0, 2.0], device='cuda')<br><\/code><\/pre>\n\n\n\n<p>\u5728\u8fdb\u884c\u6a21\u578b\u8bad\u7ec3\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u6a21\u578b\u548c\u6570\u636e\u90fd\u79fb\u52a8\u5230GPU\u4e0a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6a21\u578b<br>model = torch.nn.Linear(10, 1)<br><br># \u521b\u5efa\u4e00\u4e9b\u6570\u636e<br>data = torch.randn(100, 10)<br><br># \u79fb\u52a8\u6a21\u578b\u548c\u6570\u636e\u5230GPU<br>if torch.cuda.is_available():<br>&nbsp; &nbsp; model = model.to('cuda')<br>&nbsp; &nbsp; data = data.to('cuda')<br><\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728PyTorch\u4e2d\u8fdb\u884cGPU\u52a0\u901f\u7684\u57fa\u672c\u64cd\u4f5c\u3002\u4f7f\u7528GPU\u52a0\u901f\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u7684\u8bad\u7ec3\u901f\u5ea6\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6570\u636e\u5728CPU\u548cGPU\u4e4b\u95f4\u7684\u4f20\u8f93\u4f1a\u6d88\u8017\u4e00\u5b9a\u7684\u65f6\u95f4\uff0c\u56e0\u6b64\u6211\u4eec\u5e94\u8be5\u5c3d\u91cf\u51cf\u5c11\u6570\u636e\u7684\u4f20\u8f93\u6b21\u6570\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.3 \u81ea\u52a8\u6c42\u5bfc<\/h2>\n\n\n\n<p>\u5728\u6df1\u5ea6\u5b66\u4e60\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u8fdb\u884c\u68af\u5ea6\u4e0b\u964d\u4f18\u5316\u3002\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u8ba1\u7b97\u68af\u5ea6\uff0c\u4e5f\u5c31\u662f\u51fd\u6570\u7684\u5bfc\u6570\u3002\u5728PyTorch\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u81ea\u52a8\u6c42\u5bfc\u673a\u5236\uff08autograd\uff09\u6765\u81ea\u52a8\u8ba1\u7b97\u68af\u5ea6\u3002<\/p>\n\n\n\n<p>\u5728PyTorch\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6etensor.requires_grad=True\u6765\u8ffd\u8e2a\u5176\u4e0a\u7684\u6240\u6709\u64cd\u4f5c\u3002\u5b8c\u6210\u8ba1\u7b97\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528.backward()\u65b9\u6cd5\uff0cPyTorch\u4f1a\u81ea\u52a8\u8ba1\u7b97\u548c\u5b58\u50a8\u68af\u5ea6\u3002\u8fd9\u4e2a\u68af\u5ea6\u53ef\u4ee5\u901a\u8fc7.grad\u5c5e\u6027\u8fdb\u884c\u8bbf\u95ee\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch<br><br># \u521b\u5efa\u4e00\u4e2atensor\u5e76\u8bbe\u7f6erequires_grad=True\u6765\u8ffd\u8e2a\u5176\u8ba1\u7b97\u5386\u53f2<br>x = torch.ones(2, 2, requires_grad=True)<br><br># \u5bf9\u8fd9\u4e2atensor\u505a\u4e00\u6b21\u8fd0\u7b97\uff1a<br>y = x + 2<br><br># y\u662f\u8ba1\u7b97\u7684\u7ed3\u679c\uff0c\u6240\u4ee5\u5b83\u6709grad_fn\u5c5e\u6027<br>print(y.grad_fn)<br><br># \u5bf9y\u8fdb\u884c\u66f4\u591a\u7684\u64cd\u4f5c<br>z = y * y * 3<br>out = z.mean()<br><br>print(z, out)<br><br># \u4f7f\u7528.backward()\u6765\u8fdb\u884c\u53cd\u5411\u4f20\u64ad\uff0c\u8ba1\u7b97\u68af\u5ea6<br>out.backward()<br><br># \u8f93\u51fa\u68af\u5ea6d(out)\/dx<br>print(x.grad)<br><\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u793a\u4f8b\u4e2d\uff0cout.backward()\u7b49\u540c\u4e8eout.backward(torch.tensor(1.))\u3002\u5982\u679cout\u4e0d\u662f\u4e00\u4e2a\u6807\u91cf\uff0c\u56e0\u4e3atensor\u662f\u77e9\u9635\uff0c\u90a3\u4e48\u5728\u8c03\u7528.backward()\u65f6\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u4e0eout\u540c\u5f62\u7684\u6743\u91cd\u5411\u91cf\u8fdb\u884c\u76f8\u4e58\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = torch.randn(3, requires_grad=True)<br><br>y = x * 2<br>while y.data.norm() &lt; 1000:<br>&nbsp; &nbsp; y = y * 2<br><br>print(y)<br><br>v = torch.tensor(&#91;0.1, 1.0, 0.0001], dtype=torch.float)<br>y.backward(v)<br><br>print(x.grad)<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662fPyTorch\u4e2d\u81ea\u52a8\u6c42\u5bfc\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u3002\u81ea\u52a8\u6c42\u5bfc\u662fPyTorch\u7684\u91cd\u8981\u7279\u6027\u4e4b\u4e00\uff0c\u5b83\u4e3a\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u7684\u8bad\u7ec3\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u4fbf\u5229\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">3. PyTorch \u795e\u7ecf\u7f51\u7edc<\/h1>\n\n\n\n<p><img decoding=\"async\" class=\"lazyload\"data-src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/0l8e8dYXFXbgDOAwqZsKGUVVVs92Y9w7TV4w8SobGAu5mSN56hib549ROBvOLggj69PCvYia5znPq0pzrRCQ5PBA\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=3\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" alt=\"\u56fe\u7247\"><noscript><img decoding=\"async\" src=\"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/0l8e8dYXFXbgDOAwqZsKGUVVVs92Y9w7TV4w8SobGAu5mSN56hib549ROBvOLggj69PCvYia5znPq0pzrRCQ5PBA\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=3\" alt=\"\u56fe\u7247\"><\/noscript>\u5728\u638c\u63e1\u4e86PyTorch\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u4e4b\u540e\uff0c\u6211\u4eec\u5c06\u63a2\u7d22\u4e00\u4e9b\u66f4\u4e3a\u9ad8\u7ea7\u7684\u7279\u6027\u548c\u7528\u6cd5\u3002\u8fd9\u4e9b\u9ad8\u7ea7\u7279\u6027\u5305\u62ec\u795e\u7ecf\u7f51\u7edc\u6784\u5efa\u3001\u6570\u636e\u52a0\u8f7d\u4ee5\u53ca\u6a21\u578b\u4fdd\u5b58\u548c\u52a0\u8f7d\u7b49\u7b49\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.1 \u6784\u5efa\u795e\u7ecf\u7f51\u7edc<\/h2>\n\n\n\n<p>PyTorch\u63d0\u4f9b\u4e86torch.nn\u5e93\uff0c\u5b83\u662f\u7528\u4e8e\u6784\u5efa\u795e\u7ecf\u7f51\u7edc\u7684\u5de5\u5177\u5e93\u3002torch.nn\u5e93\u4f9d\u8d56\u4e8eautograd\u5e93\u6765\u5b9a\u4e49\u548c\u8ba1\u7b97\u68af\u5ea6\u3002nn.Module\u5305\u542b\u4e86\u795e\u7ecf\u7f51\u7edc\u7684\u5c42\u4ee5\u53ca\u8fd4\u56de\u8f93\u51fa\u7684forward(input)\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u795e\u7ecf\u7f51\u7edc\u7684\u6784\u5efa\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch<br>import torch.nn as nn<br>import torch.nn.functional as F<br><br>class Net(nn.Module):<br>&nbsp; &nbsp; def __init__(self):<br>&nbsp; &nbsp; &nbsp; &nbsp; super(Net, self).__init__()<br><br>&nbsp; &nbsp; &nbsp; &nbsp; # \u8f93\u5165\u56fe\u50cfchannel\uff1a1\uff0c\u8f93\u51fachannel\uff1a6\uff0c5x5\u5377\u79ef\u6838<br>&nbsp; &nbsp; &nbsp; &nbsp; self.conv1 = nn.Conv2d(1, 6, 5)<br>&nbsp; &nbsp; &nbsp; &nbsp; self.conv2 = nn.Conv2d(6, 16, 5)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; # \u5168\u8fde\u63a5\u5c42<br>&nbsp; &nbsp; &nbsp; &nbsp; self.fc1 = nn.Linear(16 * 5 * 5, 120)<br>&nbsp; &nbsp; &nbsp; &nbsp; self.fc2 = nn.Linear(120, 84)<br>&nbsp; &nbsp; &nbsp; &nbsp; self.fc3 = nn.Linear(84, 10)<br><br>&nbsp; &nbsp; def forward(self, x):<br>&nbsp; &nbsp; &nbsp; &nbsp; # \u4f7f\u75282x2\u7a97\u53e3\u8fdb\u884c\u6700\u5927\u6c60\u5316<br>&nbsp; &nbsp; &nbsp; &nbsp; x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))<br>&nbsp; &nbsp; &nbsp; &nbsp; # \u5982\u679c\u7a97\u53e3\u662f\u65b9\u7684\uff0c\u53ea\u9700\u8981\u6307\u5b9a\u4e00\u4e2a\u7ef4\u5ea6<br>&nbsp; &nbsp; &nbsp; &nbsp; x = F.max_pool2d(F.relu(self.conv2(x)), 2)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; x = x.view(-1, self.num_flat_features(x))<br><br>&nbsp; &nbsp; &nbsp; &nbsp; x = F.relu(self.fc1(x))<br>&nbsp; &nbsp; &nbsp; &nbsp; x = F.relu(self.fc2(x))<br>&nbsp; &nbsp; &nbsp; &nbsp; x = self.fc3(x)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; return x<br><br>&nbsp; &nbsp; def num_flat_features(self, x):<br>&nbsp; &nbsp; &nbsp; &nbsp; size = x.size()&#91;1:] &nbsp;# \u83b7\u53d6\u9664\u4e86batch\u7ef4\u5ea6\u4e4b\u5916\u7684\u5176\u4ed6\u7ef4\u5ea6<br>&nbsp; &nbsp; &nbsp; &nbsp; num_features = 1<br>&nbsp; &nbsp; &nbsp; &nbsp; for s in size:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num_features *= s<br>&nbsp; &nbsp; &nbsp; &nbsp; return num_features<br><br>net = Net()<br>print(net)<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u795e\u7ecf\u7f51\u7edc\u7684\u6784\u5efa\u65b9\u6cd5\u3002\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2aNet\u7c7b\uff0c\u8fd9\u4e2a\u7c7b\u7ee7\u627f\u81eann.Module\u3002\u7136\u540e\u5728__init__\u65b9\u6cd5\u4e2d\u5b9a\u4e49\u4e86\u7f51\u7edc\u7684\u7ed3\u6784\uff0c\u5728forward\u65b9\u6cd5\u4e2d\u5b9a\u4e49\u4e86\u6570\u636e\u7684\u6d41\u5411\u3002\u5728\u7f51\u7edc\u7684\u6784\u5efa\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55tensor\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cbackward\u51fd\u6570\uff08\u7528\u4e8e\u8ba1\u7b97\u68af\u5ea6\uff09\u4f1a\u88abautograd\u81ea\u52a8\u521b\u5efa\u548c\u5b9e\u73b0\u3002\u4f60\u53ea\u9700\u8981\u5728nn.Module\u7684\u5b50\u7c7b\u4e2d\u5b9a\u4e49forward\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u5728\u521b\u5efa\u597d\u795e\u7ecf\u7f51\u7edc\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528net.parameters()\u65b9\u6cd5\u6765\u8fd4\u56de\u7f51\u7edc\u7684\u53ef\u5b66\u4e60\u53c2\u6570\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u521a\u5f00\u59cb\u80af\u5b9a\u5f88\u96be\u4e00\u6b21\u6027\u770b\u61c2\u4ee3\u7801\uff0c\u6765\u4e2a\u8be6\u7ec6\u7684\u89e3\u91ca<\/p>\n<\/blockquote>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u662f\u4e00\u4e2a\u4f7f\u7528 PyTorch \u6784\u5efa\u7684\u7ecf\u5178\u5377\u79ef\u795e\u7ecf\u7f51\u7edc (CNN)\uff0c\u5e38\u7528\u4e8e\u56fe\u50cf\u5206\u7c7b\u4efb\u52a1\uff08\u6bd4\u5982\u624b\u5199\u6570\u5b57\u8bc6\u522b MNIST\uff09\u3002<\/p>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a&nbsp;<code>Net<\/code>&nbsp;\u7684\u5377\u79ef\u795e\u7ecf\u7f51\u7edc (CNN)\uff0c\u5b83\u5305\u542b\u4e24\u4e2a\u5377\u79ef\u5c42\u548c\u4e09\u4e2a\u5168\u8fde\u63a5\u5c42\uff0c\u7528\u4e8e\u5904\u7406\u56fe\u50cf\u5206\u7c7b\u4efb\u52a1\uff08\u5f88\u53ef\u80fd\u662f\u8bc6\u522b 10 \u7c7b\u7684\u624b\u5199\u6570\u5b57\uff09\u3002<\/p>\n\n\n\n<p><strong>\u7f51\u7edc\u901a\u8fc7\u5377\u79ef\u64cd\u4f5c\u63d0\u53d6\u56fe\u50cf\u7279\u5f81\uff0c\u5e76\u901a\u8fc7\u5168\u8fde\u63a5\u5c42\u8fdb\u884c\u5206\u7c7b\u3002<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udd0d \u9010\u90e8\u5206\u89e3\u6790<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol class=\"wp-block-list\">\n<li>\u5f15\u5165\u5fc5\u8981\u7684\u5de5\u5177\u5305<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;torch<br>import&nbsp;torch.nn&nbsp;as&nbsp;nn<br>import&nbsp;torch.nn.functional&nbsp;as&nbsp;F<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>torch<\/code>: PyTorch \u7684\u6838\u5fc3\u3002<\/li>\n\n\n\n<li><code>torch.nn<\/code>\u00a0(\u901a\u5e38\u5bfc\u5165\u4e3a\u00a0<code>nn<\/code>): \u5305\u542b\u6784\u5efa\u795e\u7ecf\u7f51\u7edc\u9700\u8981\u7684<strong>\u6a21\u5757\u548c\u7c7b<\/strong>\uff0c\u6bd4\u5982\u5404\u79cd\u7f51\u7edc\u5c42\uff08\u5377\u79ef\u5c42\u3001\u5168\u8fde\u63a5\u5c42\uff09\u3002<\/li>\n\n\n\n<li><code>torch.nn.functional<\/code>\u00a0(\u901a\u5e38\u5bfc\u5165\u4e3a\u00a0<code>F<\/code>): \u5305\u542b\u8bb8\u591a<strong>\u51fd\u6570\u5f62\u5f0f<\/strong>\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u6fc0\u6d3b\u51fd\u6570 (ReLU)\u3001\u6c60\u5316 (pooling) \u7b49\u3002\u5b83\u4eec\u901a\u5e38\u4e0d\u5305\u542b\u9700\u8981\u5b66\u4e60\u7684\u53c2\u6570\uff0c\u6309\u9700\u4f7f\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u5b9a\u4e49\u7f51\u7edc\u6a21\u578b (<code>class Net(nn.Module)<\/code>)<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<p>\u8fd9\u662f\u4ee3\u7801\u7684\u6838\u5fc3\u90e8\u5206\u3002\u5728 PyTorch \u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u7ee7\u627f\u81ea&nbsp;<code>nn.Module<\/code>&nbsp;\u7684\u7c7b\u6765\u521b\u5efa\u81ea\u5b9a\u4e49\u795e\u7ecf\u7f51\u7edc\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><code>__init__<\/code>&nbsp;\u65b9\u6cd5\uff1a\u5b9a\u4e49\u7f51\u7edc\u7ed3\u6784\u7ec4\u4ef6<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>def&nbsp;__init__(self):<br>&nbsp; &nbsp; super(Net, self).__init__() &nbsp;<em># \u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5<\/em><br>&nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp;&nbsp;<em># \u5377\u79ef\u5c42<\/em><br>&nbsp; &nbsp; self.conv1 = nn.Conv2d(1,&nbsp;6,&nbsp;5) &nbsp;&nbsp;<em># \u7b2c\u4e00\u4e2a\u5377\u79ef\u5c42<\/em><br>&nbsp; &nbsp; self.conv2 = nn.Conv2d(6,&nbsp;16,&nbsp;5) &nbsp;<em># \u7b2c\u4e8c\u4e2a\u5377\u79ef\u5c42<\/em><br>&nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp;&nbsp;<em># \u5168\u8fde\u63a5\u5c42<\/em><br>&nbsp; &nbsp; self.fc1 = nn.Linear(16&nbsp;*&nbsp;5&nbsp;*&nbsp;5,&nbsp;120)&nbsp;<em># \u7b2c\u4e00\u4e2a\u5168\u8fde\u63a5\u5c42<\/em><br>&nbsp; &nbsp; self.fc2 = nn.Linear(120,&nbsp;84) &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em># \u7b2c\u4e8c\u4e2a\u5168\u8fde\u63a5\u5c42<\/em><br>&nbsp; &nbsp; self.fc3 = nn.Linear(84,&nbsp;10) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<em># \u7b2c\u4e09\u4e2a\u5168\u8fde\u63a5\u5c42\uff08\u8f93\u51fa\u5c42\uff09<\/em><\/code><\/pre>\n\n\n\n<p>\u5728&nbsp;<code>__init__<\/code>&nbsp;\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u7f51\u7edc\u9700\u8981\u7684\u5404\u79cd<strong>\u5c42<\/strong>\uff0c\u5b83\u4eec\u901a\u5e38\u662f\u5305\u542b\u53ef\u5b66\u4e60\u53c2\u6570\u7684\u6a21\u5757\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>nn.Conv2d<\/code>\u00a0(\u4e8c\u7ef4\u5377\u79ef\u5c42)<\/strong>: \u7528\u4e8e\u4ece\u56fe\u50cf\u4e2d\u63d0\u53d6\u7279\u5f81\u3002\n<ul class=\"wp-block-list\">\n<li><code>nn.Conv2d(1, 6, 5)<\/code>: \u521b\u5efa\u4e00\u4e2a\u5377\u79ef\u5c42\uff0c\u5176\u8f93\u5165\u901a\u9053\u6570\u4e3a 1\uff08\u4f8b\u5982\u9ed1\u767d\u56fe\u50cf\uff09\uff0c\u8f93\u51fa\u901a\u9053\u6570\u4e3a 6\uff08\u5373\u4f7f\u7528 6 \u4e2a\u4e0d\u540c\u7684\u5377\u79ef\u6838\u6765\u63d0\u53d6 6 \u79cd\u7279\u5f81\uff09\uff0c\u5377\u79ef\u6838\u5927\u5c0f\u4e3a 5x5\u3002<\/li>\n\n\n\n<li><code>nn.Conv2d(6, 16, 5)<\/code>: \u8f93\u5165\u901a\u9053\u6570\u4e3a 6\uff08\u63a5\u6536\u4e0a\u4e00\u5c42\u7684 6 \u4e2a\u7279\u5f81\u56fe\uff09\uff0c\u8f93\u51fa\u901a\u9053\u6570\u4e3a 16\uff08\u63d0\u53d6 16 \u79cd\u66f4\u9ad8\u7ea7\u7684\u7279\u5f81\uff09\uff0c\u5377\u79ef\u6838\u5927\u5c0f\u4ecd\u4e3a 5x5\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>nn.Linear<\/code>\u00a0(\u5168\u8fde\u63a5\u5c42)<\/strong>: \u7528\u4e8e\u6700\u7ec8\u7684\u5206\u7c7b\u51b3\u7b56\u3002\n<ul class=\"wp-block-list\">\n<li><code>nn.Linear(16 * 5 * 5, 120)<\/code>: \u8f93\u5165\u7279\u5f81\u6570\u4e3a\u00a0<code>16 * 5 * 5<\/code>\uff08\u8fd9\u662f\u7ecf\u8fc7\u524d\u9762\u5377\u79ef\u548c\u6c60\u5316\u540e\u7279\u5f81\u56fe\u5c55\u5e73\u7684\u5927\u5c0f\uff09\uff0c\u8f93\u51fa\u7279\u5f81\u6570\u4e3a 120\u3002<\/li>\n\n\n\n<li><code>nn.Linear(120, 84)<\/code>: \u8f93\u5165 120 \u7ef4\u7279\u5f81\uff0c\u8f93\u51fa 84 \u7ef4\u7279\u5f81\u3002<\/li>\n\n\n\n<li><code>nn.Linear(84, 10)<\/code>: \u8f93\u5165 84 \u7ef4\u7279\u5f81\uff0c<strong>\u8f93\u51fa 10 \u7ef4<\/strong>\u3002\u8fd9 10 \u4e2a\u8f93\u51fa\u503c\u901a\u5e38\u5bf9\u5e94 10 \u4e2a\u7c7b\u522b\u7684\u5206\u6570\uff08\u5982\u624b\u5199\u6570\u5b57 0-9\uff09\uff0c\u540e\u7eed\u518d\u901a\u8fc7 softmax \u51fd\u6570\u8f6c\u6362\u4e3a\u6982\u7387\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><code>forward<\/code>&nbsp;\u65b9\u6cd5\uff1a\u5b9a\u4e49\u6570\u636e\u6d41\u52a8\u8def\u5f84<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>def&nbsp;forward(self, x):<br>&nbsp; &nbsp;&nbsp;<em># \u7b2c\u4e00\u7ec4\uff1a\u5377\u79ef -&gt; ReLU\u6fc0\u6d3b -&gt; \u6c60\u5316<\/em><br>&nbsp; &nbsp; x = F.max_pool2d(F.relu(self.conv1(x)), (2,&nbsp;2))<br>&nbsp; &nbsp;&nbsp;<em># \u7b2c\u4e8c\u7ec4\uff1a\u5377\u79ef -&gt; ReLU\u6fc0\u6d3b -&gt; \u6c60\u5316<\/em><br>&nbsp; &nbsp; x = F.max_pool2d(F.relu(self.conv2(x)),&nbsp;2)<br>&nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp;&nbsp;<em># \u5c06\u591a\u7ef4\u7279\u5f81\u56fe\u201c\u5c55\u5e73\u201d\u6210\u4e00\u7ef4\u5411\u91cf\uff0c\u4e3a\u5168\u8fde\u63a5\u5c42\u51c6\u5907\u8f93\u5165<\/em><br>&nbsp; &nbsp; x = x.view(-1, self.num_flat_features(x))<br>&nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp;&nbsp;<em># \u5168\u8fde\u63a5\u5c42\u90e8\u5206\uff0c\u4e2d\u95f4\u4f7f\u7528ReLU\u6fc0\u6d3b\u51fd\u6570<\/em><br>&nbsp; &nbsp; x = F.relu(self.fc1(x))<br>&nbsp; &nbsp; x = F.relu(self.fc2(x))<br>&nbsp; &nbsp; x = self.fc3(x) &nbsp;<em># \u8f93\u51fa\u5c42\u901a\u5e38\u4e0d\u7acb\u5373\u63a5\u6fc0\u6d3b\u51fd\u6570\uff0c\u8ba1\u7b97\u635f\u5931\u65f6\u4f1a\u5904\u7406<\/em><br>&nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp;&nbsp;return&nbsp;x<\/code><\/pre>\n\n\n\n<p><code>forward<\/code>&nbsp;\u65b9\u6cd5\u5b9a\u4e49\u4e86\u6570\u636e\u4ece\u8f93\u5165\u5230\u8f93\u51fa\u7684<strong>\u5b8c\u6574\u4f20\u64ad\u8def\u5f84<\/strong>\uff08\u524d\u5411\u4f20\u64ad\uff09\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5377\u79ef\u548c\u6c60\u5316<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>F.relu(self.conv1(x))<\/code>: \u6570\u636e\u5148\u901a\u8fc7\u7b2c\u4e00\u5377\u79ef\u5c42\u00a0<code>conv1<\/code>\uff0c\u7136\u540e\u7ecf\u8fc7\u00a0<strong>ReLU \u6fc0\u6d3b\u51fd\u6570<\/strong>\uff08\u5f15\u5165\u975e\u7ebf\u6027\uff0c\u4f7f\u7f51\u7edc\u80fd\u5b66\u4e60\u66f4\u590d\u6742\u7684\u6a21\u5f0f\uff09\u3002<\/li>\n\n\n\n<li><code>F.max_pool2d(..., (2, 2))<\/code>: \u63a5\u7740\u8fdb\u884c\u00a0<strong>2x2 \u7684\u6700\u5927\u6c60\u5316<\/strong>\u64cd\u4f5c\u3002\u6c60\u5316\u7528\u4e8e\u964d\u4f4e\u6570\u636e\u7ef4\u5ea6\uff0c\u51cf\u5c11\u8ba1\u7b97\u91cf\uff0c\u540c\u65f6\u4fdd\u7559\u4e3b\u8981\u7279\u5f81\uff0c\u5e76\u589e\u5f3a\u6a21\u578b\u5bf9\u5c0f\u4f4d\u79fb\u7684\u9c81\u68d2\u6027\u3002<\/li>\n\n\n\n<li>\u7b2c\u4e8c\u7ec4\u5377\u79ef\u3001ReLU \u548c\u6c60\u5316\u8fc7\u7a0b\u7c7b\u4f3c\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u5c55\u5e73\u64cd\u4f5c (<code>x.view<\/code>)<\/strong>: \u5377\u79ef\u5c42\u8f93\u51fa\u7684\u7279\u5f81\u662f\u4e8c\u7ef4\u7684\u7279\u5f81\u56fe\uff08\u591a\u4e2a\u901a\u9053\uff09\uff0c\u800c\u5168\u8fde\u63a5\u5c42\u9700\u8981\u4e00\u7ef4\u7684\u8f93\u5165\u5411\u91cf\u3002<code>x.view(-1, self.num_flat_features(x))<\/code>\u00a0\u5c06 batch \u4e2d\u6bcf\u4e2a\u6837\u672c\u7684\u7279\u5f81\u56fe<strong>\u5c55\u5e73<\/strong>\u6210\u4e00\u4e2a\u4e00\u7ef4\u5411\u91cf\u3002<code>-1<\/code>\u00a0\u8868\u793a\u8ba9 PyTorch \u81ea\u52a8\u8ba1\u7b97 batch size\uff0c<code>self.num_flat_features(x)<\/code>\u00a0\u8ba1\u7b97\u51fa\u4e00\u4e2a\u6837\u672c\u7684\u6240\u6709\u7279\u5f81\u6570\u91cf\uff08\u8fd9\u91cc\u662f\u00a0<code>16 * 5 * 5 = 400<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u5168\u8fde\u63a5\u5c42\u548c\u5206\u7c7b<\/strong>:\n<ul class=\"wp-block-list\">\n<li>\u6570\u636e\u4f9d\u6b21\u901a\u8fc7\u4e09\u4e2a\u5168\u8fde\u63a5\u5c42\u00a0<code>fc1<\/code>,\u00a0<code>fc2<\/code>,\u00a0<code>fc3<\/code>\u3002<\/li>\n\n\n\n<li>\u5728\u00a0<code>fc1<\/code>\u00a0\u548c\u00a0<code>fc2<\/code>\u00a0\u4e4b\u540e\u4f7f\u7528\u4e86 ReLU \u6fc0\u6d3b\u51fd\u6570\u3002<\/li>\n\n\n\n<li><code>fc3<\/code>\u00a0\u4f5c\u4e3a\u8f93\u51fa\u5c42\uff0c\u5176\u8f93\u51fa\u662f<strong>\u672a\u7ecf\u8fc7 softmax \u5904\u7406\u7684\u539f\u59cb\u5206\u6570<\/strong>\uff08logits\uff09\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u8bad\u7ec3\u65f6\uff0c\u635f\u5931\u51fd\u6570\uff08\u5982\u4ea4\u53c9\u71b5\u635f\u5931\uff09\u5185\u90e8\u901a\u5e38\u4f1a\u96c6\u6210 softmax \u8ba1\u7b97\u5e76\u66f4\u6570\u503c\u7a33\u5b9a\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><code>num_flat_features<\/code>&nbsp;\u65b9\u6cd5\uff1a\u8ba1\u7b97\u5c55\u5e73\u540e\u7684\u7279\u5f81\u6570<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>def&nbsp;num_flat_features(self, x):<br>&nbsp; &nbsp; size = x.size()&#91;1:] &nbsp;<em># \u83b7\u53d6\u9664\u4e86batch\u7ef4\u5ea6\u4e4b\u5916\u7684\u5176\u4ed6\u7ef4\u5ea6<\/em><br>&nbsp; &nbsp; num_features =&nbsp;1<br>&nbsp; &nbsp;&nbsp;for&nbsp;s&nbsp;in&nbsp;size:<br>&nbsp; &nbsp; &nbsp; &nbsp; num_features *= s<br>&nbsp; &nbsp;&nbsp;return&nbsp;num_features<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u8f85\u52a9\u65b9\u6cd5\u7528\u4e8e\u8ba1\u7b97\u4e00\u4e2a\u5f20\u91cf\uff08\u9664\u5f00 batch \u7ef4\u5ea6\u5916\uff09<strong>\u603b\u5171\u5305\u542b\u591a\u5c11\u4e2a\u7279\u5f81\u5143\u7d20<\/strong>\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u67d0\u4e2a\u5c42\u7684\u8f93\u51fa\u5f20\u91cf\u5c3a\u5bf8\u662f&nbsp;<code>[batch_size, 16, 5, 5]<\/code>\uff08\u4ee3\u8868\u4e00\u4e2a batch \u7684\u6570\u636e\uff0c\u6bcf\u4e2a\u6570\u636e\u6709 16 \u4e2a\u901a\u9053\uff0c\u6bcf\u4e2a\u901a\u9053\u662f 5x5 \u7684\u7279\u5f81\u56fe\uff09\uff0c\u90a3\u4e48\u8be5\u65b9\u6cd5\u8ba1\u7b97&nbsp;<code>16 * 5 * 5 = 400<\/code>\uff0c\u5e76\u8fd4\u56de 400\u3002\u8fd9\u4e2a\u503c\u7528\u4e8e\u544a\u8bc9&nbsp;<code>view<\/code>&nbsp;\u51fd\u6570\u9700\u8981\u5c06\u6bcf\u4e2a\u6837\u672c\u5c55\u5e73\u6210\u591a\u957f\u7684\u4e00\u7ef4\u5411\u91cf\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\u5b9e\u4f8b\u5316\u7f51\u7edc\u4e0e\u6253\u5370<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>net = Net()&nbsp;<em># \u521b\u5efaNet\u7c7b\u7684\u5b9e\u4f8b\uff0c\u5373\u6211\u4eec\u7684\u795e\u7ecf\u7f51\u7edc\u6a21\u578b<\/em><br>print(net) &nbsp;&nbsp;<em># \u6253\u5370\u7f51\u7edc\u7684\u7ed3\u6784\u4fe1\u606f<\/em><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>net = Net()<\/code>: \u521b\u5efa\u6211\u4eec\u5b9a\u4e49\u597d\u7684\u795e\u7ecf\u7f51\u7edc\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff08\u5bf9\u8c61\uff09\u3002\u6b64\u65f6\uff0cPyTorch \u4f1a\u81ea\u52a8\u7ba1\u7406\u7f51\u7edc\u4e2d\u7684\u6240\u6709\u53ef\u5b66\u4e60\u53c2\u6570\uff08\u5982\u5377\u79ef\u6838\u7684\u6743\u91cd\u3001\u5168\u8fde\u63a5\u5c42\u7684\u6743\u91cd\u548c\u504f\u7f6e\uff09\u3002<\/li>\n\n\n\n<li><code>print(net)<\/code>: \u6253\u5370\u7f51\u7edc\u7ed3\u6784\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u786e\u8ba4\u5c42\u7684\u8bbe\u7f6e\u662f\u5426\u6b63\u786e\uff0c\u4ee5\u53ca\u53c2\u6570\u6570\u91cf\u7b49\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udcca \u7f51\u7edc\u7ed3\u6784\u6982\u89c8<\/p>\n<\/blockquote>\n\n\n\n<p>\u4e0b\u8868\u603b\u7ed3\u4e86\u8be5\u7f51\u7edc\u5404\u5c42\u7684\u529f\u80fd\u4e0e\u6570\u636e\u53d8\u5316\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5c42\u5e8f<\/th><th>\u5c42\u7c7b\u578b\u4e0e\u64cd\u4f5c<\/th><th>\u5173\u952e\u53c2\u6570\u4e0e\u8bf4\u660e<\/th><th>\u8f93\u51fa\u5c3a\u5bf8 (\u793a\u4f8b)<\/th><\/tr><\/thead><tbody><tr><td>\u8f93\u5165<\/td><td>-<\/td><td>\u5355\u901a\u9053\u56fe\u50cf (\u5982 MNIST)<\/td><td><code>[batch, 1, 32, 32]<\/code><\/td><\/tr><tr><td>1<\/td><td><strong>\u5377\u79ef\u5c42 Conv1<\/strong>&nbsp;+&nbsp;<strong>ReLU<\/strong>&nbsp;+&nbsp;<strong>MaxPool2d<\/strong><\/td><td><code>in_ch=1, out_ch=6, kernel=5<\/code><br><code>pool_size=2x2<\/code><\/td><td><code>[batch, 6, 14, 14]<\/code><\/td><\/tr><tr><td>2<\/td><td><strong>\u5377\u79ef\u5c42 Conv2<\/strong>&nbsp;+&nbsp;<strong>ReLU<\/strong>&nbsp;+&nbsp;<strong>MaxPool2d<\/strong><\/td><td><code>in_ch=6, out_ch=16, kernel=5<\/code><br><code>pool_size=2x2<\/code><\/td><td><code>[batch, 16, 5, 5]<\/code><\/td><\/tr><tr><td>3<\/td><td><strong>\u5c55\u5e73 (Flatten)<\/strong><\/td><td><code>view(-1, 400)<\/code><\/td><td><code>[batch, 400]<\/code><\/td><\/tr><tr><td>4<\/td><td><strong>\u5168\u8fde\u63a5\u5c42 FC1<\/strong>&nbsp;+&nbsp;<strong>ReLU<\/strong><\/td><td><code>in=400, out=120<\/code><\/td><td><code>[batch, 120]<\/code><\/td><\/tr><tr><td>5<\/td><td><strong>\u5168\u8fde\u63a5\u5c42 FC2<\/strong>&nbsp;+&nbsp;<strong>ReLU<\/strong><\/td><td><code>in=120, out=84<\/code><\/td><td><code>[batch, 84]<\/code><\/td><\/tr><tr><td>6<\/td><td><strong>\u5168\u8fde\u63a5\u5c42 FC3 (\u8f93\u51fa\u5c42)<\/strong><\/td><td><code>in=84, out=10<\/code><\/td><td><code>[batch, 10]<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udca1 \u5173\u952e\u8981\u70b9\u4e0e\u5b66\u4e60\u63d0\u793a<\/p>\n<\/blockquote>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>nn.Module<\/code>\u00a0\u662f\u57fa\u77f3<\/strong>: \u5728 PyTorch \u4e2d\u6784\u5efa\u4efb\u4f55\u7f51\u7edc\uff0c\u90fd\u8981\u7ee7\u627f\u00a0<code>nn.Module<\/code>\uff0c\u5e76\u5728\u00a0<code>__init__<\/code>\u00a0\u4e2d\u5b9a\u4e49\u5c42\uff0c\u5728\u00a0<code>forward<\/code>\u00a0\u4e2d\u5b9a\u4e49\u6570\u636e\u6d41\u3002<\/li>\n\n\n\n<li><strong>\u5377\u79ef\u5c42\u7528\u4e8e\u7279\u5f81\u63d0\u53d6<\/strong>: \u7406\u89e3\u00a0<code>nn.Conv2d(in_channels, out_channels, kernel_size)<\/code>\u00a0\u7684\u53c2\u6570\u610f\u4e49\u3002<\/li>\n\n\n\n<li><strong>\u6fc0\u6d3b\u51fd\u6570\u5f15\u5165\u975e\u7ebf\u6027<\/strong>: \u5982\u00a0<code>F.relu<\/code>\uff0c\u8fd9\u662f\u795e\u7ecf\u7f51\u7edc\u80fd\u591f\u62df\u5408\u590d\u6742\u51fd\u6570\u7684\u5173\u952e\u3002<\/li>\n\n\n\n<li><strong>\u6c60\u5316\u5c42\u7528\u4e8e\u964d\u7ef4\u548c\u6297\u6270\u52a8<\/strong>: \u5982\u00a0<code>F.max_pool2d<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u5168\u8fde\u63a5\u5c42\u7528\u4e8e\u5206\u7c7b<\/strong>: \u6ce8\u610f\u5377\u79ef\u5c42\u5230\u5168\u8fde\u63a5\u5c42\u4e4b\u95f4\u9700\u8981\u7528\u00a0<code>view<\/code>\u00a0\u6216\u00a0<code>flatten<\/code>\u00a0\u64cd\u4f5c\u5c06\u591a\u7ef4\u7279\u5f81\u56fe\u8f6c\u6362\u6210\u4e00\u7ef4\u5411\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\u5c42<\/strong>: \u901a\u5e38\u8f93\u51fa raw logits\uff08\u672a\u5f52\u4e00\u5316\u7684\u5206\u6570\uff09\uff0c\u914d\u5408\u00a0<code>nn.CrossEntropyLoss<\/code>\u00a0\u7b49\u635f\u5931\u51fd\u6570\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><strong>\u8fd9\u53ea\u662f\u6a21\u578b\u5b9a\u4e49<\/strong>: \u8fd9\u6bb5\u4ee3\u7801<strong>\u53ea\u5b9a\u4e49\u4e86\u6a21\u578b\u7684\u7ed3\u6784<\/strong>\u3002\u8981\u4f7f\u7528\u8fd9\u4e2a\u6a21\u578b\uff0c\u4f60\u8fd8\u9700\u8981\uff1a\n<ul class=\"wp-block-list\">\n<li><strong>\u5b9a\u4e49\u635f\u5931\u51fd\u6570<\/strong>\u00a0(\u5982\u00a0<code>nn.CrossEntropyLoss<\/code>)<\/li>\n\n\n\n<li><strong>\u5b9a\u4e49\u4f18\u5316\u5668<\/strong>\u00a0(\u5982\u00a0<code>torch.optim.SGD(net.parameters(), lr=0.01)<\/code>)<\/li>\n\n\n\n<li><strong>\u51c6\u5907\u6570\u636e\u52a0\u8f7d\u5668<\/strong>\u00a0(<code>DataLoader<\/code>)<\/li>\n\n\n\n<li><strong>\u7f16\u5199\u8bad\u7ec3\u5faa\u73af<\/strong>\u00a0(\u524d\u5411\u4f20\u64ad -> \u8ba1\u7b97\u635f\u5931 -> \u53cd\u5411\u4f20\u64ad\u00a0<code>loss.backward()<\/code>\u00a0-> \u4f18\u5316\u5668\u66f4\u65b0\u53c2\u6570\u00a0<code>optimizer.step()<\/code>)<\/li>\n\n\n\n<li><strong>\u6a21\u578b\u8bc4\u4f30<\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u6211\u8fd9\u4e2a\u5c0f\u7b28\u7b28\u5bf9\u8fd9\u4e2a batch\u7684\u6982\u5ff5\u4e5f\u6bd4\u8f83\u597d\u5947<\/p>\n<\/blockquote>\n\n\n\n<p>Batch\uff08\u6279\u6b21\uff09\u662f\u6df1\u5ea6\u5b66\u4e60\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\u3002\u7b80\u5355\u6765\u8bf4\uff0c<strong>Batch \u6307\u7684\u662f\u6a21\u578b\u8bad\u7ec3\u65f6\uff0c\u6bcf\u6b21\u8fed\u4ee3\uff08iteration\uff09\u4e2d\u540c\u65f6\u8f93\u5165\u5230\u6a21\u578b\u4e2d\u8fdb\u884c\u5904\u7406\u7684\u4e00\u7ec4\u6837\u672c\u6570\u636e<\/strong>\u3002\u8fd9\u7ec4\u6837\u672c\u7684\u6570\u91cf\u5c31\u79f0\u4e3a&nbsp;<strong>Batch Size\uff08\u6279\u6b21\u5927\u5c0f\uff09<\/strong>\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u66f4\u76f4\u89c2\u5730\u7406\u89e3\u8bad\u7ec3\u8fc7\u7a0b\u4e2d Epoch\u3001Batch \u548c Batch Size \u7684\u5173\u7cfb\uff0c\u4ee5\u53ca\u4e0d\u540c Batch Size \u7684\u5f71\u54cd\uff0c\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u7684\u56fe\u793a\u548c\u8868\u683c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>flowchart TD<br>&nbsp; &nbsp; A&#91;\u6574\u4e2a\u8bad\u7ec3\u6570\u636e\u96c6] --&gt; B&#91;\u5212\u5206\u4e3a\u591a\u4e2a Batch]<br>&nbsp; &nbsp; B --&gt; C&#91;\u904d\u5386\u6240\u6709 Batch&lt;br&gt;\u5b8c\u6210\u4e00\u4e2a Epoch]<br>&nbsp; &nbsp; C --&gt; D{\u662f\u5426\u8fbe\u5230&lt;br&gt;\u9884\u5b9a Epoch \u6570?}<br>&nbsp; &nbsp; D -- \u662f --&gt; F&#91;\u8bad\u7ec3\u7ed3\u675f]<br>&nbsp; &nbsp; D -- \u5426 --&gt; B<br><br>&nbsp; &nbsp; subgraph B_details&#91;\u4e00\u4e2a Batch \u7684\u5904\u7406]<br>&nbsp; &nbsp; &nbsp; &nbsp; direction LR<br>&nbsp; &nbsp; &nbsp; &nbsp; G&#91;\u5355\u4e2a Batch] --&gt; H&#91;\u6a21\u578b\u8ba1\u7b97&lt;br&gt;\uff08\u524d\u5411\u4f20\u64ad\u3001\u53cd\u5411\u4f20\u64ad\uff09]<br>&nbsp; &nbsp; &nbsp; &nbsp; H --&gt; I&#91;\u53c2\u6570\u66f4\u65b0]<br>&nbsp; &nbsp; end<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Batch Size \u5927\u5c0f<\/th><th>\u8bad\u7ec3\u901f\u5ea6<\/th><th>\u5185\u5b58\u6d88\u8017<\/th><th>\u68af\u5ea6\u7a33\u5b9a\u6027<\/th><th>\u6cdb\u5316\u80fd\u529b<\/th><th>\u5e38\u89c1\u573a\u666f<\/th><\/tr><\/thead><tbody><tr><td><strong>\u5c0f<\/strong>&nbsp;(e.g., 16, 32)<\/td><td>\u8f83\u6162<\/td><td>\u8f83\u4f4e<\/td><td>\u8f83\u4f4e\uff08\u9707\u8361\uff09<\/td><td><strong>\u8f83\u597d<\/strong><\/td><td>\u8d44\u6e90\u6709\u9650\u3001\u8ffd\u6c42\u6cdb\u5316<\/td><\/tr><tr><td><strong>\u4e2d<\/strong>&nbsp;(e.g., 64, 128)<\/td><td>\u4e2d\u7b49<\/td><td>\u4e2d\u7b49<\/td><td>\u4e2d\u7b49<\/td><td>\u4e2d\u7b49<\/td><td>\u5e38\u7528\u8d77\u70b9\u3001\u5e73\u8861\u9009\u62e9<\/td><\/tr><tr><td><strong>\u5927<\/strong>&nbsp;(e.g., 256, 512)<\/td><td><strong>\u8f83\u5feb<\/strong><\/td><td><strong>\u8f83\u9ad8<\/strong><\/td><td><strong>\u8f83\u9ad8\uff08\u5e73\u6ed1\uff09<\/strong><\/td><td>\u8f83\u5dee<\/td><td>\u5927\u6570\u636e\u96c6\u3001\u7a33\u5b9a\u68af\u5ea6<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83e\udde0 \u51e0\u4e2a\u76f8\u5173\u6982\u5ff5<\/p>\n<\/blockquote>\n\n\n\n<p>\u7406\u89e3 Batch \u65f6\uff0c\u901a\u5e38\u4f1a\u540c\u65f6\u63d0\u5230\u51e0\u4e2a\u76f8\u5173\u6982\u5ff5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Epoch\uff08\u65f6\u671f\/\u8f6e\u6b21\uff09<\/strong>\uff1a\u4f7f\u7528<strong>\u6574\u4e2a\u8bad\u7ec3\u6570\u636e\u96c6<\/strong>\u5b8c\u6574\u5730\u901a\u8fc7\u6a21\u578b\u8bad\u7ec3\u4e00\u6b21\u7684\u8fc7\u7a0b\u79f0\u4e3a\u4e00\u4e2a Epoch\u3002\u4e00\u4e2a Epoch \u901a\u5e38\u5305\u542b\u591a\u4e2a Batch\u3002<\/li>\n\n\n\n<li><strong>Iteration\uff08\u8fed\u4ee3\uff09<\/strong>\uff1a\u5b8c\u6210<strong>\u4e00\u4e2a Batch<\/strong>\u00a0\u7684\u8bad\u7ec3\u548c\u53c2\u6570\u66f4\u65b0\uff0c\u79f0\u4e3a\u4e00\u6b21 Iteration\u3002\u4f8b\u5982\uff0c\u82e5\u8bad\u7ec3\u96c6\u6709 8000 \u4e2a\u6837\u672c\uff0cBatch Size \u8bbe\u4e3a 200\uff0c\u5219\u5b8c\u6210\u4e00\u4e2a Epoch \u9700\u8981 8000\/200 = 40 \u6b21 Iteration\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2696\ufe0f \u5982\u4f55\u9009\u62e9 Batch Size<\/p>\n<\/blockquote>\n\n\n\n<p>\u9009\u62e9 Batch Size \u65f6\uff0c\u9700\u8981\u5728<strong>\u5185\u5b58\u6d88\u8017<\/strong>\u3001<strong>\u8bad\u7ec3\u901f\u5ea6<\/strong>\u3001<strong>\u68af\u5ea6\u7a33\u5b9a\u6027<\/strong>\u548c<strong>\u6a21\u578b\u6cdb\u5316\u80fd\u529b<\/strong>\u4e4b\u95f4\u8fdb\u884c\u6743\u8861\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5c1d\u8bd5\u5e38\u89c1\u7684 2 \u7684\u5e42\u6b21\u65b9<\/strong>\uff1a\u5982 16, 32, 64, 128, 256 \u7b49\u3002\u8fd9\u662f\u56e0\u4e3a\u5185\u5b58\u64cd\u4f5c\u548c\u786c\u4ef6\u4f18\u5316\u65f6\uff0c2 \u7684\u5e42\u6b21\u65b9\u901a\u5e38\u6548\u7387\u66f4\u9ad8\u3002<\/li>\n\n\n\n<li><strong>\u4ece\u5927\u5230\u5c0f\u5c1d\u8bd5<\/strong>\uff1a\u53ef\u4ee5<strong>\u5148\u5c06 Batch Size \u8bbe\u7f6e\u5f97\u5927\u4e00\u4e9b\uff08\u5982 64 \u6216 128\uff09<\/strong>\uff0c\u89c2\u5bdf\u8bad\u7ec3\u635f\u5931\uff08Loss\uff09\u7684\u6536\u655b\u60c5\u51b5\u3002\u5982\u679c\u6536\u655b\u4e0d\u4f73\uff08\u5982\u6ce2\u52a8\u5f88\u5927\uff09\uff0c\u518d\u5c1d\u8bd5<strong>\u8c03\u5c0f Batch Size<\/strong>\u3002<\/li>\n\n\n\n<li><strong>\u5173\u6ce8\u786c\u4ef6\u9650\u5236<\/strong>\uff1a\u786e\u4fdd\u9009\u62e9\u7684 Batch Size \u4e0d\u4f1a\u5bfc\u81f4 GPU \u6216 CPU \u5185\u5b58\u6ea2\u51fa\uff08Out Of Memory, OOM\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udc8e \u8bb0\u4f4f\u8981\u70b9<\/p>\n<\/blockquote>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Batch Size \u662f\u91cd\u8981\u7684\u8d85\u53c2\u6570<\/strong>\uff1a\u9700\u8981\u6839\u636e\u5177\u4f53\u4efb\u52a1\u3001\u6570\u636e\u96c6\u548c\u786c\u4ef6\u6761\u4ef6\u8fdb\u884c\u8c03\u6574\u548c\u5b9e\u9a8c\u3002<\/li>\n\n\n\n<li><strong>\u6ca1\u6709\u7edd\u5bf9\u6700\u4f18\u503c<\/strong>\uff1a\u4e0d\u540c\u7684\u6a21\u578b\u548c\u6570\u636e\u96c6\uff0c\u5176\u6700\u4f18\u7684 Batch Size \u53ef\u80fd\u4e0d\u540c\u3002<\/li>\n\n\n\n<li><strong>\u4e0e\u5176\u4ed6\u8d85\u53c2\u6570\u8054\u52a8<\/strong>\uff1a\u8c03\u6574 Batch Size \u65f6\uff0c\u6709\u65f6\u4e5f\u9700\u8981<strong>\u8054\u52a8\u8c03\u6574\u5b66\u4e60\u7387\uff08Learning Rate\uff09<\/strong>\u00a0\u7b49\u5176\u4ed6\u8d85\u53c2\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3.2 \u6570\u636e\u52a0\u8f7d\u548c\u5904\u7406<\/h2>\n\n\n\n<p>\u5728\u6df1\u5ea6\u5b66\u4e60\u9879\u76ee\u4e2d\uff0c\u9664\u4e86\u6a21\u578b\u8bbe\u8ba1\u4e4b\u5916\uff0c\u6570\u636e\u7684\u52a0\u8f7d\u548c\u5904\u7406\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002PyTorch\u63d0\u4f9b\u4e86torch.utils.data.DataLoader\u7c7b\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u7684\u52a0\u8f7d\u548c\u5904\u7406\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.2.1 DataLoader\u4ecb\u7ecd<\/h3>\n\n\n\n<p>DataLoader\u7c7b\u63d0\u4f9b\u4e86\u5bf9\u6570\u636e\u96c6\u7684\u5e76\u884c\u52a0\u8f7d\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u52a0\u8f7d\u5927\u91cf\u6570\u636e\uff0c\u5e76\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u91c7\u6837\u65b9\u5f0f\u3002\u5e38\u7528\u7684\u53c2\u6570\u6709\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>dataset\uff1a\u52a0\u8f7d\u7684\u6570\u636e\u96c6\uff08Dataset\u5bf9\u8c61\uff09<\/li>\n\n\n\n<li>batch_size\uff1abatch\u5927\u5c0f<\/li>\n\n\n\n<li>shuffle\uff1a\u662f\u5426\u6bcf\u4e2aepoch\u65f6\u90fd\u6253\u4e71\u6570\u636e<\/li>\n\n\n\n<li>num_workers\uff1a\u4f7f\u7528\u591a\u8fdb\u7a0b\u52a0\u8f7d\u7684\u8fdb\u7a0b\u6570\uff0c0\u8868\u793a\u4e0d\u4f7f\u7528\u591a\u8fdb\u7a0b \u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f7f\u7528\u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>from torch.utils.data import DataLoader<br>from torchvision import datasets, transforms<br><br># \u6570\u636e\u8f6c\u6362<br>transform = transforms.Compose(&#91;<br>&nbsp; &nbsp; transforms.ToTensor(),<br>&nbsp; &nbsp; transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))<br>])<br><br># \u4e0b\u8f7d\u5e76\u52a0\u8f7d\u8bad\u7ec3\u96c6<br>trainset = datasets.CIFAR10(root='.\/data', train=True, download=True, transform=transform)<br>trainloader = DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2)<br><br># \u4e0b\u8f7d\u5e76\u52a0\u8f7d\u6d4b\u8bd5\u96c6<br>testset = datasets.CIFAR10(root='.\/data', train=False, download=True, transform=transform)<br>testloader = DataLoader(testset, batch_size=4, shuffle=False, num_workers=2)<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u6765\u6765\u7ee7\u7eed\u8bfb\u4ee3\u7801<\/p>\n<\/blockquote>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u662f\u4f7f\u7528PyTorch\u51c6\u5907CIFAR-10\u6570\u636e\u96c6\u5e76\u8fdb\u884c\u6570\u636e\u52a0\u8f7d\u7684\u5e38\u89c1\u505a\u6cd5\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udce6 PyTorch \u6570\u636e\u52a0\u8f7d\u4ee3\u7801\u89e3\u6790<\/p>\n<\/blockquote>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u8fd9\u6bb5\u4ee3\u7801\u5c31\u50cf\u662f<strong>\u4e3a\u6a21\u578b\u8bad\u7ec3\u51c6\u5907\u4e00\u4e2a\u81ea\u52a8\u5316\u7684\u201c\u98df\u6750\u5904\u7406\u6d41\u6c34\u7ebf\u201d<\/strong>\uff0c\u5b83\u8d1f\u8d23\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4e0b\u8f7d\u6570\u636e<\/strong>\uff08\u5c31\u50cf\u83b7\u53d6\u539f\u59cb\u98df\u6750\uff09<\/li>\n\n\n\n<li><strong>\u8f6c\u6362\u6570\u636e<\/strong>\uff08\u5c31\u50cf\u6e05\u6d17\u3001\u5207\u5272\u3001\u814c\u5236\u98df\u6750\uff09<\/li>\n\n\n\n<li><strong>\u5206\u6279\u52a0\u8f7d\u6570\u636e<\/strong>\uff08\u5c31\u50cf\u5c06\u5904\u7406\u597d\u7684\u98df\u6750\u6309\u4efd\u88c5\u76d8\uff0c\u65b9\u4fbf\u53a8\u5e08\u53d6\u7528\uff09<\/li>\n<\/ol>\n\n\n\n<p>\u4e0b\u9762\u662f\u8fd9\u6bb5\u4ee3\u7801\u7684\u9010\u884c\u89e3\u91ca\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udd27 1. \u5bfc\u5165\u5fc5\u8981\u7684\u5de5\u5177\u5305<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>from&nbsp;torch.utils.data&nbsp;import&nbsp;DataLoader<br>from&nbsp;torchvision&nbsp;import&nbsp;datasets, transforms<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>DataLoader<\/code>: PyTorch \u4e2d\u7528\u4e8e<strong>\u6279\u91cf\u52a0\u8f7d\u6570\u636e<\/strong>\u7684\u6838\u5fc3\u5de5\u5177\uff0c\u5b83\u652f\u6301\u81ea\u52a8\u6279\u5904\u7406\u3001\u6570\u636e\u6253\u4e71\u3001\u591a\u8fdb\u7a0b\u52a0\u8f7d\u7b49\u529f\u80fd \u3002<\/li>\n\n\n\n<li><code>datasets<\/code>: \u63d0\u4f9b\u5e38\u7528\u6570\u636e\u96c6\uff08\u5982 CIFAR-10\u3001MNIST\uff09\u7684\u5feb\u901f\u4e0b\u8f7d\u548c\u52a0\u8f7d\u65b9\u5f0f \u3002<\/li>\n\n\n\n<li><code>transforms<\/code>: \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u6570\u636e\u9884\u5904\u7406\u548c\u6570\u636e\u589e\u5f3a\u7684\u65b9\u6cd5 \u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83c\udf00 2. \u5b9a\u4e49\u6570\u636e\u8f6c\u6362\uff08\u9884\u5904\u7406\uff09\u6d41\u7a0b<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>transform = transforms.Compose(&#91;<br>&nbsp; &nbsp; transforms.ToTensor(),<br>&nbsp; &nbsp; transforms.Normalize((0.5,&nbsp;0.5,&nbsp;0.5), (0.5,&nbsp;0.5,&nbsp;0.5))<br>])<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u5b9a\u4e49\u4e86\u4e00\u4e2a<strong>\u6570\u636e\u9884\u5904\u7406\u6d41\u6c34\u7ebf<\/strong>\uff0c\u4f7f\u7528&nbsp;<code>transforms.Compose<\/code>&nbsp;\u5c06\u591a\u4e2a\u6b65\u9aa4\u7ec4\u5408\u5728\u4e00\u8d77 \uff1ahttps:\/\/wxa.wxs.qq.com\/tmpl\/oc\/base_tmpl.html<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>transforms.ToTensor()<\/code>: \u5c06 PIL \u56fe\u50cf\u6216 NumPy \u6570\u7ec4\u8f6c\u6362\u4e3a PyTorch \u5f20\u91cf\uff08Tensor\uff09\u3002\u540c\u65f6\u4f1a\u5c06\u56fe\u50cf\u6570\u636e\u4ece\u539f\u59cb\u7684\u00a0<code>[0, 255]<\/code>\u00a0\u8303\u56f4\u6216\u00a0<code>[0, 1]<\/code>\u00a0\u7684 PIL \u56fe\u50cf\u683c\u5f0f\u8f6c\u6362\u4e3a\u00a0<code>[0.0, 1.0]<\/code>\u00a0\u7684\u6d6e\u70b9\u6570\u8303\u56f4\uff0c\u5e76\u8c03\u6574\u7ef4\u5ea6\u987a\u5e8f\u4e3a PyTorch \u9884\u671f\u7684\u00a0<code>(C, H, W)<\/code>\uff08\u901a\u9053\uff0c\u9ad8\uff0c\u5bbd\uff09\u683c\u5f0f \u3002<\/li>\n\n\n\n<li><code>transforms.Normalize(mean, std)<\/code>: \u5bf9\u5f20\u91cf\u8fdb\u884c<strong>\u6807\u51c6\u5316<\/strong>\uff08\u5f52\u4e00\u5316\uff09\uff0c\u8fd9\u91cc\u4f7f\u7528\u7684\u662f\u5747\u503c\u00a0<code>mean=(0.5, 0.5, 0.5)<\/code>\u00a0\u548c\u6807\u51c6\u5dee\u00a0<code>std=(0.5, 0.5, 0.5)<\/code>\u3002\u8ba1\u7b97\u516c\u5f0f\u4e3a\uff1a<code>output = (input - mean) \/ std<\/code>\u00a0\u3002\n<ul class=\"wp-block-list\">\n<li>\u56e0\u4e3a\u539f\u59cb\u6570\u636e\u7ecf\u8fc7\u00a0<code>ToTensor<\/code>\u00a0\u540e\u8303\u56f4\u662f\u00a0<code>[0.0, 1.0]<\/code>\uff0c\u7ecf\u8fc7\u8fd9\u91cc\u7684\u5f52\u4e00\u5316\u540e\uff08<code>(0.0 - 0.5)\/0.5 = -1.0<\/code>,\u00a0<code>(1.0 - 0.5)\/0.5 = 1.0<\/code>\uff09\uff0c\u6bcf\u4e2a\u901a\u9053\u7684\u6570\u636e\u8303\u56f4\u5c31\u53d8\u6210\u4e86\u00a0<strong><code>[-1.0, 1.0]<\/code><\/strong>\u3002\u5f52\u4e00\u5316\u53ef\u4ee5\u4f7f\u6a21\u578b\u8bad\u7ec3\u66f4\u7a33\u5b9a\u3001\u6536\u655b\u66f4\u5feb\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udce5 3. \u4e0b\u8f7d\u5e76\u52a0\u8f7d\u8bad\u7ec3\u96c6<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>trainset = datasets.CIFAR10(root='.\/data', train=True, download=True, transform=transform)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u884c\u4ee3\u7801\u4f7f\u7528\u00a0<code>torchvision.datasets.CIFAR10<\/code>\u00a0\u6765\u52a0\u8f7d\u00a0<strong>CIFAR-10 \u6570\u636e\u96c6<\/strong>\u00a0\u3002<ul><li><code>\u98de\u673a\uff08airplane\uff09<\/code>\u3001<code>\u6c7d\u8f66\uff08automobile\uff09<\/code>\u3001<code>\u9e1f\uff08bird\uff09<\/code>\u3001<code>\u732b\uff08cat\uff09<\/code>\u3001<code>\u9e7f\uff08deer\uff09<\/code><\/li><li><code>\u72d7\uff08dog\uff09<\/code>\u3001<code>\u9752\u86d9\uff08frog\uff09<\/code>\u3001<code>\u9a6c\uff08horse\uff09<\/code>\u3001<code>\u8239\uff08ship\uff09<\/code>\u3001<code>\u5361\u8f66\uff08truck\uff09<\/code><\/li><\/ul>\n<ul class=\"wp-block-list\">\n<li>CIFAR-10 \u6570\u636e\u96c6\u5305\u542b 10 \u4e2a\u7c7b\u522b\u7684 6 \u4e07\u5f20 32x32 \u5f69\u8272\u56fe\u50cf\uff1a<\/li>\n\n\n\n<li>\u5176\u4e2d 5 \u4e07\u5f20\u7528\u4e8e\u8bad\u7ec3\uff0c1 \u4e07\u5f20\u7528\u4e8e\u6d4b\u8bd5 \u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u53c2\u6570\u8bf4\u660e<\/strong>\u00a0\uff1a\n<ul class=\"wp-block-list\">\n<li><code>root='.\/data'<\/code>: \u6307\u5b9a\u6570\u636e\u96c6\u4e0b\u8f7d\u548c\u5b58\u50a8\u7684\u6839\u76ee\u5f55\u3002<\/li>\n\n\n\n<li><code>train=True<\/code>: \u6307\u5b9a\u52a0\u8f7d\u7684\u662f<strong>\u8bad\u7ec3\u96c6<\/strong>\uff08\u5982\u679c\u4e3a\u00a0<code>False<\/code>\u00a0\u5219\u52a0\u8f7d\u6d4b\u8bd5\u96c6\uff09\u3002<\/li>\n\n\n\n<li><code>download=True<\/code>: \u5982\u679c\u6307\u5b9a\u8def\u5f84\u4e0b\u6ca1\u6709\u6570\u636e\uff0c\u5219\u81ea\u52a8\u4ece\u4e92\u8054\u7f51\u4e0b\u8f7d\u3002<\/li>\n\n\n\n<li><code>transform=transform<\/code>: \u5c06\u4e4b\u524d\u5b9a\u4e49\u7684\u6570\u636e\u9884\u5904\u7406\u6d41\u6c34\u7ebf\u5e94\u7528\u4e8e\u52a0\u8f7d\u7684\u6bcf\u4e00\u5f20\u56fe\u50cf\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\ude9a 4. \u521b\u5efa\u8bad\u7ec3\u96c6\u7684\u6570\u636e\u52a0\u8f7d\u5668 (DataLoader)<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>trainloader = DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u884c\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u00a0<strong><code>DataLoader<\/code><\/strong>\u00a0\u5bf9\u8c61\uff0c\u5b83\u8d1f\u8d23\u4ece\u6570\u636e\u96c6\u4e2d<strong>\u6309\u6279\u6b21\u62bd\u53d6\u6570\u636e<\/strong>\uff0c\u975e\u5e38\u9002\u5408\u5728\u8bad\u7ec3\u5faa\u73af\u4e2d\u8fed\u4ee3\u4f7f\u7528 \u3002<\/li>\n\n\n\n<li><strong>\u53c2\u6570\u8bf4\u660e<\/strong>\u00a0\uff1a\n<ul class=\"wp-block-list\">\n<li><code>dataset=trainset<\/code>: \u6307\u5b9a\u8981\u52a0\u8f7d\u7684\u6570\u636e\u96c6\u3002<\/li>\n\n\n\n<li><code>batch_size=4<\/code>: \u6307\u5b9a<strong>\u6bcf\u4e2a\u6279\u6b21\u5305\u542b\u7684\u6837\u672c\u6570\u91cf<\/strong>\u3002\u8fd9\u91cc\u8bbe\u7f6e\u4e3a 4\uff0c\u610f\u5473\u7740\u6bcf\u6b21\u8fed\u4ee3\u4f1a\u8fd4\u56de 4 \u5f20\u56fe\u50cf\u548c\u5bf9\u5e94\u7684 4 \u4e2a\u6807\u7b7e\u3002\u6279\u91cf\u5904\u7406\u53ef\u4ee5\u63d0\u9ad8\u5185\u5b58\u5229\u7528\u7387\u548c\u8ba1\u7b97\u6548\u7387 \u3002<\/li>\n\n\n\n<li><code>shuffle=True<\/code>: \u8868\u793a<strong>\u5728\u6bcf\u4e2a epoch\uff08\u8bad\u7ec3\u8f6e\u6b21\uff09\u5f00\u59cb\u65f6\u6253\u4e71\u6570\u636e\u987a\u5e8f<\/strong>\u00a0\u3002\u8fd9\u975e\u5e38\u91cd\u8981\uff0c\u53ef\u4ee5\u9632\u6b62\u6a21\u578b\u5b66\u4e60\u5230\u6570\u636e\u51fa\u73b0\u7684\u987a\u5e8f\u504f\u5dee\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u6a21\u578b\u7684\u6cdb\u5316\u80fd\u529b \u3002<\/li>\n\n\n\n<li><code>num_workers=2<\/code>: \u6307\u5b9a\u4f7f\u7528<strong>\u591a\u4e2a\u5b50\u8fdb\u7a0b\u6765\u5e76\u884c\u52a0\u8f7d\u6570\u636e<\/strong>\u00a0\u3002\u8fd9\u53ef\u4ee5\u663e\u8457\u52a0\u901f\u6570\u636e\u52a0\u8f7d\u8fc7\u7a0b\uff0c\u907f\u514d\u6570\u636e\u52a0\u8f7d\u6210\u4e3a\u8bad\u7ec3\u901f\u5ea6\u7684\u74f6\u9888\u3002\u901a\u5e38\u8bbe\u7f6e\u4e3a CPU \u6838\u5fc3\u6570\u7684 2-4 \u500d \u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udcca 5. \u4e0b\u8f7d\u5e76\u52a0\u8f7d\u6d4b\u8bd5\u96c6<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>testset = datasets.CIFAR10(root='.\/data', train=False, download=True, transform=transform)<br>testloader = DataLoader(testset, batch_size=4, shuffle=False, num_workers=2)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u4e24\u884c\u4ee3\u7801\u7684\u4f5c\u7528\u4e0e\u52a0\u8f7d\u8bad\u7ec3\u96c6\u7c7b\u4f3c\uff0c\u4f46\u6709\u4e00\u4e9b\u5173\u952e\u533a\u522b\uff1a\n<ul class=\"wp-block-list\">\n<li><code>train=False<\/code>: \u6307\u5b9a\u52a0\u8f7d\u7684\u662f<strong>\u6d4b\u8bd5\u96c6<\/strong>\uff08\u9a8c\u8bc1\u96c6\uff09\u3002<\/li>\n\n\n\n<li><code>shuffle=False<\/code>: \u6d4b\u8bd5\u65f6<strong>\u901a\u5e38\u4e0d\u9700\u8981\u6253\u4e71\u6570\u636e\u987a\u5e8f<\/strong>\u00a0\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u4e3a\u4e86\u66f4\u76f4\u89c2\u5730\u7406\u89e3\u6570\u636e\u5728\u6574\u4e2a\u6d41\u7a0b\u4e2d\u7684\u53d8\u5316\uff0c\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u7684\u56fe\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"330\" height=\"711\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060524758.png\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" alt=\"\" class=\"wp-image-6754 lazyload\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060524758.png 330w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060524758-139x300.png 139w\" sizes=\"auto, (max-width: 330px) 100vw, 330px\" \/><noscript><img loading=\"lazy\" decoding=\"async\" width=\"330\" height=\"711\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060524758.png\" alt=\"\" class=\"wp-image-6754\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060524758.png 330w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060524758-139x300.png 139w\" sizes=\"auto, (max-width: 330px) 100vw, 330px\" \/><\/noscript><\/figure>\n\n\n\n<p>\ud83d\udca1 \u5173\u952e\u8981\u70b9\u4e0e\u4f7f\u7528\u573a\u666f<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>DataLoader \u7684\u4f18\u52bf<\/strong>\u00a0:\n<ul class=\"wp-block-list\">\n<li><strong>\u6279\u5904\u7406 (Batching)<\/strong>: \u63d0\u9ad8\u5185\u5b58\u5229\u7528\u7387\u548c\u8ba1\u7b97\u6548\u7387\uff08GPU \u64c5\u957f\u5e76\u884c\u8ba1\u7b97\uff09\u3002<\/li>\n\n\n\n<li><strong>\u6253\u4e71 (Shuffling)<\/strong>: \u907f\u514d\u6a21\u578b\u5b66\u4e60\u5230\u65e0\u5173\u7684\u987a\u5e8f\u7279\u5f81\uff0c\u63d0\u5347\u6cdb\u5316\u80fd\u529b\u3002<\/li>\n\n\n\n<li><strong>\u5e76\u884c\u52a0\u8f7d (Multiprocessing)<\/strong>: \u4f7f\u7528\u00a0<code>num_workers<\/code>\u00a0\u52a0\u901f\u6570\u636e\u52a0\u8f7d\uff0c\u907f\u514d I\/O \u74f6\u9888\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u8bad\u7ec3\u96c6\u4e0e\u6d4b\u8bd5\u96c6\u7684\u4e0d\u540c\u5904\u7406<\/strong>:\n<ul class=\"wp-block-list\">\n<li>\u8bad\u7ec3\u96c6\u901a\u5e38\u9700\u8981\u00a0<code>shuffle=True<\/code>\uff0c\u800c\u6d4b\u8bd5\u96c6\u5219\u8bbe\u4e3a\u00a0<code>False<\/code>\u3002<\/li>\n\n\n\n<li>\u8bad\u7ec3\u548c\u6d4b\u8bd5\u901a\u5e38\u4f7f\u7528\u76f8\u540c\u7684\u00a0<code>transform<\/code>\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u5206\u5e03\u4e00\u81f4\u3002\u4f46\u6709\u65f6\u6d4b\u8bd5\u4f1a\u4f7f\u7528\u4e0d\u540c\u7684\u53d8\u6362\uff08\u4f8b\u5982\uff0c\u4e0d\u5305\u542b\u6570\u636e\u589e\u5f3a\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u540e\u7eed\u4f7f\u7528<\/strong>:\n<ul class=\"wp-block-list\">\n<li>\u5728\u8bad\u7ec3\u5faa\u73af\u4e2d\uff0c\u4f60\u4f1a\u8fd9\u6837\u4f7f\u7528\u5b83\uff1a<code>for\u00a0epoch\u00a0in\u00a0range(num_epochs):\u00a0<em># \u904d\u5386\u6240\u6709\u6570\u636e\u591a\u8f6e<\/em><br>\u00a0 \u00a0\u00a0for\u00a0images, labels\u00a0in\u00a0trainloader:\u00a0<em># \u8fed\u4ee3\u6279\u6b21<\/em><br>\u00a0 \u00a0 \u00a0 \u00a0\u00a0<em># 1. \u5c06 images \u8f93\u5165\u6a21\u578b\uff0c\u5f97\u5230\u9884\u6d4b\u8f93\u51fa<\/em><br>\u00a0 \u00a0 \u00a0 \u00a0\u00a0<em># 2. \u8ba1\u7b97\u635f\u5931 (loss)<\/em><br>\u00a0 \u00a0 \u00a0 \u00a0\u00a0<em># 3. \u53cd\u5411\u4f20\u64ad\uff0c\u66f4\u65b0\u6a21\u578b\u53c2\u6570<\/em><br>\u00a0 \u00a0 \u00a0 \u00a0 ...<\/code><\/li>\n\n\n\n<li>\u5728\u8bc4\u4f30\u6a21\u578b\u65f6\uff0c\u5219\u4f7f\u7528\u00a0<code>testloader<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u6570\u636e\u52a0\u8f7d\u662f\u6a21\u578b\u8bad\u7ec3\u7684\u7b2c\u4e00\u6b65\uff0c\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u6b65\u3002PyTorch \u7684&nbsp;<code>DataLoader<\/code>&nbsp;\u548c&nbsp;<code>torchvision.datasets<\/code>&nbsp;\u8ba9\u8fd9\u4e2a\u8fc7\u7a0b\u53d8\u5f97\u975e\u5e38\u7b80\u6d01\u548c\u9ad8\u6548\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.2.2 \u81ea\u5b9a\u4e49\u6570\u636e\u96c6<\/h3>\n\n\n\n<p>\u9664\u4e86\u4f7f\u7528\u5185\u7f6e\u7684\u6570\u636e\u96c6\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6570\u636e\u96c6\u3002\u81ea\u5b9a\u4e49\u6570\u636e\u96c6\u9700\u8981\u7ee7\u627fDataset\u7c7b\uff0c\u5e76\u5b9e\u73b0__len__\u548c__getitem__\u4e24\u4e2a\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6570\u636e\u96c6\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from torch.utils.data import Dataset, DataLoader<br><br>class MyDataset(Dataset):<br>&nbsp; &nbsp; def __init__(self, x_tensor, y_tensor):<br>&nbsp; &nbsp; &nbsp; &nbsp; self.x = x_tensor<br>&nbsp; &nbsp; &nbsp; &nbsp; self.y = y_tensor<br><br>&nbsp; &nbsp; def __getitem__(self, index):<br>&nbsp; &nbsp; &nbsp; &nbsp; return (self.x&#91;index], self.y&#91;index])<br><br>&nbsp; &nbsp; def __len__(self):<br>&nbsp; &nbsp; &nbsp; &nbsp; return len(self.x)<br><br>x = torch.arange(10)<br>y = torch.arange(10) + 1<br><br>my_dataset = MyDataset(x, y)<br>loader = DataLoader(my_dataset, batch_size=4, shuffle=True, num_workers=0)<br><br>for x, y in loader:<br>&nbsp; &nbsp; print(\"x:\", x, \"y:\", y)<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u96c6\uff0c\u5305\u542b10\u4e2a\u6570\u636e\u3002\u7136\u540e\u6211\u4eec\u4f7f\u7528DataLoader\u52a0\u8f7d\u6570\u636e\uff0c\u5e76\u8bbe\u7f6e\u4e86batch\u5927\u5c0f\u548cshuffle\u53c2\u6570\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662fPyTorch\u4e2d\u6570\u636e\u52a0\u8f7d\u548c\u5904\u7406\u7684\u4e3b\u8981\u65b9\u6cd5\uff0c\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u52a0\u8f7d\u548c\u5904\u7406\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7ee7\u7eed\u8bfb\u4ee3\u7801<\/p>\n<\/blockquote>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5728 PyTorch \u4e2d<strong>\u81ea\u5b9a\u4e49\u6570\u636e\u96c6 (Dataset)<\/strong>&nbsp;\u5e76\u4f7f\u7528&nbsp;<strong>\u6570\u636e\u52a0\u8f7d\u5668 (DataLoader)<\/strong>&nbsp;\u8fdb\u884c\u6279\u91cf\u52a0\u8f7d\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udce6 PyTorch \u81ea\u5b9a\u4e49\u6570\u636e\u96c6\u4e0e\u6570\u636e\u52a0\u8f7d\u5668\u89e3\u6790<\/p>\n<\/blockquote>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u7684\u6838\u5fc3\u662f\u6559\u4f60\u5982\u4f55\u5c06\u81ea\u5df1\u7684\u6570\u636e\u5305\u88c5\u6210 PyTorch \u80fd\u591f\u8bc6\u522b\u548c\u6279\u91cf\u5904\u7406\u7684\u683c\u5f0f\u3002\u6574\u4e2a\u8fc7\u7a0b\u53ef\u4ee5\u6982\u62ec\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"314\" height=\"590\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060529748.png\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" alt=\"\" class=\"wp-image-6755 lazyload\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060529748.png 314w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060529748-160x300.png 160w\" sizes=\"auto, (max-width: 314px) 100vw, 314px\" \/><noscript><img loading=\"lazy\" decoding=\"async\" width=\"314\" height=\"590\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060529748.png\" alt=\"\" class=\"wp-image-6755\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060529748.png 314w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060529748-160x300.png 160w\" sizes=\"auto, (max-width: 314px) 100vw, 314px\" \/><\/noscript><\/figure>\n\n\n\n<p>\ud83d\udd0d \u4ee3\u7801\u9010\u884c\u89e3\u8bfb<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol class=\"wp-block-list\">\n<li>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>from&nbsp;torch.utils.data&nbsp;import&nbsp;Dataset, DataLoader<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Dataset<\/code>: PyTorch \u4e2d\u6240\u6709\u6570\u636e\u96c6\u7684<strong>\u62bd\u8c61\u57fa\u7c7b<\/strong>\u3002\u4f60\u8981\u521b\u5efa\u81ea\u5df1\u7684\u6570\u636e\u96c6\uff0c\u5c31\u5fc5\u987b\u7ee7\u627f\u8fd9\u4e2a\u7c7b\u5e76\u5b9e\u73b0\u5176\u7279\u5b9a\u65b9\u6cd5 \u3002<\/li>\n\n\n\n<li><code>DataLoader<\/code>: \u4e00\u4e2a<strong>\u5305\u88c5\u5668<\/strong>\u00a0(Wrapper)\uff0c\u5b83\u63a5\u6536\u4e00\u4e2a\u00a0<code>Dataset<\/code>\u00a0\u5bf9\u8c61\u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u63d0\u4f9b<strong>\u6279\u91cf\u52a0\u8f7d<\/strong>\u3001<strong>\u6570\u636e\u6253\u4e71<\/strong>\u3001<strong>\u591a\u8fdb\u7a0b\u8bfb\u53d6<\/strong>\u7b49\u529f\u80fd\uff0c\u975e\u5e38\u4fbf\u4e8e\u5728\u8bad\u7ec3\u5faa\u73af\u4e2d\u8fed\u4ee3\u4f7f\u7528 \u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u5b9a\u4e49\u81ea\u5b9a\u4e49\u6570\u636e\u96c6\u7c7b\u00a0<code>MyDataset<\/code><\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>class&nbsp;MyDataset(Dataset):<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a&nbsp;<code>MyDataset<\/code>&nbsp;\u7684\u7c7b\uff0c\u5b83\u7ee7\u627f\u81ea&nbsp;<code>torch.utils.data.Dataset<\/code>\u3002\u7ee7\u627f&nbsp;<code>Dataset<\/code>&nbsp;\u7c7b\u540e\uff0c\u5fc5\u987b\u5b9e\u73b0&nbsp;<code>__init__<\/code>\u3001<code>__getitem__<\/code>&nbsp;\u548c&nbsp;<code>__len__<\/code>&nbsp;\u8fd9\u4e09\u4e2a\u65b9\u6cd5 \u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><code>__init__<\/code>&nbsp;\u65b9\u6cd5\uff1a\u521d\u59cb\u5316<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp; &nbsp;&nbsp;def&nbsp;__init__(self, x_tensor, y_tensor):<br>&nbsp; &nbsp; &nbsp; &nbsp; self.x = x_tensor<br>&nbsp; &nbsp; &nbsp; &nbsp; self.y = y_tensor<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u662f\u7c7b\u7684<strong>\u6784\u9020\u51fd\u6570<\/strong>\uff0c\u5728\u521b\u5efa\u6570\u636e\u96c6\u5bf9\u8c61\u65f6\u81ea\u52a8\u8c03\u7528\u3002<\/li>\n\n\n\n<li>\u5b83\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\u00a0<code>x_tensor<\/code>\u00a0\u548c\u00a0<code>y_tensor<\/code>\uff08\u901a\u5e38\u662f\u7279\u5f81\u6570\u636e\u548c\u6807\u7b7e\u6570\u636e\uff09\uff0c\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u4e3a\u7c7b\u7684\u5c5e\u6027\uff08<code>self.x<\/code>\u00a0\u548c\u00a0<code>self.y<\/code>\uff09\uff0c\u4ee5\u4fbf\u7c7b\u4e2d\u7684\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528 \u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><code>__getitem__<\/code>&nbsp;\u65b9\u6cd5\uff1a\u6309\u7d22\u5f15\u83b7\u53d6\u6837\u672c<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp; &nbsp;&nbsp;def&nbsp;__getitem__(self, index):<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;(self.x&#91;index], self.y&#91;index])<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u662f Dataset \u7c7b\u7684<strong>\u6838\u5fc3\u65b9\u6cd5\u4e4b\u4e00<\/strong>\u3002\u5f53\u4f60\u4f7f\u7528\u00a0<code>dataset[index]<\/code>\u00a0\u8fd9\u6837\u7684\u7d22\u5f15\u64cd\u4f5c\u65f6\uff0cPython \u4f1a\u81ea\u52a8\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li>\u5b83\u6839\u636e\u4f20\u5165\u7684\u00a0<code>index<\/code>\uff08\u7d22\u5f15\uff09\uff0c\u4ece\u4e4b\u524d\u5b58\u50a8\u7684\u00a0<code>self.x<\/code>\u00a0\u548c\u00a0<code>self.y<\/code>\u00a0\u4e2d\u53d6\u51fa\u5bf9\u5e94\u7684\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\u00a0<code>(\u7279\u5f81, \u6807\u7b7e)<\/code>\u3002<strong>\u8fd9\u6b63\u597d\u662f\u6a21\u578b\u8bad\u7ec3\u65f6\u6700\u9700\u8981\u7684\u6570\u636e\u5f62\u5f0f<\/strong>\u00a0\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><code>__len__<\/code>&nbsp;\u65b9\u6cd5\uff1a\u83b7\u53d6\u6570\u636e\u96c6\u5927\u5c0f<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp; &nbsp;&nbsp;def&nbsp;__len__(self):<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;len(self.x)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u662f\u53e6\u4e00\u4e2a\u6838\u5fc3\u65b9\u6cd5\u3002\u5f53\u4f60\u4f7f\u7528 Python \u7684\u5185\u7f6e\u51fd\u6570\u00a0<code>len(dataset)<\/code>\u00a0\u65f6\uff0c\u4f1a\u81ea\u52a8\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li>\u5b83\u8fd4\u56de\u6570\u636e\u96c6\u4e2d\u7684<strong>\u603b\u6837\u672c\u6570<\/strong>\uff0c\u901a\u5e38\u662f\u7279\u5f81\u6570\u636e\u00a0<code>self.x<\/code>\u00a0\u7684\u957f\u5ea6 \u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>x = torch.arange(10) &nbsp; &nbsp; &nbsp;<em># \u521b\u5efa\u4e00\u4e2a\u503c\u4e3a &#91;0, 1, 2, ..., 9] \u7684\u5f20\u91cf<\/em><br>y = torch.arange(10) +&nbsp;1&nbsp;&nbsp;<em># \u521b\u5efa\u4e00\u4e2a\u503c\u4e3a &#91;1, 2, 3, ..., 10] \u7684\u5f20\u91cf<\/em><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u91cc\u521b\u5efa\u4e86\u4e24\u4e2a\u7b80\u5355\u7684 PyTorch \u5f20\u91cf\u4f5c\u4e3a\u793a\u4f8b\u6570\u636e\u3002<code>x<\/code>\u00a0\u53ef\u4ee5\u770b\u4f5c\u662f\u7279\u5f81\uff0c<code>y<\/code>\u00a0\u662f\u5bf9\u5e94\u7684\u6807\u7b7e\uff08\u8fd9\u91cc\u53ea\u662f\u7b80\u5355\u6f14\u793a\uff0c\u5b9e\u9645\u6570\u636e\u53ef\u80fd\u662f\u56fe\u50cf\u3001\u6587\u672c\u7b49\uff09\u3002<\/li>\n\n\n\n<li><code>x[i]<\/code>\u00a0\u548c\u00a0<code>y[i]<\/code>\u00a0\u662f\u4e00\u4e00\u5bf9\u5e94\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"4\" class=\"wp-block-list\">\n<li>\u5b9e\u4f8b\u5316\u81ea\u5b9a\u4e49\u6570\u636e\u96c6<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>my_dataset = MyDataset(x, y)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u6211\u4eec\u5b9a\u4e49\u597d\u7684\u00a0<code>MyDataset<\/code>\u00a0\u7c7b\u4ee5\u53ca\u6570\u636e\u00a0<code>x<\/code>\u00a0\u548c\u00a0<code>y<\/code>\uff0c\u521b\u5efa\u4e00\u4e2a<strong>\u6570\u636e\u96c6\u5bf9\u8c61\u00a0<code>my_dataset<\/code><\/strong>\u3002<\/li>\n\n\n\n<li>\u73b0\u5728\uff0c<code>my_dataset<\/code>\u00a0\u5c31\u5177\u5907\u4e86 PyTorch Dataset \u7684\u6240\u6709\u884c\u4e3a\uff1a\n<ul class=\"wp-block-list\">\n<li>\u4f60\u53ef\u4ee5\u7528\u00a0<code>len(my_dataset)<\/code>\u00a0\u83b7\u53d6\u5b83\u7684\u957f\u5ea6\uff08\u8fd4\u56de 10\uff09\u3002<\/li>\n\n\n\n<li>\u4f60\u53ef\u4ee5\u7528\u00a0<code>my_dataset[i]<\/code>\u00a0\u83b7\u53d6\u7b2c i \u4e2a\u6837\u672c\uff08\u8fd4\u56de\u00a0<code>(x[i], y[i])<\/code>\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"5\" class=\"wp-block-list\">\n<li>\u521b\u5efa\u6570\u636e\u52a0\u8f7d\u5668 (DataLoader)<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>loader = DataLoader(my_dataset, batch_size=4, shuffle=True, num_workers=0)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u662f<strong>\u5173\u952e\u4e00\u6b65<\/strong>\u3002\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u00a0<code>DataLoader<\/code>\u00a0\u5bf9\u8c61\uff0c\u5b83\u5c06\u8d1f\u8d23\u4ece\u00a0<code>my_dataset<\/code>\u00a0\u4e2d<strong>\u6309\u6279\u6b21\u62bd\u53d6\u6570\u636e<\/strong>\u00a0\u3002<\/li>\n\n\n\n<li><strong>\u53c2\u6570\u89e3\u6790<\/strong>\u00a0\uff1a<ul><li><code>0<\/code>\u00a0\u8868\u793a<strong>\u53ea\u5728\u4e3b\u8fdb\u7a0b\u4e2d\u52a0\u8f7d\u6570\u636e<\/strong>\uff08\u9ed8\u8ba4\u503c\uff09\u3002<\/li><li>\u5982\u679c\u8bbe\u7f6e\u4e3a\u5927\u4e8e 0 \u7684\u6570\uff08\u5982 2\u30014\uff09\uff0c\u5219\u4f1a\u5f00\u542f\u591a\u4e2a\u5b50\u8fdb\u7a0b\u6765\u5e76\u884c\u52a0\u8f7d\u6570\u636e\uff0c<strong>\u8fd9\u901a\u5e38\u53ef\u4ee5\u663e\u8457\u52a0\u901f\u6570\u636e\u52a0\u8f7d\u8fc7\u7a0b<\/strong>\uff0c\u907f\u514d\u6570\u636e\u52a0\u8f7d\u6210\u4e3a\u8bad\u7ec3\u901f\u5ea6\u7684\u74f6\u9888\u3002\u4f46\u5728\u67d0\u4e9b\u73af\u5883\uff08\u5982 Windows \u4e0b\u7684 Jupyter Notebook\uff09\u4e0b\uff0c\u591a\u8fdb\u7a0b\u8bbe\u7f6e\u6709\u65f6\u4f1a\u51fa\u9519\uff0c\u6240\u4ee5\u793a\u4f8b\u4e2d\u5148\u7528 0\u3002<\/li><\/ul>\n<ul class=\"wp-block-list\">\n<li><code>dataset=my_dataset<\/code>: \u6307\u5b9a\u8981\u52a0\u8f7d\u7684\u6570\u636e\u96c6\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><code>batch_size=4<\/code>:\u00a0<strong>\u6279\u6b21\u5927\u5c0f<\/strong>\u3002\u8fd9\u662f\u6700\u91cd\u8981\u7684\u53c2\u6570\u4e4b\u4e00\uff0c\u5b83\u544a\u8bc9 DataLoader \u6bcf\u6b21\u8fed\u4ee3\u8fd4\u56de\u00a0<strong>4 \u4e2a\u6837\u672c<\/strong>\uff08\u800c\u4e0d\u662f\u4e00\u4e2a\u4e00\u4e2a\u5730\u8fd4\u56de\uff09\u3002\u6279\u91cf\u5904\u7406\u80fd\u6781\u5927\u63d0\u9ad8\u8bad\u7ec3\u6548\u7387\u3002<\/li>\n\n\n\n<li><code>shuffle=True<\/code>:\u00a0<strong>\u662f\u5426\u6253\u4e71\u6570\u636e<\/strong>\u3002\u5728\u8bad\u7ec3\u6a21\u578b\u65f6\uff0c\u901a\u5e38\u8981\u8bbe\u7f6e\u4e3a\u00a0<code>True<\/code>\uff0c\u8fd9\u6837\u6bcf\u4e2a epoch\uff08\u904d\u5386\u6240\u6709\u6570\u636e\u7684\u4e00\u6b21\u5faa\u73af\uff09\u5f00\u59cb\u65f6\uff0c\u6570\u636e\u987a\u5e8f\u90fd\u4f1a\u88ab\u968f\u673a\u6253\u4e71\u3002\u8fd9\u6709\u52a9\u4e8e\u9632\u6b62\u6a21\u578b\u5b66\u4e60\u5230\u6570\u636e\u987a\u5e8f\u7684\u504f\u5dee\uff0c\u63d0\u5347\u6cdb\u5316\u80fd\u529b\u3002<strong>\u5bf9\u4e8e\u6d4b\u8bd5\u96c6\u6216\u9a8c\u8bc1\u96c6\uff0c\u901a\u5e38\u8bbe\u7f6e\u4e3a\u00a0<code>False<\/code><\/strong>\u3002<\/li>\n\n\n\n<li><code>num_workers=0<\/code>:\u00a0<strong>\u7528\u4e8e\u6570\u636e\u52a0\u8f7d\u7684\u5b50\u8fdb\u7a0b\u6570<\/strong>\u00a0\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"6\" class=\"wp-block-list\">\n<li>\u904d\u5386\u6570\u636e\u52a0\u8f7d\u5668<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>for&nbsp;x_batch, y_batch&nbsp;in&nbsp;loader:<br>&nbsp; &nbsp; print(\"x:\", x_batch,&nbsp;\"y:\", y_batch)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u91cc\u6f14\u793a\u4e86\u5982\u4f55\u5728\u8bad\u7ec3\u5faa\u73af\u4e2d\u4f7f\u7528\u00a0<code>DataLoader<\/code>\u3002<strong>\u76f4\u63a5\u8fed\u4ee3\u00a0<code>loader<\/code>\u00a0\u5bf9\u8c61\u5373\u53ef<\/strong>\u3002<\/li>\n\n\n\n<li>\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\uff1a\n<ul class=\"wp-block-list\">\n<li><code>x_batch<\/code>\u00a0\u662f\u4e00\u4e2a\u5305\u542b\u00a0<strong>4 \u4e2a\u7279\u5f81<\/strong>\u00a0\u7684\u5f20\u91cf\uff08\u56e0\u4e3a\u00a0<code>batch_size=4<\/code>\uff09\u3002<\/li>\n\n\n\n<li><code>y_batch<\/code>\u00a0\u662f\u5bf9\u5e94\u7684\u00a0<strong>4 \u4e2a\u6807\u7b7e<\/strong>\u00a0\u7684\u5f20\u91cf\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u7531\u4e8e\u8bbe\u7f6e\u4e86\u00a0<code>shuffle=True<\/code>\uff0c<strong>\u6bcf\u6b21\u8fd0\u884c\u4ee3\u7801\u65f6\uff0c\u6253\u5370\u51fa\u7684\u6279\u6b21\u987a\u5e8f\u548c\u6279\u6b21\u5185\u7684\u6837\u672c\u7ec4\u5408\u5f88\u53ef\u80fd\u90fd\u4e0d\u4e00\u6837<\/strong>\u3002\u4f8b\u5982\uff0c\u4e00\u6b21\u8fd0\u884c\u53ef\u80fd\u8f93\u51fa\uff1a<code>x: tensor([0, 1, 2, 3]) y: tensor([1, 2, 3, 4])<br>x: tensor([4, 5, 6, 7]) y: tensor([5, 6, 7, 8])<br>x: tensor([8, 9]) y: tensor([9, 10]) # \u6700\u540e\u4e00\u4e2a\u6279\u6b21\u4e0d\u8db34\u4e2a\u6837\u672c<br><\/code>\u53e6\u4e00\u6b21\u8fd0\u884c\u53ef\u80fd\u8f93\u51fa\uff1a<code>x: tensor([5, 7, 3, 9]) y: tensor([6, 8, 4, 10])<br>x: tensor([4, 0, 1, 8]) y: tensor([5, 1, 2, 9])<br>x: tensor([6, 2]) y: tensor([7, 3])<\/code><\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udca1 \u5173\u952e\u8981\u70b9\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/blockquote>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>Dataset<\/code>\u00a0\u662f\u201c\u83dc\u201d\uff0c<code>DataLoader<\/code>\u00a0\u662f\u201c\u6253\u996d\u7684\u5e08\u5085\u201d<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>Dataset<\/code>\u00a0\u8d1f\u8d23\u5b9a\u4e49\u6570\u636e\u5982\u4f55\u5b58\u50a8\u4ee5\u53ca\u5982\u4f55\u901a\u8fc7\u7d22\u5f15\u83b7\u53d6<strong>\u5355\u4e2a<\/strong>\u6837\u672c\u00a0<code>(x_i, y_i)<\/code>\u3002<\/li>\n\n\n\n<li><code>DataLoader<\/code>\u00a0\u8d1f\u8d23\u4ece\u00a0<code>Dataset<\/code>\u00a0\u4e2d\u53d6\u6570\u636e\uff0c\u5e76\u6309\u4f60\u8bbe\u5b9a\u7684\u89c4\u5219\uff08\u6279\u91cf\u3001\u6253\u4e71\u3001\u5e76\u884c\uff09\u7ec4\u6210<strong>\u6279\u6b21<\/strong><code>(x_batch, y_batch)<\/code>\u00a0\u9001\u7ed9\u4f60\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>shuffle=True<\/code>\u00a0\u7684\u91cd\u8981\u6027<\/strong>\uff1a\u5728<strong>\u8bad\u7ec3\u9636\u6bb5<\/strong>\u52a1\u5fc5\u8bbe\u7f6e\u00a0<code>shuffle=True<\/code>\uff0c\u9632\u6b62\u6a21\u578b\u8fc7\u62df\u5408\u4e8e\u6570\u636e\u7684\u987a\u5e8f\u3002\u5728<strong>\u9a8c\u8bc1\/\u6d4b\u8bd5\u9636\u6bb5<\/strong>\u5219\u5e94\u8bbe\u7f6e\u4e3a\u00a0<code>False<\/code>\uff0c\u4ee5\u786e\u4fdd\u7ed3\u679c\u7684\u53ef\u91cd\u590d\u6027\u548c\u6b63\u786e\u8bc4\u4f30\u3002<\/li>\n\n\n\n<li><code>num_workers<\/code>\u00a0\u7684\u8bbe\u5b9a\uff1a\u5728\u672c\u5730\u73af\u5883\u8c03\u8bd5\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u589e\u52a0\u00a0<code>num_workers<\/code>\uff08\u5982\u8bbe\u7f6e\u4e3a 2\u30014\uff09\u6765<strong>\u52a0\u901f\u6570\u636e\u52a0\u8f7d<\/strong>\u3002\u4f46\u8981\u6ce8\u610f\uff0c\u5e76\u975e workers \u8d8a\u591a\u8d8a\u597d\uff0c\u4e00\u822c\u8bbe\u7f6e\u4e3a CPU \u6838\u5fc3\u6570\u6216\u5176\u4e24\u500d\u5de6\u53f3\u5373\u53ef\u3002<\/li>\n\n\n\n<li><strong>\u5904\u7406\u6700\u540e\u4e00\u4e2a\u4e0d\u5b8c\u6574\u7684\u6279\u6b21<\/strong>\uff1a\u5f53\u6570\u636e\u96c6\u5927\u5c0f\u4e0d\u80fd\u88ab\u00a0<code>batch_size<\/code>\u00a0\u6574\u9664\u65f6\uff0c\u6700\u540e\u4e00\u4e2a\u6279\u6b21\u4f1a\u5c0f\u4e8e\u8bbe\u5b9a\u7684\u6279\u6b21\u5927\u5c0f\u3002\u5982\u679c\u4f60\u5e0c\u671b\u4e22\u5f03\u6700\u540e\u4e00\u4e2a\u4e0d\u5b8c\u6574\u7684\u6279\u6b21\uff08\u786e\u4fdd\u6bcf\u4e2a\u6279\u6b21\u5927\u5c0f\u4e25\u683c\u4e00\u81f4\uff09\uff0c\u53ef\u4ee5\u5728\u00a0<code>DataLoader<\/code>\u00a0\u4e2d\u8bbe\u7f6e\u53c2\u6570\u00a0<code>drop_last=True<\/code>\u00a0\u3002<\/li>\n\n\n\n<li><strong>\u771f\u5b9e\u4e16\u754c\u7684\u5e94\u7528<\/strong>\uff1a\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u7684\u6570\u636e\u662f\u7b80\u5355\u7684\u5f20\u91cf\u3002\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u4f60\u7684\u00a0<code>__getitem__<\/code>\u00a0\u65b9\u6cd5\u91cc\u53ef\u80fd\u4f1a\u5305\u542b\uff1a\n<ul class=\"wp-block-list\">\n<li>\u4ece\u6587\u4ef6\u4e2d\u52a0\u8f7d\u56fe\u50cf\u6216\u97f3\u9891\u3002<\/li>\n\n\n\n<li>\u590d\u6742\u7684\u6570\u636e\u9884\u5904\u7406\u548c\u6570\u636e\u589e\u5f3a\uff08\u5982\u968f\u673a\u88c1\u526a\u3001\u7ffb\u8f6c\u7b49\uff09\u3002<\/li>\n\n\n\n<li>\u6587\u672c\u7684\u5206\u8bcd\u548c\u7f16\u7801\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">3.3 \u6a21\u578b\u7684\u4fdd\u5b58\u548c\u52a0\u8f7d<\/h2>\n\n\n\n<p>\u5728\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u7684\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u4fdd\u5b58\u6a21\u578b\u7684\u53c2\u6570\u4ee5\u4fbf\u4e8e\u5c06\u6765\u91cd\u65b0\u52a0\u8f7d\u3002\u8fd9\u5bf9\u4e8e\u4e2d\u65ad\u7684\u8bad\u7ec3\u8fc7\u7a0b\u7684\u6062\u590d\uff0c\u6216\u8005\u7528\u4e8e\u6a21\u578b\u7684\u5206\u4eab\u548c\u90e8\u7f72\u90fd\u662f\u975e\u5e38\u6709\u7528\u7684\u3002<\/p>\n\n\n\n<p>PyTorch\u63d0\u4f9b\u4e86\u7b80\u5355\u7684API\u6765\u4fdd\u5b58\u548c\u52a0\u8f7d\u6a21\u578b\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528torch.save\u6765\u4fdd\u5b58\u6a21\u578b\u7684\u53c2\u6570\uff0c\u7136\u540e\u901a\u8fc7torch.load\u6765\u52a0\u8f7d\u6a21\u578b\u7684\u53c2\u6570\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.3.1 \u4fdd\u5b58\u548c\u52a0\u8f7d\u6a21\u578b\u53c2\u6570<\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4fdd\u5b58<br>torch.save(model.state_dict(), PATH)<br><br># \u52a0\u8f7d<br>model = TheModelClass(*args, **kwargs)<br>model.load_state_dict(torch.load(PATH))<br>model.eval()<\/code><\/pre>\n\n\n\n<p>\u5728\u4fdd\u5b58\u6a21\u578b\u53c2\u6570\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528.state_dict()\u65b9\u6cd5\u6765\u83b7\u53d6\u6a21\u578b\u7684\u53c2\u6570\u3002.state_dict()\u662f\u4e00\u4e2a\u4ece\u53c2\u6570\u540d\u5b57\u6620\u5c04\u5230\u53c2\u6570\u503c\u7684\u5b57\u5178\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u5728\u52a0\u8f7d\u6a21\u578b\u53c2\u6570\u65f6\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u5b9e\u4f8b\u5316\u4e00\u4e2a\u548c\u539f\u6a21\u578b\u7ed3\u6784\u76f8\u540c\u7684\u6a21\u578b\uff0c\u7136\u540e\u4f7f\u7528.load_state_dict()\u65b9\u6cd5\u52a0\u8f7d\u53c2\u6570\u3002<\/p>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0cload_state_dict()\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5b57\u5178\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u4fdd\u5b58\u5bf9\u8c61\u7684\u8def\u5f84\u3002\u8fd9\u610f\u5473\u7740\u5728\u4f60\u4f20\u5165load_state_dict()\u51fd\u6570\u4e4b\u524d\uff0c\u4f60\u5fc5\u987b\u53cd\u5e8f\u5217\u5316\u4f60\u7684\u4fdd\u5b58\u7684state_dict\u3002<\/p>\n\n\n\n<p>\u5728\u52a0\u8f7d\u6a21\u578b\u540e\uff0c\u6211\u4eec\u901a\u5e38\u8c03\u7528.eval()\u65b9\u6cd5\u5c06dropout\u548cbatch normalization\u5c42\u8bbe\u7f6e\u4e3a\u8bc4\u4f30\u6a21\u5f0f\u3002\u5426\u5219\uff0c\u5b83\u4eec\u4f1a\u5728\u8bc4\u4f30\u6a21\u5f0f\u4e0b\u4fdd\u6301\u8bad\u7ec3\u6a21\u5f0f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.3.2 \u4fdd\u5b58\u548c\u52a0\u8f7d\u6574\u4e2a\u6a21\u578b<\/h3>\n\n\n\n<p>\u9664\u4e86\u4fdd\u5b58\u6a21\u578b\u7684\u53c2\u6570\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4fdd\u5b58\u6574\u4e2a\u6a21\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4fdd\u5b58<br>torch.save(model, PATH)<br><br># \u52a0\u8f7d<br>model = torch.load(PATH)<br>model.eval()<\/code><\/pre>\n\n\n\n<p>\u4fdd\u5b58\u6574\u4e2a\u6a21\u578b\u4f1a\u5c06\u6a21\u578b\u7684\u7ed3\u6784\u548c\u53c2\u6570\u4e00\u8d77\u4fdd\u5b58\u3002\u8fd9\u610f\u5473\u7740\u5728\u52a0\u8f7d\u6a21\u578b\u65f6\uff0c\u6211\u4eec\u4e0d\u518d\u9700\u8981\u624b\u52a8\u521b\u5efa\u6a21\u578b\u5b9e\u4f8b\u3002\u4f46\u662f\uff0c\u8fd9\u79cd\u65b9\u5f0f\u9700\u8981\u66f4\u591a\u7684\u78c1\u76d8\u7a7a\u95f4\uff0c\u5e76\u4e14\u53ef\u80fd\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u5bfc\u81f4\u4ee3\u7801\u7684\u6df7\u4e71\uff0c\u6240\u4ee5\u5e76\u4e0d\u603b\u662f\u63a8\u8350\u7684\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662fPyTorch\u4e2d\u6a21\u578b\u7684\u4fdd\u5b58\u548c\u52a0\u8f7d\u7684\u57fa\u672c\u65b9\u6cd5\u3002\u9002\u5f53\u7684\u4fdd\u5b58\u548c\u52a0\u8f7d\u6a21\u578b\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u8fdb\u884c\u6a21\u578b\u7684\u8bad\u7ec3\u548c\u8bc4\u4f30\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u8fd9\u91cc\u5199\u7684\u86ee\u8be6\u7ec6\u7684\uff0c\u4f46\u8fd8\u662f\u5c55\u5f00\u770b\u770b<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udcbe PyTorch \u6a21\u578b\u4fdd\u5b58\u4e0e\u52a0\u8f7d\u89e3\u6790<\/p>\n<\/blockquote>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u8fd9\u6bb5\u4ee3\u7801\u5b8c\u6210\u4e86\u4e24\u4ef6\u4e8b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4fdd\u5b58<\/strong>\uff1a\u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u201c\u53c2\u6570\u201d\uff08\u5b66\u5230\u7684\u77e5\u8bc6\uff09\u6253\u5305\u5b58\u5230\u4e00\u4e2a\u6587\u4ef6\u91cc\u3002<\/li>\n\n\n\n<li><strong>\u52a0\u8f7d<\/strong>\uff1a\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u201c\u53c2\u6570\u201d\uff0c\u5e76\u585e\u56de\u4e00\u4e2a<strong>\u7ed3\u6784\u5b8c\u5168\u76f8\u540c<\/strong>\u7684\u201c\u7a7a\u6a21\u578b\u201d\u4e2d\uff0c\u8ba9\u5b83\u6062\u590d\u201c\u667a\u6167\u201d\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u4e0b\u9762\u662f\u9010\u884c\u89e3\u91ca\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udca1 \u6838\u5fc3\u6982\u5ff5\uff1astate_dict<\/p>\n<\/blockquote>\n\n\n\n<p>\u5728\u6df1\u5165\u4ee3\u7801\u4e4b\u524d\uff0c\u7406\u89e3&nbsp;<code>state_dict<\/code>&nbsp;\u5f88\u91cd\u8981\u3002\u4f60\u53ef\u4ee5\u628a\u5b83\u60f3\u8c61\u6210\u6a21\u578b\u7684**\u201c\u8eab\u4efd\u8bc1\u201d\u6216\u201c\u68c0\u67e5\u70b9\u201d**\uff0c\u5b83\u662f\u4e00\u4e2a Python \u5b57\u5178\u5bf9\u8c61\uff0c\u8bb0\u5f55\u4e86\u6a21\u578b\u6240\u6709\u53ef\u5b66\u4e60\u53c2\u6570\uff08\u5982\u6743\u91cd\u548c\u504f\u7f6e\uff09\u7684\u5f53\u524d\u72b6\u6001\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>conv1.weight<\/code><\/li>\n\n\n\n<li><code>conv2.bias<\/code><\/li>\n\n\n\n<li><code>fc1.weight<\/code>\u00a0\u7b49\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u53ea\u4fdd\u5b58&nbsp;<code>state_dict<\/code>&nbsp;\u800c\u4e0d\u662f\u6574\u4e2a\u6a21\u578b\uff0c\u4f7f\u5f97\u4fdd\u5b58\u7684\u6587\u4ef6\u66f4\u5c0f\uff0c\u4e5f\u66f4\u7075\u6d3b\uff0c\u662f PyTorch \u5b98\u65b9\u63a8\u8350\u7684\u65b9\u5f0f\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udcdd \u4ee3\u7801\u9010\u884c\u89e3\u8bfb<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol class=\"wp-block-list\">\n<li>\u4fdd\u5b58\u6a21\u578b\u53c2\u6570 (<code>torch.save<\/code>)<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>torch.save(model.state_dict(), PATH)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>model.state_dict()<\/code>: \u83b7\u53d6\u6a21\u578b\u5f53\u524d\u7684<strong>\u53c2\u6570\u72b6\u6001\u5b57\u5178<\/strong>\uff08\u5c31\u662f\u4e0a\u9762\u8bf4\u7684\u201c\u8eab\u4efd\u8bc1\u201d\uff09\u3002<\/li>\n\n\n\n<li><code>torch.save(..., PATH)<\/code>: \u4f7f\u7528 PyTorch \u7684\u00a0<code>save<\/code>\u00a0\u51fd\u6570\u5c06\u8fd9\u4e2a\u5b57\u5178\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u8def\u5f84\u00a0<code>PATH<\/code>\uff08\u901a\u5e38\u6587\u4ef6\u6269\u5c55\u540d\u4e3a\u00a0<code>.pth<\/code>\u00a0\u6216\u00a0<code>.pt<\/code>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u8fd9\u5c31\u597d\u6bd4\u4f60\u628a\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u7684\u6240\u6709\u201c\u77e5\u8bc6\u8981\u70b9\u201d\uff08\u53c2\u6570\uff09\u8bb0\u5728\u4e86\u4e00\u4e2a\u7b14\u8bb0\u672c\u4e0a\uff0c\u7136\u540e\u628a\u7b14\u8bb0\u672c\u5b58\u8fdb\u4e86\u4fdd\u9669\u7bb1\u3002<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u52a0\u8f7d\u6a21\u578b\u53c2\u6570<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>model = TheModelClass(*args, **kwargs) &nbsp;<em># 1. \u521b\u5efa\u4e00\u4e2a\u7a7a\u6a21\u578b<\/em><br>model.load_state_dict(torch.load(PATH))&nbsp;<em># 2. \u52a0\u8f7d\u53c2\u6570<\/em><br>model.eval() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em># 3. \u8bbe\u7f6e\u4e3a\u8bc4\u4f30\u6a21\u5f0f<\/em><\/code><\/pre>\n\n\n\n<p>\u52a0\u8f7d\u8fc7\u7a0b\u5206\u4e3a\u4e09\u6b65\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u91cd\u5efa\u6a21\u578b\u7ed3\u6784<\/strong>\uff1a<code>model = TheModelClass(*args, **kwargs)<\/code>\n<ul class=\"wp-block-list\">\n<li>\u4f60\u5fc5\u987b\u5148\u521b\u5efa\u4e00\u4e2a<strong>\u4e0e\u4e4b\u524d\u4fdd\u5b58\u53c2\u6570\u65f6\u5b8c\u5168\u76f8\u540c\u7684\u6a21\u578b\u7ed3\u6784<\/strong>\uff08<code>TheModelClass<\/code>\uff09\u3002<code>*args, **kwargs<\/code>\u00a0\u662f\u521b\u5efa\u8fd9\u4e2a\u6a21\u578b\u65f6\u9700\u8981\u7684\u53c2\u6570\uff08\u4f8b\u5982\u8f93\u5165\u7279\u5f81\u6570\u3001\u5c42\u6570\u7b49\uff09\u3002<\/li>\n\n\n\n<li><strong>\u8fd9\u4e00\u6b65\u81f3\u5173\u91cd\u8981\uff01<\/strong>\u00a0\u4f60\u76f8\u5f53\u4e8e\u6309\u7167\u539f\u6765\u7684\u201c\u56fe\u7eb8\u201d\uff08\u7c7b\u5b9a\u4e49\uff09\uff0c\u91cd\u65b0\u9020\u4e86\u4e00\u4e2a\u7a7a\u7684\u201c\u5927\u8111\u58f3\u201d\u3002\u5982\u679c\u7ed3\u6784\u5bf9\u4e0d\u4e0a\uff0c\u53c2\u6570\u662f\u88c5\u4e0d\u8fdb\u53bb\u7684\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u52a0\u8f7d\u53c2\u6570<\/strong>\uff1a<code>model.load_state_dict(torch.load(PATH))<\/code>\n<ul class=\"wp-block-list\">\n<li><code>torch.load(PATH)<\/code>: \u4ece\u6307\u5b9a\u8def\u5f84\u00a0<code>PATH<\/code>\u00a0\u7684\u6587\u4ef6\u4e2d<strong>\u52a0\u8f7d<\/strong>\u4e4b\u524d\u4fdd\u5b58\u7684\u53c2\u6570\u5b57\u5178\u3002<\/li>\n\n\n\n<li><code>model.load_state_dict(...)<\/code>: \u5c06\u52a0\u8f7d\u51fa\u6765\u7684\u53c2\u6570<strong>\u52a0\u8f7d\u5230<\/strong>\u4f60\u521a\u521a\u521b\u5efa\u7684\u7a7a\u6a21\u578b\u91cc\u3002<\/li>\n\n\n\n<li><strong>\u8fd9\u5c31\u50cf\u4f60\u4ece\u4fdd\u9669\u7bb1\u91cc\u62ff\u51fa\u7b14\u8bb0\u672c\uff0c\u7136\u540e\u628a\u201c\u77e5\u8bc6\u8981\u70b9\u201d\u4e00\u5b57\u4e0d\u5dee\u5730\u5199\u8fdb\u8fd9\u4e2a\u65b0\u7684\u5927\u8111\u58f3\u91cc\u3002<\/strong><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u5207\u6362\u4e3a\u8bc4\u4f30\u6a21\u5f0f<\/strong>\uff1a<code>model.eval()<\/code>\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u662f\u4e00\u4e2a<strong>\u975e\u5e38\u91cd\u8981\u7684\u6b65\u9aa4<\/strong>\uff0c\u5c24\u5176\u662f\u5728\u4f60\u51c6\u5907\u7528\u6a21\u578b\u8fdb\u884c<strong>\u63a8\u7406\uff08\u9884\u6d4b\u3001\u8bc4\u4f30\uff09<\/strong>\u00a0\u800c\u4e0d\u662f\u7ee7\u7eed\u8bad\u7ec3\u7684\u65f6\u5019\u3002<\/li>\n\n\n\n<li><code>model.eval()<\/code>\u00a0\u4f1a\u5c06\u6a21\u578b\u8bbe\u7f6e\u4e3a<strong>\u8bc4\u4f30\u6a21\u5f0f<\/strong>\u3002\u5728\u8fd9\u4e2a\u6a21\u5f0f\u4e0b\uff0c\u90a3\u4e9b\u5728\u8bad\u7ec3\u548c\u9884\u6d4b\u65f6\u884c\u4e3a\u4e0d\u540c\u7684\u5c42\uff08\u5982\u00a0<strong>Dropout<\/strong>\u00a0\u548c\u00a0<strong>Batch Normalization<\/strong>\uff09\u4f1a\u88ab\u8c03\u6574\u5230\u9884\u6d4b\u72b6\u6001\uff08\u4f8b\u5982\uff0cDropout \u4f1a\u5931\u6548\uff0cBatchNorm \u4f1a\u4f7f\u7528\u8bad\u7ec3\u9636\u6bb5\u7edf\u8ba1\u597d\u7684\u5168\u5c40\u5747\u503c\u548c\u65b9\u5dee\uff0c\u800c\u4e0d\u662f\u5f53\u524d\u6279\u6b21\u7684\u7edf\u8ba1\u91cf\uff09\u3002<\/li>\n\n\n\n<li><strong>\u5fd8\u8bb0\u8fd9\u4e00\u6b65\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6a21\u578b\u8f93\u51fa\u4e0d\u4e00\u81f4\u548c\u4e0d\u53ef\u9884\u6d4b\u7684\u7ed3\u679c\u3002<\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u26a0\ufe0f \u91cd\u8981\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/blockquote>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6a21\u578b\u7ed3\u6784\u5fc5\u987b\u5339\u914d<\/strong>\uff1a\u52a0\u8f7d\u53c2\u6570\u65f6\uff0c\u4f60\u5fc5\u987b\u80fd\u591f<strong>\u91cd\u65b0\u5b9e\u4f8b\u5316\u4e00\u4e2a\u7ed3\u6784\u5b8c\u5168\u76f8\u540c\u7684\u6a21\u578b<\/strong>\u00a0(<code>TheModelClass<\/code>)\u3002\u8fd9\u610f\u5473\u7740\u4f60\u9700\u8981\u80fd\u591f\u8bbf\u95ee\u5230\u8be5\u6a21\u578b\u7c7b\u7684\u539f\u59cb\u5b9a\u4e49\u3002<\/li>\n\n\n\n<li><strong>\u8bbe\u5907\u95ee\u9898\uff08CPU\/GPU\uff09<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u6a21\u578b\u662f\u5728\u00a0<strong>GPU<\/strong>\u00a0\u4e0a\u8bad\u7ec3\u7684\u5e76\u4fdd\u5b58\uff0c\u4f46\u4f60\u60f3\u5728\u00a0<strong>CPU<\/strong>\u00a0\u4e0a\u52a0\u8f7d\u8fd0\u884c\uff0c\u52a0\u8f7d\u65f6\u9700\u8981\u6307\u5b9a\u00a0<code>map_location<\/code>\uff1a<code>model.load_state_dict(torch.load(PATH, map_location=torch.device('cpu')))<\/code><\/li>\n\n\n\n<li>\u53cd\u4e4b\uff0c\u5982\u679c\u662f\u5728 CPU \u4e0a\u8bad\u7ec3\u4fdd\u5b58\uff0c\u60f3\u52a0\u8f7d\u5230 GPU \u4e0a\uff1a<code>model.load_state_dict(torch.load(PATH, map_location=torch.device('cuda')))<br><em># \u7136\u540e\u518d\u5c06\u6a21\u578b\u653e\u5230GPU\u4e0a<\/em><br>model.to('cuda')<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>eval()<\/code>\u00a0\u6a21\u5f0f\u4e0e\u00a0<code>train()<\/code>\u00a0\u6a21\u5f0f<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u8fdb\u884c<strong>\u63a8\u7406<\/strong>\u65f6\uff0c\u52a1\u5fc5\u4f7f\u7528\u00a0<code>model.eval()<\/code>\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u52a0\u8f7d\u6a21\u578b\u662f\u4e3a\u4e86<strong>\u7ee7\u7eed\u8bad\u7ec3<\/strong>\uff0c\u90a3\u4e48\u5728\u52a0\u8f7d\u53c2\u6570\u540e\uff0c\u5e94\u8be5\u8c03\u7528\u00a0<code>model.train()<\/code>\u00a0\u6765\u786e\u4fdd\u6a21\u578b\u5904\u4e8e\u8bad\u7ec3\u6a21\u5f0f\uff08Dropout \u548c BatchNorm \u4f1a\u6062\u590d\u5176\u8bad\u7ec3\u884c\u4e3a\uff09\uff0c\u5e76\u4e14\u8fd8\u9700\u8981\u52a0\u8f7d\u4f18\u5316\u5668\u7684\u72b6\u6001\u7b49\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83c\udd9a \u53e6\u4e00\u79cd\u65b9\u6cd5\uff1a\u4fdd\u5b58\u6574\u4e2a\u6a21\u578b<\/p>\n<\/blockquote>\n\n\n\n<p>\u4f60\u53ef\u80fd\u4f1a\u770b\u5230\u53e6\u4e00\u79cd\u65b9\u5f0f\uff1a<code>torch.save(model, PATH)<\/code>&nbsp;\u548c&nbsp;<code>model = torch.load(PATH)<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f1a<strong>\u5c06\u6574\u4e2a\u6a21\u578b\u5bf9\u8c61\uff08\u5305\u62ec\u7ed3\u6784\u548c\u53c2\u6570\uff09\u4e00\u8d77\u4fdd\u5b58<\/strong>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u52a0\u8f7d\u65f6\u4e0d\u9700\u8981\u4e8b\u5148\u77e5\u9053\u6a21\u578b\u7ed3\u6784\uff0c\u76f4\u63a5\u52a0\u8f7d\u5373\u53ef\u3002<\/li>\n\n\n\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u4fdd\u5b58\u7684\u6587\u4ef6\u66f4\u5927\u3002<\/li>\n\n\n\n<li>\u7075\u6d3b\u6027\u5dee\uff0c\u5e8f\u5217\u5316\u4f9d\u8d56\u4e8e\u5177\u4f53\u7684\u7c7b\u5b9a\u4e49\uff0c\u751a\u81f3 PyTorch \u7248\u672c\uff0c\u66f4\u5bb9\u6613\u51fa\u73b0\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/li>\n\n\n\n<li>\u4e00\u822c\u4e0d\u63a8\u8350\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u56e0\u6b64\uff0c<strong>\u63d0\u4f9b\u7684\u8fd9\u79cd\u53ea\u4fdd\u5b58\u548c\u52a0\u8f7d&nbsp;<code>state_dict<\/code>&nbsp;\u7684\u65b9\u6cd5\u662f\u66f4\u4f18\u3001\u66f4\u63a8\u8350\u7684\u505a\u6cd5\u3002<\/strong><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">4. PyTorch GPT\u52a0\u901f<\/h1>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"391\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060536144-1024x391.png\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" alt=\"\" class=\"wp-image-6756 lazyload\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060536144-1024x391.png 1024w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060536144-300x114.png 300w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060536144-768x293.png 768w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060536144.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><noscript><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"391\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060536144-1024x391.png\" alt=\"\" class=\"wp-image-6756\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060536144-1024x391.png 1024w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060536144-300x114.png 300w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060536144-768x293.png 768w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060536144.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/noscript><\/figure>\n\n\n\n<p>\u638c\u63e1\u4e86PyTorch\u7684\u57fa\u7840\u548c\u9ad8\u7ea7\u7528\u6cd5\u4e4b\u540e\uff0c\u6211\u4eec\u73b0\u5728\u8981\u63a2\u8ba8\u4e00\u4e9bPyTorch\u7684\u8fdb\u9636\u6280\u5de7\uff0c\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u8fd9\u4e2a\u5f3a\u5927\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.1 \u4f7f\u7528GPU\u52a0\u901f<\/h2>\n\n\n\n<p>PyTorch\u652f\u6301\u4f7f\u7528GPU\u8fdb\u884c\u8ba1\u7b97\uff0c\u8fd9\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u8bad\u7ec3\u548c\u63a8\u7406\u7684\u901f\u5ea6\u3002\u4f7f\u7528GPU\u8fdb\u884c\u8ba1\u7b97\u7684\u6838\u5fc3\u5c31\u662f\u5c06Tensor\u548c\u6a21\u578b\u8f6c\u79fb\u5230GPU\u4e0a\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.1.1 \u5224\u65ad\u662f\u5426\u652f\u6301GPU<\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5224\u65ad\u5f53\u524d\u7684\u73af\u5883\u662f\u5426\u652f\u6301GPU\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7torch.cuda.is_available()\u6765\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(torch.cuda.is_available()) &nbsp;# \u8f93\u51fa\uff1aTrue \u6216 False<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.1.2 Tensor\u5728CPU\u548cGPU\u4e4b\u95f4\u8f6c\u79fb<\/h3>\n\n\n\n<p>\u5982\u679c\u652f\u6301GPU\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528.to(device)\u6216.cuda()\u65b9\u6cd5\u5c06Tensor\u8f6c\u79fb\u5230GPU\u4e0a\u3002\u540c\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528.cpu()\u65b9\u6cd5\u5c06Tensor\u8f6c\u79fb\u5230CPU\u4e0a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5224\u65ad\u662f\u5426\u652f\u6301CUDA<br>device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")<br><br># \u521b\u5efa\u4e00\u4e2aTensor<br>x = torch.rand(3, 3)<br><br># \u5c06Tensor\u8f6c\u79fb\u5230GPU\u4e0a<br>x_gpu = x.to(device)<br><br># \u6216\u8005<br>x_gpu = x.cuda()<br><br># \u5c06Tensor\u8f6c\u79fb\u5230CPU\u4e0a<br>x_cpu = x_gpu.cpu()<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.1.3 \u5c06\u6a21\u578b\u8f6c\u79fb\u5230GPU\u4e0a<\/h3>\n\n\n\n<p>\u7c7b\u4f3c\u7684\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5c06\u6a21\u578b\u8f6c\u79fb\u5230GPU\u4e0a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>model = Model()<br>model.to(device)<\/code><\/pre>\n\n\n\n<p>\u5f53\u6a21\u578b\u5728GPU\u4e0a\u65f6\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u8f93\u5165\u7684Tensor\u4e5f\u5728GPU\u4e0a\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff0c\u5c06\u6a21\u578b\u8f6c\u79fb\u5230GPU\u4e0a\u540e\uff0c\u6a21\u578b\u7684\u6240\u6709\u53c2\u6570\u548c\u7f13\u51b2\u533a\u90fd\u4f1a\u8f6c\u79fb\u5230GPU\u4e0a\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528GPU\u8fdb\u884c\u8ba1\u7b97\u7684\u57fa\u672c\u65b9\u6cd5\u3002\u901a\u8fc7\u5408\u7406\u7684\u4f7f\u7528GPU\uff0c\u6211\u4eec\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6a21\u578b\u7684\u8bad\u7ec3\u548c\u63a8\u7406\u901f\u5ea6\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.2 \u4f7f\u7528torchvision\u8fdb\u884c\u56fe\u50cf\u64cd\u4f5c<\/h2>\n\n\n\n<p>torchvision\u662f\u4e00\u4e2a\u72ec\u7acb\u4e8ePyTorch\u7684\u5305\uff0c\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u56fe\u50cf\u6570\u636e\u96c6\uff0c\u56fe\u50cf\u5904\u7406\u5de5\u5177\u548c\u9884\u8bad\u7ec3\u6a21\u578b\u7b49\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.2.1 torchvision.datasets<\/h3>\n\n\n\n<p>torchvision.datasets\u6a21\u5757\u63d0\u4f9b\u4e86\u5404\u79cd\u516c\u5171\u6570\u636e\u96c6\uff0c\u5982CIFAR10\u3001MNIST\u3001ImageNet\u7b49\uff0c\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u4e0b\u8f7d\u548c\u4f7f\u7528\u8fd9\u4e9b\u6570\u636e\u96c6\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4e0b\u8f7d\u548c\u52a0\u8f7dCIFAR10\u6570\u636e\u96c6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from torchvision import datasets, transforms<br><br># \u6570\u636e\u8f6c\u6362<br>transform = transforms.Compose(&#91;<br>&nbsp; &nbsp; transforms.ToTensor(),<br>&nbsp; &nbsp; transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))<br>])<br><br># \u4e0b\u8f7d\u5e76\u52a0\u8f7d\u8bad\u7ec3\u96c6<br>trainset = datasets.CIFAR10(root='.\/data', train=True, download=True, transform=transform)<br>trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2)<br><br># \u4e0b\u8f7d\u5e76\u52a0\u8f7d\u6d4b\u8bd5\u96c6<br>testset = datasets.CIFAR10(root='.\/data', train=False, download=True, transform=transform)<br>testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2)<br><\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7785\u7785\u4ee3\u7801<\/p>\n<\/blockquote>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u662f\u4f7f\u7528PyTorch\u52a0\u8f7d\u548c\u9884\u5904\u7406CIFAR-10\u6570\u636e\u96c6\u7684\u5e38\u89c1\u505a\u6cd5\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udce6 PyTorch CIFAR-10 \u6570\u636e\u52a0\u8f7d\u4e0e\u9884\u5904\u7406\u89e3\u6790<\/p>\n<\/blockquote>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u8fd9\u6bb5\u4ee3\u7801\u5c31\u50cf\u662f<strong>\u4e3a\u6a21\u578b\u8bad\u7ec3\u51c6\u5907\u4e00\u4e2a\u81ea\u52a8\u5316\u7684\u201c\u98df\u6750\u5904\u7406\u6d41\u6c34\u7ebf\u201d<\/strong>\uff0c\u5b83\u8d1f\u8d23\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4e0b\u8f7d\u6570\u636e<\/strong>\uff08\u5c31\u50cf\u83b7\u53d6\u539f\u59cb\u98df\u6750\uff09<\/li>\n\n\n\n<li><strong>\u8f6c\u6362\u6570\u636e<\/strong>\uff08\u5c31\u50cf\u6e05\u6d17\u3001\u5207\u5272\u3001\u814c\u5236\u98df\u6750\uff09<\/li>\n\n\n\n<li><strong>\u5206\u6279\u52a0\u8f7d\u6570\u636e<\/strong>\uff08\u5c31\u50cf\u5c06\u5904\u7406\u597d\u7684\u98df\u6750\u6309\u4efd\u88c5\u76d8\uff0c\u65b9\u4fbf\u53a8\u5e08\u53d6\u7528\uff09<\/li>\n<\/ol>\n\n\n\n<p>\u4e0b\u9762\u662f\u8fd9\u6bb5\u4ee3\u7801\u7684\u9010\u884c\u89e3\u91ca\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udd27 1. \u5bfc\u5165\u5fc5\u8981\u7684\u5de5\u5177\u5305<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>from&nbsp;torchvision&nbsp;import&nbsp;datasets, transforms<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>torchvision<\/code>: PyTorch \u7684\u4e00\u4e2a\u5b98\u65b9\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u3002<\/li>\n\n\n\n<li><code>datasets<\/code>: \u63d0\u4f9b\u5e38\u7528\u6570\u636e\u96c6\uff08\u5982 CIFAR-10\u3001MNIST\uff09\u7684\u5feb\u901f\u4e0b\u8f7d\u548c\u52a0\u8f7d\u65b9\u5f0f\u3002<\/li>\n\n\n\n<li><code>transforms<\/code>: \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u6570\u636e\u9884\u5904\u7406\u548c\u6570\u636e\u589e\u5f3a\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83c\udf00 2. \u5b9a\u4e49\u6570\u636e\u8f6c\u6362\uff08\u9884\u5904\u7406\uff09\u6d41\u7a0b<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>transform = transforms.Compose(&#91;<br>&nbsp; &nbsp; transforms.ToTensor(),<br>&nbsp; &nbsp; transforms.Normalize((0.5,&nbsp;0.5,&nbsp;0.5), (0.5,&nbsp;0.5,&nbsp;0.5))<br>])<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u5b9a\u4e49\u4e86\u4e00\u4e2a<strong>\u6570\u636e\u9884\u5904\u7406\u6d41\u6c34\u7ebf<\/strong>\uff0c\u4f7f\u7528&nbsp;<code>transforms.Compose<\/code>&nbsp;\u5c06\u591a\u4e2a\u6b65\u9aa4\u7ec4\u5408\u5728\u4e00\u8d77\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>transforms.ToTensor()<\/code>:\u00a0<strong>\u5c06 PIL \u56fe\u50cf\u6216 NumPy \u6570\u7ec4\u8f6c\u6362\u4e3a PyTorch \u5f20\u91cf (Tensor)<\/strong>\u3002\u8fd9\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a PyTorch \u7684\u6a21\u578b\u53ea\u5904\u7406\u5f20\u91cf\u3002\u540c\u65f6\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u4f1a\u81ea\u52a8\u5c06\u56fe\u50cf\u6570\u636e\u4ece\u539f\u59cb\u7684\u00a0<code>[0, 255]<\/code>\u00a0\u8303\u56f4\u6216\u00a0<code>[0, 1]<\/code>\u00a0\u7684 PIL \u56fe\u50cf\u683c\u5f0f\u8f6c\u6362\u4e3a\u00a0<code>[0.0, 1.0]<\/code>\u00a0\u7684\u6d6e\u70b9\u6570\u8303\u56f4\uff0c\u5e76\u8c03\u6574\u7ef4\u5ea6\u987a\u5e8f\u4e3a PyTorch \u9884\u671f\u7684\u00a0<code>(C, H, W)<\/code>\uff08\u901a\u9053\uff0c\u9ad8\uff0c\u5bbd\uff09\u683c\u5f0f\u3002<\/li>\n\n\n\n<li><code>transforms.Normalize(mean, std)<\/code>:\u00a0<strong>\u5bf9\u5f20\u91cf\u8fdb\u884c\u6807\u51c6\u5316\uff08\u5f52\u4e00\u5316\uff09<\/strong>\u3002\u8fd9\u91cc\u4f7f\u7528\u7684\u662f\u5747\u503c\u00a0<code>mean=(0.5, 0.5, 0.5)<\/code>\u00a0\u548c\u6807\u51c6\u5dee\u00a0<code>std=(0.5, 0.5, 0.5)<\/code>\u3002\u8ba1\u7b97\u516c\u5f0f\u4e3a\uff1a<code>output = (input - mean) \/ std<\/code>\u3002\n<ul class=\"wp-block-list\">\n<li>\u56e0\u4e3a\u539f\u59cb\u6570\u636e\u7ecf\u8fc7\u00a0<code>ToTensor<\/code>\u00a0\u540e\u8303\u56f4\u662f\u00a0<code>[0.0, 1.0]<\/code>\uff0c\u7ecf\u8fc7\u8fd9\u91cc\u7684\u5f52\u4e00\u5316\u540e\uff08<code>(0.0 - 0.5)\/0.5 = -1.0<\/code>,\u00a0<code>(1.0 - 0.5)\/0.5 = 1.0<\/code>\uff09\uff0c\u6bcf\u4e2a\u901a\u9053\u7684\u6570\u636e\u8303\u56f4\u5c31\u53d8\u6210\u4e86\u00a0<strong><code>[-1.0, 1.0]<\/code><\/strong>\u3002<strong>\u5f52\u4e00\u5316\u53ef\u4ee5\u4f7f\u6a21\u578b\u8bad\u7ec3\u66f4\u7a33\u5b9a\u3001\u6536\u655b\u66f4\u5feb<\/strong>\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udce5 3. \u4e0b\u8f7d\u5e76\u52a0\u8f7d\u8bad\u7ec3\u96c6<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>trainset = datasets.CIFAR10(root='.\/data', train=True, download=True, transform=transform)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u884c\u4ee3\u7801\u4f7f\u7528\u00a0<code>torchvision.datasets.CIFAR10<\/code>\u00a0\u6765\u52a0\u8f7d\u00a0<strong>CIFAR-10 \u6570\u636e\u96c6<\/strong>\u3002\n<ul class=\"wp-block-list\">\n<li>CIFAR-10 \u6570\u636e\u96c6\u5305\u542b 10 \u4e2a\u7c7b\u522b\u7684 6 \u4e07\u5f20 32x32 \u5f69\u8272\u56fe\u50cf\uff0c\u6bcf\u7c7b 6000 \u5f20\uff0c\u5206\u4e3a 50,000 \u5f20\u8bad\u7ec3\u96c6\u548c 10,000 \u5f20\u6d4b\u8bd5\u96c6\uff0c\u7c7b\u522b\u5305\u62ec\u98de\u673a\u3001\u6c7d\u8f66\u3001\u9e1f\u7c7b\u3001\u732b\u3001\u9e7f\u3001\u72d7\u3001\u9752\u86d9\u3001\u9a6c\u3001\u8239\u3001\u5361\u8f66\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u53c2\u6570\u8bf4\u660e<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>root='.\/data'<\/code>: \u6307\u5b9a\u6570\u636e\u96c6\u4e0b\u8f7d\u548c\u5b58\u50a8\u7684\u6839\u76ee\u5f55\u3002<\/li>\n\n\n\n<li><code>train=True<\/code>: \u6307\u5b9a\u52a0\u8f7d\u7684\u662f<strong>\u8bad\u7ec3\u96c6<\/strong>\uff08\u5982\u679c\u4e3a\u00a0<code>False<\/code>\u00a0\u5219\u52a0\u8f7d\u6d4b\u8bd5\u96c6\uff09\u3002<\/li>\n\n\n\n<li><code>download=True<\/code>: \u5982\u679c\u6307\u5b9a\u8def\u5f84\u4e0b\u6ca1\u6709\u6570\u636e\uff0c\u5219\u81ea\u52a8\u4ece\u4e92\u8054\u7f51\u4e0b\u8f7d\u3002<\/li>\n\n\n\n<li><code>transform=transform<\/code>: \u5c06\u4e4b\u524d\u5b9a\u4e49\u7684\u6570\u636e\u9884\u5904\u7406\u6d41\u6c34\u7ebf\u5e94\u7528\u4e8e\u52a0\u8f7d\u7684\u6bcf\u4e00\u5f20\u56fe\u50cf\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\ude9a 4. \u521b\u5efa\u8bad\u7ec3\u96c6\u7684\u6570\u636e\u52a0\u8f7d\u5668 (DataLoader)<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u884c\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u00a0<strong><code>DataLoader<\/code><\/strong>\u00a0\u5bf9\u8c61\uff0c\u5b83\u8d1f\u8d23\u4ece\u6570\u636e\u96c6\u4e2d<strong>\u6309\u6279\u6b21\u62bd\u53d6\u6570\u636e<\/strong>\uff0c\u975e\u5e38\u9002\u5408\u5728\u8bad\u7ec3\u5faa\u73af\u4e2d\u8fed\u4ee3\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><strong>\u53c2\u6570\u8bf4\u660e<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>dataset=trainset<\/code>: \u6307\u5b9a\u8981\u52a0\u8f7d\u7684\u6570\u636e\u96c6\u3002<\/li>\n\n\n\n<li><code>batch_size=4<\/code>: \u6307\u5b9a<strong>\u6bcf\u4e2a\u6279\u6b21\u5305\u542b\u7684\u6837\u672c\u6570\u91cf<\/strong>\u3002\u8fd9\u91cc\u8bbe\u7f6e\u4e3a 4\uff0c\u610f\u5473\u7740\u6bcf\u6b21\u8fed\u4ee3\u4f1a\u8fd4\u56de 4 \u5f20\u56fe\u50cf\u548c\u5bf9\u5e94\u7684 4 \u4e2a\u6807\u7b7e\u3002<strong>\u6279\u91cf\u5904\u7406\u53ef\u4ee5\u63d0\u9ad8\u5185\u5b58\u5229\u7528\u7387\u548c\u8ba1\u7b97\u6548\u7387<\/strong>\uff08GPU \u64c5\u957f\u5e76\u884c\u8ba1\u7b97\uff09\u3002<\/li>\n\n\n\n<li><code>shuffle=True<\/code>: \u8868\u793a<strong>\u5728\u6bcf\u4e2a epoch\uff08\u8bad\u7ec3\u8f6e\u6b21\uff09\u5f00\u59cb\u65f6\u6253\u4e71\u6570\u636e\u987a\u5e8f<\/strong>\u3002<strong>\u8fd9\u975e\u5e38\u91cd\u8981<\/strong>\uff0c\u53ef\u4ee5\u9632\u6b62\u6a21\u578b\u5b66\u4e60\u5230\u6570\u636e\u51fa\u73b0\u7684\u987a\u5e8f\u504f\u5dee\uff08\u4f8b\u5982\u524d\u4e00\u534a\u90fd\u662f\u67d0\u4e00\u7c7b\uff0c\u540e\u4e00\u534a\u662f\u53e6\u4e00\u7c7b\uff09\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u6a21\u578b\u7684\u6cdb\u5316\u80fd\u529b\u3002<\/li>\n\n\n\n<li><code>num_workers=2<\/code>: \u6307\u5b9a\u4f7f\u7528<strong>\u591a\u4e2a\u5b50\u8fdb\u7a0b\u6765\u5e76\u884c\u52a0\u8f7d\u6570\u636e<\/strong>\u3002\u8fd9\u53ef\u4ee5\u663e\u8457\u52a0\u901f\u6570\u636e\u52a0\u8f7d\u8fc7\u7a0b\uff0c\u907f\u514d\u6570\u636e\u52a0\u8f7d\u6210\u4e3a\u8bad\u7ec3\u901f\u5ea6\u7684\u74f6\u9888\u3002\u901a\u5e38\u8bbe\u7f6e\u4e3a CPU \u6838\u5fc3\u6570\u7684 2-4 \u500d\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udcca 5. \u4e0b\u8f7d\u5e76\u52a0\u8f7d\u6d4b\u8bd5\u96c6<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>testset = datasets.CIFAR10(root='.\/data', train=False, download=True, transform=transform)<br>testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u4e24\u884c\u4ee3\u7801\u7684\u4f5c\u7528\u4e0e\u52a0\u8f7d\u8bad\u7ec3\u96c6\u7c7b\u4f3c\uff0c\u4f46\u6709\u4e00\u4e9b\u5173\u952e\u533a\u522b\uff1a\n<ul class=\"wp-block-list\">\n<li><code>train=False<\/code>: \u6307\u5b9a\u52a0\u8f7d\u7684\u662f<strong>\u6d4b\u8bd5\u96c6<\/strong>\uff08\u9a8c\u8bc1\u96c6\uff09\u3002<\/li>\n\n\n\n<li><code>shuffle=False<\/code>:\u00a0<strong>\u6d4b\u8bd5\u65f6\u901a\u5e38\u4e0d\u9700\u8981\u6253\u4e71\u6570\u636e\u987a\u5e8f<\/strong>\u3002\u56e0\u4e3a\u6d4b\u8bd5\u7684\u76ee\u7684\u662f\u8bc4\u4f30\u6a21\u578b\u5728\u56fa\u5b9a\u6570\u636e\u4e0a\u7684\u6027\u80fd\uff0c\u6253\u4e71\u987a\u5e8f\u4e0d\u4f1a\u5e26\u6765\u597d\u5904\uff0c\u53cd\u800c\u53ef\u80fd\u4f7f\u8bc4\u4f30\u7ed3\u679c\u4e0d\u7a33\u5b9a\u6216\u96be\u4ee5\u89e3\u91ca\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u4e3a\u4e86\u66f4\u76f4\u89c2\u5730\u7406\u89e3\u6570\u636e\u5728\u6574\u4e2a\u6d41\u7a0b\u4e2d\u7684\u53d8\u5316\uff0c\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u7684\u56fe\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"386\" height=\"722\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060542939.png\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" alt=\"\" class=\"wp-image-6757 lazyload\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060542939.png 386w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060542939-160x300.png 160w\" sizes=\"auto, (max-width: 386px) 100vw, 386px\" \/><noscript><img loading=\"lazy\" decoding=\"async\" width=\"386\" height=\"722\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060542939.png\" alt=\"\" class=\"wp-image-6757\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060542939.png 386w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060542939-160x300.png 160w\" sizes=\"auto, (max-width: 386px) 100vw, 386px\" \/><\/noscript><\/figure>\n\n\n\n<p>\ud83d\udca1 \u5173\u952e\u8981\u70b9\u4e0e\u4f7f\u7528\u573a\u666f<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>DataLoader \u7684\u4f18\u52bf<\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>\u6279\u5904\u7406 (Batching)<\/strong>: \u63d0\u9ad8\u5185\u5b58\u5229\u7528\u7387\u548c\u8ba1\u7b97\u6548\u7387\uff08GPU \u64c5\u957f\u5e76\u884c\u8ba1\u7b97\uff09\u3002<\/li>\n\n\n\n<li><strong>\u6253\u4e71 (Shuffling)<\/strong>: \u907f\u514d\u6a21\u578b\u5b66\u4e60\u5230\u65e0\u5173\u7684\u987a\u5e8f\u7279\u5f81\uff0c\u63d0\u5347\u6cdb\u5316\u80fd\u529b\u3002<\/li>\n\n\n\n<li><strong>\u5e76\u884c\u52a0\u8f7d (Multiprocessing)<\/strong>: \u4f7f\u7528\u00a0<code>num_workers<\/code>\u00a0\u52a0\u901f\u6570\u636e\u52a0\u8f7d\uff0c\u907f\u514d I\/O \u74f6\u9888\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u8bad\u7ec3\u96c6\u4e0e\u6d4b\u8bd5\u96c6\u7684\u4e0d\u540c\u5904\u7406<\/strong>:\n<ul class=\"wp-block-list\">\n<li>\u8bad\u7ec3\u96c6\u901a\u5e38\u9700\u8981\u00a0<code>shuffle=True<\/code>\uff0c\u800c\u6d4b\u8bd5\u96c6\u5219\u8bbe\u4e3a\u00a0<code>False<\/code>\u3002<\/li>\n\n\n\n<li>\u8bad\u7ec3\u548c\u6d4b\u8bd5\u901a\u5e38\u4f7f\u7528\u76f8\u540c\u7684\u00a0<code>transform<\/code>\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u5206\u5e03\u4e00\u81f4\u3002\u4f46\u6709\u65f6\u6d4b\u8bd5\u4f1a\u4f7f\u7528\u4e0d\u540c\u7684\u53d8\u6362\uff08\u4f8b\u5982\uff0c\u4e0d\u5305\u542b\u6570\u636e\u589e\u5f3a\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u5f52\u4e00\u5316\u7684\u4f5c\u7528<\/strong>: \u5f52\u4e00\u5316\u80fd\u591f<strong>\u52a0\u901f\u6a21\u578b\u6536\u655b<\/strong>\uff0c\u5e76<strong>\u63d0\u9ad8\u8bad\u7ec3\u7a33\u5b9a\u6027<\/strong>\uff0c\u662f\u6df1\u5ea6\u5b66\u4e60\u9884\u5904\u7406\u4e2d\u975e\u5e38\u5173\u952e\u7684\u4e00\u6b65\u3002<\/li>\n\n\n\n<li><strong>\u540e\u7eed\u4f7f\u7528<\/strong>: \u5728\u8bad\u7ec3\u5faa\u73af\u4e2d\uff0c\u4f60\u4f1a\u8fd9\u6837\u4f7f\u7528\u5b83\uff1a<code>for\u00a0epoch\u00a0in\u00a0range(num_epochs):\u00a0<em># \u904d\u5386\u6240\u6709\u6570\u636e\u591a\u8f6e<\/em><br>\u00a0 \u00a0\u00a0for\u00a0images, labels\u00a0in\u00a0trainloader:\u00a0<em># \u8fed\u4ee3\u6279\u6b21<\/em><br>\u00a0 \u00a0 \u00a0 \u00a0\u00a0<em># 1. \u5c06 images \u8f93\u5165\u6a21\u578b\uff0c\u5f97\u5230\u9884\u6d4b\u8f93\u51fa<\/em><br>\u00a0 \u00a0 \u00a0 \u00a0\u00a0<em># 2. \u8ba1\u7b97\u635f\u5931 (loss)<\/em><br>\u00a0 \u00a0 \u00a0 \u00a0\u00a0<em># 3. \u53cd\u5411\u4f20\u64ad\uff0c\u66f4\u65b0\u6a21\u578b\u53c2\u6570<\/em><br>\u00a0 \u00a0 \u00a0 \u00a0 ...<br><\/code>\u5728\u8bc4\u4f30\u6a21\u578b\u65f6\uff0c\u5219\u4f7f\u7528\u00a0<code>testloader<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2699\ufe0f \u5173\u952e\u53c2\u6570\u603b\u7ed3\u4e0e\u5e38\u89c1\u8bbe\u7f6e<\/p>\n<\/blockquote>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u53c2\u6570<\/th><th>\u4f5c\u7528<\/th><th>\u5e38\u89c1\u8bbe\u7f6e<\/th><th>\u6ce8\u610f\u4e8b\u9879<\/th><\/tr><\/thead><tbody><tr><td><strong><code>batch_size<\/code><\/strong><\/td><td>\u6307\u5b9a\u6bcf\u4e2a\u6279\u6b21\u5305\u542b\u7684\u6837\u672c\u6570<\/td><td>16, 32, 64, 128, 256<\/td><td>\u6839\u636eGPU\u5185\u5b58\u8c03\u6574\uff0c\u8d8a\u5927\u8bad\u7ec3\u8d8a\u5feb\u4f46\u53ef\u80fd\u6cdb\u5316\u6027\u7a0d\u5dee<\/td><\/tr><tr><td><strong><code>shuffle<\/code><\/strong><\/td><td>\u662f\u5426\u5728\u6bcf\u4e2aepoch\u5f00\u59cb\u65f6\u6253\u4e71\u6570\u636e\u987a\u5e8f<\/td><td>\u8bad\u7ec3\u96c6:&nbsp;<code>True<\/code><br>\u6d4b\u8bd5\u96c6:&nbsp;<code>False<\/code><\/td><td>\u8bad\u7ec3\u96c6\u6253\u4e71\u9632\u6b62\u6a21\u578b\u8bb0\u5fc6\u987a\u5e8f<\/td><\/tr><tr><td><strong><code>num_workers<\/code><\/strong><\/td><td>\u7528\u4e8e\u6570\u636e\u52a0\u8f7d\u7684\u5b50\u8fdb\u7a0b\u6570<\/td><td>2, 4, 8 (\u901a\u5e38\u4e3aCPU\u6838\u5fc3\u6570\u76842-4\u500d)<\/td><td>\u8bbe\u7f6e\u592a\u9ad8\u53ef\u80fd\u8017\u5c3d\u5185\u5b58\uff0cWindows\u4e0b\u6709\u65f6\u6709\u95ee\u9898<\/td><\/tr><tr><td><strong><code>transform<\/code><\/strong><\/td><td>\u5b9a\u4e49\u6570\u636e\u7684\u9884\u5904\u7406\/\u589e\u5f3a\u6d41\u7a0b<\/td><td>\u5305\u542b<code>ToTensor<\/code>,&nbsp;<code>Normalize<\/code>\u7b49<\/td><td>\u8bad\u7ec3\u548c\u6d4b\u8bd5\u96c6\u7684\u53d8\u6362\u901a\u5e38\u4e00\u81f4<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u6570\u636e\u52a0\u8f7d\u662f\u6a21\u578b\u8bad\u7ec3\u7684\u7b2c\u4e00\u6b65\uff0c\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u6b65\u3002PyTorch \u7684&nbsp;<code>DataLoader<\/code>&nbsp;\u548c&nbsp;<code>torchvision.datasets<\/code>&nbsp;\u8ba9\u8fd9\u4e2a\u8fc7\u7a0b\u53d8\u5f97\u975e\u5e38\u7b80\u6d01\u548c\u9ad8\u6548\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.2.2 torchvision.transforms<\/h3>\n\n\n\n<p><strong>torchvision.transforms\u6a21\u5757\u63d0\u4f9b\u4e86\u5404\u79cd\u56fe\u50cf\u8f6c\u6362\u7684\u5de5\u5177<\/strong>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u8fdb\u884c\u56fe\u50cf\u9884\u5904\u7406\u548c\u6570\u636e\u589e\u5f3a\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Compose\u51fd\u6570\u6765\u7ec4\u5408\u4e86\u4e24\u4e2a\u56fe\u50cf\u5904\u7406\u64cd\u4f5c\uff1aToTensor\uff08\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3aTensor\uff09\u548cNormalize\uff08\u6807\u51c6\u5316\u56fe\u50cf\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.2.3 torchvision.models<\/h3>\n\n\n\n<p>torchvision.models\u6a21\u5757\u63d0\u4f9b\u4e86\u9884\u8bad\u7ec3\u7684\u6a21\u578b\uff0c\u5982ResNet\u3001VGG\u3001AlexNet\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u52a0\u8f7d\u8fd9\u4e9b\u6a21\u578b\uff0c\u5e76\u4f7f\u7528\u8fd9\u4e9b\u6a21\u578b\u8fdb\u884c\u8fc1\u79fb\u5b66\u4e60\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torchvision.models as models<br><br># \u52a0\u8f7d\u9884\u8bad\u7ec3\u7684resnet18\u6a21\u578b<br>resnet18 = models.resnet18(pretrained=True)<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662ftorchvision\u7684\u57fa\u672c\u4f7f\u7528\uff0c\u5b83\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u975e\u5e38\u4e30\u5bcc\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u5347\u6211\u4eec\u5904\u7406\u56fe\u50cf\u6570\u636e\u7684\u6548\u7387\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u518d\u5570\u55e6\u4e00\u4e0b\uff01<\/p>\n<\/blockquote>\n\n\n\n<p><code>torchvision.models<\/code>&nbsp;\u662f PyTorch \u751f\u6001\u4e2d\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u5e93\u3002\u4e0b\u9762\u8be6\u7ec6\u89e3\u91ca\u4e00\u4e0b\u4f60\u63d0\u5230\u7684\u4ee3\u7801\uff0c\u5e76\u8865\u5145\u4e00\u4e9b\u91cd\u8981\u6982\u5ff5\u548c\u5e38\u89c1\u7528\u6cd5\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83c\udfaf Torchvision Models \u4e0e\u8fc1\u79fb\u5b66\u4e60\u89e3\u6790<\/p>\n<\/blockquote>\n\n\n\n<p><code>torchvision.models<\/code>&nbsp;\u6a21\u5757\u662f PyTorch \u9488\u5bf9\u8ba1\u7b97\u673a\u89c6\u89c9\u4efb\u52a1\u63d0\u4f9b\u7684\u4e00\u4e2a<strong>\u9884\u8bad\u7ec3\u6a21\u578b\u5b9d\u5e93<\/strong>\u3002\u5b83\u6781\u5927\u5730\u7b80\u5316\u4e86\u5229\u7528\u524d\u6cbf\u6a21\u578b\u8fdb\u884c\u5f00\u53d1\u548c\u7814\u7a76\u7684\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol class=\"wp-block-list\">\n<li>\u52a0\u8f7d\u9884\u8bad\u7ec3\u6a21\u578b<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<p>\u4f60\u63d0\u4f9b\u7684\u4ee3\u7801\u662f\u52a0\u8f7d\u9884\u8bad\u7ec3 ResNet-18 \u6a21\u578b\u7684\u7ecf\u5178\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;torchvision.models&nbsp;as&nbsp;models<br>resnet18 = models.resnet18(pretrained=True)&nbsp;<em># \u52a0\u8f7d\u9884\u8bad\u7ec3\u7684resnet18\u6a21\u578b<\/em><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>models.resnet18()<\/code>: \u8fd9\u4f1a\u521b\u5efa\u4e00\u4e2a\u00a0<strong>ResNet-18 \u6a21\u578b\u7684\u7ed3\u6784<\/strong>\u3002<\/li>\n\n\n\n<li><code>pretrained=True<\/code>: \u8fd9\u662f<strong>\u5173\u952e\u53c2\u6570<\/strong>\u3002\u5f53\u8bbe\u7f6e\u4e3a\u00a0<code>True<\/code>\u00a0\u65f6\uff0cPyTorch \u4f1a\u81ea\u52a8\u4e0b\u8f7d\u5728\u00a0<strong>ImageNet \u6570\u636e\u96c6<\/strong>\uff08\u4e00\u4e2a\u5305\u542b1000\u4e2a\u7c7b\u522b\u7684\u5927\u89c4\u6a21\u56fe\u50cf\u6570\u636e\u96c6\uff09\u4e0a\u9884\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u6743\u91cd\uff0c\u5e76\u52a0\u8f7d\u5230\u8be5\u7ed3\u6784\u4e2d\u3002\u8fd9\u6837\u4f60\u5c31\u5f97\u5230\u4e86\u4e00\u4e2a\u5df2\u7ecf\u5177\u5907\u5f3a\u5927\u56fe\u50cf\u7279\u5f81\u63d0\u53d6\u80fd\u529b\u7684\u6a21\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u26a0\ufe0f \u65b0\u65e7\u7248\u672c\u5dee\u5f02<\/p>\n<\/blockquote>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728<strong>\u8f83\u65b0\u7248\u672c<\/strong>\u7684 PyTorch Torchvision \u4e2d\uff0c\u5b98\u65b9\u63a8\u8350\u4f7f\u7528\u66f4\u663e\u5f0f\u7684\u65b9\u5f0f\u6765\u6307\u5b9a\u6743\u91cd\uff0c\u800c\u4e0d\u662f&nbsp;<code>pretrained=True<\/code>\uff08\u8be5\u53c2\u6570\u5728\u4e00\u4e9b\u7248\u672c\u4e2d\u5df2\u88ab\u5f03\u7528\uff09\u3002\u63a8\u8350\u5199\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;torchvision.models&nbsp;as&nbsp;models<br>from&nbsp;torchvision.models&nbsp;import&nbsp;ResNet18_Weights&nbsp;<em># \u663e\u5f0f\u5bfc\u5165\u6743\u91cd<\/em><br><br><em># \u65b0\u7248\u672c\u63a8\u8350\u5199\u6cd5\uff1a\u4f7f\u7528 'weights' \u53c2\u6570<\/em><br>resnet18 = models.resnet18(weights=ResNet18_Weights.IMAGENET1K_V1)<br><em># \u6216\u8005\u7b49\u4ef7\u4e8e<\/em><br><em># resnet18 = models.resnet18(weights='IMAGENET1K_V1')<\/em><\/code><\/pre>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u5f0f\u660e\u786e\u6307\u5b9a\u4e86\u6240\u4f7f\u7528\u7684\u6743\u91cd\u7248\u672c\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u66f4\u597d\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u4e3a\u4ec0\u4e48\u4f7f\u7528\u9884\u8bad\u7ec3\u6a21\u578b\uff1a\u8fc1\u79fb\u5b66\u4e60<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<p>\u4f60\u63d0\u5230\u4e86\u201c\u8fc1\u79fb\u5b66\u4e60\u201d\uff0c\u8fd9\u6b63\u662f\u4f7f\u7528\u9884\u8bad\u7ec3\u6a21\u578b\u7684\u6838\u5fc3\u76ee\u7684\u3002\u5176<strong>\u6838\u5fc3\u601d\u60f3<\/strong>\u662f\u5229\u7528\u5728\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff08\u5982 ImageNet\uff09\u4e0a\u9884\u8bad\u7ec3\u7684\u6a21\u578b\u4f5c\u4e3a\u8d77\u70b9\uff0c\u5c06\u5176\u77e5\u8bc6\uff08\u5b66\u4e60\u5230\u7684\u7279\u5f81\u8868\u793a\uff09\u8fc1\u79fb\u5230\u65b0\u7684\u3001\u901a\u5e38\u6570\u636e\u91cf\u8f83\u5c0f\u7684\u7279\u5b9a\u4efb\u52a1\u4e2d\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83c\udfaf \u8fc1\u79fb\u5b66\u4e60\u7684\u4f18\u52bf<\/p>\n<\/blockquote>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8282\u7701\u8d44\u6e90\u548c\u65f6\u95f4<\/strong>\uff1a\u65e0\u9700\u4ece\u5934\u8bad\u7ec3\uff08\u53ef\u80fd\u9700\u6570\u5929\u751a\u81f3\u6570\u5468\uff09\uff0c\u53ea\u9700\u5fae\u8c03\uff0c\u5927\u5927\u51cf\u5c11\u8ba1\u7b97\u6210\u672c\u548c\u65f6\u95f4\u3002<\/li>\n\n\n\n<li><strong>\u63d0\u5347\u6027\u80fd<\/strong>\uff1a\u9884\u8bad\u7ec3\u6a21\u578b\u5df2\u5b66\u4e60\u5230\u901a\u7528\u7279\u5f81\uff08\u5982\u8fb9\u7f18\u3001\u7eb9\u7406\u3001\u5f62\u72b6\uff09\uff0c\u5373\u4f7f\u5728\u5c0f\u6570\u636e\u96c6\u4e0a\u5fae\u8c03\uff0c\u4e5f\u5e38\u80fd\u83b7\u5f97\u6bd4\u4ece\u96f6\u8bad\u7ec3\u66f4\u597d\u7684\u6027\u80fd\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u6570\u636e\u91cf\u6709\u9650\u7684\u4efb\u52a1\u3002<\/li>\n\n\n\n<li><strong>\u964d\u4f4e\u8fc7\u62df\u5408\u98ce\u9669<\/strong>\uff1a\u9884\u8bad\u7ec3\u6743\u91cd\u63d0\u4f9b\u4e86\u826f\u597d\u7684\u8d77\u70b9\u548c\u6b63\u5219\u5316\u6548\u679c\uff0c\u5728\u5c0f\u6570\u636e\u96c6\u4e0a\u8bad\u7ec3\u65f6\u66f4\u4e0d\u5bb9\u6613\u8fc7\u62df\u5408\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\u8fc1\u79fb\u5b66\u4e60\u7684\u5e38\u89c1\u7b56\u7565<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<p>\u6839\u636e\u4f60\u7684\u65b0\u4efb\u52a1\u548c\u6570\u636e\u91cf\uff0c\u901a\u5e38\u6709\u4e24\u79cd\u5fae\u8c03\u7b56\u7565\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7b56\u7565<\/th><th>\u64cd\u4f5c<\/th><th>\u9002\u7528\u573a\u666f<\/th><\/tr><\/thead><tbody><tr><td><strong>\u7279\u5f81\u63d0\u53d6 (Feature Extraction)<\/strong><\/td><td><strong>\u51bb\u7ed3<\/strong>\u9884\u8bad\u7ec3\u6a21\u578b\u7684\u6240\u6709\u5c42\uff08\u8bbe\u7f6e&nbsp;<code>requires_grad=False<\/code>\uff09\uff0c\u4ec5<strong>\u8bad\u7ec3\u65b0\u589e\u7684\u6216\u66ff\u6362\u7684\u5206\u7c7b\u5c42<\/strong>\u3002<\/td><td>\u6570\u636e\u96c6\u5f88\u5c0f\uff0c\u4e0e\u9884\u8bad\u7ec3\u6a21\u578b\u539f\u59cb\u4efb\u52a1\uff08\u5982 ImageNet\uff09\u76f8\u4f3c\u5ea6\u8f83\u9ad8\u3002<\/td><\/tr><tr><td><strong>\u5fae\u8c03 (Fine-Tuning)<\/strong><\/td><td><strong>\u89e3\u51bb<\/strong>\u9884\u8bad\u7ec3\u6a21\u578b\u7684\u90e8\u5206\u6216\u5168\u90e8\u5c42\uff08\u8bbe\u7f6e&nbsp;<code>requires_grad=True<\/code>\uff09\uff0c<strong>\u540c\u65f6\u8bad\u7ec3\u8fd9\u4e9b\u5c42\u548c\u65b0\u5206\u7c7b\u5c42<\/strong>\u3002<\/td><td>\u6570\u636e\u96c6\u8f83\u5927\uff0c\u6216\u65b0\u4efb\u52a1\u4e0e\u539f\u59cb\u4efb\u52a1\u5dee\u5f02\u8f83\u5927\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b56\u7565\u9009\u62e9\u793a\u4f8b<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"462\" height=\"739\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060547752.png\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" alt=\"\" class=\"wp-image-6758 lazyload\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060547752.png 462w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060547752-188x300.png 188w\" sizes=\"auto, (max-width: 462px) 100vw, 462px\" \/><noscript><img loading=\"lazy\" decoding=\"async\" width=\"462\" height=\"739\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060547752.png\" alt=\"\" class=\"wp-image-6758\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060547752.png 462w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060547752-188x300.png 188w\" sizes=\"auto, (max-width: 462px) 100vw, 462px\" \/><\/noscript><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><br><\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"4\" class=\"wp-block-list\">\n<li>\u8fc1\u79fb\u5b66\u4e60\u5b9e\u6218\u6b65\u9aa4<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u66f4\u5b8c\u6574\u7684\u8fc1\u79fb\u5b66\u4e60\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4fee\u6539\u6a21\u578b\u5e76\u8fdb\u884c\u8bad\u7ec3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;torch<br>import&nbsp;torch.nn&nbsp;as&nbsp;nn<br>import&nbsp;torch.optim&nbsp;as&nbsp;optim<br>import&nbsp;torchvision.models&nbsp;as&nbsp;models<br>from&nbsp;torchvision.models&nbsp;import&nbsp;ResNet18_Weights<br><br><em># 1. \u52a0\u8f7d\u9884\u8bad\u7ec3\u6a21\u578b<\/em><br>model = models.resnet18(weights=ResNet18_Weights.IMAGENET1K_V1)<br><br><em># 2. \u51bb\u7ed3\u6a21\u578b\u5e95\u5c42\u53c2\u6570\uff08\u6839\u636e\u7b56\u7565\u9009\u62e9\uff09<\/em><br><em># &nbsp; &nbsp;\u7279\u5f81\u63d0\u53d6\u65f6\u5e38\u7528\uff1a\u51bb\u7ed3\u6240\u6709\u5c42\uff0c\u53ea\u8bad\u7ec3\u5168\u8fde\u63a5\u5c42<\/em><br>for&nbsp;param&nbsp;in&nbsp;model.parameters():<br>&nbsp; &nbsp; param.requires_grad =&nbsp;False<br><br><em># 3. \u66ff\u6362\u6700\u540e\u7684\u5168\u8fde\u63a5\u5c42\uff08fc\uff09\u4ee5\u9002\u5e94\u65b0\u4efb\u52a1<\/em><br><em># &nbsp; &nbsp;\u5047\u8bbe\u65b0\u4efb\u52a1\u6709 10 \u4e2a\u7c7b\u522b<\/em><br>num_ftrs = model.fc.in_features<br>model.fc = nn.Linear(num_ftrs,&nbsp;10)&nbsp;<em># \u66ff\u6362\u4e3a\u8f93\u51fa10\u4e2a\u795e\u7ecf\u5143\u7684\u65b0\u5c42<\/em><br><br><em># 4. \u5b9a\u4e49\u635f\u5931\u51fd\u6570\u548c\u4f18\u5316\u5668<\/em><br><em># &nbsp; &nbsp;\u4f18\u5316\u5668\u901a\u5e38\u53ea\u4f18\u5316\u6700\u540e\u6dfb\u52a0\u7684\u5c42\uff08\u5982\u679c\u524d\u9762\u88ab\u51bb\u7ed3\u4e86\uff09<\/em><br>criterion = nn.CrossEntropyLoss()<br>optimizer = optim.Adam(model.fc.parameters(), lr=0.001)&nbsp;<em># \u53ea\u4f18\u5316\u5168\u8fde\u63a5\u5c42\u53c2\u6570<\/em><br><br><em># 5. \u8bad\u7ec3\u5faa\u73af\uff08\u6b64\u5904\u4e3a\u793a\u610f\uff0c\u9700\u63a5\u5165\u4f60\u7684DataLoader\uff09<\/em><br><em># ... \u5728\u8bad\u7ec3\u6570\u636e\u4e0a\u5faa\u73af\uff0c\u8ba1\u7b97\u635f\u5931\uff0c\u53cd\u5411\u4f20\u64ad\uff0c\u66f4\u65b0\u53c2\u6570 ...<\/em><br><em># for inputs, labels in your_dataloader:<\/em><br><em># &nbsp; &nbsp; optimizer.zero_grad()<\/em><br><em># &nbsp; &nbsp; outputs = model(inputs)<\/em><br><em># &nbsp; &nbsp; loss = criterion(outputs, labels)<\/em><br><em># &nbsp; &nbsp; loss.backward()<\/em><br><em># &nbsp; &nbsp; optimizer.step()<\/em><\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"5\" class=\"wp-block-list\">\n<li>Torchvision \u63d0\u4f9b\u7684\u6a21\u578b\u5bb6\u65cf<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<p><code>torchvision.models<\/code>&nbsp;\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u9884\u8bad\u7ec3\u6a21\u578b\uff0c\u8fdc\u8d85 ResNet\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6a21\u578b\u7c7b\u522b<\/th><th>\u4ee3\u8868\u6027\u6a21\u578b<\/th><th>\u7279\u70b9<\/th><\/tr><\/thead><tbody><tr><td><strong>\u7ecf\u5178CNN<\/strong><\/td><td>AlexNet, VGG, GoogLeNet<\/td><td>\u65e9\u671f\u7a81\u7834\uff0c\u7ed3\u6784\u76f8\u5bf9\u7b80\u5355<\/td><\/tr><tr><td><strong>\u6df1\u5ea6\u6b8b\u5dee\u7f51\u7edc<\/strong><\/td><td><strong>ResNet<\/strong>&nbsp;(18, 34, 50, 101, 152), Wide ResNet<\/td><td>\u901a\u8fc7\u6b8b\u5dee\u8fde\u63a5\u89e3\u51b3\u6df1\u5ea6\u7f51\u7edc\u68af\u5ea6\u6d88\u5931\u95ee\u9898<\/td><\/tr><tr><td><strong>\u9ad8\u6548\u79fb\u52a8\u7aef\u6a21\u578b<\/strong><\/td><td><strong>MobileNetV2, MobileNetV3<\/strong>, MnasNet<\/td><td>\u4e3a\u79fb\u52a8\u548c\u5d4c\u5165\u5f0f\u8bbe\u5907\u8bbe\u8ba1\uff0c\u8f7b\u91cf\u4e14\u9ad8\u6548<\/td><\/tr><tr><td><strong>\u73b0\u4ee3\u67b6\u6784<\/strong><\/td><td><strong>EfficientNet<\/strong>, ConvNeXt,&nbsp;<strong>Vision Transformer (ViT)<\/strong><\/td><td>\u8ffd\u6c42\u66f4\u597d\u7684\u7cbe\u5ea6-\u6548\u7387\u5e73\u8861\uff0c\u6216\u5f15\u5165Transformer\u7ed3\u6784<\/td><\/tr><tr><td><strong>\u5176\u4ed6\u4efb\u52a1<\/strong><\/td><td>Faster R-CNN, Mask R-CNN, FCN, DeepLabV3<\/td><td>\u7528\u4e8e\u76ee\u6807\u68c0\u6d4b\u3001\u5b9e\u4f8b\u5206\u5272\u3001\u8bed\u4e49\u5206\u5272\u7b49<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u6839\u636e\u4efb\u52a1\u9700\u6c42\uff08\u7cbe\u5ea6\u8981\u6c42\u3001\u901f\u5ea6\u8981\u6c42\u3001\u8ba1\u7b97\u8d44\u6e90\u7b49\uff09\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b\u3002\u4f8b\u5982\uff0c<code>resnet18<\/code>&nbsp;\u662f\u901f\u5ea6\u548c\u7cbe\u5ea6\u7684\u4e00\u4e2a\u826f\u597d\u5e73\u8861\u70b9\uff0c\u975e\u5e38\u9002\u5408\u5165\u95e8\u548c\u5feb\u901f\u539f\u578b\u5f00\u53d1\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udc8e \u91cd\u8981\u63d0\u9192<\/p>\n<\/blockquote>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8f93\u5165\u9884\u5904\u7406<\/strong>\uff1a\u4f7f\u7528\u9884\u8bad\u7ec3\u6a21\u578b\u65f6\uff0c\u8f93\u5165\u56fe\u50cf\u901a\u5e38\u9700\u8981<strong>\u6807\u51c6\u5316\uff08Normalize\uff09<\/strong>\u3002\u4e0d\u540c\u6a21\u578b\u8981\u6c42\u4e0d\u540c\u7684\u5747\u503c\u548c\u6807\u51c6\u5dee\uff08\u5e38\u89c1\u7684\u662f\u00a0<code>mean=[0.485, 0.456, 0.406]<\/code>,\u00a0<code>std=[0.229, 0.224, 0.225]<\/code>\uff09\u3002\u52a1\u5fc5\u4f7f\u7528\u4e0e\u6a21\u578b\u8bad\u7ec3\u65f6\u76f8\u540c\u7684\u9884\u5904\u7406\uff0c\u5426\u5219\u6027\u80fd\u4f1a\u4e0b\u964d\u3002<\/li>\n\n\n\n<li><strong>\u6a21\u578b\u6a21\u5f0f<\/strong>\uff1a\u8fdb\u884c<strong>\u8bad\u7ec3<\/strong>\u65f6\uff0c\u8c03\u7528\u00a0<code>model.train()<\/code>\uff1b\u8fdb\u884c<strong>\u63a8\u7406\/\u8bc4\u4f30<\/strong>\u65f6\uff0c\u8c03\u7528\u00a0<code>model.eval()<\/code>\u3002\u8fd9\u4f1a\u5f71\u54cd\u67d0\u4e9b\u5c42\uff08\u5982 Dropout \u548c BatchNorm\uff09\u7684\u884c\u4e3a\u3002<\/li>\n\n\n\n<li><strong>\u8bbe\u5907\u653e\u7f6e<\/strong>\uff1a\u4f7f\u7528\u00a0<code>model.to(device)<\/code>\uff08\u5982\u00a0<code>model.to('cuda')<\/code>\uff09\u5c06\u6a21\u578b\u79fb\u52a8\u5230 GPU \u4e0a\u52a0\u901f\u8bad\u7ec3\u548c\u63a8\u7406\u3002<\/li>\n<\/ol>\n\n\n\n<p><code>torchvision.models<\/code>&nbsp;\u6781\u5927\u5730\u964d\u4f4e\u4e86\u8ba1\u7b97\u673a\u89c6\u89c9\u5e94\u7528\u7684\u95e8\u69db\uff0c\u662f PyTorch \u751f\u6001\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.3 \u4f7f\u7528TensorBoard\u8fdb\u884c\u53ef\u89c6\u5316<\/h2>\n\n\n\n<p>TensorBoard \u662f\u4e00\u4e2a\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\uff0c\u4f18\u5316\uff0c\u548c\u8c03\u8bd5\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u3002PyTorch \u63d0\u4f9b\u4e86\u5bf9 TensorBoard \u7684\u652f\u6301\uff0c\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u4f7f\u7528 TensorBoard \u6765\u76d1\u63a7\u6a21\u578b\u7684\u8bad\u7ec3\u8fc7\u7a0b\uff0c\u6bd4\u8f83\u4e0d\u540c\u6a21\u578b\u7684\u6027\u80fd\uff0c\u53ef\u89c6\u5316\u6a21\u578b\u7ed3\u6784\uff0c\u7b49\u7b49\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.3.1 \u542f\u52a8 TensorBoard<\/h3>\n\n\n\n<p>\u8981\u542f\u52a8 TensorBoard\uff0c\u6211\u4eec\u9700\u8981\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c tensorboard --logdir=runs \u547d\u4ee4\uff0c\u5176\u4e2d runs \u662f\u4fdd\u5b58 TensorBoard \u6570\u636e\u7684\u76ee\u5f55\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.3.2 \u8bb0\u5f55\u6570\u636e<\/h3>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 torch.utils.tensorboard \u6a21\u5757\u6765\u8bb0\u5f55\u6570\u636e\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a SummaryWriter \u5bf9\u8c61\uff0c\u7136\u540e\u901a\u8fc7\u8fd9\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u8bb0\u5f55\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from torch.utils.tensorboard import SummaryWriter<br><br># \u521b\u5efa\u4e00\u4e2a SummaryWriter \u5bf9\u8c61<br>writer = SummaryWriter('runs\/experiment1')<br><br># \u4f7f\u7528 writer \u6765\u8bb0\u5f55\u6570\u636e<br>for n_iter in range(100):<br>&nbsp; &nbsp; writer.add_scalar('Loss\/train', np.random.random(), n_iter)<br>&nbsp; &nbsp; writer.add_scalar('Loss\/test', np.random.random(), n_iter)<br>&nbsp; &nbsp; writer.add_scalar('Accuracy\/train', np.random.random(), n_iter)<br>&nbsp; &nbsp; writer.add_scalar('Accuracy\/test', np.random.random(), n_iter)<br><br># \u5173\u95ed writer<br>writer.close()<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.3.3 \u53ef\u89c6\u5316\u6a21\u578b\u7ed3\u6784<\/h3>\n\n\n\n<p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 TensorBoard \u6765\u53ef\u89c6\u5316\u6a21\u578b\u7ed3\u6784\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u6dfb\u52a0\u6a21\u578b<br>writer.add_graph(model, images)<\/code><\/pre>\n\n\n\n<p>4.3.4 \u53ef\u89c6\u5316\u9ad8\u7ef4\u6570\u636e \u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 TensorBoard \u7684\u5d4c\u5165\u529f\u80fd\u6765\u53ef\u89c6\u5316\u9ad8\u7ef4\u6570\u636e\uff0c\u5982\u56fe\u50cf\u7279\u5f81\u3001\u8bcd\u5d4c\u5165\u7b49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u6dfb\u52a0\u5d4c\u5165<br>writer.add_embedding(features, metadata=class_labels, label_img=images)<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u5c31\u662f TensorBoard \u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u3002\u901a\u8fc7\u4f7f\u7528 TensorBoard\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f18\u5316\u6211\u4eec\u7684\u6a21\u578b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7ee7\u7eed\u767d\u8bdd<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4f5c\u4e3a\u6df1\u5ea6\u5b66\u4e60\u7684\u91cd\u8981\u5de5\u5177\uff0cTensorBoard \u80fd\u76f4\u89c2\u5730\u76d1\u63a7\u548c\u8c03\u8bd5\u6a21\u578b\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udcca \u4f7f\u7528 TensorBoard \u53ef\u89c6\u5316\u6a21\u578b\u8bad\u7ec3<\/p>\n<\/blockquote>\n\n\n\n<p>TensorBoard \u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u53ef\u89c6\u5316\u5de5\u5177\u5305\uff0c\u5b83 originally \u7531 TensorFlow \u56e2\u961f\u5f00\u53d1\uff0c\u4f46\u73b0\u5728\u5df2\u5b8c\u7f8e\u96c6\u6210\u5230 PyTorch \u4e2d\u3002\u5b83\u5141\u8bb8\u4f60\u8ddf\u8e2a\u548c\u53ef\u89c6\u5316\u5b9e\u9a8c\u4e2d\u7684\u5404\u79cd\u6307\u6807\uff0c\u5982\u635f\u5931\u548c\u51c6\u786e\u7387\u3001\u68c0\u67e5\u6a21\u578b\u7ed3\u6784\u3001\u5206\u6790\u6743\u91cd\u5206\u5e03\u7b49\uff0c\u4ece\u800c\u66f4\u597d\u5730\u7406\u89e3\u3001\u4f18\u5316\u548c\u8c03\u8bd5\u4f60\u7684\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol class=\"wp-block-list\">\n<li>\u5b89\u88c5\u4e0e\u542f\u52a8 TensorBoard<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5b89\u88c5 \u5728\u4f7f\u7528 TensorBoard \u4e4b\u524d\uff0c\u4f60\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u901a\u5e38\uff0c\u5982\u679c\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86 PyTorch\uff0c<code>tensorboard<\/code>&nbsp;\u53ef\u80fd\u5df2\u7ecf\u4f5c\u4e3a\u4f9d\u8d56\u4e4b\u4e00\u88ab\u5b89\u88c5\u4e86\u3002\u5982\u679c\u6ca1\u6709\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 pip \u5355\u72ec\u5b89\u88c5\uff1a<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install tensorboard<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u542f\u52a8 TensorBoard TensorBoard \u901a\u8fc7\u8bfb\u53d6\u65e5\u5fd7\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u6765\u5de5\u4f5c\u3002\u542f\u52a8 TensorBoard \u670d\u52a1\u5668\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u67e5\u770b\u53ef\u89c6\u5316\u754c\u9762\u3002<\/p>\n<\/blockquote>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5728<strong>\u547d\u4ee4\u884c<\/strong>\u6216<strong>\u7ec8\u7aef<\/strong>\u4e2d\uff0c\u5bfc\u822a\u5230\u4f60\u7684\u9879\u76ee\u76ee\u5f55\u3002<\/li>\n\n\n\n<li>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff0c\u5c06\u00a0<code>--logdir<\/code>\u00a0\u53c2\u6570\u6307\u5411\u4f60\u7684\u65e5\u5fd7\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff08\u4f8b\u5982\u00a0<code>runs<\/code>\uff09\uff1a<code>tensorboard --logdir=runs<\/code><\/li>\n\n\n\n<li>\u547d\u4ee4\u884c\u4f1a\u8f93\u51fa\u4e00\u4e2a URL\uff08\u901a\u5e38\u662f\u00a0<code>http:\/\/localhost:6006<\/code>\uff09\uff0c\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u5b83\u5373\u53ef\u8bbf\u95ee TensorBoard \u754c\u9762\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>\u63d0\u793a<\/strong>\uff1a\u4e3a\u4e86\u66f4\u597d\u7684\u7ec4\u7ec7\u6027\uff0c\u901a\u5e38\u4f1a\u5c06\u4e0d\u540c\u5b9e\u9a8c\u7684\u65e5\u5fd7\u4fdd\u5b58\u5728&nbsp;<code>runs<\/code>&nbsp;\u76ee\u5f55\u4e0b\u7684\u4e0d\u540c\u5b50\u6587\u4ef6\u5939\u4e2d\uff08\u4f8b\u5982&nbsp;<code>runs\/experiment_1<\/code>,&nbsp;<code>runs\/experiment_2<\/code>\uff09\u3002TensorBoard \u4f1a\u81ea\u52a8\u805a\u5408\u6240\u6709\u5b50\u76ee\u5f55\u4e2d\u7684\u65e5\u5fd7\uff0c\u4f60\u53ef\u4ee5\u5728\u754c\u9762\u9876\u90e8\u9009\u62e9\u67e5\u770b\u54ea\u4e2a\u6216\u54ea\u4e9b\u5b9e\u9a8c\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u5728 PyTorch \u4e2d\u8bb0\u5f55\u6570\u636e\uff1a<code>SummaryWriter<\/code><\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<p>PyTorch \u901a\u8fc7&nbsp;<code>torch.utils.tensorboard.SummaryWriter<\/code>&nbsp;\u7c7b\u6765\u5411\u65e5\u5fd7\u76ee\u5f55\u5199\u5165\u6570\u636e\uff0c\u8fd9\u662f\u4e0e TensorBoard \u4ea4\u4e92\u7684\u6838\u5fc3\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u521d\u59cb\u5316 SummaryWriter<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>from&nbsp;torch.utils.tensorboard&nbsp;import&nbsp;SummaryWriter<br><br><em># \u521b\u5efa\u4e00\u4e2a SummaryWriter \u5bf9\u8c61\uff0c\u6307\u5b9a\u65e5\u5fd7\u4fdd\u5b58\u7684\u76ee\u5f55\u3002<\/em><br><em># \u5982\u679c\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u4f1a\u81ea\u52a8\u521b\u5efa\u3002\u901a\u5e38\u5efa\u8bae\u4f7f\u7528\u5177\u6709\u63cf\u8ff0\u6027\u7684\u76ee\u5f55\u540d\uff0c\u4ee5\u4fbf\u533a\u5206\u4e0d\u540c\u5b9e\u9a8c\u3002<\/em><br>writer = SummaryWriter('runs\/experiment1')<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u8bb0\u5f55\u6807\u91cf\u6570\u636e\uff08Scalars\uff09 \u8fd9\u662f\u6700\u5e38\u7528\u7684\u529f\u80fd\uff0c\u7528\u4e8e\u8ddf\u8e2a\u968f\u65f6\u95f4\u53d8\u5316\u7684\u6307\u6807\uff0c\u5982\u635f\u5931\u548c\u51c6\u786e\u7387\u3002<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>for&nbsp;epoch&nbsp;in&nbsp;range(100):<br>&nbsp; &nbsp;&nbsp;<em># ... \u4f60\u7684\u8bad\u7ec3\u4ee3\u7801 ...<\/em><br>&nbsp; &nbsp;&nbsp;<em># \u5047\u8bbe\u5728\u6bcf\u4e2a epoch \u7ed3\u675f\u540e\u8ba1\u7b97\u51fa\u4e86\u8bad\u7ec3\u635f\u5931\u548c\u6d4b\u8bd5\u51c6\u786e\u7387<\/em><br>&nbsp; &nbsp; train_loss = ...<br>&nbsp; &nbsp; test_accuracy = ...<br><br>&nbsp; &nbsp;&nbsp;<em># \u4f7f\u7528 add_scalar \u8bb0\u5f55\u6807\u91cf\u503c<\/em><br>&nbsp; &nbsp;&nbsp;<em># 'Loss\/train' \u662f\u6807\u7b7e\u540d\uff0cTensorBoard \u4f1a\u6839\u636e\u659c\u6760\u8fdb\u884c\u5206\u7ec4\u663e\u793a<\/em><br>&nbsp; &nbsp;&nbsp;<em># train_loss \u662f\u8981\u8bb0\u5f55\u7684\u503c<\/em><br>&nbsp; &nbsp;&nbsp;<em># epoch \u662f\u5168\u5c40\u6b65\u957f\uff08x\u8f74\u7684\u503c\uff09<\/em><br>&nbsp; &nbsp; writer.add_scalar('Loss\/train', train_loss, epoch)<br>&nbsp; &nbsp; writer.add_scalar('Accuracy\/test', test_accuracy, epoch)<\/code><\/pre>\n\n\n\n<p>\u5728 TensorBoard \u7684&nbsp;<strong>Scalars<\/strong>&nbsp;\u6807\u7b7e\u9875\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u8fd9\u4e9b\u6307\u6807\u968f\u65f6\u95f4\u53d8\u5316\u7684\u66f2\u7ebf\uff0c\u8fd9\u5bf9\u4e8e\u76d1\u63a7\u8bad\u7ec3\u8fdb\u7a0b\u548c\u68c0\u6d4b\u8fc7\u62df\u5408\u975e\u5e38\u6709\u7528\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u53ef\u89c6\u5316\u6a21\u578b\u7ed3\u6784\uff08Graphs\uff09 \u4f60\u53ef\u4ee5\u5c06\u6a21\u578b\u7684\u8ba1\u7b97\u56fe\u53ef\u89c6\u5316\uff0c\u8fd9\u6709\u52a9\u4e8e\u7406\u89e3\u6a21\u578b\u7684\u5c42\u6b21\u7ed3\u6784\u548c\u6570\u636e\u6d41\u5411\u3002<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code><em># \u5047\u8bbe\u4f60\u5df2\u7ecf\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6a21\u578b<\/em><br>model = YourModel()<br><em># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u8f93\u5165\uff08\u901a\u5e38\u662f\u4e00\u4e2a\u4e0e\u771f\u5b9e\u8f93\u5165\u5c3a\u5bf8\u76f8\u540c\u7684\u865a\u62df\u5f20\u91cf\uff09<\/em><br>dummy_input = torch.randn(1,&nbsp;3,&nbsp;32,&nbsp;32) &nbsp;<em># \u793a\u4f8b\uff1abatch_size=1, 3\u901a\u9053, 32x32\u56fe\u50cf<\/em><br><br><em># \u5c06\u6a21\u578b\u548c\u793a\u4f8b\u8f93\u5165\u6dfb\u52a0\u5230 TensorBoard<\/em><br>writer.add_graph(model, dummy_input)<\/code><\/pre>\n\n\n\n<p>\u5728 TensorBoard \u7684&nbsp;<strong>Graphs<\/strong>&nbsp;\u6807\u7b7e\u9875\uff0c\u4f60\u53ef\u4ee5\u4ea4\u4e92\u5f0f\u5730\u67e5\u770b\u6a21\u578b\u7684\u5c42\u6b21\u7ed3\u6784\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u8bb0\u5f55\u56fe\u50cf\u6570\u636e\uff08Images\uff09 \u8fd9\u5bf9\u4e8e\u53ef\u89c6\u5316\u6a21\u578b\u7684\u8f93\u5165\u3001\u8f93\u51fa\u6216\u4e2d\u95f4\u7279\u5f81\u56fe\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code><em># \u5047\u8bbe 'images' \u662f\u4e00\u4e2a\u6279\u6b21\u4e2d\u7684\u56fe\u50cf\u5f20\u91cf<\/em><br><em># \u4f7f\u7528 torchvision.utils.make_grid \u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u56fe\u50cf\u7684\u7f51\u683c\u56fe<\/em><br>img_grid = torchvision.utils.make_grid(images)<br><br><em># \u5c06\u56fe\u50cf\u7f51\u683c\u6dfb\u52a0\u5230 TensorBoard<\/em><br>writer.add_image('example_images', img_grid, epoch) &nbsp;<em># \u53ef\u4ee5\u6307\u5b9a\u6b65\u957f\uff0c\u5982\u5f53\u524d epoch<\/em><\/code><\/pre>\n\n\n\n<p>\u5728 TensorBoard \u7684&nbsp;<strong>Images<\/strong>&nbsp;\u6807\u7b7e\u9875\uff0c\u4f60\u53ef\u4ee5\u67e5\u770b\u8fd9\u4e9b\u56fe\u50cf\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u8bb0\u5f55\u76f4\u65b9\u56fe\uff08Histograms\uff09 \u76f4\u65b9\u56fe\u53ef\u4ee5\u663e\u793a\u5f20\u91cf\u503c\u968f\u65f6\u95f4\u7684\u5206\u5e03\u53d8\u5316\uff0c\u5e38\u7528\u4e8e\u76d1\u63a7\u6743\u91cd\u548c\u68af\u5ea6\u7684\u5206\u5e03\uff0c\u5e2e\u52a9\u8bca\u65ad\u68af\u5ea6\u6d88\u5931\u6216\u7206\u70b8\u7b49\u95ee\u9898\u3002<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code><em># \u5b9a\u671f\u8bb0\u5f55\u6a21\u578b\u53c2\u6570\u7684\u5206\u5e03<\/em><br>if&nbsp;epoch %&nbsp;10&nbsp;==&nbsp;0: &nbsp;<em># \u6bcf10\u4e2aepoch\u8bb0\u5f55\u4e00\u6b21<\/em><br>&nbsp; &nbsp;&nbsp;for&nbsp;name, param&nbsp;in&nbsp;model.named_parameters():<br>&nbsp; &nbsp; &nbsp; &nbsp; writer.add_histogram(f'weights\/{name}', param, epoch)<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em># \u8fd8\u53ef\u4ee5\u8bb0\u5f55\u68af\u5ea6<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em># writer.add_histogram(f'grads\/{name}', param.grad, epoch)<\/em><\/code><\/pre>\n\n\n\n<p>\u5728 TensorBoard \u7684&nbsp;<strong>Histograms<\/strong>&nbsp;\u548c&nbsp;<strong>Distributions<\/strong>&nbsp;\u6807\u7b7e\u9875\u53ef\u4ee5\u67e5\u770b\u8fd9\u4e9b\u5206\u5e03\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u8bb0\u5f55\u5d4c\u5165\uff08Embeddings\uff09<code>add_embedding<\/code>&nbsp;\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5c06\u9ad8\u7ef4\u6570\u636e\uff08\u5982\u56fe\u50cf\u7279\u5f81\u3001\u8bcd\u5411\u91cf\uff09\u6295\u5f71\u5230\u4f4e\u7ef4\u7a7a\u95f4\uff08\u5982\u4f7f\u7528PCA\u3001t-SNE\uff09\uff0c\u4ece\u800c\u53ef\u89c6\u5316\u6570\u636e\u70b9\u4e4b\u95f4\u7684\u5173\u7cfb\u548c\u805a\u7c7b\u60c5\u51b5\u3002<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code><em># \u5047\u8bbe features \u662f\u4ece\u6a21\u578b\u67d0\u4e00\u5c42\u63d0\u53d6\u7684\u7279\u5f81\u5411\u91cf\uff0cmetadata \u662f\u5bf9\u5e94\u7684\u6807\u7b7e\uff08\u5982\u7c7b\u522b\uff09<\/em><br><em># label_img \u662f\u53ef\u9009\u7684\uff0c\u662f\u4e0e\u7279\u5f81\u5bf9\u5e94\u7684\u539f\u59cb\u56fe\u50cf\uff0c\u7528\u4e8e\u5728\u53ef\u89c6\u5316\u4e2d\u70b9\u5f00\u67e5\u770b<\/em><br>writer.add_embedding(features,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; metadata=class_labels,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label_img=images,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; global_step=epoch)<\/code><\/pre>\n\n\n\n<p>\u5728 TensorBoard \u7684&nbsp;<strong>Projector<\/strong>&nbsp;\u6807\u7b7e\u9875\u53ef\u4ee5\u4ea4\u4e92\u5f0f\u5730\u63a2\u7d22\u8fd9\u4e9b\u5d4c\u5165\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5173\u95ed SummaryWriter \u5b8c\u6210\u540e\uff0c\u52a1\u5fc5\u5173\u95ed&nbsp;<code>SummaryWriter<\/code>&nbsp;\u4ee5\u786e\u4fdd\u6240\u6709\u6570\u636e\u90fd\u5df2\u5199\u5165\u78c1\u76d8\u3002<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>writer.close()<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\u6838\u5fc3\u529f\u80fd\u4e0e\u4f7f\u7528\u573a\u666f\u603b\u7ed3<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<p>\u4e3a\u4e86\u8ba9 TensorBoard \u7684\u4f17\u591a\u529f\u80fd\u66f4\u4e00\u76ee\u4e86\u7136\uff0c\u6211\u4e3a\u4f60\u603b\u7ed3\u4e86\u4e00\u4e2a\u6838\u5fc3\u65b9\u6cd5\u8868\u683c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u4f5c\u7528<\/th><th>TensorBoard \u6807\u7b7e\u9875<\/th><th>\u4e3b\u8981\u5e94\u7528\u573a\u666f<\/th><\/tr><\/thead><tbody><tr><td><strong><code>add_scalar()<\/code><\/strong><\/td><td>\u8bb0\u5f55<strong>\u6807\u91cf\u6307\u6807<\/strong><\/td><td>SCALARS<\/td><td>\u8ddf\u8e2a\u635f\u5931\u3001\u51c6\u786e\u7387\u3001\u5b66\u4e60\u7387\u7b49\u6570\u503c\u968f\u65f6\u95f4\u7684\u53d8\u5316\u3002<\/td><\/tr><tr><td><strong><code>add_graph()<\/code><\/strong><\/td><td>\u8bb0\u5f55<strong>\u6a21\u578b\u8ba1\u7b97\u56fe<\/strong><\/td><td>GRAPHS<\/td><td>\u53ef\u89c6\u5316\u6a21\u578b\u7ed3\u6784\uff0c\u7406\u89e3\u6570\u636e\u6d41\u5411\u548c\u5c42\u95f4\u8fde\u63a5\u3002<\/td><\/tr><tr><td><strong><code>add_image()<\/code><\/strong>&nbsp;\/&nbsp;<strong><code>add_images()<\/code><\/strong><\/td><td>\u8bb0\u5f55<strong>\u56fe\u50cf\u6570\u636e<\/strong><\/td><td>IMAGES<\/td><td>\u67e5\u770b\u8f93\u5165\/\u8f93\u51fa\u56fe\u50cf\u3001\u4e2d\u95f4\u7279\u5f81\u56fe\u3001\u9519\u8bef\u9884\u6d4b\u6837\u672c\u3002<\/td><\/tr><tr><td><strong><code>add_histogram()<\/code><\/strong><\/td><td>\u8bb0\u5f55<strong>\u5f20\u91cf\u5206\u5e03\u76f4\u65b9\u56fe<\/strong><\/td><td>HISTOGRAMS, DISTRIBUTIONS<\/td><td>\u76d1\u63a7\u6743\u91cd\u3001\u68af\u5ea6\u7684\u5206\u5e03\uff0c\u8bca\u65ad\u68af\u5ea6\u95ee\u9898\u3002<\/td><\/tr><tr><td><strong><code>add_embedding()<\/code><\/strong><\/td><td>\u8bb0\u5f55<strong>\u9ad8\u7ef4\u6570\u636e\u5d4c\u5165<\/strong><\/td><td>PROJECTOR<\/td><td>\u53ef\u89c6\u5316\u7279\u5f81\u7a7a\u95f4\uff0c\u89c2\u5bdf\u6570\u636e\u805a\u7c7b\u548c\u5206\u5e03\u3002<\/td><\/tr><tr><td><strong><code>add_hparams()<\/code><\/strong><\/td><td>\u8bb0\u5f55<strong>\u8d85\u53c2\u6570\u7ec4\u5408<\/strong><\/td><td>HPARAMS<\/td><td>\u5bf9\u6bd4\u4e0d\u540c\u8d85\u53c2\u6570\u8bbe\u7f6e\u5bf9\u6a21\u578b\u6027\u80fd\u7684\u5f71\u54cd\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"4\" class=\"wp-block-list\">\n<li>\u6700\u4f73\u5b9e\u8df5\u4e0e\u6280\u5de7<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6709\u6761\u7406\u5730\u7ec4\u7ec7\u5b9e\u9a8c<\/strong>\uff1a\u4e3a\u4e0d\u540c\u7684\u5b9e\u9a8c\u8fd0\u884c\uff08\u4f8b\u5982\uff0c\u4e0d\u540c\u7684\u8d85\u53c2\u6570\u914d\u7f6e\u3001\u4e0d\u540c\u7684\u6a21\u578b\u67b6\u6784\uff09\u4f7f\u7528\u4e0d\u540c\u7684\u65e5\u5fd7\u5b50\u76ee\u5f55\uff08\u5982\u00a0<code>runs\/exp_lr0.01<\/code>,\u00a0<code>runs\/exp_lr0.001<\/code>\uff09\u3002TensorBoard \u53ef\u4ee5\u540c\u65f6\u663e\u793a\u548c\u5bf9\u6bd4\u591a\u4e2a\u8fd0\u884c\u7684\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u63a7\u5236\u8bb0\u5f55\u9891\u7387<\/strong>\uff1a\u5bf9\u4e8e\u6807\u91cf\u6570\u636e\uff0c\u53ef\u4ee5\u6bcf\u4e2a epoch \u8bb0\u5f55\u4e00\u6b21\u3002\u5bf9\u4e8e\u76f4\u65b9\u56fe\u548c\u56fe\u50cf\uff0c\u8bb0\u5f55\u9891\u7387\u4e0d\u5b9c\u8fc7\u9ad8\uff08\u4f8b\u5982\u6bcf\u51e0\u4e2a epoch \u4e00\u6b21\uff09\uff0c\u56e0\u4e3a\u5b83\u4eec\u4f1a\u751f\u6210\u8f83\u5927\u7684\u65e5\u5fd7\u6587\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u6709\u610f\u4e49\u7684\u6807\u7b7e\u540d\u79f0<\/strong>\uff1a\u4f7f\u7528\u7c7b\u4f3c\u00a0<code>'Loss\/train'<\/code>\u00a0\u548c\u00a0<code>'Accuracy\/val'<\/code>\u00a0\u7684\u547d\u540d\u65b9\u5f0f\uff0cTensorBoard \u4f1a\u81ea\u52a8\u6839\u636e\u659c\u6760\u8fdb\u884c\u5206\u7ec4\uff0c\u4f7f\u754c\u9762\u66f4\u52a0\u6e05\u6670\u3002<\/li>\n\n\n\n<li><strong>\u5145\u5206\u5229\u7528\u5bf9\u6bd4\u529f\u80fd<\/strong>\uff1a\u5728 TensorBoard \u7684 Scalars \u9762\u677f\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5e73\u6ed1\u66f2\u7ebf\u3001\u8c03\u6574\u6bd4\u4f8b\uff0c\u5e76\u5e76\u6392\u6bd4\u8f83\u591a\u4e2a\u5b9e\u9a8c\u7684\u8fd0\u884c\u7ed3\u679c\uff0c\u8fd9\u5bf9\u4e8e\u8d85\u53c2\u6570\u8c03\u4f18\u81f3\u5173\u91cd\u8981\u3002<\/li>\n\n\n\n<li><strong>\u5b9a\u671f\u6e05\u7406\u65e7\u65e5\u5fd7<\/strong>\uff1a\u65e5\u5fd7\u6587\u4ef6\u53ef\u80fd\u4f1a\u5360\u7528\u5927\u91cf\u78c1\u76d8\u7a7a\u95f4\uff0c\u8bb0\u5f97\u5b9a\u671f\u6e05\u7406\u4e0d\u518d\u9700\u8981\u7684\u65e7\u65e5\u5fd7\u6587\u4ef6\u3002<\/li>\n<\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ol start=\"5\" class=\"wp-block-list\">\n<li>\u7b80\u5355\u793a\u4f8b\uff1a\u8bad\u7ec3\u5faa\u73af\u4e2d\u7684\u96c6\u6210<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u66f4\u5b8c\u6574\u7684\u5728\u8bad\u7ec3\u5faa\u73af\u4e2d\u4f7f\u7528 TensorBoard \u7684\u7247\u6bb5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from&nbsp;torch.utils.tensorboard&nbsp;import&nbsp;SummaryWriter<br>import&nbsp;torchvision<br><br>writer = SummaryWriter('runs\/my_experiment')<br><br>for&nbsp;epoch&nbsp;in&nbsp;range(num_epochs):<br>&nbsp; &nbsp;&nbsp;<em># ... \u8bad\u7ec3\u4e00\u4e2a epoch ...<\/em><br>&nbsp; &nbsp; train_loss = ...<br>&nbsp; &nbsp; train_acc = ...<br>&nbsp; &nbsp; val_loss = ...<br>&nbsp; &nbsp; val_acc = ...<br><br>&nbsp; &nbsp;&nbsp;<em># \u8bb0\u5f55\u6807\u91cf<\/em><br>&nbsp; &nbsp; writer.add_scalar('Loss\/train', train_loss, epoch)<br>&nbsp; &nbsp; writer.add_scalar('Accuracy\/train', train_acc, epoch)<br>&nbsp; &nbsp; writer.add_scalar('Loss\/validation', val_loss, epoch)<br>&nbsp; &nbsp; writer.add_scalar('Accuracy\/validation', val_acc, epoch)<br><br>&nbsp; &nbsp;&nbsp;<em># \u8bb0\u5f55\u4e00\u4e9b\u6743\u91cd\u76f4\u65b9\u56fe<\/em><br>&nbsp; &nbsp;&nbsp;if&nbsp;epoch %&nbsp;5&nbsp;==&nbsp;0:<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;for&nbsp;name, param&nbsp;in&nbsp;model.named_parameters():<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.add_histogram(name, param, epoch)<br><br>&nbsp; &nbsp;&nbsp;<em># \u8bb0\u5f55\u4e00\u6279\u9a8c\u8bc1\u56fe\u50cf\u548c\u9884\u6d4b\uff08\u793a\u4f8b\uff09<\/em><br>&nbsp; &nbsp;&nbsp;if&nbsp;epoch ==&nbsp;0or&nbsp;epoch %&nbsp;10&nbsp;==&nbsp;0:<br>&nbsp; &nbsp; &nbsp; &nbsp; images, labels = next(iter(val_loader))<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;with&nbsp;torch.no_grad():<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputs = model(images)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em># ... \u5904\u7406\u5e76\u8bb0\u5f55\u56fe\u50cf ...<\/em><br><br>writer.close()<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7\u4f7f\u7528 TensorBoard\uff0c\u53ef\u4ee5\u5c06\u8bad\u7ec3\u8fc7\u7a0b\u4ece\u201c\u9ed1\u76d2\u201d\u8f6c\u53d8\u4e3a\u53ef\u89c2\u5bdf\u3001\u53ef\u5206\u6790\u7684\u8fc7\u7a0b\uff0c\u6781\u5927\u5730\u63d0\u5347\u6df1\u5ea6\u5b66\u4e60\u9879\u76ee\u7684\u5f00\u53d1\u6548\u7387\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">5. PyTorch\u5b9e\u6218\u6848\u4f8b<\/h1>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"408\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060553245-1024x408.png\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" alt=\"\" class=\"wp-image-6759 lazyload\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060553245-1024x408.png 1024w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060553245-300x119.png 300w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060553245-768x306.png 768w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060553245.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><noscript><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"408\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060553245-1024x408.png\" alt=\"\" class=\"wp-image-6759\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060553245-1024x408.png 1024w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060553245-300x119.png 300w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060553245-768x306.png 768w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060553245.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/noscript><\/figure>\n\n\n\n<p>\u5728\u8fd9\u4e00\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u6765\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528PyTorch\u8fdb\u884c\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u7684\u5f00\u53d1\u3002\u6211\u4eec\u5c06\u4f7f\u7528CIFAR10\u6570\u636e\u96c6\u6765\u8bad\u7ec3\u4e00\u4e2a\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\uff08Convolutional Neural Network\uff0cCNN\uff09\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.1 \u6570\u636e\u52a0\u8f7d\u548c\u9884\u5904\u7406<\/h2>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u52a0\u8f7d\u6570\u636e\u5e76\u8fdb\u884c\u9884\u5904\u7406\u3002\u6211\u4eec\u5c06\u4f7f\u7528torchvision\u5305\u6765\u4e0b\u8f7dCIFAR10\u6570\u636e\u96c6\uff0c\u5e76\u4f7f\u7528transforms\u6a21\u5757\u6765\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch<br>from torchvision import datasets, transforms<br><br># \u5b9a\u4e49\u6570\u636e\u9884\u5904\u7406\u64cd\u4f5c<br>transform = transforms.Compose(&#91;<br>&nbsp; &nbsp; transforms.RandomHorizontalFlip(), &nbsp;# \u6570\u636e\u589e\u5f3a\uff1a\u968f\u673a\u7ffb\u8f6c\u56fe\u7247<br>&nbsp; &nbsp; transforms.RandomCrop(32, padding=4), &nbsp;# \u6570\u636e\u589e\u5f3a\uff1a\u968f\u673a\u88c1\u526a\u56fe\u7247<br>&nbsp; &nbsp; transforms.ToTensor(), &nbsp;# \u5c06PIL.Image\u6216\u8005numpy.ndarray\u6570\u636e\u7c7b\u578b\u8f6c\u5316\u4e3atorch.FloadTensor\uff0c\u5e76\u5f52\u4e00\u5316\u5230&#91;0.0, 1.0]<br>&nbsp; &nbsp; transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)) &nbsp;# \u6807\u51c6\u5316\uff08\u8fd9\u91cc\u7684\u5747\u503c\u548c\u6807\u51c6\u5dee\u662fCIFAR10\u6570\u636e\u96c6\u7684\uff09<br>])<br><br># \u4e0b\u8f7d\u5e76\u52a0\u8f7d\u8bad\u7ec3\u6570\u636e\u96c6<br>trainset = datasets.CIFAR10(root='.\/data', train=True, download=True, transform=transform)<br>trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True, num_workers=2)<br><br># \u4e0b\u8f7d\u5e76\u52a0\u8f7d\u6d4b\u8bd5\u6570\u636e\u96c6<br>testset = datasets.CIFAR10(root='.\/data', train=False, download=True, transform=transform)<br>testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=False, num_workers=2)<br><\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u7cfb\u5217\u7684\u6570\u636e\u9884\u5904\u7406\u64cd\u4f5c\uff0c\u7136\u540e\u4f7f\u7528datasets.CIFAR10\u6765\u4e0b\u8f7dCIFAR10\u6570\u636e\u96c6\u5e76\u8fdb\u884c\u9884\u5904\u7406\uff0c\u6700\u540e\u4f7f\u7528torch.utils.data.DataLoader\u6765\u521b\u5efa\u6570\u636e\u52a0\u8f7d\u5668\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u6309\u7167\u6279\u6b21\u83b7\u53d6\u6570\u636e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.2 \u5b9a\u4e49\u7f51\u7edc\u6a21\u578b<\/h2>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5b9a\u4e49\u6211\u4eec\u7684\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u3002\u5728\u8fd9\u4e2a\u6848\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u4e24\u4e2a\u5377\u79ef\u5c42\u548c\u4e24\u4e2a\u5168\u8fde\u63a5\u5c42\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch.nn as nn<br>import torch.nn.functional as F<br><br>class Net(nn.Module):<br>&nbsp; &nbsp; def __init__(self):<br>&nbsp; &nbsp; &nbsp; &nbsp; super(Net, self).__init__()<br>&nbsp; &nbsp; &nbsp; &nbsp; self.conv1 = nn.Conv2d(3, 6, 5) &nbsp;# \u8f93\u5165\u901a\u9053\u65703\uff0c\u8f93\u51fa\u901a\u9053\u65706\uff0c\u5377\u79ef\u6838\u5927\u5c0f5<br>&nbsp; &nbsp; &nbsp; &nbsp; self.pool = nn.MaxPool2d(2, 2) &nbsp;# \u6700\u5927\u6c60\u5316\uff0c\u6838\u5927\u5c0f2\uff0c\u6b65\u957f2<br>&nbsp; &nbsp; &nbsp; &nbsp; self.conv2 = nn.Conv2d(6, 16, 5) &nbsp;# \u8f93\u5165\u901a\u9053\u65706\uff0c\u8f93\u51fa\u901a\u9053\u657016\uff0c\u5377\u79ef\u6838\u5927\u5c0f5<br>&nbsp; &nbsp; &nbsp; &nbsp; self.fc1 = nn.Linear(16 * 5 * 5, 120) &nbsp;# \u5168\u8fde\u63a5\u5c42\uff0c\u8f93\u5165\u7ef4\u5ea616*5*5\uff0c\u8f93\u51fa\u7ef4\u5ea6120<br>&nbsp; &nbsp; &nbsp; &nbsp; self.fc2 = nn.Linear(120, 84) &nbsp;# \u5168\u8fde\u63a5\u5c42\uff0c\u8f93\u5165\u7ef4\u5ea6120\uff0c\u8f93\u51fa\u7ef4\u5ea684<br>&nbsp; &nbsp; &nbsp; &nbsp; self.fc3 = nn.Linear(84, 10) &nbsp;# \u5168\u8fde\u63a5\u5c42\uff0c\u8f93\u5165\u7ef4\u5ea684\uff0c\u8f93\u51fa\u7ef4\u5ea610\uff08CIFAR10\u670910\u7c7b\uff09<br><br>&nbsp; &nbsp; def forward(self, x):<br>&nbsp; &nbsp; &nbsp; &nbsp; x = self.pool(F.relu(self.conv1(x))) &nbsp;# \u7b2c\u4e00\u5c42\u5377\u79ef+ReLU\u6fc0\u6d3b\u51fd\u6570+\u6c60\u5316<br>&nbsp; &nbsp; &nbsp; &nbsp; x = self.pool(F.relu(self.conv2(x))) &nbsp;# \u7b2c\u4e8c\u5c42\u5377\u79ef+ReLU\u6fc0\u6d3b\u51fd\u6570+\u6c60\u5316<br>&nbsp; &nbsp; &nbsp; &nbsp; x = x.view(-1, 16 * 5 * 5) &nbsp;# \u5c06\u7279\u5f81\u56fe\u5c55\u5e73<br>&nbsp; &nbsp; &nbsp; &nbsp; x = F.relu(self.fc1(x)) &nbsp;# \u7b2c\u4e00\u5c42\u5168\u8fde\u63a5+ReLU\u6fc0\u6d3b\u51fd\u6570<br>&nbsp; &nbsp; &nbsp; &nbsp; x = F.relu(self.fc2(x)) &nbsp;# \u7b2c\u4e8c\u5c42\u5168\u8fde\u63a5+ReLU\u6fc0\u6d3b\u51fd\u6570<br>&nbsp; &nbsp; &nbsp; &nbsp; x = self.fc3(x) &nbsp;# \u7b2c\u4e09\u5c42\u5168\u8fde\u63a5<br>&nbsp; &nbsp; &nbsp; &nbsp; return x<br><br># \u521b\u5efa\u7f51\u7edc<br>net = Net()<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u7f51\u7edc\u6a21\u578b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528nn.Module\u6765\u5b9a\u4e49\u6211\u4eec\u7684\u7f51\u7edc\u6a21\u578b\uff0c\u7136\u540e\u5728__init__\u65b9\u6cd5\u4e2d\u5b9a\u4e49\u7f51\u7edc\u7684\u5c42\uff0c\u6700\u540e\u5728forward\u65b9\u6cd5\u4e2d\u5b9a\u4e49\u7f51\u7edc\u7684\u524d\u5411\u4f20\u64ad\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.3 \u5b9a\u4e49\u635f\u5931\u51fd\u6570\u548c\u4f18\u5316\u5668<\/h2>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u6709\u4e86\u6570\u636e\u548c\u6a21\u578b\uff0c\u4e0b\u4e00\u6b65\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u635f\u5931\u51fd\u6570\u548c\u4f18\u5316\u5668\u3002\u635f\u5931\u51fd\u6570\u7528\u4e8e\u8861\u91cf\u6a21\u578b\u7684\u9884\u6d4b\u4e0e\u771f\u5b9e\u6807\u7b7e\u7684\u5dee\u8ddd\uff0c\u4f18\u5316\u5668\u5219\u7528\u4e8e\u4f18\u5316\u6a21\u578b\u7684\u53c2\u6570\u4ee5\u51cf\u5c11\u635f\u5931\u3002<\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u6848\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u4ea4\u53c9\u71b5\u635f\u5931\u51fd\u6570\uff08Cross Entropy Loss\uff09\u548c\u968f\u673a\u68af\u5ea6\u4e0b\u964d\u4f18\u5316\u5668\uff08Stochastic Gradient Descent\uff0cSGD\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch.optim as optim<br><br># \u5b9a\u4e49\u635f\u5931\u51fd\u6570<br>criterion = nn.CrossEntropyLoss()<br><br># \u5b9a\u4e49\u4f18\u5316\u5668<br>optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)<br><\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528nn.CrossEntropyLoss\u6765\u5b9a\u4e49\u635f\u5931\u51fd\u6570\uff0c\u7136\u540e\u4f7f\u7528optim.SGD\u6765\u5b9a\u4e49\u4f18\u5316\u5668\u3002\u6211\u4eec\u9700\u8981\u5c06\u7f51\u7edc\u7684\u53c2\u6570\u4f20\u9012\u7ed9\u4f18\u5316\u5668\uff0c\u7136\u540e\u8bbe\u7f6e\u5b66\u4e60\u7387\u548c\u52a8\u91cf\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.4 \u8bad\u7ec3\u7f51\u7edc<\/h2>\n\n\n\n<p>\u4e00\u5207\u51c6\u5907\u5c31\u7eea\u540e\uff0c\u6211\u4eec\u5f00\u59cb\u8bad\u7ec3\u7f51\u7edc\u3002\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7\u7f51\u7edc\u8fdb\u884c\u524d\u5411\u4f20\u64ad\u5f97\u5230\u8f93\u51fa\uff0c\u7136\u540e\u8ba1\u7b97\u8f93\u51fa\u4e0e\u771f\u5b9e\u6807\u7b7e\u7684\u635f\u5931\uff0c\u63a5\u7740\u901a\u8fc7\u540e\u5411\u4f20\u64ad\u8ba1\u7b97\u68af\u5ea6\uff0c\u6700\u540e\u4f7f\u7528\u4f18\u5316\u5668\u66f4\u65b0\u6a21\u578b\u53c2\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for epoch in range(2): &nbsp;# \u5728\u6570\u636e\u96c6\u4e0a\u8bad\u7ec3\u4e24\u904d<br><br>&nbsp; &nbsp; running_loss = 0.0<br>&nbsp; &nbsp; for i, data in enumerate(trainloader, 0):<br>&nbsp; &nbsp; &nbsp; &nbsp; # \u83b7\u53d6\u8f93\u5165\u6570\u636e<br>&nbsp; &nbsp; &nbsp; &nbsp; inputs, labels = data<br><br>&nbsp; &nbsp; &nbsp; &nbsp; # \u68af\u5ea6\u6e05\u96f6<br>&nbsp; &nbsp; &nbsp; &nbsp; optimizer.zero_grad()<br><br>&nbsp; &nbsp; &nbsp; &nbsp; # \u524d\u5411\u4f20\u64ad<br>&nbsp; &nbsp; &nbsp; &nbsp; outputs = net(inputs)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; # \u8ba1\u7b97\u635f\u5931<br>&nbsp; &nbsp; &nbsp; &nbsp; loss = criterion(outputs, labels)<br><br>&nbsp; &nbsp; &nbsp; &nbsp; # \u53cd\u5411\u4f20\u64ad<br>&nbsp; &nbsp; &nbsp; &nbsp; loss.backward()<br><br>&nbsp; &nbsp; &nbsp; &nbsp; # \u66f4\u65b0\u53c2\u6570<br>&nbsp; &nbsp; &nbsp; &nbsp; optimizer.step()<br><br>&nbsp; &nbsp; &nbsp; &nbsp; # \u6253\u5370\u7edf\u8ba1\u4fe1\u606f<br>&nbsp; &nbsp; &nbsp; &nbsp; running_loss += loss.item()<br>&nbsp; &nbsp; &nbsp; &nbsp; if i % 2000 == 1999: &nbsp;# \u6bcf2000\u4e2a\u6279\u6b21\u6253\u5370\u4e00\u6b21<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('&#91;%d, %5d] loss: %.3f' %<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (epoch + 1, i + 1, running_loss \/ 2000))<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; running_loss = 0.0<br><br>print('Finished Training')<br><\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bf9\u6570\u636e\u96c6\u8fdb\u884c\u4e24\u8f6e\u8bad\u7ec3\u3002\u5728\u6bcf\u8f6e\u8bad\u7ec3\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u6570\u636e\u52a0\u8f7d\u5668\uff0c\u83b7\u53d6\u4e00\u6279\u6570\u636e\uff0c\u7136\u540e\u901a\u8fc7\u7f51\u7edc\u8fdb\u884c\u524d\u5411\u4f20\u64ad\u5f97\u5230\u8f93\u51fa\uff0c\u8ba1\u7b97\u635f\u5931\uff0c\u8fdb\u884c\u53cd\u5411\u4f20\u64ad\uff0c\u6700\u540e\u66f4\u65b0\u53c2\u6570\u3002\u6211\u4eec\u8fd8\u5728\u6bcf2000\u4e2a\u6279\u6b21\u540e\u6253\u5370\u4e00\u6b21\u635f\u5931\u4fe1\u606f\uff0c\u4ee5\u4fbf\u6211\u4eec\u4e86\u89e3\u8bad\u7ec3\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.5 \u6d4b\u8bd5\u7f51\u7edc<\/h2>\n\n\n\n<p>\u8bad\u7ec3\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u9700\u8981\u5728\u6d4b\u8bd5\u96c6\u4e0a\u6d4b\u8bd5\u7f51\u7edc\u7684\u6027\u80fd\u3002\u8fd9\u53ef\u4ee5\u8ba9\u6211\u4eec\u4e86\u89e3\u6a21\u578b\u5728\u672a\u89c1\u8fc7\u7684\u6570\u636e\u4e0a\u7684\u8868\u73b0\u5982\u4f55\uff0c\u4ee5\u8bc4\u4f30\u5176\u6cdb\u5316\u80fd\u529b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u52a0\u8f7d\u4e00\u4e9b\u6d4b\u8bd5\u56fe\u7247<br>dataiter = iter(testloader)<br>images, labels = dataiter.next()<br><br># \u6253\u5370\u56fe\u7247<br>imshow(torchvision.utils.make_grid(images))<br><br># \u663e\u793a\u771f\u5b9e\u7684\u6807\u7b7e<br>print('GroundTruth: ', ' '.join('%5s' % classes&#91;labels&#91;j]] for j in range(4)))<br><br># \u8ba9\u7f51\u7edc\u505a\u51fa\u9884\u6d4b<br>outputs = net(images)<br><br># \u9884\u6d4b\u7684\u6807\u7b7e\u662f\u6700\u5927\u8f93\u51fa\u7684\u6807\u7b7e<br>_, predicted = torch.max(outputs, 1)<br><br># \u663e\u793a\u9884\u6d4b\u7684\u6807\u7b7e<br>print('Predicted: ', ' '.join('%5s' % classes&#91;predicted&#91;j]] for j in range(4)))<br><br># \u5728\u6574\u4e2a\u6d4b\u8bd5\u96c6\u4e0a\u6d4b\u8bd5\u7f51\u7edc<br>correct = 0<br>total = 0<br>with torch.no_grad():<br>&nbsp; &nbsp; for data in testloader:<br>&nbsp; &nbsp; &nbsp; &nbsp; images, labels = data<br>&nbsp; &nbsp; &nbsp; &nbsp; outputs = net(images)<br>&nbsp; &nbsp; &nbsp; &nbsp; _, predicted = torch.max(outputs.data, 1)<br>&nbsp; &nbsp; &nbsp; &nbsp; total += labels.size(0)<br>&nbsp; &nbsp; &nbsp; &nbsp; correct += (predicted == labels).sum().item()<br><br>print('Accuracy of the network on the 10000 test images: %d %%' % (<br>&nbsp; &nbsp; 100 * correct \/ total))<br><\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u52a0\u8f7d\u4e00\u4e9b\u6d4b\u8bd5\u56fe\u7247\uff0c\u5e76\u6253\u5370\u51fa\u771f\u5b9e\u7684\u6807\u7b7e\u3002\u7136\u540e\u6211\u4eec\u8ba9\u7f51\u7edc\u5bf9\u8fd9\u4e9b\u56fe\u7247\u505a\u51fa\u9884\u6d4b\uff0c\u5e76\u6253\u5370\u51fa\u9884\u6d4b\u7684\u6807\u7b7e\u3002\u6700\u540e\uff0c\u6211\u4eec\u5728\u6574\u4e2a\u6d4b\u8bd5\u96c6\u4e0a\u6d4b\u8bd5\u7f51\u7edc\uff0c\u5e76\u6253\u5370\u51fa\u7f51\u7edc\u5728\u6d4b\u8bd5\u96c6\u4e0a\u7684\u51c6\u786e\u7387\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.6 \u4fdd\u5b58\u548c\u52a0\u8f7d\u6a21\u578b<\/h2>\n\n\n\n<p>\u5728\u8bad\u7ec3\u5b8c\u7f51\u7edc\u5e76\u4e14\u5bf9\u5176\u8fdb\u884c\u4e86\u6d4b\u8bd5\u540e\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u4fdd\u5b58\u8bad\u7ec3\u597d\u7684\u6a21\u578b\uff0c\u4ee5\u4fbf\u4e8e\u5c06\u6765\u4f7f\u7528\uff0c\u6216\u8005\u7ee7\u7eed\u8bad\u7ec3\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4fdd\u5b58\u6a21\u578b<br>torch.save(net.state_dict(), '.\/cifar_net.pth')<br>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528torch.save\u51fd\u6570\uff0c\u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u53c2\u6570\uff08\u901a\u8fc7net.state_dict()\u83b7\u5f97\uff09\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002<br><br>\u5f53\u6211\u4eec\u9700\u8981\u52a0\u8f7d\u6a21\u578b\u65f6\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6a21\u578b\u5b9e\u4f8b\uff0c\u7136\u540e\u4f7f\u7528load_state_dict\u65b9\u6cd5\u5c06\u53c2\u6570\u52a0\u8f7d\u5230\u6a21\u578b\u4e2d\u3002<br><br># \u52a0\u8f7d\u6a21\u578b<br>net = Net() &nbsp;# \u521b\u5efa\u65b0\u7684\u7f51\u7edc\u5b9e\u4f8b<br>net.load_state_dict(torch.load('.\/cifar_net.pth')) &nbsp;# \u52a0\u8f7d\u6a21\u578b\u53c2\u6570<\/code><\/pre>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cload_state_dict\u65b9\u6cd5\u52a0\u8f7d\u7684\u662f\u6a21\u578b\u7684\u53c2\u6570\uff0c\u800c\u4e0d\u662f\u6a21\u578b\u672c\u8eab\u3002\u56e0\u6b64\uff0c\u5728\u52a0\u8f7d\u6a21\u578b\u53c2\u6570\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u5148\u521b\u5efa\u4e00\u4e2a\u6a21\u578b\u5b9e\u4f8b\uff0c\u8fd9\u4e2a\u6a21\u578b\u9700\u8981\u4e0e\u4fdd\u5b58\u7684\u6a21\u578b\u5177\u6709\u76f8\u540c\u7684\u7ed3\u6784\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u8fd9\u4e2a\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50\uff0c\u5f88\u4e0d\u9519\uff01\u770b\u5b8c\u540e\u6211\u4eec\u518d\u4e00\u8d77\u68b3\u7406\u4e00\u4e0b\u8109\u7edc\uff0c\u6bd5\u7adf \u6211\u662f\u5c0f\u767d\u3002\u91cd\u590d\uff01\uff01<\/p>\n<\/blockquote>\n\n\n\n<p>\u68b3\u7406\u4f7f\u7528PyTorch\u8fdb\u884cCIFAR-10\u56fe\u50cf\u5206\u7c7b\u7684\u5b8c\u6574\u6d41\u7a0b\uff0c\u5e76\u7528\u6d41\u7a0b\u56fe\u548c\u8868\u683c\u6765\u76f4\u89c2\u5c55\u793a\u5173\u952e\u6b65\u9aa4\u548c\u51b3\u7b56\u70b9\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83e\udde0 PyTorch CIFAR-10\u56fe\u50cf\u5206\u7c7b\u5b9e\u6218\u5168\u9762\u89e3\u6790<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udd0d 1. \u9879\u76ee\u6982\u8ff0\u4e0e\u6838\u5fc3\u76ee\u6807<\/p>\n<\/blockquote>\n\n\n\n<p>CIFAR-10\u6570\u636e\u96c6\u5305\u542b<strong>10\u4e2a\u7c7b\u522b<\/strong>\u7684<strong>60,000\u5f2032x32\u5f69\u8272\u56fe\u50cf<\/strong>\uff0c\u5176\u4e2d50,000\u5f20\u7528\u4e8e\u8bad\u7ec3\uff0c10,000\u5f20\u7528\u4e8e\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u591a\u7c7b\u522b\u56fe\u50cf\u5206\u7c7b\u4efb\u52a1\uff0c\u76ee\u6807\u662f\u8bad\u7ec3\u4e00\u4e2a\u6a21\u578b\uff0c\u80fd\u591f\u51c6\u786e\u8bc6\u522b\u56fe\u50cf\u4e2d\u7684\u7269\u4f53\u5c5e\u4e8e\u98de\u673a\u3001\u6c7d\u8f66\u3001\u9e1f\u3001\u732b\u3001\u9e7f\u3001\u72d7\u3001\u9752\u86d9\u3001\u9a6c\u3001\u8239\u6216\u5361\u8f66\u4e2d\u7684\u54ea\u4e00\u7c7b\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udcca 2. \u5b8c\u6574\u5de5\u4f5c\u6d41\u7a0b<\/p>\n<\/blockquote>\n\n\n\n<p>\u4e0b\u56fe\u6982\u62ec\u4e86\u4f7f\u7528PyTorch\u89e3\u51b3\u56fe\u50cf\u5206\u7c7b\u4efb\u52a1\u7684<strong>\u6838\u5fc3\u6b65\u9aa4\u548c\u5173\u952e\u51b3\u7b56\u70b9<\/strong>\uff0c\u5e2e\u52a9\u60a8\u4ece\u5b8f\u89c2\u4e0a\u7406\u89e3\u6574\u4e2a\u6d41\u7a0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"533\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060559102-1024x533.png\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" alt=\"\" class=\"wp-image-6760 lazyload\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060559102-1024x533.png 1024w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060559102-300x156.png 300w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060559102-768x399.png 768w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060559102.png 1244w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><noscript><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"533\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060559102-1024x533.png\" alt=\"\" class=\"wp-image-6760\" srcset=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060559102-1024x533.png 1024w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060559102-300x156.png 300w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060559102-768x399.png 768w, https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060559102.png 1244w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/noscript><\/figure>\n\n\n\n<p>\u2699\ufe0f 3. \u6570\u636e\u51c6\u5907\u4e0e\u9884\u5904\u7406\u8be6\u89e3<\/p>\n\n\n\n<p>\u6570\u636e\u9884\u5904\u7406\u662f\u6a21\u578b\u6210\u529f\u7684\u57fa\u77f3\uff0c\u4ee5\u4e0b\u662f\u5173\u952e\u6b65\u9aa4\u548c\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>3.1 \u6570\u636e\u8f6c\u6362\u4e0e\u589e\u5f3a<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;torch<br>import&nbsp;torchvision<br>import&nbsp;torchvision.transforms&nbsp;as&nbsp;transforms<br><br><em># \u5b9a\u4e49\u8bad\u7ec3\u548c\u6d4b\u8bd5\u7684\u6570\u636e\u9884\u5904\u7406\u6d41\u7a0b<\/em><br>transform_train = transforms.Compose(&#91;<br>&nbsp; &nbsp; transforms.RandomHorizontalFlip(), &nbsp;<em># \u968f\u673a\u6c34\u5e73\u7ffb\u8f6c<\/em><br>&nbsp; &nbsp; transforms.RandomCrop(32, padding=4), &nbsp;<em># \u968f\u673a\u88c1\u526a<\/em><br>&nbsp; &nbsp; transforms.ToTensor(), &nbsp;<em># \u8f6c\u6362\u4e3aTensor\u5e76\u5f52\u4e00\u5316\u5230&#91;0,1]<\/em><br>&nbsp; &nbsp; transforms.Normalize((0.4914,&nbsp;0.4822,&nbsp;0.4465), (0.2023,&nbsp;0.1994,&nbsp;0.2010)) &nbsp;<em># \u6807\u51c6\u5316<\/em><br>])<br><br>transform_test = transforms.Compose(&#91;<br>&nbsp; &nbsp; transforms.ToTensor(),<br>&nbsp; &nbsp; transforms.Normalize((0.4914,&nbsp;0.4822,&nbsp;0.4465), (0.2023,&nbsp;0.1994,&nbsp;0.2010))<br>])<br><br><em># \u52a0\u8f7d\u6570\u636e\u96c6<\/em><br>trainset = torchvision.datasets.CIFAR10(<br>&nbsp; &nbsp; root='.\/data', train=True, download=True, transform=transform_train<br>)<br>testset = torchvision.datasets.CIFAR10(<br>&nbsp; &nbsp; root='.\/data', train=False, download=True, transform=transform_test<br>)<br><br><em># \u521b\u5efa\u6570\u636e\u52a0\u8f7d\u5668<\/em><br>trainloader = torch.utils.data.DataLoader(<br>&nbsp; &nbsp; trainset, batch_size=128, shuffle=True, num_workers=2<br>)<br>testloader = torch.utils.data.DataLoader(<br>&nbsp; &nbsp; testset, batch_size=100, shuffle=False, num_workers=2<br>)<br><br><em># \u7c7b\u522b\u540d\u79f0<\/em><br>classes = ('plane',&nbsp;'car',&nbsp;'bird',&nbsp;'cat',&nbsp;'deer',&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'dog',&nbsp;'frog',&nbsp;'horse',&nbsp;'ship',&nbsp;'truck')<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>3.2 \u6570\u636e\u9884\u5904\u7406\u7684\u91cd\u8981\u6027<\/p>\n<\/blockquote>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u589e\u5f3a<\/strong>\uff1a\u901a\u8fc7\u968f\u673a\u7ffb\u8f6c\u3001\u88c1\u526a\u7b49\u64cd\u4f5c\u589e\u52a0\u6570\u636e\u591a\u6837\u6027\uff0c<strong>\u63d0\u9ad8\u6a21\u578b\u6cdb\u5316\u80fd\u529b<\/strong>\uff0c\u9632\u6b62\u8fc7\u62df\u5408\u3002<\/li>\n\n\n\n<li><strong>\u6807\u51c6\u5316<\/strong>\uff1a\u4f7f\u8f93\u5165\u6570\u636e\u5747\u503c\u4e3a0\uff0c\u6807\u51c6\u5dee\u4e3a1\uff0c<strong>\u52a0\u901f\u6a21\u578b\u6536\u655b<\/strong>\uff0c\u63d0\u9ad8\u8bad\u7ec3\u7a33\u5b9a\u6027\u3002<\/li>\n\n\n\n<li><strong>\u6279\u5904\u7406<\/strong>\uff1a\u4f7f\u7528<code>DataLoader<\/code>\u8fdb\u884c\u6279\u5904\u7406\uff0c<strong>\u63d0\u9ad8\u5185\u5b58\u5229\u7528\u7387\u548c\u8bad\u7ec3\u6548\u7387<\/strong>\uff0c\u5145\u5206\u5229\u7528GPU\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83e\udde9 4. \u6a21\u578b\u6784\u5efa\u7b56\u7565\u4e0e\u9009\u62e9<\/p>\n<\/blockquote>\n\n\n\n<p>\u5728\u6a21\u578b\u6784\u5efa\u9636\u6bb5\uff0c\u60a8\u6709\u4e24\u79cd\u4e3b\u8981\u7b56\u7565\u53ef\u9009\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>4.1 \u81ea\u5b9a\u4e49CNN\u6a21\u578b\uff08\u9002\u5408\u5b66\u4e60\u7406\u89e3\uff09<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;torch.nn&nbsp;as&nbsp;nn<br>import&nbsp;torch.nn.functional&nbsp;as&nbsp;F<br><br>class&nbsp;CNN(nn.Module):<br>&nbsp; &nbsp;&nbsp;def&nbsp;__init__(self):<br>&nbsp; &nbsp; &nbsp; &nbsp; super(CNN, self).__init__()<br>&nbsp; &nbsp; &nbsp; &nbsp; self.conv1 = nn.Conv2d(3,&nbsp;32,&nbsp;3, padding=1)<br>&nbsp; &nbsp; &nbsp; &nbsp; self.bn1 = nn.BatchNorm2d(32)<br>&nbsp; &nbsp; &nbsp; &nbsp; self.conv2 = nn.Conv2d(32,&nbsp;64,&nbsp;3, padding=1)<br>&nbsp; &nbsp; &nbsp; &nbsp; self.bn2 = nn.BatchNorm2d(64)<br>&nbsp; &nbsp; &nbsp; &nbsp; self.pool = nn.MaxPool2d(2,&nbsp;2)<br>&nbsp; &nbsp; &nbsp; &nbsp; self.dropout = nn.Dropout(0.5)<br>&nbsp; &nbsp; &nbsp; &nbsp; self.fc1 = nn.Linear(64&nbsp;*&nbsp;8&nbsp;*&nbsp;8,&nbsp;512)<br>&nbsp; &nbsp; &nbsp; &nbsp; self.fc2 = nn.Linear(512,&nbsp;10)<br><br>&nbsp; &nbsp;&nbsp;def&nbsp;forward(self, x):<br>&nbsp; &nbsp; &nbsp; &nbsp; x = self.pool(F.relu(self.bn1(self.conv1(x))))<br>&nbsp; &nbsp; &nbsp; &nbsp; x = self.pool(F.relu(self.bn2(self.conv2(x))))<br>&nbsp; &nbsp; &nbsp; &nbsp; x = x.view(-1,&nbsp;64&nbsp;*&nbsp;8&nbsp;*&nbsp;8)<br>&nbsp; &nbsp; &nbsp; &nbsp; x = self.dropout(x)<br>&nbsp; &nbsp; &nbsp; &nbsp; x = F.relu(self.fc1(x))<br>&nbsp; &nbsp; &nbsp; &nbsp; x = self.dropout(x)<br>&nbsp; &nbsp; &nbsp; &nbsp; x = self.fc2(x)<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;x<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>4.2 \u4f7f\u7528\u9884\u8bad\u7ec3\u6a21\u578b\uff08\u9002\u5408\u5b9e\u6218\u5e94\u7528\uff09<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;torchvision.models&nbsp;as&nbsp;models<br><br><em># \u52a0\u8f7d\u9884\u8bad\u7ec3\u7684ResNet18\u5e76\u8c03\u6574\u6700\u540e\u4e00\u5c42<\/em><br>model = models.resnet18(pretrained=True)<br>num_ftrs = model.fc.in_features<br>model.fc = nn.Linear(num_ftrs,&nbsp;10) &nbsp;<em># CIFAR-10\u670910\u4e2a\u7c7b\u522b<\/em><\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>4.3 \u6a21\u578b\u9009\u62e9\u5efa\u8bae<\/p>\n<\/blockquote>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6a21\u578b\u7c7b\u578b<\/th><th>\u9002\u7528\u573a\u666f<\/th><th>\u8bad\u7ec3\u65f6\u95f4<\/th><th>\u9884\u671f\u51c6\u786e\u7387<\/th><th>\u4f18\u70b9<\/th><th>\u7f3a\u70b9<\/th><\/tr><\/thead><tbody><tr><td><strong>\u81ea\u5b9a\u4e49CNN<\/strong><\/td><td>\u5b66\u4e60\u3001\u5b9e\u9a8c<\/td><td>\u4e2d\u7b49<\/td><td>75-85%<\/td><td>\u7075\u6d3b\u53ef\u63a7\uff0c\u6613\u4e8e\u7406\u89e3<\/td><td>\u9700\u8981\u81ea\u884c\u8bbe\u8ba1\u67b6\u6784<\/td><\/tr><tr><td><strong>\u9884\u8bad\u7ec3\u6a21\u578b<\/strong><\/td><td>\u751f\u4ea7\u5e94\u7528<\/td><td>\u8f83\u957f<\/td><td>85-95%+<\/td><td>\u6027\u80fd\u4f18\u5f02\uff0c\u8282\u7701\u65f6\u95f4<\/td><td>\u9ed1\u76d2\u6027\uff0c\u8ba1\u7b97\u9700\u6c42\u9ad8<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83c\udfaf 5. \u8bad\u7ec3\u914d\u7f6e\u4e0e\u4f18\u5316\u7b56\u7565<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>5.1 \u635f\u5931\u51fd\u6570\u4e0e\u4f18\u5316\u5668\u9009\u62e9<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;torch.optim&nbsp;as&nbsp;optim<br><br><em># \u5b9a\u4e49\u635f\u5931\u51fd\u6570\u548c\u4f18\u5316\u5668<\/em><br>criterion = nn.CrossEntropyLoss()<br>optimizer = optim.Adam(model.parameters(), lr=0.001, weight_decay=1e-4)<br><br><em># \u5b66\u4e60\u7387\u8c03\u5ea6\u5668<\/em><br>scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.1)<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>5.2 \u8bad\u7ec3\u5faa\u73af\u5b9e\u73b0<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>def&nbsp;train_model(model, trainloader, testloader, criterion, optimizer, scheduler, num_epochs=50):<br>&nbsp; &nbsp; device = torch.device(\"cuda\"if&nbsp;torch.cuda.is_available()&nbsp;else\"cpu\")<br>&nbsp; &nbsp; model.to(device)<br>&nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; history = {'train_loss': &#91;],&nbsp;'train_acc': &#91;],&nbsp;'val_loss': &#91;],&nbsp;'val_acc': &#91;]}<br>&nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp;&nbsp;for&nbsp;epoch&nbsp;in&nbsp;range(num_epochs):<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em># \u8bad\u7ec3\u9636\u6bb5<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; model.train()<br>&nbsp; &nbsp; &nbsp; &nbsp; running_loss, correct, total =&nbsp;0.0,&nbsp;0,&nbsp;0<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;for&nbsp;inputs, labels&nbsp;in&nbsp;trainloader:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inputs, labels = inputs.to(device), labels.to(device)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; optimizer.zero_grad()<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputs = model(inputs)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loss = criterion(outputs, labels)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loss.backward()<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; optimizer.step()<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; running_loss += loss.item()<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _, predicted = torch.max(outputs.data,&nbsp;1)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total += labels.size(0)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; correct += (predicted == labels).sum().item()<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em># \u9a8c\u8bc1\u9636\u6bb5<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; model.eval()<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em># ... \u9a8c\u8bc1\u4ee3\u7801\u7c7b\u4f3c ...<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<em># \u5b66\u4e60\u7387\u8c03\u6574<\/em><br>&nbsp; &nbsp; &nbsp; &nbsp; scheduler.step()<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp;&nbsp;return&nbsp;model, history<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udcc8 6. \u6a21\u578b\u8bc4\u4f30\u4e0e\u6027\u80fd\u5206\u6790<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>6.1 \u7efc\u5408\u8bc4\u4f30\u6307\u6807 \u8bad\u7ec3\u5b8c\u6210\u540e\uff0c\u9700\u8981\u4ece\u591a\u4e2a\u89d2\u5ea6\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\uff1a<\/p>\n<\/blockquote>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6d4b\u8bd5\u51c6\u786e\u7387<\/strong>\uff1a\u6a21\u578b\u5728\u672a\u89c1\u8fc7\u7684\u6d4b\u8bd5\u96c6\u4e0a\u7684\u6574\u4f53\u5206\u7c7b\u51c6\u786e\u7387<\/li>\n\n\n\n<li><strong>\u6df7\u6dc6\u77e9\u9635<\/strong>\uff1a\u8be6\u7ec6\u5206\u6790\u6bcf\u4e2a\u7c7b\u522b\u7684\u5206\u7c7b\u60c5\u51b5\uff0c\u8bc6\u522b\u6a21\u578b\u5bb9\u6613\u6df7\u6dc6\u7684\u7c7b\u522b<\/li>\n\n\n\n<li><strong>\u635f\u5931\u66f2\u7ebf<\/strong>\uff1a\u76d1\u63a7\u8bad\u7ec3\u548c\u9a8c\u8bc1\u635f\u5931\uff0c\u8bc6\u522b\u8fc7\u62df\u5408\u6216\u6b20\u62df\u5408\u95ee\u9898<\/li>\n\n\n\n<li><strong>\u7c7b\u522b\u6307\u6807<\/strong>\uff1a\u8ba1\u7b97\u7cbe\u786e\u7387\u3001\u53ec\u56de\u7387\u548cF1\u5206\u6570\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u4e0d\u5e73\u8861\u6570\u636e\u96c6<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>6.2 \u6027\u80fd\u4f18\u5316\u7b56\u7565 \u5982\u679c\u6a21\u578b\u6027\u80fd\u4e0d\u7406\u60f3\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u4f18\u5316\u65b9\u5411\uff1a<\/p>\n<\/blockquote>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u95ee\u9898\u73b0\u8c61<\/th><th>\u53ef\u80fd\u539f\u56e0<\/th><th>\u89e3\u51b3\u65b9\u6848<\/th><\/tr><\/thead><tbody><tr><td><strong>\u8bad\u7ec3\u51c6\u786e\u7387\u9ad8\uff0c\u6d4b\u8bd5\u51c6\u786e\u7387\u4f4e<\/strong><\/td><td>\u8fc7\u62df\u5408<\/td><td>\u589e\u52a0Dropout\u6bd4\u4f8b\u3001\u6dfb\u52a0\u6570\u636e\u589e\u5f3a\u3001\u4f7f\u7528\u6743\u91cd\u8870\u51cf\u3001\u65e9\u505c\u6cd5<\/td><\/tr><tr><td><strong>\u8bad\u7ec3\u548c\u6d4b\u8bd5\u51c6\u786e\u7387\u90fd\u4f4e<\/strong><\/td><td>\u6b20\u62df\u5408<\/td><td>\u589e\u52a0\u6a21\u578b\u590d\u6742\u5ea6\u3001\u5ef6\u957f\u8bad\u7ec3\u65f6\u95f4\u3001\u51cf\u5c11\u6b63\u5219\u5316<\/td><\/tr><tr><td><strong>\u8bad\u7ec3\u8fc7\u7a0b\u4e0d\u7a33\u5b9a<\/strong><\/td><td>\u5b66\u4e60\u7387\u4e0d\u5f53<\/td><td>\u8c03\u6574\u5b66\u4e60\u7387\u3001\u4f7f\u7528\u5b66\u4e60\u7387\u8c03\u5ea6\u5668<\/td><\/tr><tr><td><strong>\u67d0\u4e9b\u7c7b\u522b\u51c6\u786e\u7387\u4f4e<\/strong><\/td><td>\u7c7b\u522b\u4e0d\u5e73\u8861<\/td><td>\u4f7f\u7528\u7c7b\u522b\u6743\u91cd\u3001\u6570\u636e\u91cd\u91c7\u6837<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udcbe 7. \u6a21\u578b\u4fdd\u5b58\u4e0e\u90e8\u7f72<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>7.1 \u4fdd\u5b58\u8bad\u7ec3\u597d\u7684\u6a21\u578b<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code><em># \u4fdd\u5b58\u6574\u4e2a\u6a21\u578b<\/em><br>torch.save(model,&nbsp;'cifar10_model.pth')<br><br><em># \u53ea\u4fdd\u5b58\u6a21\u578b\u53c2\u6570\uff08\u63a8\u8350\u65b9\u5f0f\uff09<\/em><br>torch.save(model.state_dict(),&nbsp;'cifar10_model_state_dict.pth')<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>7.2 \u52a0\u8f7d\u6a21\u578b\u8fdb\u884c\u9884\u6d4b<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code><em># \u52a0\u8f7d\u6a21\u578b\u8fdb\u884c\u9884\u6d4b<\/em><br>model = CNN() &nbsp;<em># \u9700\u8981\u5148\u521b\u5efa\u6a21\u578b\u5b9e\u4f8b<\/em><br>model.load_state_dict(torch.load('cifar10_model_state_dict.pth'))<br>model.eval() &nbsp;<em># \u8bbe\u7f6e\u4e3a\u8bc4\u4f30\u6a21\u5f0f<\/em><br><br><em># \u8fdb\u884c\u9884\u6d4b<\/em><br>with&nbsp;torch.no_grad():<br>&nbsp; &nbsp; outputs = model(inputs)<br>&nbsp; &nbsp; _, predicted = torch.max(outputs,&nbsp;1)<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udca1 8. \u8fdb\u9636\u6280\u5de7\u4e0e\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/blockquote>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528TensorBoard\u53ef\u89c6\u5316<\/strong>\uff1a\u76d1\u63a7\u8bad\u7ec3\u8fc7\u7a0b\uff0c\u53ef\u89c6\u5316\u635f\u5931\u548c\u51c6\u786e\u7387\u66f2\u7ebf\uff0c\u5206\u6790\u6a21\u578b\u6027\u80fd<\/li>\n\n\n\n<li><strong>\u6df7\u5408\u7cbe\u5ea6\u8bad\u7ec3<\/strong>\uff1a\u4f7f\u7528<code>torch.cuda.amp<\/code>\u8fdb\u884c\u6df7\u5408\u7cbe\u5ea6\u8bad\u7ec3\uff0c<strong>\u51cf\u5c11\u663e\u5b58\u5360\u7528\uff0c\u52a0\u5feb\u8bad\u7ec3\u901f\u5ea6<\/strong><\/li>\n\n\n\n<li><strong>\u6a21\u578b\u91cf\u5316<\/strong>\uff1a\u8bad\u7ec3\u5b8c\u6210\u540e\u5bf9\u6a21\u578b\u8fdb\u884c\u91cf\u5316\uff0c<strong>\u51cf\u5c11\u6a21\u578b\u5927\u5c0f\uff0c\u52a0\u5feb\u63a8\u7406\u901f\u5ea6<\/strong>\uff0c\u4fbf\u4e8e\u90e8\u7f72<\/li>\n\n\n\n<li><strong>\u8d85\u53c2\u6570\u4f18\u5316<\/strong>\uff1a\u4f7f\u7528\u7f51\u683c\u641c\u7d22\u3001\u968f\u673a\u641c\u7d22\u6216\u8d1d\u53f6\u65af\u4f18\u5316\u7b49\u65b9\u6cd5\u5bfb\u627e\u6700\u4f73\u8d85\u53c2\u6570\u7ec4\u5408<\/li>\n<\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">6. \u603b\u7ed3<\/h1>\n\n\n\n<p>\u8fd9\u7bc7\u6587\u7ae0\u901a\u8fc7\u8be6\u7ec6\u4e14\u5b9e\u8df5\u6027\u7684\u65b9\u5f0f\u4ecb\u7ecd\u4e86 PyTorch \u7684\u4f7f\u7528\uff0c\u5305\u62ec\u73af\u5883\u5b89\u88c5\u3001\u57fa\u7840\u77e5\u8bc6\u3001\u5f20\u91cf\u64cd\u4f5c\u3001\u81ea\u52a8\u6c42\u5bfc\u673a\u5236\u3001\u795e\u7ecf\u7f51\u7edc\u521b\u5efa\u3001\u6570\u636e\u5904\u7406\u3001\u6a21\u578b\u8bad\u7ec3\u3001\u6d4b\u8bd5\u4ee5\u53ca\u6a21\u578b\u7684\u4fdd\u5b58\u548c\u52a0\u8f7d\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5229\u7528 PyTorch \u4ece\u5934\u5230\u5c3e\u5b8c\u6210\u4e86\u4e00\u4e2a\u5b8c\u6574\u7684\u795e\u7ecf\u7f51\u7edc\u8bad\u7ec3\u6d41\u7a0b\uff0c\u5e76\u5728 CIFAR10 \u6570\u636e\u96c6\u4e0a\u6d4b\u8bd5\u4e86\u7f51\u7edc\u7684\u6027\u80fd\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u6df1\u5165\u4e86\u89e3\u4e86 PyTorch \u63d0\u4f9b\u7684\u5404\u79cd\u529f\u80fd\u548c\u5de5\u5177\u3002<\/p>\n\n\n\n<p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5bf9\u4f60\u5b66\u4e60 PyTorch \u63d0\u4f9b\u5e2e\u52a9\uff0c\u5bf9\u4e8e\u60f3\u8981\u66f4\u6df1\u5165\u4e86\u89e3 PyTorch \u7684\u8bfb\u8005\uff0c\u6211\u5efa\u8bae\u53c2\u8003 PyTorch \u7684\u5b98\u65b9\u6587\u6863\u4ee5\u53ca\u5404\u79cd\u5f00\u6e90\u6559\u7a0b\u3002\u5b9e\u8df5\u662f\u6700\u597d\u7684\u5b66\u4e60\u65b9\u6cd5\uff0c\u53ea\u6709\u901a\u8fc7\u5927\u91cf\u7684\u7ec3\u4e60\u548c\u5b9e\u8df5\uff0c\u624d\u80fd\u771f\u6b63\u638c\u63e1 PyTorch \u548c\u6df1\u5ea6\u5b66\u4e60\u3002<\/p>\n\n\n\n<p>\u8c22\u8c22\u4f60\u7684\u9605\u8bfb\uff0c\u5e0c\u671b\u4f60\u5728\u6df1\u5ea6\u5b66\u4e60\u7684\u9053\u8def\u4e0a\u8d8a\u8d70\u8d8a\u8fdc\uff01<\/p>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>\u5728\u8fdb\u884c\u4e0b\u4e00\u6b65\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981Python\u548cPytorch\u7684\u57fa\u7840\u3002\u4e8e\u662f\u5728\u7f51\u4e0a\u627e\u4e86\u4e00\u7bc7\u5f88\u5b8c\u6574\u7684\u535a\u6587\uff0c\u5185\u5bb9\u7cfb\u7edf\u4e14\u4e30\u5bcc\u3002 \u6211\u57fa\u4e8e\u6b64\u505a\u4e86\u4e00\u6b21\u6ce8 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[29,51,46],"tags":[],"class_list":["post-6750","post","type-post","status-publish","format-standard","hentry","category-python","category-pytorch","category-yolo"],"post_thumbnail_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060505968.png","content_first_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060505968.png","post_medium_image_300":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060505968.png","post_thumbnail_image_624":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060505968.png","post_frist_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060505968.png","post_medium_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060505968.png","post_large_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060505968.png","post_full_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060505968.png","post_all_images":[{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060505968.png","id":"image0"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060505968.png","id":"image1"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060510154.png","id":"image2"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060510154.png","id":"image3"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060516262-1024x641.png","id":"image4"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060516262-1024x641.png","id":"image5"},{"imagesurl":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/0l8e8dYXFXbgDOAwqZsKGUVVVs92Y9w7TV4w8SobGAu5mSN56hib549ROBvOLggj69PCvYia5znPq0pzrRCQ5PBA\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=3","id":"image6"},{"imagesurl":"https:\/\/mmbiz.qpic.cn\/mmbiz_png\/0l8e8dYXFXbgDOAwqZsKGUVVVs92Y9w7TV4w8SobGAu5mSN56hib549ROBvOLggj69PCvYia5znPq0pzrRCQ5PBA\/640?wx_fmt=png&amp;from=appmsg&amp;watermark=1&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1#imgIndex=3","id":"image7"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060524758.png","id":"image8"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060524758.png","id":"image9"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060529748.png","id":"image10"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060529748.png","id":"image11"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060536144-1024x391.png","id":"image12"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060536144-1024x391.png","id":"image13"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060542939.png","id":"image14"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060542939.png","id":"image15"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060547752.png","id":"image16"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060547752.png","id":"image17"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060553245-1024x408.png","id":"image18"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060553245-1024x408.png","id":"image19"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060559102-1024x533.png","id":"image20"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2025\/09\/20250929060559102-1024x533.png","id":"image21"}],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"Python","post_date":"2025-09-29","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":411,"next_post_id":6762,"next_post_title":"YOLO\u5165\u95e8\u6559\u7a0b\uff08\u4e8c\uff09\uff1a\u6d45\u6790YOLOv1","previous_post_id":6736,"previous_post_title":"\u57fa\u4e8e Java \u4e0e YOLOv8 \u5b9e\u73b0\u8868\u683c\u56fe\u50cf\u884c\u5217\u68c0\u6d4b\u7684\u5b8c\u6574\u5b9e\u8df5","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/6750","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=6750"}],"version-history":[{"count":1,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/6750\/revisions"}],"predecessor-version":[{"id":6761,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/6750\/revisions\/6761"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=6750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=6750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=6750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}