{"id":62140,"date":"2026-01-19T11:50:48","date_gmt":"2026-01-19T03:50:48","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/62140.html"},"modified":"2026-01-19T11:50:48","modified_gmt":"2026-01-19T03:50:48","slug":"%e3%80%90pytorch%e5%85%a5%e9%97%a8%e3%80%91%e6%89%8b%e6%8a%8a%e6%89%8b%e5%b8%a6%e4%bd%a0%e6%90%ad%e5%bb%ba%e7%ac%ac%e4%b8%80%e4%b8%aa%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c%ef%bc%88%e5%8f%82%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/62140.html","title":{"rendered":"\u3010PyTorch\u5165\u95e8\u3011\u624b\u628a\u624b\u5e26\u4f60\u642d\u5efa\u7b2c\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\uff08\u53c2\u6570\u8ba1\u7b97\u8be6\u89e3+\u4ee3\u7801\u5b9e\u6218\uff09"},"content":{"rendered":"<h3>\u4e00\u3001\u5982\u4f55\u4ece\u96f6\u201c\u642d\u5efa\u201d\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc&#xff1f;<\/h3>\n<p>\u5927\u5bb6\u597d&#xff0c;\u5f88\u591a\u521d\u5b66\u8005\u5728\u5165\u95e8\u6df1\u5ea6\u5b66\u4e60\u65f6&#xff0c;\u5e38\u5e38\u4f1a\u5bf9\u201c\u5982\u4f55\u4ece\u96f6\u5f00\u59cb\u642d\u5efa\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\u201d\u611f\u5230\u56f0\u60d1\u3002\u522b\u62c5\u5fc3&#xff0c;\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u5c06\u624b\u628a\u624b\u5e26\u4f60\u7528 PyTorch \u5b8c\u6210\u8fd9\u4e2a\u8fc7\u7a0b&#xff0c;\u4e0d\u4ec5\u6559\u4f60\u201c\u600e\u4e48\u505a\u201d&#xff0c;\u66f4\u8ba9\u4f60\u660e\u767d\u201c\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u505a\u201d\u3002<\/p>\n<p>\u5728 PyTorch \u4e2d&#xff0c;\u642d\u5efa\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\u5c31\u50cf\u642d\u79ef\u6728\u4e00\u6837\u7b80\u5355&#xff0c;\u6211\u4eec\u53ea\u9700\u8981\u5c06\u5404\u79cd\u201c\u5c42\u201d&#xff08;Layers&#xff09;\u5806\u53e0\u8d77\u6765\u3002\u6574\u4e2a\u8fc7\u7a0b\u4e3b\u8981\u56f4\u7ed5\u7740\u4e00\u4e2a\u6838\u5fc3\u7c7b nn.Module \u6765\u5c55\u5f00&#xff0c;\u6211\u4eec\u9700\u8981\u505a\u4e24\u4ef6\u4e8b&#xff1a;<\/p>\n<li>&#x1f9f1; \u5728 __init__ \u65b9\u6cd5\u4e2d\u5b9a\u4e49\u79ef\u6728&#xff1a;\u5728\u8fd9\u91cc&#xff0c;\u6211\u4eec\u4f1a\u5b9a\u4e49\u597d\u7f51\u7edc\u9700\u8981\u7528\u5230\u7684\u6240\u6709\u5c42\u7ed3\u6784&#xff0c;\u6bd4\u5982\u5168\u8fde\u63a5\u5c42&#xff08;nn.Linear&#xff09;&#xff0c;\u5e76\u5bf9\u5b83\u4eec\u7684\u53c2\u6570\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/li>\n<li>&#x1f517; \u5728 forward \u65b9\u6cd5\u4e2d\u62fc\u63a5\u79ef\u6728&#xff1a;\u8fd9\u91cc\u5b9a\u4e49\u4e86\u6570\u636e\u662f\u5982\u4f55\u5728\u8fd9\u4e9b\u5c42\u4e4b\u95f4\u6d41\u52a8\u7684&#xff0c;\u4e5f\u5c31\u662f\u524d\u5411\u4f20\u64ad\u7684\u8fc7\u7a0b\u3002\u5f53\u4f60\u8c03\u7528\u6a21\u578b\u5b9e\u4f8b\u65f6&#xff08;\u4f8b\u5982 model(data)&#xff09;&#xff0c;PyTorch \u4f1a\u81ea\u52a8\u6267\u884c\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/li>\n<p>\u63a5\u4e0b\u6765&#xff0c;\u6211\u4eec\u5c31\u4ee5\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50&#xff0c;\u6765\u6784\u5efa\u4e0b\u9762\u8fd9\u4e2a\u7b80\u5355\u7684\u4e09\u5c42\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u3002<\/p>\n<p align=\"center\">\n    <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119035047-696daa17528f9.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/>\n<\/p>\n<p>\u6211\u4eec\u7684\u8bbe\u8ba1\u8981\u6c42\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>\u8f93\u5165\u5c42&#xff1a;\u63a5\u65363\u4e2a\u7279\u5f81\u3002<\/li>\n<li>\u7b2c1\u4e2a\u9690\u85cf\u5c42&#xff1a;3\u4e2a\u795e\u7ecf\u5143&#xff0c;\u6743\u91cd\u91c7\u7528 Xavier \u521d\u59cb\u5316&#xff0c;\u6fc0\u6d3b\u51fd\u6570\u4f7f\u7528 Sigmoid\u3002<\/li>\n<li>\u7b2c2\u4e2a\u9690\u85cf\u5c42&#xff1a;2\u4e2a\u795e\u7ecf\u5143&#xff0c;\u6743\u91cd\u91c7\u7528 Kaiming (He) \u521d\u59cb\u5316&#xff0c;\u6fc0\u6d3b\u51fd\u6570\u91c7\u7528 ReLU\u3002<\/li>\n<li>\u8f93\u51fa\u5c42&#xff1a;2\u4e2a\u795e\u7ecf\u5143&#xff0c;\u56e0\u4e3a\u662f\u591a\u5206\u7c7b\u4efb\u52a1&#xff0c;\u6700\u540e\u4f7f\u7528 Softmax \u8fdb\u884c\u5f52\u4e00\u5316\u3002<\/li>\n<\/ul>\n<h3>\u4e8c\u3001\u642d\u5efa\u795e\u7ecf\u7f51\u7edc\u6a21\u578b&#xff08;PyTorch\u4ee3\u7801\u5b9e\u6218&#xff09;<\/h3>\n<p>\u6211\u4eec\u5148\u6765\u7f16\u5199\u6a21\u578b\u7684\u6838\u5fc3\u4ee3\u7801\u3002\u8fd9\u91cc\u6211\u4eec\u521b\u5efa\u4e00\u4e2a Model \u7c7b&#xff0c;\u5b83\u7ee7\u627f\u81ea nn.Module\u3002<\/p>\n<h4>1. \u6784\u9020\u795e\u7ecf\u7f51\u7edc\u6a21\u578b<\/h4>\n<p><span class=\"token keyword\">import<\/span> torch<br \/>\n<span class=\"token keyword\">import<\/span> torch<span class=\"token punctuation\">.<\/span>nn <span class=\"token keyword\">as<\/span> nn<br \/>\n<span class=\"token comment\"># torchsummary \u7528\u4e8e\u8ba1\u7b97\u6a21\u578b\u53c2\u6570\u91cf\u548c\u67e5\u770b\u6a21\u578b\u7ed3\u6784, \u9700\u8981\u5148\u5b89\u88c5<\/span><br \/>\n<span class=\"token comment\"># pip install torchsummary -i https:\/\/mirrors.aliyun.com\/pypi\/simple\/<\/span><br \/>\n<span class=\"token keyword\">from<\/span> torchsummary <span class=\"token keyword\">import<\/span> summary<\/p>\n<p><span class=\"token comment\"># 1. \u521b\u5efa\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u7c7b<\/span><br \/>\n<span class=\"token keyword\">class<\/span> <span class=\"token class-name\">Model<\/span><span class=\"token punctuation\">(<\/span>nn<span class=\"token punctuation\">.<\/span>Module<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token comment\"># \u521d\u59cb\u5316\u7f51\u7edc\u7ed3\u6784<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">__init__<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token comment\"># \u8c03\u7528\u7236\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5&#xff0c;\u8fd9\u662f\u5fc5\u987b\u7684\u6b65\u9aa4&#xff0c;\u786e\u4fddnn.Module\u7684\u529f\u80fd\u88ab\u6b63\u786e\u7ee7\u627f<\/span><br \/>\n        <span class=\"token builtin\">super<\/span><span class=\"token punctuation\">(<\/span>Model<span class=\"token punctuation\">,<\/span> self<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>__init__<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>        <span class=\"token comment\"># &#8212; \u5b9a\u4e49\u7f51\u7edc\u4e2d\u7684\u201c\u79ef\u6728\u201d&#xff08;\u5404\u4e2a\u5c42&#xff09; &#8212;<\/span><br \/>\n        <span class=\"token comment\"># \u7b2c\u4e00\u4e2a\u9690\u85cf\u5c42: 3\u4e2a\u8f93\u5165\u7279\u5f81 -&gt; 3\u4e2a\u8f93\u51fa\u7279\u5f81<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>linear1 <span class=\"token operator\">&#061;<\/span> nn<span class=\"token punctuation\">.<\/span>Linear<span class=\"token punctuation\">(<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">3<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token comment\"># \u7b2c\u4e8c\u4e2a\u9690\u85cf\u5c42: 3\u4e2a\u8f93\u5165\u7279\u5f81 -&gt; 2\u4e2a\u8f93\u51fa\u7279\u5f81<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>linear2 <span class=\"token operator\">&#061;<\/span> nn<span class=\"token punctuation\">.<\/span>Linear<span class=\"token punctuation\">(<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token comment\"># \u8f93\u51fa\u5c42: 2\u4e2a\u8f93\u5165\u7279\u5f81 -&gt; 2\u4e2a\u8f93\u51fa\u7279\u5f81 (\u5bf9\u5e942\u4e2a\u7c7b\u522b)<\/span><br \/>\n        self<span class=\"token punctuation\">.<\/span>out <span class=\"token operator\">&#061;<\/span> nn<span class=\"token punctuation\">.<\/span>Linear<span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>        <span class=\"token comment\"># &#8212; \u5bf9\u201c\u79ef\u6728\u201d\u7684\u53c2\u6570\u8fdb\u884c\u521d\u59cb\u5316 &#8212;<\/span><br \/>\n        <span class=\"token comment\"># \u5bf9\u7b2c\u4e00\u4e2a\u9690\u85cf\u5c42\u7684\u6743\u91cd\u4f7f\u7528 Xavier \u521d\u59cb\u5316&#xff0c;\u504f\u7f6e\u521d\u59cb\u5316\u4e3a0<\/span><br \/>\n        nn<span class=\"token punctuation\">.<\/span>init<span class=\"token punctuation\">.<\/span>xavier_normal_<span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">.<\/span>linear1<span class=\"token punctuation\">.<\/span>weight<span class=\"token punctuation\">)<\/span><br \/>\n        nn<span class=\"token punctuation\">.<\/span>init<span class=\"token punctuation\">.<\/span>zeros_<span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">.<\/span>linear1<span class=\"token punctuation\">.<\/span>bias<span class=\"token punctuation\">)<\/span><\/p>\n<p>        <span class=\"token comment\"># \u5bf9\u7b2c\u4e8c\u4e2a\u9690\u85cf\u5c42\u7684\u6743\u91cd\u4f7f\u7528 Kaiming \u521d\u59cb\u5316&#xff0c;\u504f\u7f6e\u521d\u59cb\u5316\u4e3a0<\/span><br \/>\n        <span class=\"token comment\"># nonlinearity&#061;&#039;relu&#039; \u53c2\u6570\u6307\u660e\u8fd9\u79cd\u521d\u59cb\u5316\u662f\u4e3aReLU\u6fc0\u6d3b\u51fd\u6570\u8bbe\u8ba1\u7684<\/span><br \/>\n        nn<span class=\"token punctuation\">.<\/span>init<span class=\"token punctuation\">.<\/span>kaiming_normal_<span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">.<\/span>linear2<span class=\"token punctuation\">.<\/span>weight<span class=\"token punctuation\">,<\/span> nonlinearity<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;relu&#039;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        nn<span class=\"token punctuation\">.<\/span>init<span class=\"token punctuation\">.<\/span>zeros_<span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">.<\/span>linear2<span class=\"token punctuation\">.<\/span>bias<span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># 2. \u5b9a\u4e49\u524d\u5411\u4f20\u64ad\u8def\u5f84&#xff0c;\u5373\u201c\u62fc\u63a5\u79ef\u6728\u201d<\/span><br \/>\n    <span class=\"token comment\"># \u5f53\u6211\u4eec\u8c03\u7528 model(data) \u65f6&#xff0c;PyTorch\u4f1a\u81ea\u52a8\u6267\u884c\u6b64\u65b9\u6cd5<\/span><br \/>\n    <span class=\"token keyword\">def<\/span> <span class=\"token function\">forward<\/span><span class=\"token punctuation\">(<\/span>self<span class=\"token punctuation\">,<\/span> x<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token comment\"># \u6570\u636e\u6d41\u7ecf\u7b2c\u4e00\u4e2a\u9690\u85cf\u5c42&#xff0c;\u5e76\u5e94\u7528 Sigmoid \u6fc0\u6d3b\u51fd\u6570<\/span><br \/>\n        x <span class=\"token operator\">&#061;<\/span> self<span class=\"token punctuation\">.<\/span>linear1<span class=\"token punctuation\">(<\/span>x<span class=\"token punctuation\">)<\/span><br \/>\n        x <span class=\"token operator\">&#061;<\/span> torch<span class=\"token punctuation\">.<\/span>sigmoid<span class=\"token punctuation\">(<\/span>x<span class=\"token punctuation\">)<\/span><\/p>\n<p>        <span class=\"token comment\"># \u6570\u636e\u6d41\u7ecf\u7b2c\u4e8c\u4e2a\u9690\u85cf\u5c42&#xff0c;\u5e76\u5e94\u7528 ReLU \u6fc0\u6d3b\u51fd\u6570<\/span><br \/>\n        x <span class=\"token operator\">&#061;<\/span> self<span class=\"token punctuation\">.<\/span>linear2<span class=\"token punctuation\">(<\/span>x<span class=\"token punctuation\">)<\/span><br \/>\n        x <span class=\"token operator\">&#061;<\/span> torch<span class=\"token punctuation\">.<\/span>relu<span class=\"token punctuation\">(<\/span>x<span class=\"token punctuation\">)<\/span><\/p>\n<p>        <span class=\"token comment\"># \u6570\u636e\u6d41\u7ecf\u8f93\u51fa\u5c42<\/span><br \/>\n        x <span class=\"token operator\">&#061;<\/span> self<span class=\"token punctuation\">.<\/span>out<span class=\"token punctuation\">(<\/span>x<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token comment\"># \u5e94\u7528 Softmax \u6fc0\u6d3b\u51fd\u6570&#xff0c;\u5f97\u5230\u6bcf\u4e2a\u7c7b\u522b\u7684\u6982\u7387<\/span><br \/>\n        <span class=\"token comment\"># dim&#061;-1 \u8868\u793a\u5728\u6700\u540e\u4e00\u4e2a\u7ef4\u5ea6&#xff08;\u8fd9\u91cc\u662f\u7279\u5f81\u7ef4\u5ea6&#xff09;\u4e0a\u8fdb\u884c Softmax<\/span><br \/>\n        x <span class=\"token operator\">&#061;<\/span> torch<span class=\"token punctuation\">.<\/span>softmax<span class=\"token punctuation\">(<\/span>x<span class=\"token punctuation\">,<\/span> dim<span class=\"token operator\">&#061;<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>        <span class=\"token keyword\">return<\/span> x<\/p>\n<h4>2. \u8fd0\u884c\u6a21\u578b\u5e76\u89c2\u5bdf\u7ed3\u679c<\/h4>\n<p>\u63a5\u4e0b\u6765&#xff0c;\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u5b9e\u4f8b\u5316\u6a21\u578b&#xff0c;\u5e76\u7528\u968f\u673a\u6570\u636e\u8fdb\u884c\u4e00\u6b21\u524d\u5411\u4f20\u64ad&#xff0c;\u770b\u770b\u6570\u636e\u7684\u5f62\u72b6\u662f\u5982\u4f55\u53d8\u5316\u7684\u3002<\/p>\n<p><span class=\"token comment\"># \u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6765\u8fd0\u884c\u548c\u6d4b\u8bd5\u6a21\u578b<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">run_demo<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token comment\"># \u5b9e\u4f8b\u5316\u6a21\u578b\u5bf9\u8c61<\/span><br \/>\n    my_model <span class=\"token operator\">&#061;<\/span> Model<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># &#8212; \u51c6\u5907\u8f93\u5165\u6570\u636e &#8212;<\/span><br \/>\n    <span class=\"token comment\"># \u968f\u673a\u751f\u6210\u4e00\u4e2a 5&#215;3 \u7684\u5f20\u91cf&#xff0c;\u6a21\u62df\u4e00\u4e2a\u6279\u6b21(batch_size&#061;5)\u7684\u6570\u636e&#xff0c;\u6bcf\u4e2a\u6570\u636e\u67093\u4e2a\u7279\u5f81<\/span><br \/>\n    my_data <span class=\"token operator\">&#061;<\/span> torch<span class=\"token punctuation\">.<\/span>randn<span class=\"token punctuation\">(<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">3<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u8f93\u5165\u6570\u636e (my_data):\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> my_data<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u8f93\u5165\u6570\u636e\u5f62\u72b6 (my_data.shape):&#034;<\/span><span class=\"token punctuation\">,<\/span> my_data<span class=\"token punctuation\">.<\/span>shape<span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># &#8212; \u5c06\u6570\u636e\u9001\u5165\u6a21\u578b\u8fdb\u884c\u524d\u5411\u4f20\u64ad &#8212;<\/span><br \/>\n    output <span class=\"token operator\">&#061;<\/span> my_model<span class=\"token punctuation\">(<\/span>my_data<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\\\\n\u8f93\u51fa\u6570\u636e (output):\\\\n&#034;<\/span><span class=\"token punctuation\">,<\/span> output<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u8f93\u51fa\u6570\u636e\u5f62\u72b6 (output.shape):&#034;<\/span><span class=\"token punctuation\">,<\/span> output<span class=\"token punctuation\">.<\/span>shape<span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># &#8212; \u4f7f\u7528 torchsummary \u8ba1\u7b97\u5e76\u6253\u5370\u6a21\u578b\u53c2\u6570\u4fe1\u606f &#8212;<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\\\\n&#061;&#061;&#061;&#061;&#061;&#061; \u4f7f\u7528 torchsummary \u5206\u6790\u6a21\u578b &#061;&#061;&#061;&#061;&#061;&#061;&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token comment\"># input_size&#061;(3,) \u8868\u793a\u5355\u4e2a\u6837\u672c\u7684\u7279\u5f81\u6570\u662f3<\/span><br \/>\n    summary<span class=\"token punctuation\">(<\/span>my_model<span class=\"token punctuation\">,<\/span> input_size<span class=\"token operator\">&#061;<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">,<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> batch_size<span class=\"token operator\">&#061;<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>    <span class=\"token comment\"># &#8212; \u624b\u52a8\u904d\u5386\u5e76\u67e5\u770b\u6a21\u578b\u7684\u53ef\u5b66\u4e60\u53c2\u6570 (\u6743\u91cdw\u548c\u504f\u7f6eb) &#8212;<\/span><br \/>\n    <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\\\\n&#061;&#061;&#061;&#061;&#061;&#061; \u67e5\u770b\u6a21\u578b\u5177\u4f53\u53c2\u6570 &#061;&#061;&#061;&#061;&#061;&#061;&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">for<\/span> name<span class=\"token punctuation\">,<\/span> parameter <span class=\"token keyword\">in<\/span> my_model<span class=\"token punctuation\">.<\/span>named_parameters<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-interpolation\"><span class=\"token string\">f&#034;\u53c2\u6570\u540d: <\/span><span class=\"token interpolation\"><span class=\"token punctuation\">{<\/span>name<span class=\"token punctuation\">}<\/span><\/span><span class=\"token string\">&#034;<\/span><\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>parameter<span class=\"token punctuation\">.<\/span>data<span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;-&#034;<\/span> <span class=\"token operator\">*<\/span> <span class=\"token number\">30<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">if<\/span> __name__ <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token string\">&#039;__main__&#039;<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    run_demo<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<h3>\u4e09\u3001\u7ed3\u679c\u5206\u6790\u4e0e\u53c2\u6570\u8ba1\u7b97\u8be6\u89e3<\/h3>\n<p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801&#xff0c;\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u8be6\u7ec6\u7684\u8f93\u51fa\u7ed3\u679c&#xff0c;\u6211\u4eec\u6765\u9010\u4e00\u5206\u6790\u3002<\/p>\n<h4>1. \u89c2\u5bdf\u6570\u636e\u5f62\u72b6\u53d8\u5316<\/h4>\n<ul>\n<li>\u8f93\u5165&#xff1a;[5, 3] \u8868\u793a\u6211\u4eec\u67095\u4e2a\u6837\u672c&#xff0c;\u6bcf\u4e2a\u6837\u672c\u67093\u4e2a\u7279\u5f81\u3002<\/li>\n<li>\u8f93\u51fa&#xff1a;[5, 2] \u8868\u793a\u7ecf\u8fc7\u7f51\u7edc\u5904\u7406\u540e&#xff0c;\u6211\u4eec\u5f97\u5230\u4e865\u4e2a\u6837\u672c\u7684\u9884\u6d4b\u7ed3\u679c&#xff0c;\u6bcf\u4e2a\u7ed3\u679c\u662f\u4e00\u4e2a\u5305\u542b2\u4e2a\u503c\u7684\u5411\u91cf&#xff0c;\u8fd9\u4e24\u4e2a\u503c\u4ee3\u8868\u4e86\u8be5\u6837\u672c\u5c5e\u4e8e\u4e24\u4e2a\u7c7b\u522b\u7684\u6982\u7387\u3002<\/li>\n<li>\u8fd9\u4e2a\u8fc7\u7a0b\u6e05\u6670\u5730\u5c55\u793a\u4e86\u6570\u636e\u5728\u7f51\u7edc\u4e2d\u5982\u4f55\u4ece\u8f93\u5165\u7ef4\u5ea6 in_features&#061;3 \u8f6c\u6362\u5230\u8f93\u51fa\u7ef4\u5ea6 out_features&#061;2\u3002<\/li>\n<\/ul>\n<p>  mydata<span class=\"token punctuation\">.<\/span>shape<span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&gt;<\/span> torch<span class=\"token punctuation\">.<\/span>Size<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">3<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><br \/>\n  output<span class=\"token punctuation\">.<\/span>shape<span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&gt;<\/span> torch<span class=\"token punctuation\">.<\/span>Size<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span><span class=\"token number\">5<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><br \/>\n  mydata<span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&gt;<\/span><br \/>\n    tensor<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">[<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token number\">0.3714<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">0.8578<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1.6988<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span><br \/>\n          <span class=\"token punctuation\">[<\/span> <span class=\"token number\">0.3149<\/span><span class=\"token punctuation\">,<\/span>  <span class=\"token number\">0.0142<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1.0432<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span><br \/>\n          <span class=\"token punctuation\">[<\/span> <span class=\"token number\">0.5374<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">0.1479<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">2.0006<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span><br \/>\n          <span class=\"token punctuation\">[<\/span> <span class=\"token number\">0.4327<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">0.3214<\/span><span class=\"token punctuation\">,<\/span>  <span class=\"token number\">1.0928<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span><br \/>\n          <span class=\"token punctuation\">[<\/span> <span class=\"token number\">2.2156<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1.1640<\/span><span class=\"token punctuation\">,<\/span>  <span class=\"token number\">1.0289<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><br \/>\n  output<span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&gt;<\/span><br \/>\n   tensor<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">[<\/span><span class=\"token number\">0.5095<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">0.4905<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span><br \/>\n          <span class=\"token punctuation\">[<\/span><span class=\"token number\">0.5218<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">0.4782<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span><br \/>\n          <span class=\"token punctuation\">[<\/span><span class=\"token number\">0.5419<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">0.4581<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span><br \/>\n          <span class=\"token punctuation\">[<\/span><span class=\"token number\">0.5163<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">0.4837<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span><br \/>\n          <span class=\"token punctuation\">[<\/span><span class=\"token number\">0.6030<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">0.3970<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> grad_fn<span class=\"token operator\">&#061;<\/span><span class=\"token operator\">&lt;<\/span>SoftmaxBackward<span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<h4>2. \u6a21\u578b\u53c2\u6570\u8ba1\u7b97&#xff08;\u6838\u5fc3\u91cd\u70b9&#xff01;&#xff09;<\/h4>\n<p>torchsummary \u5e93\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u76f4\u89c2\u7684\u6a21\u578b\u7ed3\u6784\u548c\u53c2\u6570\u7edf\u8ba1\u8868&#xff1a;<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n        Layer <span class=\"token punctuation\">(<\/span>type<span class=\"token punctuation\">)<\/span>               Output Shape         Param <span class=\"token comment\">#<\/span><br \/>\n<span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><br \/>\n            Linear-1                     <span class=\"token punctuation\">[<\/span><span class=\"token number\">5<\/span>, <span class=\"token number\">3<\/span><span class=\"token punctuation\">]<\/span>              <span class=\"token number\">12<\/span><br \/>\n            Linear-2                     <span class=\"token punctuation\">[<\/span><span class=\"token number\">5<\/span>, <span class=\"token number\">2<\/span><span class=\"token punctuation\">]<\/span>               <span class=\"token number\">8<\/span><br \/>\n            Linear-3                     <span class=\"token punctuation\">[<\/span><span class=\"token number\">5<\/span>, <span class=\"token number\">2<\/span><span class=\"token punctuation\">]<\/span>               <span class=\"token number\">6<\/span><br \/>\n<span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><span class=\"token operator\">&#061;&#061;<\/span><br \/>\nTotal params: <span class=\"token number\">26<\/span><\/p>\n<p>\u8fd9\u91cc\u7684 Param # \u662f\u5982\u4f55\u8ba1\u7b97\u7684\u5462&#xff1f;\u6211\u4eec\u6765\u624b\u52a8\u62c6\u89e3\u4e00\u4e0b&#xff0c;\u8fd9\u4e00\u70b9\u5bf9\u4e8e\u521d\u5b66\u8005\u7406\u89e3\u7f51\u7edc\u7ed3\u6784\u81f3\u5173\u91cd\u8981&#xff01;<\/p>\n<p>\u6838\u5fc3\u516c\u5f0f&#xff1a;\u53c2\u6570\u91cf &#061; \u8f93\u5165\u7ef4\u5ea6 \u00d7 \u8f93\u51fa\u7ef4\u5ea6 &#043; \u8f93\u51fa\u7ef4\u5ea6  (\u5373 w \u7684\u6570\u91cf &#043; b \u7684\u6570\u91cf)<\/p>\n<ul>\n<li>\n<p>\u7b2c\u4e00\u4e2a\u9690\u85cf\u5c42 (Linear-1)<\/p>\n<ul>\n<li>\u8f93\u5165\u7ef4\u5ea6 in_features &#061; 3<\/li>\n<li>\u8f93\u51fa\u7ef4\u5ea6 out_features &#061; 3<\/li>\n<li>\u6743\u91cd w \u7684\u6570\u91cf &#061; 3 * 3 &#061; 9<\/li>\n<li>\u504f\u7f6e b \u7684\u6570\u91cf &#061; 3 (\u6bcf\u4e2a\u8f93\u51fa\u795e\u7ecf\u5143\u4e00\u4e2a\u504f\u7f6e)<\/li>\n<li>\u603b\u53c2\u6570\u91cf &#061; 9 &#043; 3 &#061; 12<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7b2c\u4e8c\u4e2a\u9690\u85cf\u5c42 (Linear-2)<\/p>\n<ul>\n<li>\u8f93\u5165\u7ef4\u5ea6 in_features &#061; 3 (\u6765\u81ea\u4e0a\u4e00\u5c42\u7684\u8f93\u51fa)<\/li>\n<li>\u8f93\u51fa\u7ef4\u5ea6 out_features &#061; 2<\/li>\n<li>\u6743\u91cd w \u7684\u6570\u91cf &#061; 3 * 2 &#061; 6<\/li>\n<li>\u504f\u7f6e b \u7684\u6570\u91cf &#061; 2<\/li>\n<li>\u603b\u53c2\u6570\u91cf &#061; 6 &#043; 2 &#061; 8<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8f93\u51fa\u5c42 (Linear-3)<\/p>\n<ul>\n<li>\u8f93\u5165\u7ef4\u5ea6 in_features &#061; 2 (\u6765\u81ea\u4e0a\u4e00\u5c42\u7684\u8f93\u51fa)<\/li>\n<li>\u8f93\u51fa\u7ef4\u5ea6 out_features &#061; 2<\/li>\n<li>\u6743\u91cd w \u7684\u6570\u91cf &#061; 2 * 2 &#061; 4<\/li>\n<li>\u504f\u7f6e b \u7684\u6570\u91cf &#061; 2<\/li>\n<li>\u603b\u53c2\u6570\u91cf &#061; 4 &#043; 2 &#061; 6<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u603b\u8ba1&#xff1a;12 &#043; 8 &#043; 6 &#061; 26 \u4e2a\u53ef\u8bad\u7ec3\u53c2\u6570&#xff0c;\u4e0e\u5de5\u5177\u5206\u6790\u7684\u7ed3\u679c\u5b8c\u5168\u4e00\u81f4&#xff01;<\/p>\n<p>\u56fe\u89e3\u7b2c\u4e00\u4e2a\u9690\u85cf\u5c42\u7684\u53c2\u6570\u8ba1\u7b97&#xff1a;<\/p>\n<p align=\"center\">\n    <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119035047-696daa176ba56.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/>\n<\/p>\n<p>\u91cd\u8981\u63d0\u793a&#xff1a;\u4e00\u5b9a\u8981\u5206\u6e05\u8f93\u5165\u6570\u636e\u548c\u7f51\u7edc\u6743\u91cd\u662f\u4e24\u56de\u4e8b&#xff01;\u6570\u636e\u662f\u6d41\u52a8\u7684&#xff0c;\u800c\u6743\u91cd\u662f\u7f51\u7edc\u81ea\u8eab\u7684\u3001\u9700\u8981\u901a\u8fc7\u8bad\u7ec3\u5b66\u4e60\u7684\u56fa\u5b9a\u53c2\u6570\u3002<\/p>\n<h3>\u56db\u3001\u795e\u7ecf\u7f51\u7edc\u7684\u4f18\u7f3a\u70b9<\/h3>\n<p>\u6700\u540e&#xff0c;\u6211\u4eec\u7b80\u5355\u603b\u7ed3\u4e00\u4e0b\u795e\u7ecf\u7f51\u7edc\u8fd9\u79cd\u65b9\u6cd5\u7684\u666e\u904d\u4f18\u7f3a\u70b9&#xff1a;<\/p>\n<h5>\u4f18\u70b9 &#x1f44d;<\/h5>\n<li>\u7cbe\u5ea6\u9ad8&#xff1a;\u5728\u8bb8\u591a\u590d\u6742\u4efb\u52a1\u4e0a&#xff0c;\u5176\u6027\u80fd\u8fdc\u8d85\u4f20\u7edf\u673a\u5668\u5b66\u4e60\u65b9\u6cd5\u3002<\/li>\n<li>\u5f3a\u5927\u7684\u975e\u7ebf\u6027\u62df\u5408\u80fd\u529b&#xff1a;\u7406\u8bba\u4e0a\u53ef\u4ee5\u8fd1\u4f3c\u4efb\u4f55\u590d\u6742\u7684\u51fd\u6570\u5173\u7cfb\u3002<\/li>\n<li>\u751f\u6001\u4e30\u5bcc&#xff1a;\u62e5\u6709\u5927\u91cf\u6210\u719f\u7684\u5f00\u6e90\u6846\u67b6&#xff08;\u5982 PyTorch, TensorFlow&#xff09;\u548c\u6d3b\u8dc3\u7684\u793e\u533a\u652f\u6301\u3002<\/li>\n<h5>\u7f3a\u70b9 &#x1f44e;<\/h5>\n<li>\u9ed1\u7bb1\u6a21\u578b&#xff1a;\u53ef\u89e3\u91ca\u6027\u5dee&#xff0c;\u5f88\u96be\u5b8c\u5168\u7406\u89e3\u6a21\u578b\u505a\u51fa\u67d0\u4e2a\u5177\u4f53\u51b3\u7b56\u7684\u539f\u56e0\u3002<\/li>\n<li>\u8ba1\u7b97\u6210\u672c\u9ad8&#xff1a;\u8bad\u7ec3\u65f6\u95f4\u957f&#xff0c;\u9700\u8981\u5f3a\u5927\u7684\u786c\u4ef6&#xff08;\u5982GPU&#xff09;\u652f\u6301\u3002<\/li>\n<li>\u8c03\u53c2\u590d\u6742&#xff1a;\u7f51\u7edc\u7ed3\u6784\u3001\u5b66\u4e60\u7387\u7b49\u8d85\u53c2\u6570\u7684\u9009\u62e9\u5bf9\u7ed3\u679c\u5f71\u54cd\u5de8\u5927&#xff0c;\u901a\u5e38\u9700\u8981\u5927\u91cf\u5b9e\u9a8c\u3002<\/li>\n<li>\u6570\u636e\u4f9d\u8d56\u6027\u5f3a&#xff1a;\u5728\u5c0f\u6570\u636e\u96c6\u4e0a\u5bb9\u6613\u53d1\u751f\u8fc7\u62df\u5408&#xff0c;\u8868\u73b0\u4e0d\u4f73\u3002<\/li>\n<h3>&#x1f4dc; \u603b\u7ed3<\/h3>\n<p>\u606d\u559c\u4f60&#xff01;\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0&#xff0c;\u4f60\u4e0d\u4ec5\u7406\u89e3\u4e86\u5982\u4f55\u4f7f\u7528 PyTorch \u4ece\u96f6\u642d\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u795e\u7ecf\u7f51\u7edc\u7684\u601d\u8def&#xff0c;\u8fd8\u638c\u63e1\u4e86\u5982\u4f55\u5206\u6790\u5176\u7ed3\u6784\u3001\u8ba1\u7b97\u53c2\u6570\u91cf\u3002<\/p>\n<p>\u6838\u5fc3\u8981\u70b9\u56de\u987e&#xff1a;<\/p>\n<ul>\n<li>\u7ee7\u627f nn.Module&#xff0c;\u5728 __init__ \u4e2d\u5b9a\u4e49\u5c42&#xff0c;\u5728 forward \u4e2d\u5b9a\u4e49\u6570\u636e\u6d41\u3002<\/li>\n<li>\u4f7f\u7528\u5408\u7406\u7684\u53c2\u6570\u521d\u59cb\u5316\u65b9\u6cd5\u662f\u6210\u529f\u8bad\u7ec3\u7684\u5173\u952e\u3002<\/li>\n<li>\u53c2\u6570\u91cf\u7684\u8ba1\u7b97\u516c\u5f0f in * out &#043; out \u662f\u7406\u89e3\u7f51\u7edc\u590d\u6742\u5ea6\u7684\u57fa\u7840\u3002<\/li>\n<\/ul>\n<p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u6210\u4e3a\u4f60\u6df1\u5ea6\u5b66\u4e60\u4e4b\u8def\u4e0a\u7684\u4e00\u4e2a\u575a\u5b9e\u53f0\u9636\u3002\u5982\u679c\u4f60\u89c9\u5f97\u6709\u5e2e\u52a9&#xff0c;\u522b\u5fd8\u4e86\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u5173\u6ce8\u4e09\u8fde\u54e6&#xff01;\u6211\u4eec\u4e0b\u671f\u518d\u89c1&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5982\u4f55\u4ece\u96f6\u201c\u642d\u5efa\u201d\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc&#xff1f;<br \/>\n\u5927\u5bb6\u597d&#xff0c;\u5f88\u591a\u521d\u5b66\u8005\u5728\u5165\u95e8\u6df1\u5ea6\u5b66\u4e60\u65f6&#xff0c;\u5e38\u5e38\u4f1a\u5bf9\u201c\u5982\u4f55\u4ece\u96f6\u5f00\u59cb\u642d\u5efa\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\u201d\u611f\u5230\u56f0\u60d1\u3002\u522b\u62c5\u5fc3&#xff0c;\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u5c06\u624b\u628a\u624b\u5e26\u4f60\u7528 PyTorch \u5b8c\u6210\u8fd9\u4e2a\u8fc7\u7a0b&#xff0c;\u4e0d\u4ec5\u6559\u4f60\u201c\u600e\u4e48\u505a\u201d&#xff0c;\u66f4\u8ba9\u4f60\u660e\u767d\u201c\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u505a\u201d\u3002<br \/>\n\u5728 PyTorch \u4e2d&#xff0c;\u642d\u5efa\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\u5c31\u50cf\u642d\u79ef\u6728\u4e00\u6837\u7b80\u5355&#xff0c;\u6211\u4eec\u53ea\u9700\u8981\u5c06\u5404\u79cd\u201c\u5c42\u201d&#xff08;Layers&#xff09;\u5806\u53e0<\/p>\n","protected":false},"author":2,"featured_media":62138,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[587,152,50],"topic":[],"class_list":["post-62140","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-587","tag-pytorch","tag-50"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010PyTorch\u5165\u95e8\u3011\u624b\u628a\u624b\u5e26\u4f60\u642d\u5efa\u7b2c\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\uff08\u53c2\u6570\u8ba1\u7b97\u8be6\u89e3+\u4ee3\u7801\u5b9e\u6218\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/62140.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010PyTorch\u5165\u95e8\u3011\u624b\u628a\u624b\u5e26\u4f60\u642d\u5efa\u7b2c\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\uff08\u53c2\u6570\u8ba1\u7b97\u8be6\u89e3+\u4ee3\u7801\u5b9e\u6218\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u5982\u4f55\u4ece\u96f6\u201c\u642d\u5efa\u201d\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc&#xff1f; \u5927\u5bb6\u597d&#xff0c;\u5f88\u591a\u521d\u5b66\u8005\u5728\u5165\u95e8\u6df1\u5ea6\u5b66\u4e60\u65f6&#xff0c;\u5e38\u5e38\u4f1a\u5bf9\u201c\u5982\u4f55\u4ece\u96f6\u5f00\u59cb\u642d\u5efa\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\u201d\u611f\u5230\u56f0\u60d1\u3002\u522b\u62c5\u5fc3&#xff0c;\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u5c06\u624b\u628a\u624b\u5e26\u4f60\u7528 PyTorch \u5b8c\u6210\u8fd9\u4e2a\u8fc7\u7a0b&#xff0c;\u4e0d\u4ec5\u6559\u4f60\u201c\u600e\u4e48\u505a\u201d&#xff0c;\u66f4\u8ba9\u4f60\u660e\u767d\u201c\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u505a\u201d\u3002 \u5728 PyTorch \u4e2d&#xff0c;\u642d\u5efa\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\u5c31\u50cf\u642d\u79ef\u6728\u4e00\u6837\u7b80\u5355&#xff0c;\u6211\u4eec\u53ea\u9700\u8981\u5c06\u5404\u79cd\u201c\u5c42\u201d&#xff08;Layers&#xff09;\u5806\u53e0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/62140.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-19T03:50:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119035047-696daa17528f9.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/62140.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/62140.html\",\"name\":\"\u3010PyTorch\u5165\u95e8\u3011\u624b\u628a\u624b\u5e26\u4f60\u642d\u5efa\u7b2c\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\uff08\u53c2\u6570\u8ba1\u7b97\u8be6\u89e3+\u4ee3\u7801\u5b9e\u6218\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-19T03:50:48+00:00\",\"dateModified\":\"2026-01-19T03:50:48+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/62140.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/62140.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/62140.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010PyTorch\u5165\u95e8\u3011\u624b\u628a\u624b\u5e26\u4f60\u642d\u5efa\u7b2c\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\uff08\u53c2\u6570\u8ba1\u7b97\u8be6\u89e3+\u4ee3\u7801\u5b9e\u6218\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010PyTorch\u5165\u95e8\u3011\u624b\u628a\u624b\u5e26\u4f60\u642d\u5efa\u7b2c\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\uff08\u53c2\u6570\u8ba1\u7b97\u8be6\u89e3+\u4ee3\u7801\u5b9e\u6218\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/62140.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010PyTorch\u5165\u95e8\u3011\u624b\u628a\u624b\u5e26\u4f60\u642d\u5efa\u7b2c\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\uff08\u53c2\u6570\u8ba1\u7b97\u8be6\u89e3+\u4ee3\u7801\u5b9e\u6218\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001\u5982\u4f55\u4ece\u96f6\u201c\u642d\u5efa\u201d\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc&#xff1f; \u5927\u5bb6\u597d&#xff0c;\u5f88\u591a\u521d\u5b66\u8005\u5728\u5165\u95e8\u6df1\u5ea6\u5b66\u4e60\u65f6&#xff0c;\u5e38\u5e38\u4f1a\u5bf9\u201c\u5982\u4f55\u4ece\u96f6\u5f00\u59cb\u642d\u5efa\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\u201d\u611f\u5230\u56f0\u60d1\u3002\u522b\u62c5\u5fc3&#xff0c;\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u5c06\u624b\u628a\u624b\u5e26\u4f60\u7528 PyTorch \u5b8c\u6210\u8fd9\u4e2a\u8fc7\u7a0b&#xff0c;\u4e0d\u4ec5\u6559\u4f60\u201c\u600e\u4e48\u505a\u201d&#xff0c;\u66f4\u8ba9\u4f60\u660e\u767d\u201c\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u505a\u201d\u3002 \u5728 PyTorch \u4e2d&#xff0c;\u642d\u5efa\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\u5c31\u50cf\u642d\u79ef\u6728\u4e00\u6837\u7b80\u5355&#xff0c;\u6211\u4eec\u53ea\u9700\u8981\u5c06\u5404\u79cd\u201c\u5c42\u201d&#xff08;Layers&#xff09;\u5806\u53e0","og_url":"https:\/\/www.wsisp.com\/helps\/62140.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-19T03:50:48+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260119035047-696daa17528f9.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/62140.html","url":"https:\/\/www.wsisp.com\/helps\/62140.html","name":"\u3010PyTorch\u5165\u95e8\u3011\u624b\u628a\u624b\u5e26\u4f60\u642d\u5efa\u7b2c\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\uff08\u53c2\u6570\u8ba1\u7b97\u8be6\u89e3+\u4ee3\u7801\u5b9e\u6218\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-19T03:50:48+00:00","dateModified":"2026-01-19T03:50:48+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/62140.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/62140.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/62140.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010PyTorch\u5165\u95e8\u3011\u624b\u628a\u624b\u5e26\u4f60\u642d\u5efa\u7b2c\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\uff08\u53c2\u6570\u8ba1\u7b97\u8be6\u89e3+\u4ee3\u7801\u5b9e\u6218\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/62140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=62140"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/62140\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/62138"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=62140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=62140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=62140"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=62140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}