{"id":73643,"date":"2026-02-08T01:42:25","date_gmt":"2026-02-07T17:42:25","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/73643.html"},"modified":"2026-02-08T01:42:25","modified_gmt":"2026-02-07T17:42:25","slug":"%e3%80%8atransformer%e6%a8%a1%e5%9e%8bpytorch%e5%ae%9e%e7%8e%b0%e5%85%a8%e6%94%bb%e7%95%a5%ef%bc%9a%e6%9e%b6%e6%9e%84%e6%8b%86%e8%a7%a3%e3%80%81%e4%bb%a3%e7%a0%81%e7%a4%ba%e4%be%8b%e4%b8%8e%e4%bc%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/73643.html","title":{"rendered":"\u300aTransformer\u6a21\u578bPyTorch\u5b9e\u73b0\u5168\u653b\u7565\uff1a\u67b6\u6784\u62c6\u89e3\u3001\u4ee3\u7801\u793a\u4f8b\u4e0e\u4f18\u5316\u6280\u5de7\u300b"},"content":{"rendered":"<h2 id=\"%E6%9C%AC%E7%AF%87%E6%8A%80%E6%9C%AF%E5%8D%9A%E6%96%87%E6%91%98%E8%A6%81%20%F0%9F%8C%9F\" style=\"background-color:transparent;text-align:center\">\u672c\u7bc7\u6280\u672f\u535a\u6587\u6458\u8981 &#x1f31f;<\/h2>\n<ul>\n<li>\u6587\u7ae0\u5f00\u7bc7\u5373\u9610\u660e\u4f7f\u7528PyTorch\u6846\u67b6\u5b9e\u73b0Transformer\u7684\u5b9e\u9645\u4ef7\u503c\u4e0e\u91cd\u8981\u610f\u4e49&#xff0c;\u6307\u51fa\u5176\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\u7b49\u9886\u57df\u7684\u6838\u5fc3\u5730\u4f4d\u3002<\/li>\n<li>\u968f\u540e&#xff0c;\u6587\u7ae0\u4ee5\u6e05\u6670\u7684\u6b65\u9aa4\u5316\u7ed3\u6784\u5c55\u5f00&#xff1a;\u9996\u5148\u6307\u5bfc\u8bfb\u8005\u5bfc\u5165\u5fc5\u8981\u7684PyTorch\u5e93\u4e0e\u6a21\u5757&#xff0c;\u5960\u5b9a\u5f00\u53d1\u57fa\u7840&#xff1b;<\/li>\n<li>\u63a5\u7740\u6df1\u5165\u62c6\u89e3Transformer\u7684\u6838\u5fc3\u7ec4\u4ef6&#xff0c;\u5305\u62ec\u591a\u5934\u6ce8\u610f\u529b\u673a\u5236\u3001\u4f4d\u7f6e\u524d\u9988\u7f51\u7edc\u4e0e\u4f4d\u7f6e\u7f16\u7801\u7684\u539f\u7406\u4e0e\u5b9e\u73b0&#xff0c;\u5e76\u9010\u4e00\u63d0\u4f9b\u53ef\u8fd0\u884c\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/li>\n<li>\u5728\u6b64\u57fa\u7840\u4e0a&#xff0c;\u6587\u7ae0\u8be6\u7ec6\u8bb2\u89e3\u4e86\u5982\u4f55\u5c06\u8fd9\u4e9b\u57fa\u7840\u6a21\u5757\u7ec4\u5408\u4e3a\u7f16\u7801\u5668\u5c42\u4e0e\u89e3\u7801\u5668\u5c42&#xff0c;\u8fdb\u800c\u6784\u5efa\u5b8c\u6574\u7684Transformer\u6a21\u578b\u67b6\u6784&#xff0c;\u540c\u65f6\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u6a21\u578b\u53c2\u6570\u521d\u59cb\u5316\u8bf4\u660e\u3002<\/li>\n<li>\u6587\u7ae0\u4e0d\u4ec5\u505c\u7559\u5728\u6a21\u578b\u6784\u5efa\u5c42\u9762&#xff0c;\u66f4\u8fdb\u4e00\u6b65\u6f14\u793a\u4e86\u6a21\u578b\u7684\u8bad\u7ec3\u6d41\u7a0b\u4e0e\u8bc4\u4f30\u65b9\u6cd5&#xff0c;\u5305\u62ec\u635f\u5931\u51fd\u6570\u9009\u62e9\u3001\u4f18\u5316\u5668\u914d\u7f6e\u4ee5\u53ca\u6027\u80fd\u8bc4\u4f30\u6307\u6807&#xff0c;\u6700\u540e\u5bf9\u4ee3\u7801\u4f18\u5316\u4e0e\u6a21\u578b\u8bc4\u4f30\u7ed3\u679c\u8fdb\u884c\u4e86\u7cfb\u7edf\u6027\u6c47\u603b&#xff0c;\u771f\u6b63\u5b9e\u73b0\u4e86\u4ece\u7406\u8bba\u5230\u5b9e\u8df5\u3001\u4ece\u6784\u5efa\u5230\u8bc4\u4f30\u7684\u5168\u94fe\u8def\u8d2f\u901a\u3002<\/li>\n<\/ul>\n<h2 id=\"%E5%BC%95%E8%A8%80%20%F0%9F%93%98\" style=\"background-color:transparent;text-align:center\">\u5f15\u8a00 &#x1f4d8;<\/h2>\n<ul>\n<li><span style=\"color:#38d8f0\">\u5728\u8fd9\u4e2a\u53d8\u5e7b\u83ab\u6d4b\u3001\u5feb\u901f\u53d1\u5c55\u7684\u6280\u672f\u65f6\u4ee3&#xff0c;\u4e0e\u65f6\u4ff1\u8fdb\u662f\u6bcf\u4e2aIT\u5de5\u7a0b\u5e08\u7684\u5fc5\u4fee\u8bfe\u3002<\/span><\/li>\n<li><span style=\"color:#38d8f0\">\u6211\u662f\u76db\u900f\u4fa7\u89c6\u653b\u57ce\u72ee&#xff0c;\u4e00\u540d\u4ec0\u4e48\u90fd\u4f1a\u4e00\u4e22\u4e22\u7684\u7f51\u7edc\u5b89\u5168\u5de5\u7a0b\u5e08&#xff0c;\u4e5f\u662f\u4f17\u591a\u6280\u672f\u793e\u533a\u7684\u6d3b\u8dc3\u6210\u5458\u4ee5\u53ca\u591a\u5bb6\u5927\u5382\u5b98\u65b9\u8ba4\u53ef\u4eba\u5458&#xff0c;\u5e0c\u671b\u80fd\u591f\u4e0e\u5404\u4f4d\u5728\u6b64\u5171\u540c\u6210\u957f\u3002<\/span><\/li>\n<\/ul>\n<h2 id=\"\"><\/h2>\n<p style=\"text-align:center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"438\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174215-698779770c6a7.gif\" width=\"1200\" \/><\/p>\n<\/p>\n<h2 id=\"%E4%B8%8A%E8%8A%82%E5%9B%9E%E9%A1%BE\" style=\"background-color:transparent;text-align:center\">\u4e0a\u8282\u56de\u987e<\/h2>\n<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E6%9C%AC%E7%AF%87%E6%8A%80%E6%9C%AF%E5%8D%9A%E6%96%87%E6%91%98%E8%A6%81%20%F0%9F%8C%9F-toc\" style=\"margin-left:0px\">\u672c\u7bc7\u6280\u672f\u535a\u6587\u6458\u8981 &#x1f31f;<\/p>\n<p id=\"%E5%BC%95%E8%A8%80%20%F0%9F%93%98-toc\" style=\"margin-left:0px\">\u5f15\u8a00 &#x1f4d8;<\/p>\n<p id=\"-toc\" style=\"margin-left:0px\">\n<p id=\"%E4%B8%8A%E8%8A%82%E5%9B%9E%E9%A1%BE-toc\" style=\"margin-left:0px\">\u4e0a\u8282\u56de\u987e<\/p>\n<p id=\"1.PyTorch%20%E6%9E%84%E5%BB%BA%20Transformer%20%E6%A8%A1%E5%9E%8B%E6%84%8F%E4%B9%89-toc\" style=\"margin-left:0px\">1.PyTorch \u6784\u5efa Transformer \u6a21\u578b\u610f\u4e49<\/p>\n<p id=\"2.%E5%85%B7%E4%BD%93%E4%BD%BF%E7%94%A8%20PyTorch%20%E6%9E%84%E5%BB%BA%20Transformer%20%E6%A8%A1%E5%9E%8B%E6%AD%A5%E9%AA%A4-toc\" style=\"margin-left:0px\">2.\u5177\u4f53\u4f7f\u7528 PyTorch \u6784\u5efa Transformer \u6a21\u578b\u6b65\u9aa4<\/p>\n<p id=\"2.1%E5%AF%BC%E5%85%A5%E5%BF%85%E8%A6%81%E7%9A%84%E5%BA%93%E5%92%8C%E6%A8%A1%E5%9D%97-toc\" style=\"margin-left:40px\">2.1\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u548c\u6a21\u5757<\/p>\n<p id=\"2.2%E5%AE%9A%E4%B9%89%E5%9F%BA%E6%9C%AC%E6%9E%84%E5%BB%BA%E5%9D%97%EF%BC%9A-toc\" style=\"margin-left:40px\">2.2\u5b9a\u4e49\u57fa\u672c\u6784\u5efa\u5757&#xff1a;<\/p>\n<p id=\"2.2.1%E5%A4%9A%E5%A4%B4%E6%B3%A8%E6%84%8F%E5%8A%9B-toc\" style=\"margin-left:80px\">2.2.1\u591a\u5934\u6ce8\u610f\u529b<\/p>\n<p id=\"%E5%A4%9A%E5%A4%B4%E6%B3%A8%E6%84%8F%E5%8A%9B%E5%85%B7%E4%BD%93%E7%A4%BA%E4%BE%8B%EF%BC%9A-toc\" style=\"margin-left:120px\">\u591a\u5934\u6ce8\u610f\u529b\u5177\u4f53\u793a\u4f8b&#xff1a;<\/p>\n<p id=\"2.2.2%E4%BD%8D%E7%BD%AE%E5%89%8D%E9%A6%88%E7%BD%91%E7%BB%9C%EF%BC%88Position-wise%20Feed-Forward%20Network%EF%BC%89-toc\" style=\"margin-left:80px\">2.2.2\u4f4d\u7f6e\u524d\u9988\u7f51\u7edc&#xff08;Position-wise Feed-Forward Network&#xff09;<\/p>\n<p id=\"%E4%BD%8D%E7%BD%AE%E5%89%8D%E9%A6%88%E7%BD%91%E7%BB%9C%E5%85%B7%E4%BD%93%E7%A4%BA%E4%BE%8B%EF%BC%9A-toc\" style=\"margin-left:120px\">\u4f4d\u7f6e\u524d\u9988\u7f51\u7edc\u5177\u4f53\u793a\u4f8b&#xff1a;<\/p>\n<p id=\"2.2.3%E4%BD%8D%E7%BD%AE%E7%BC%96%E7%A0%81-toc\" style=\"margin-left:80px\">2.2.3\u4f4d\u7f6e\u7f16\u7801<\/p>\n<p id=\"%E4%BD%8D%E7%BD%AE%E7%BC%96%E7%A0%81%E5%85%B7%E4%BD%93%E7%A4%BA%E4%BE%8B%EF%BC%9A-toc\" style=\"margin-left:120px\">\u4f4d\u7f6e\u7f16\u7801\u5177\u4f53\u793a\u4f8b&#xff1a;<\/p>\n<p id=\"2.3%E6%9E%84%E5%BB%BA%E7%BC%96%E7%A0%81%E5%99%A8%E5%9D%97%EF%BC%88Encoder%20Layer%EF%BC%89-toc\" style=\"margin-left:40px\">2.3\u6784\u5efa\u7f16\u7801\u5668\u5757&#xff08;Encoder Layer&#xff09;<\/p>\n<p id=\"2.3.1%E7%BC%96%E7%A0%81%E5%99%A8%E5%B1%82-toc\" style=\"margin-left:80px\">2.3.1\u7f16\u7801\u5668\u5c42<\/p>\n<p id=\"%E7%BC%96%E7%A0%81%E5%99%A8%E5%B1%82%E5%85%B7%E4%BD%93%E7%A4%BA%E4%BE%8B%EF%BC%9A-toc\" style=\"margin-left:120px\">\u7f16\u7801\u5668\u5c42\u5177\u4f53\u793a\u4f8b&#xff1a;<\/p>\n<p id=\"2.4%E6%9E%84%E5%BB%BA%E8%A7%A3%E7%A0%81%E5%99%A8%E6%A8%A1%E5%9D%97-toc\" style=\"margin-left:40px\">2.4\u6784\u5efa\u89e3\u7801\u5668\u6a21\u5757<\/p>\n<p id=\"2.4.1%E8%A7%A3%E7%A0%81%E5%99%A8%E5%B1%82-toc\" style=\"margin-left:80px\">2.4.1\u89e3\u7801\u5668\u5c42<\/p>\n<p id=\"%E8%A7%A3%E7%A0%81%E5%99%A8%E5%B1%82%E5%85%B7%E4%BD%93%E7%A4%BA%E4%BE%8B%EF%BC%9A-toc\" style=\"margin-left:120px\">\u89e3\u7801\u5668\u5c42\u5177\u4f53\u793a\u4f8b&#xff1a;<\/p>\n<p id=\"3.%E5%A6%82%E4%BD%95%E6%9E%84%E5%BB%BA%E5%AE%8C%E6%95%B4%E7%9A%84%20Transformer%20%E6%A8%A1%E5%9E%8B-toc\" style=\"margin-left:0px\">3.\u5982\u4f55\u6784\u5efa\u5b8c\u6574\u7684 Transformer \u6a21\u578b<\/p>\n<p id=\"3.1%E5%A6%82%E4%BD%95%E6%9E%84%E5%BB%BA%E5%AE%8C%E6%95%B4%E7%9A%84%20Transformer%20%E6%A8%A1%E5%9E%8B%E5%85%B7%E4%BD%93%E7%A4%BA%E4%BE%8B%EF%BC%9A-toc\" style=\"margin-left:40px\">3.1\u5982\u4f55\u6784\u5efa\u5b8c\u6574\u7684 Transformer \u6a21\u578b\u5177\u4f53\u793a\u4f8b&#xff1a;<\/p>\n<p id=\"3.2%E6%A8%A1%E5%9E%8B%E5%88%9D%E5%A7%8B%E5%8C%96%E5%8F%82%E6%95%B0%E8%AF%B4%E6%98%8E%EF%BC%9A-toc\" style=\"margin-left:40px\">3.2\u6a21\u578b\u521d\u59cb\u5316\u53c2\u6570\u8bf4\u660e&#xff1a;<\/p>\n<p id=\"4.%E8%AE%AD%E7%BB%83%20PyTorch%20Transformer%20%E6%A8%A1%E5%9E%8B-toc\" style=\"margin-left:0px\">4.\u8bad\u7ec3 PyTorch Transformer \u6a21\u578b<\/p>\n<p id=\"4.1%E8%AE%AD%E7%BB%83%20PyTorch%20Transformer%20%E6%A8%A1%E5%9E%8B%E7%A4%BA%E4%BE%8B%EF%BC%9A-toc\" style=\"margin-left:40px\">4.1\u8bad\u7ec3 PyTorch Transformer \u6a21\u578b\u793a\u4f8b&#xff1a;<\/p>\n<p id=\"5.%E6%A8%A1%E5%9E%8B%E8%AF%84%E4%BC%B0%E6%80%A7%E8%83%BD-toc\" style=\"margin-left:0px\">5.\u6a21\u578b\u8bc4\u4f30\u6027\u80fd<\/p>\n<p id=\"5.1%E6%A8%A1%E5%9E%8B%E8%AF%84%E4%BC%B0%E6%80%A7%E8%83%BD%E5%85%B7%E4%BD%93%E7%A4%BA%E4%BE%8B%EF%BC%9A-toc\" style=\"margin-left:40px\">5.1\u6a21\u578b\u8bc4\u4f30\u6027\u80fd\u5177\u4f53\u793a\u4f8b&#xff1a;<\/p>\n<p id=\"6.PyTorch%20%E6%9E%84%E5%BB%BA%2B%E8%AF%84%E4%BC%B0%C2%A0Transformer%20%E6%A8%A1%E5%9E%8B%E4%BB%A3%E7%A0%81%E4%BC%98%E5%8C%96%E6%B1%87%E6%80%BB-toc\" style=\"margin-left:0px\">6.PyTorch \u6784\u5efa&#043;\u8bc4\u4f30\u00a0Transformer \u6a21\u578b\u4ee3\u7801\u4f18\u5316\u6c47\u603b<\/p>\n<p id=\"6.1%E6%A8%A1%E5%9E%8B%E6%80%A7%E8%83%BD%E8%AF%84%E4%BC%B0%E7%BB%93%E6%9E%9C%E5%A6%82%E4%B8%8B%EF%BC%9A%E2%80%8B%E7%BC%96%E8%BE%91-toc\" style=\"margin-left:40px\">6.1\u6a21\u578b\u6027\u80fd\u8bc4\u4f30\u7ed3\u679c\u5982\u4e0b&#xff1a;\u200b<\/p>\n<p id=\"%E6%AC%A2%E8%BF%8E%E5%90%84%E4%BD%8D%E5%BD%A6%E7%A5%96%E4%B8%8E%E7%83%AD%E5%B7%B4%E7%95%85%E6%B8%B8%E6%9C%AC%E4%BA%BA%E4%B8%93%E6%A0%8F%E4%B8%8E%E5%8D%9A%E5%AE%A2-toc\" style=\"margin-left:0px\">\u6b22\u8fce\u5404\u4f4d\u5f66\u7956\u4e0e\u70ed\u5df4\u7545\u6e38\u672c\u4eba\u4e13\u680f\u4e0e\u6280\u672f\u535a\u5ba2<\/p>\n<p id=\"%E4%BD%A0%E7%9A%84%E4%B8%89%E8%BF%9E%E6%98%AF%E6%88%91%E6%9C%80%E5%A4%A7%E7%9A%84%E5%8A%A8%E5%8A%9B-toc\" style=\"margin-left:0px\">\u4f60\u7684\u4e09\u8fde\u662f\u6211\u6700\u5927\u7684\u52a8\u529b<\/p>\n<p id=\"%E4%BB%A5%E4%B8%8B%E5%9B%BE%E7%89%87%E4%BB%85%E4%BB%A3%E8%A1%A8%E4%B8%93%E6%A0%8F%E7%89%B9%E8%89%B2%20%5B%E7%82%B9%E5%87%BB%E7%AE%AD%E5%A4%B4%E6%8C%87%E5%90%91%E7%9A%84%E4%B8%93%E6%A0%8F%E5%90%8D%E5%8D%B3%E5%8F%AF%E9%97%AA%E7%8E%B0%5D-toc\" style=\"margin-left:40px\">\u70b9\u51fb\u27a1\ufe0f\u6307\u5411\u7684\u4e13\u680f\u540d\u5373\u53ef\u95ea\u73b0<\/p>\n<hr id=\"hr-toc\" \/>\n<p style=\"text-align:center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1080\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174215-69877977289b9.gif\" width=\"1200\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1095\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174215-698779773fb1e.png\" width=\"991\" \/><\/p>\n<h2 id=\"1.PyTorch%20%E6%9E%84%E5%BB%BA%20Transformer%20%E6%A8%A1%E5%9E%8B%E6%84%8F%E4%B9%89\">1.PyTorch \u6784\u5efa Transformer \u6a21\u578b\u610f\u4e49<\/h2>\n<ul>\n<li>\n<p>Transformer \u662f\u73b0\u4ee3\u673a\u5668\u5b66\u4e60\u4e2d\u6700\u5f3a\u5927\u7684\u6a21\u578b\u4e4b\u4e00\u3002<\/p>\n<\/li>\n<li>\n<p>Transformer \u6a21\u578b\u662f\u4e00\u79cd\u57fa\u4e8e\u81ea\u6ce8\u610f\u529b\u673a\u5236&#xff08;Self-Attention&#xff09; \u7684\u6df1\u5ea6\u5b66\u4e60\u67b6\u6784&#xff0c;\u5b83\u5f7b\u5e95\u6539\u53d8\u4e86\u81ea\u7136\u8bed\u8a00\u5904\u7406&#xff08;NLP&#xff09;\u9886\u57df&#xff0c;\u5e76\u6210\u4e3a\u73b0\u4ee3\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b&#xff08;\u5982 BERT\u3001GPT \u7b49&#xff09;\u7684\u57fa\u7840\u3002<\/p>\n<\/li>\n<li>\n<p>Transformer \u662f\u73b0\u4ee3 NLP \u9886\u57df\u7684\u6838\u5fc3\u67b6\u6784&#xff0c;\u51ed\u501f\u5176\u5f3a\u5927\u7684\u957f\u8ddd\u79bb\u4f9d\u8d56\u5efa\u6a21\u80fd\u529b\u548c\u9ad8\u6548\u7684\u5e76\u884c\u8ba1\u7b97\u4f18\u52bf&#xff0c;\u5728\u8bed\u8a00\u7ffb\u8bd1\u548c\u6587\u672c\u6458\u8981\u7b49\u4efb\u52a1\u4e2d\u8d85\u8d8a\u4e86\u4f20\u7edf\u7684 \u957f\u77ed\u671f\u8bb0\u5fc6 (LSTM) \u7f51\u7edc\u3002<\/p>\n<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"927\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174215-69877977addcf.png\" width=\"1301\" \/><\/p>\n<h2 id=\"2.%E5%85%B7%E4%BD%93%E4%BD%BF%E7%94%A8%20PyTorch%20%E6%9E%84%E5%BB%BA%20Transformer%20%E6%A8%A1%E5%9E%8B%E6%AD%A5%E9%AA%A4\">2.\u5177\u4f53\u4f7f\u7528 PyTorch \u6784\u5efa Transformer \u6a21\u578b\u6b65\u9aa4<\/h2>\n<h3 id=\"2.1%E5%AF%BC%E5%85%A5%E5%BF%85%E8%A6%81%E7%9A%84%E5%BA%93%E5%92%8C%E6%A8%A1%E5%9D%97\">2.1\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u548c\u6a21\u5757<\/h3>\n<ul>\n<li>\u5bfc\u5165 PyTorch \u6838\u5fc3\u5e93\u3001\u795e\u7ecf\u7f51\u7edc\u6a21\u5757\u3001\u4f18\u5316\u5668\u6a21\u5757\u3001\u6570\u636e\u5904\u7406\u5de5\u5177&#xff0c;\u4ee5\u53ca\u6570\u5b66\u548c\u5bf9\u8c61\u590d\u5236\u6a21\u5757&#xff0c;\u4e3a\u5b9a\u4e49\u6a21\u578b\u67b6\u6784\u3001\u7ba1\u7406\u6570\u636e\u548c\u8bad\u7ec3\u8fc7\u7a0b\u63d0\u4f9b\u652f\u6301\u3002<\/li>\n<\/ul>\n<p>import torch<br \/>\nimport torch.nn as nn<br \/>\nimport torch.optim as optim<br \/>\nimport torch.utils.data as data<br \/>\nimport math<br \/>\nimport copy <\/p>\n<ul>\n<li>\n<p>\u8bf4\u660e&#xff1a;<\/p>\n<ul>\n<li>\n<p>torch&#xff1a;PyTorch \u7684\u6838\u5fc3\u5e93&#xff0c;\u7528\u4e8e\u5f20\u91cf\u64cd\u4f5c\u548c\u81ea\u52a8\u6c42\u5bfc\u3002<\/p>\n<\/li>\n<li>\n<p>torch.nn&#xff1a;PyTorch \u7684\u795e\u7ecf\u7f51\u7edc\u6a21\u5757&#xff0c;\u5305\u542b\u5404\u79cd\u5c42\u548c\u635f\u5931\u51fd\u6570\u3002<\/p>\n<\/li>\n<li>\n<p>torch.optim&#xff1a;\u4f18\u5316\u7b97\u6cd5\u6a21\u5757&#xff0c;\u5982 Adam\u3001SGD \u7b49\u3002<\/p>\n<\/li>\n<li>\n<p>math&#xff1a;\u6570\u5b66\u51fd\u6570\u5e93&#xff0c;\u7528\u4e8e\u8ba1\u7b97\u5e73\u65b9\u6839\u7b49\u3002<\/p>\n<\/li>\n<li>\n<p>copy&#xff1a;\u7528\u4e8e\u6df1\u5ea6\u590d\u5236\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3 id=\"2.2%E5%AE%9A%E4%B9%89%E5%9F%BA%E6%9C%AC%E6%9E%84%E5%BB%BA%E5%9D%97%EF%BC%9A\" style=\"background-color:transparent\">2.2\u5b9a\u4e49\u57fa\u672c\u6784\u5efa\u5757&#xff1a;<\/h3>\n<h4 id=\"2.2.1%E5%A4%9A%E5%A4%B4%E6%B3%A8%E6%84%8F%E5%8A%9B\">2.2.1\u591a\u5934\u6ce8\u610f\u529b<\/h4>\n<ul>\n<li>\u901a\u8fc7\u591a\u4e2a&#034;\u6ce8\u610f\u529b\u5934&#034;\u8ba1\u7b97\u5e8f\u5217\u4e2d\u6bcf\u5bf9\u4f4d\u7f6e\u4e4b\u95f4\u7684\u5173\u7cfb&#xff0c;\u80fd\u591f\u6355\u6349\u8f93\u5165\u5e8f\u5217\u7684\u4e0d\u540c\u7279\u5f81\u548c\u6a21\u5f0f\u3002<\/li>\n<\/ul>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"537\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174216-69877978c9e0b.png\" width=\"720\" \/><\/p>\n<ul>\n<li>MultiHeadAttention \u7c7b\u5c01\u88c5\u4e86 Transformer \u6a21\u578b\u4e2d\u5e38\u7528\u7684\u591a\u5934\u6ce8\u610f\u529b\u673a\u5236&#xff0c;\u8d1f\u8d23\u5c06\u8f93\u5165\u62c6\u5206\u6210\u591a\u4e2a\u6ce8\u610f\u529b\u5934&#xff0c;\u5bf9\u6bcf\u4e2a\u6ce8\u610f\u529b\u5934\u65bd\u52a0\u6ce8\u610f\u529b&#xff0c;\u7136\u540e\u5c06\u7ed3\u679c\u7ec4\u5408\u8d77\u6765&#xff0c;\u8fd9\u6837\u6a21\u578b\u5c31\u53ef\u4ee5\u5728\u4e0d\u540c\u5c3a\u5ea6\u4e0a\u6355\u6349\u8f93\u5165\u6570\u636e\u4e2d\u7684\u5404\u79cd\u5173\u7cfb&#xff0c;\u63d0\u9ad8\u6a21\u578b\u7684\u8868\u8fbe\u80fd\u529b\u3002<\/li>\n<\/ul>\n<h5 id=\"%E5%A4%9A%E5%A4%B4%E6%B3%A8%E6%84%8F%E5%8A%9B%E5%85%B7%E4%BD%93%E7%A4%BA%E4%BE%8B%EF%BC%9A\" style=\"background-color:transparent\">\u591a\u5934\u6ce8\u610f\u529b\u5177\u4f53\u793a\u4f8b&#xff1a;<\/h5>\n<p>class MultiHeadAttention(nn.Module):<br \/>\n    def __init__(self, d_model, num_heads):<br \/>\n        super(MultiHeadAttention, self).__init__()<br \/>\n        assert d_model % num_heads &#061;&#061; 0, &#034;d_model\u5fc5\u987b\u80fd\u88abnum_heads\u6574\u9664&#034;<\/p>\n<p>        self.d_model &#061; d_model    # \u6a21\u578b\u7ef4\u5ea6&#xff08;\u5982512&#xff09;<br \/>\n        self.num_heads &#061; num_heads # \u6ce8\u610f\u529b\u5934\u6570&#xff08;\u59828&#xff09;<br \/>\n        self.d_k &#061; d_model \/\/ num_heads # \u6bcf\u4e2a\u5934\u7684\u7ef4\u5ea6&#xff08;\u598264&#xff09;<\/p>\n<p>        # \u5b9a\u4e49\u7ebf\u6027\u53d8\u6362\u5c42&#xff08;\u65e0\u9700\u504f\u7f6e&#xff09;<br \/>\n        self.W_q &#061; nn.Linear(d_model, d_model) # \u67e5\u8be2\u53d8\u6362<br \/>\n        self.W_k &#061; nn.Linear(d_model, d_model) # \u952e\u53d8\u6362<br \/>\n        self.W_v &#061; nn.Linear(d_model, d_model) # \u503c\u53d8\u6362<br \/>\n        self.W_o &#061; nn.Linear(d_model, d_model) # \u8f93\u51fa\u53d8\u6362<\/p>\n<p>    def scaled_dot_product_attention(self, Q, K, V, mask&#061;None):<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u8ba1\u7b97\u7f29\u653e\u70b9\u79ef\u6ce8\u610f\u529b<br \/>\n        \u8f93\u5165\u5f62\u72b6&#xff1a;<br \/>\n            Q: (batch_size, num_heads, seq_length, d_k)<br \/>\n            K, V: \u540cQ<br \/>\n        \u8f93\u51fa\u5f62\u72b6&#xff1a; (batch_size, num_heads, seq_length, d_k)<br \/>\n        &#034;&#034;&#034;<br \/>\n        # \u8ba1\u7b97\u6ce8\u610f\u529b\u5206\u6570&#xff08;Q\u548cK\u7684\u70b9\u79ef&#xff09;<br \/>\n        attn_scores &#061; torch.matmul(Q, K.transpose(-2, -1)) \/ math.sqrt(self.d_k)<\/p>\n<p>        # \u5e94\u7528\u63a9\u7801&#xff08;\u5982\u586b\u5145\u63a9\u7801\u6216\u672a\u6765\u4fe1\u606f\u63a9\u7801&#xff09;<br \/>\n        if mask is not None:<br \/>\n            attn_scores &#061; attn_scores.masked_fill(mask &#061;&#061; 0, -1e9)<\/p>\n<p>        # \u8ba1\u7b97\u6ce8\u610f\u529b\u6743\u91cd&#xff08;softmax\u5f52\u4e00\u5316&#xff09;<br \/>\n        attn_probs &#061; torch.softmax(attn_scores, dim&#061;-1)<\/p>\n<p>        # \u5bf9\u503c\u5411\u91cf\u52a0\u6743\u6c42\u548c<br \/>\n        output &#061; torch.matmul(attn_probs, V)<br \/>\n        return output<\/p>\n<p>    def split_heads(self, x):<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u5c06\u8f93\u5165\u5f20\u91cf\u5206\u5272\u4e3a\u591a\u4e2a\u5934<br \/>\n        \u8f93\u5165\u5f62\u72b6: (batch_size, seq_length, d_model)<br \/>\n        \u8f93\u51fa\u5f62\u72b6: (batch_size, num_heads, seq_length, d_k)<br \/>\n        &#034;&#034;&#034;<br \/>\n        batch_size, seq_length, d_model &#061; x.size()<br \/>\n        return x.view(batch_size, seq_length, self.num_heads, self.d_k).transpose(1, 2)<\/p>\n<p>    def combine_heads(self, x):<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u5c06\u591a\u4e2a\u5934\u7684\u8f93\u51fa\u5408\u5e76\u56de\u539f\u59cb\u5f62\u72b6<br \/>\n        \u8f93\u5165\u5f62\u72b6: (batch_size, num_heads, seq_length, d_k)<br \/>\n        \u8f93\u51fa\u5f62\u72b6: (batch_size, seq_length, d_model)<br \/>\n        &#034;&#034;&#034;<br \/>\n        batch_size, _, seq_length, d_k &#061; x.size()<br \/>\n        return x.transpose(1, 2).contiguous().view(batch_size, seq_length, self.d_model)<\/p>\n<p>    def forward(self, Q, K, V, mask&#061;None):<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u524d\u5411\u4f20\u64ad<br \/>\n        \u8f93\u5165\u5f62\u72b6: Q\/K\/V: (batch_size, seq_length, d_model)<br \/>\n        \u8f93\u51fa\u5f62\u72b6: (batch_size, seq_length, d_model)<br \/>\n        &#034;&#034;&#034;<br \/>\n        # \u7ebf\u6027\u53d8\u6362\u5e76\u5206\u5272\u591a\u5934<br \/>\n        Q &#061; self.split_heads(self.W_q(Q)) # (batch, heads, seq_len, d_k)<br \/>\n        K &#061; self.split_heads(self.W_k(K))<br \/>\n        V &#061; self.split_heads(self.W_v(V))<\/p>\n<p>        # \u8ba1\u7b97\u6ce8\u610f\u529b<br \/>\n        attn_output &#061; self.scaled_dot_product_attention(Q, K, V, mask)<\/p>\n<p>        # \u5408\u5e76\u591a\u5934\u5e76\u8f93\u51fa\u53d8\u6362<br \/>\n        output &#061; self.W_o(self.combine_heads(attn_output))<br \/>\n        return output <\/p>\n<ul>\n<li>\u8865\u5145\u8bf4\u660e&#xff1a;\n<ul>\n<li>\n<p>\u591a\u5934\u6ce8\u610f\u529b\u673a\u5236&#xff1a;\u5c06\u8f93\u5165\u5206\u5272\u6210\u591a\u4e2a\u5934&#xff0c;\u6bcf\u4e2a\u5934\u72ec\u7acb\u8ba1\u7b97\u6ce8\u610f\u529b&#xff0c;\u6700\u540e\u5c06\u7ed3\u679c\u5408\u5e76\u3002<\/p>\n<\/li>\n<li>\n<p>\u7f29\u653e\u70b9\u79ef\u6ce8\u610f\u529b&#xff1a;\u8ba1\u7b97\u67e5\u8be2\u548c\u952e\u7684\u70b9\u79ef&#xff0c;\u7f29\u653e\u540e\u4f7f\u7528 softmax \u8ba1\u7b97\u6ce8\u610f\u529b\u6743\u91cd&#xff0c;\u6700\u540e\u5bf9\u503c\u8fdb\u884c\u52a0\u6743\u6c42\u548c\u3002<\/p>\n<\/li>\n<li>\n<p>\u63a9\u7801&#xff1a;\u7528\u4e8e\u5c4f\u853d\u65e0\u6548\u4f4d\u7f6e&#xff08;\u5982\u586b\u5145\u90e8\u5206&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"928\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174216-69877978e6238.png\" width=\"932\" \/><\/p>\n<h4 id=\"2.2.2%E4%BD%8D%E7%BD%AE%E5%89%8D%E9%A6%88%E7%BD%91%E7%BB%9C%EF%BC%88Position-wise%20Feed-Forward%20Network%EF%BC%89\" style=\"background-color:transparent\">2.2.2\u4f4d\u7f6e\u524d\u9988\u7f51\u7edc&#xff08;Position-wise Feed-Forward Network&#xff09;<\/h4>\n<ul>\n<li>\u7531\u4e24\u4e2a\u5168\u8fde\u63a5\u5c42\u548c\u4e00\u4e2a ReLU \u6fc0\u6d3b\u51fd\u6570\u7ec4\u6210&#xff0c;\u7528\u4e8e\u8fdb\u4e00\u6b65\u5904\u7406\u6ce8\u610f\u529b\u673a\u5236\u7684\u8f93\u51fa\u3002<\/li>\n<\/ul>\n<h5 id=\"%E4%BD%8D%E7%BD%AE%E5%89%8D%E9%A6%88%E7%BD%91%E7%BB%9C%E5%85%B7%E4%BD%93%E7%A4%BA%E4%BE%8B%EF%BC%9A\">\u4f4d\u7f6e\u524d\u9988\u7f51\u7edc\u5177\u4f53\u793a\u4f8b&#xff1a;<\/h5>\n<p>class PositionWiseFeedForward(nn.Module):<br \/>\n    def __init__(self, d_model, d_ff):<br \/>\n        super(PositionWiseFeedForward, self).__init__()<br \/>\n        self.fc1 &#061; nn.Linear(d_model, d_ff)  # \u7b2c\u4e00\u5c42\u5168\u8fde\u63a5<br \/>\n        self.fc2 &#061; nn.Linear(d_ff, d_model)  # \u7b2c\u4e8c\u5c42\u5168\u8fde\u63a5<br \/>\n        self.relu &#061; nn.ReLU()  # \u6fc0\u6d3b\u51fd\u6570<\/p>\n<p>    def forward(self, x):<br \/>\n        # \u524d\u9988\u7f51\u7edc\u7684\u8ba1\u7b97<br \/>\n        return self.fc2(self.relu(self.fc1(x))) <\/p>\n<h4 id=\"2.2.3%E4%BD%8D%E7%BD%AE%E7%BC%96%E7%A0%81\" style=\"background-color:transparent\">2.2.3\u4f4d\u7f6e\u7f16\u7801<\/h4>\n<ul>\n<li>\n<p>\u4f4d\u7f6e\u7f16\u7801\u7528\u4e8e\u6ce8\u5165\u8f93\u5165\u5e8f\u5217\u4e2d\u6bcf\u4e2a token \u7684\u4f4d\u7f6e\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u4e0d\u540c\u9891\u7387\u7684\u6b63\u5f26\u548c\u4f59\u5f26\u51fd\u6570\u6765\u751f\u6210\u4f4d\u7f6e\u7f16\u7801\u3002<\/p>\n<\/li>\n<\/ul>\n<h5 id=\"%E4%BD%8D%E7%BD%AE%E7%BC%96%E7%A0%81%E5%85%B7%E4%BD%93%E7%A4%BA%E4%BE%8B%EF%BC%9A\">\u4f4d\u7f6e\u7f16\u7801\u5177\u4f53\u793a\u4f8b&#xff1a;<\/h5>\n<p>class PositionalEncoding(nn.Module):<br \/>\n    def __init__(self, d_model, max_seq_length):<br \/>\n        super(PositionalEncoding, self).__init__()<br \/>\n        pe &#061; torch.zeros(max_seq_length, d_model)  # \u521d\u59cb\u5316\u4f4d\u7f6e\u7f16\u7801\u77e9\u9635<br \/>\n        position &#061; torch.arange(0, max_seq_length, dtype&#061;torch.float).unsqueeze(1)<br \/>\n        div_term &#061; torch.exp(torch.arange(0, d_model, 2).float() * -(math.log(10000.0) \/ d_model))<br \/>\n        pe[:, 0::2] &#061; torch.sin(position * div_term)  # \u5076\u6570\u4f4d\u7f6e\u4f7f\u7528\u6b63\u5f26\u51fd\u6570<br \/>\n        pe[:, 1::2] &#061; torch.cos(position * div_term)  # \u5947\u6570\u4f4d\u7f6e\u4f7f\u7528\u4f59\u5f26\u51fd\u6570<br \/>\n        self.register_buffer(&#039;pe&#039;, pe.unsqueeze(0))  # \u6ce8\u518c\u4e3a\u7f13\u51b2\u533a<\/p>\n<p>    def forward(self, x):<br \/>\n        # \u5c06\u4f4d\u7f6e\u7f16\u7801\u6dfb\u52a0\u5230\u8f93\u5165\u4e2d<br \/>\n        return x &#043; self.pe[:, :x.size(1)] <\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174217-698779798b225.png\" \/><\/p>\n<h3 id=\"2.3%E6%9E%84%E5%BB%BA%E7%BC%96%E7%A0%81%E5%99%A8%E5%9D%97%EF%BC%88Encoder%20Layer%EF%BC%89\" style=\"background-color:transparent\">2.3\u6784\u5efa\u7f16\u7801\u5668\u5757&#xff08;Encoder Layer&#xff09;<\/h3>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"473\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174218-6987797a349e7.png\" width=\"276\" \/><\/p>\n<h4 id=\"2.3.1%E7%BC%96%E7%A0%81%E5%99%A8%E5%B1%82\" style=\"background-color:transparent\">2.3.1\u7f16\u7801\u5668\u5c42<\/h4>\n<ul>\n<li>\u5305\u542b\u4e00\u4e2a\u81ea\u6ce8\u610f\u529b\u673a\u5236\u548c\u4e00\u4e2a\u524d\u9988\u7f51\u7edc&#xff0c;\u6bcf\u4e2a\u5b50\u5c42\u540e\u63a5\u6b8b\u5dee\u8fde\u63a5\u548c\u5c42\u5f52\u4e00\u5316\u3002<\/li>\n<\/ul>\n<h5 id=\"%E7%BC%96%E7%A0%81%E5%99%A8%E5%B1%82%E5%85%B7%E4%BD%93%E7%A4%BA%E4%BE%8B%EF%BC%9A\">\u7f16\u7801\u5668\u5c42\u5177\u4f53\u793a\u4f8b&#xff1a;<\/h5>\n<p>class EncoderLayer(nn.Module):<br \/>\n    def __init__(self, d_model, num_heads, d_ff, dropout):<br \/>\n        super(EncoderLayer, self).__init__()<br \/>\n        self.self_attn &#061; MultiHeadAttention(d_model, num_heads)  # \u81ea\u6ce8\u610f\u529b\u673a\u5236<br \/>\n        self.feed_forward &#061; PositionWiseFeedForward(d_model, d_ff)  # \u524d\u9988\u7f51\u7edc<br \/>\n        self.norm1 &#061; nn.LayerNorm(d_model)  # \u5c42\u5f52\u4e00\u5316<br \/>\n        self.norm2 &#061; nn.LayerNorm(d_model)<br \/>\n        self.dropout &#061; nn.Dropout(dropout)  # Dropout<\/p>\n<p>    def forward(self, x, mask):<br \/>\n        # \u81ea\u6ce8\u610f\u529b\u673a\u5236<br \/>\n        attn_output &#061; self.self_attn(x, x, x, mask)<br \/>\n        x &#061; self.norm1(x &#043; self.dropout(attn_output))  # \u6b8b\u5dee\u8fde\u63a5\u548c\u5c42\u5f52\u4e00\u5316<\/p>\n<p>        # \u524d\u9988\u7f51\u7edc<br \/>\n        ff_output &#061; self.feed_forward(x)<br \/>\n        x &#061; self.norm2(x &#043; self.dropout(ff_output))  # \u6b8b\u5dee\u8fde\u63a5\u548c\u5c42\u5f52\u4e00\u5316<br \/>\n        return x <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"822\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174218-6987797a4aa93.png\" width=\"1447\" \/><\/p>\n<h3 id=\"2.4%E6%9E%84%E5%BB%BA%E8%A7%A3%E7%A0%81%E5%99%A8%E6%A8%A1%E5%9D%97\" style=\"background-color:transparent\">2.4\u6784\u5efa\u89e3\u7801\u5668\u6a21\u5757<\/h3>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"749\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174219-6987797b5335b.png\" width=\"276\" \/><\/p>\n<h4 id=\"2.4.1%E8%A7%A3%E7%A0%81%E5%99%A8%E5%B1%82\" style=\"background-color:transparent\">2.4.1\u89e3\u7801\u5668\u5c42<\/h4>\n<ul>\n<li>\u5305\u542b\u4e00\u4e2a\u81ea\u6ce8\u610f\u529b\u673a\u5236\u3001\u4e00\u4e2a\u4ea4\u53c9\u6ce8\u610f\u529b\u673a\u5236\u548c\u4e00\u4e2a\u524d\u9988\u7f51\u7edc&#xff0c;\u6bcf\u4e2a\u5b50\u5c42\u540e\u63a5\u6b8b\u5dee\u8fde\u63a5\u548c\u5c42\u5f52\u4e00\u5316\u3002<\/li>\n<\/ul>\n<h5 id=\"%E8%A7%A3%E7%A0%81%E5%99%A8%E5%B1%82%E5%85%B7%E4%BD%93%E7%A4%BA%E4%BE%8B%EF%BC%9A\">\u89e3\u7801\u5668\u5c42\u5177\u4f53\u793a\u4f8b&#xff1a;<\/h5>\n<p>class DecoderLayer(nn.Module):<br \/>\n    def __init__(self, d_model, num_heads, d_ff, dropout):<br \/>\n        super(DecoderLayer, self).__init__()<br \/>\n        self.self_attn &#061; MultiHeadAttention(d_model, num_heads)  # \u81ea\u6ce8\u610f\u529b\u673a\u5236<br \/>\n        self.cross_attn &#061; MultiHeadAttention(d_model, num_heads)  # \u4ea4\u53c9\u6ce8\u610f\u529b\u673a\u5236<br \/>\n        self.feed_forward &#061; PositionWiseFeedForward(d_model, d_ff)  # \u524d\u9988\u7f51\u7edc<br \/>\n        self.norm1 &#061; nn.LayerNorm(d_model)  # \u5c42\u5f52\u4e00\u5316<br \/>\n        self.norm2 &#061; nn.LayerNorm(d_model)<br \/>\n        self.norm3 &#061; nn.LayerNorm(d_model)<br \/>\n        self.dropout &#061; nn.Dropout(dropout)  # Dropout<\/p>\n<p>    def forward(self, x, enc_output, src_mask, tgt_mask):<br \/>\n        # \u81ea\u6ce8\u610f\u529b\u673a\u5236<br \/>\n        attn_output &#061; self.self_attn(x, x, x, tgt_mask)<br \/>\n        x &#061; self.norm1(x &#043; self.dropout(attn_output))  # \u6b8b\u5dee\u8fde\u63a5\u548c\u5c42\u5f52\u4e00\u5316<\/p>\n<p>        # \u4ea4\u53c9\u6ce8\u610f\u529b\u673a\u5236<br \/>\n        attn_output &#061; self.cross_attn(x, enc_output, enc_output, src_mask)<br \/>\n        x &#061; self.norm2(x &#043; self.dropout(attn_output))  # \u6b8b\u5dee\u8fde\u63a5\u548c\u5c42\u5f52\u4e00\u5316<\/p>\n<p>        # \u524d\u9988\u7f51\u7edc<br \/>\n        ff_output &#061; self.feed_forward(x)<br \/>\n        x &#061; self.norm3(x &#043; self.dropout(ff_output))  # \u6b8b\u5dee\u8fde\u63a5\u548c\u5c42\u5f52\u4e00\u5316<br \/>\n        return x <\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174219-6987797b6ab01.png\" \/><\/p>\n<h2 id=\"3.%E5%A6%82%E4%BD%95%E6%9E%84%E5%BB%BA%E5%AE%8C%E6%95%B4%E7%9A%84%20Transformer%20%E6%A8%A1%E5%9E%8B\">3.\u5982\u4f55\u6784\u5efa\u5b8c\u6574\u7684 Transformer \u6a21\u578b<\/h2>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"739\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174220-6987797c16073.png\" width=\"545\" \/><\/p>\n<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"808\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174220-6987797c37224.png\" width=\"1121\" \/><\/p>\n<h3 id=\"3.1%E5%A6%82%E4%BD%95%E6%9E%84%E5%BB%BA%E5%AE%8C%E6%95%B4%E7%9A%84%20Transformer%20%E6%A8%A1%E5%9E%8B%E5%85%B7%E4%BD%93%E7%A4%BA%E4%BE%8B%EF%BC%9A\">3.1\u5982\u4f55\u6784\u5efa\u5b8c\u6574\u7684 Transformer \u6a21\u578b\u5177\u4f53\u793a\u4f8b&#xff1a;<\/h3>\n<p>class Transformer(nn.Module):<br \/>\n    def __init__(self, src_vocab_size, tgt_vocab_size, d_model, num_heads, num_layers, d_ff, max_seq_length, dropout):<br \/>\n        super(Transformer, self).__init__()<br \/>\n        self.encoder_embedding &#061; nn.Embedding(src_vocab_size, d_model)  # \u7f16\u7801\u5668\u8bcd\u5d4c\u5165<br \/>\n        self.decoder_embedding &#061; nn.Embedding(tgt_vocab_size, d_model)  # \u89e3\u7801\u5668\u8bcd\u5d4c\u5165<br \/>\n        self.positional_encoding &#061; PositionalEncoding(d_model, max_seq_length)  # \u4f4d\u7f6e\u7f16\u7801<\/p>\n<p>        # \u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\u5c42<br \/>\n        self.encoder_layers &#061; nn.ModuleList([EncoderLayer(d_model, num_heads, d_ff, dropout) for _ in range(num_layers)])<br \/>\n        self.decoder_layers &#061; nn.ModuleList([DecoderLayer(d_model, num_heads, d_ff, dropout) for _ in range(num_layers)])<\/p>\n<p>        self.fc &#061; nn.Linear(d_model, tgt_vocab_size)  # \u6700\u7ec8\u7684\u5168\u8fde\u63a5\u5c42<br \/>\n        self.dropout &#061; nn.Dropout(dropout)  # Dropout<\/p>\n<p>    def generate_mask(self, src, tgt):<br \/>\n        # \u6e90\u63a9\u7801&#xff1a;\u5c4f\u853d\u586b\u5145\u7b26&#xff08;\u5047\u8bbe\u586b\u5145\u7b26\u7d22\u5f15\u4e3a0&#xff09;<br \/>\n        # \u5f62\u72b6&#xff1a;(batch_size, 1, 1, seq_length)<br \/>\n        src_mask &#061; (src !&#061; 0).unsqueeze(1).unsqueeze(2)<\/p>\n<p>        # \u76ee\u6807\u63a9\u7801&#xff1a;\u5c4f\u853d\u586b\u5145\u7b26\u548c\u672a\u6765\u4fe1\u606f<br \/>\n        # \u5f62\u72b6&#xff1a;(batch_size, 1, seq_length, 1)<br \/>\n        tgt_mask &#061; (tgt !&#061; 0).unsqueeze(1).unsqueeze(3)<br \/>\n        seq_length &#061; tgt.size(1)<br \/>\n        # \u751f\u6210\u4e0a\u4e09\u89d2\u77e9\u9635\u63a9\u7801&#xff0c;\u9632\u6b62\u89e3\u7801\u65f6\u770b\u5230\u672a\u6765\u4fe1\u606f<br \/>\n        nopeak_mask &#061; (1 &#8211; torch.triu(torch.ones(1, seq_length, seq_length), diagonal&#061;1)).bool()<br \/>\n        tgt_mask &#061; tgt_mask &amp; nopeak_mask  # \u5408\u5e76\u586b\u5145\u63a9\u7801\u548c\u672a\u6765\u4fe1\u606f\u63a9\u7801<br \/>\n        return src_mask, tgt_mask<\/p>\n<p>    def forward(self, src, tgt):<br \/>\n        # \u751f\u6210\u63a9\u7801<br \/>\n        src_mask, tgt_mask &#061; self.generate_mask(src, tgt)<\/p>\n<p>        # \u7f16\u7801\u5668\u90e8\u5206<br \/>\n        src_embedded &#061; self.dropout(self.positional_encoding(self.encoder_embedding(src)))<br \/>\n        enc_output &#061; src_embedded<br \/>\n        for enc_layer in self.encoder_layers:<br \/>\n            enc_output &#061; enc_layer(enc_output, src_mask)<\/p>\n<p>        # \u89e3\u7801\u5668\u90e8\u5206<br \/>\n        tgt_embedded &#061; self.dropout(self.positional_encoding(self.decoder_embedding(tgt)))<br \/>\n        dec_output &#061; tgt_embedded<br \/>\n        for dec_layer in self.decoder_layers:<br \/>\n            dec_output &#061; dec_layer(dec_output, enc_output, src_mask, tgt_mask)<\/p>\n<p>        # \u6700\u7ec8\u8f93\u51fa<br \/>\n        output &#061; self.fc(dec_output)<br \/>\n        return output <\/p>\n<ul>\n<li>\u8bf4\u660e&#xff1a;\n<ul>\n<li>\n<p>Transformer \u6a21\u578b&#xff1a;\u5305\u542b\u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\u90e8\u5206&#xff0c;\u6bcf\u4e2a\u90e8\u5206\u7531\u591a\u4e2a\u5c42\u5806\u53e0\u800c\u6210\u3002<\/p>\n<\/li>\n<li>\n<p>\u63a9\u7801\u751f\u6210&#xff1a;\u7528\u4e8e\u5c4f\u853d\u65e0\u6548\u4f4d\u7f6e\u548c\u672a\u6765\u4fe1\u606f\u3002<\/p>\n<\/li>\n<li>\n<p>\u524d\u5411\u4f20\u64ad&#xff1a;\u4f9d\u6b21\u901a\u8fc7\u7f16\u7801\u5668\u548c\u89e3\u7801\u5668&#xff0c;\u6700\u540e\u901a\u8fc7\u5168\u8fde\u63a5\u5c42\u8f93\u51fa\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3 id=\"3.2%E6%A8%A1%E5%9E%8B%E5%88%9D%E5%A7%8B%E5%8C%96%E5%8F%82%E6%95%B0%E8%AF%B4%E6%98%8E%EF%BC%9A\">3.2\u6a21\u578b\u521d\u59cb\u5316\u53c2\u6570\u8bf4\u660e&#xff1a;<\/h3>\n<p>class Transformer(nn.Module):<br \/>\n    def __init__(<br \/>\n        self,<br \/>\n        src_vocab_size,  # \u6e90\u8bed\u8a00\u8bcd\u6c47\u8868\u5927\u5c0f&#xff08;\u5982\u82f1\u6587\u5355\u8bcd\u6570&#xff09;<br \/>\n        tgt_vocab_size,  # \u76ee\u6807\u8bed\u8a00\u8bcd\u6c47\u8868\u5927\u5c0f&#xff08;\u5982\u4e2d\u6587\u5355\u8bcd\u6570&#xff09;<br \/>\n        d_model&#061;512,     # \u6a21\u578b\u7ef4\u5ea6&#xff08;\u6bcf\u4e2a\u8bcd\u5411\u91cf\u7684\u957f\u5ea6&#xff09;<br \/>\n        num_heads&#061;8,     # \u591a\u5934\u6ce8\u610f\u529b\u7684\u5934\u6570<br \/>\n        num_layers&#061;6,    # \u7f16\u7801\u5668\/\u89e3\u7801\u5668\u7684\u5806\u53e0\u5c42\u6570<br \/>\n        d_ff&#061;2048,       # \u524d\u9988\u7f51\u7edc\u9690\u85cf\u5c42\u7ef4\u5ea6<br \/>\n        max_seq_length&#061;100, # \u6700\u5927\u5e8f\u5217\u957f\u5ea6&#xff08;\u7528\u4e8e\u4f4d\u7f6e\u7f16\u7801&#xff09;<br \/>\n        dropout&#061;0.1      # Dropout\u6982\u7387<br \/>\n    ): <\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174221-6987797da5068.png\" \/><\/p>\n<h2 id=\"4.%E8%AE%AD%E7%BB%83%20PyTorch%20Transformer%20%E6%A8%A1%E5%9E%8B\" style=\"background-color:transparent\">4.\u8bad\u7ec3 PyTorch Transformer \u6a21\u578b<\/h2>\n<ul>\n<li>\u4f7f\u7528\u968f\u673a\u6570\u636e\u8bad\u7ec3\u6a21\u578b&#xff0c;\u8ba1\u7b97\u635f\u5931\u5e76\u66f4\u65b0\u53c2\u6570\u3002<\/li>\n<\/ul>\n<h3 id=\"4.1%E8%AE%AD%E7%BB%83%20PyTorch%20Transformer%20%E6%A8%A1%E5%9E%8B%E7%A4%BA%E4%BE%8B%EF%BC%9A\">4.1\u8bad\u7ec3 PyTorch Transformer \u6a21\u578b\u793a\u4f8b&#xff1a;<\/h3>\n<p># \u8d85\u53c2\u6570<br \/>\nsrc_vocab_size &#061; 5000  # \u6e90\u8bcd\u6c47\u8868\u5927\u5c0f<br \/>\ntgt_vocab_size &#061; 5000  # \u76ee\u6807\u8bcd\u6c47\u8868\u5927\u5c0f<br \/>\nd_model &#061; 512  # \u6a21\u578b\u7ef4\u5ea6<br \/>\nnum_heads &#061; 8  # \u6ce8\u610f\u529b\u5934\u6570\u91cf<br \/>\nnum_layers &#061; 6  # \u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\u5c42\u6570<br \/>\nd_ff &#061; 2048  # \u524d\u9988\u7f51\u7edc\u5185\u5c42\u7ef4\u5ea6<br \/>\nmax_seq_length &#061; 100  # \u6700\u5927\u5e8f\u5217\u957f\u5ea6<br \/>\ndropout &#061; 0.1  # Dropout \u6982\u7387<\/p>\n<p># \u521d\u59cb\u5316\u6a21\u578b<br \/>\ntransformer &#061; Transformer(src_vocab_size, tgt_vocab_size, d_model, num_heads, num_layers, d_ff, max_seq_length, dropout)<\/p>\n<p># \u751f\u6210\u968f\u673a\u6570\u636e<br \/>\nsrc_data &#061; torch.randint(1, src_vocab_size, (64, max_seq_length))  # \u6e90\u5e8f\u5217<br \/>\ntgt_data &#061; torch.randint(1, tgt_vocab_size, (64, max_seq_length))  # \u76ee\u6807\u5e8f\u5217<\/p>\n<p># \u5b9a\u4e49\u635f\u5931\u51fd\u6570\u548c\u4f18\u5316\u5668<br \/>\ncriterion &#061; nn.CrossEntropyLoss(ignore_index&#061;0)  # \u5ffd\u7565\u586b\u5145\u90e8\u5206\u7684\u635f\u5931<br \/>\noptimizer &#061; optim.Adam(transformer.parameters(), lr&#061;0.0001, betas&#061;(0.9, 0.98), eps&#061;1e-9)<\/p>\n<p># \u8bad\u7ec3\u5faa\u73af<br \/>\ntransformer.train()<br \/>\nfor epoch in range(100):<br \/>\n    optimizer.zero_grad()  # \u6e05\u7a7a\u68af\u5ea6&#xff0c;\u9632\u6b62\u7d2f\u79ef<\/p>\n<p>    # \u8f93\u5165\u76ee\u6807\u5e8f\u5217\u65f6\u53bb\u6389\u6700\u540e\u4e00\u4e2a\u8bcd&#xff08;\u7528\u4e8e\u9884\u6d4b\u4e0b\u4e00\u4e2a\u8bcd&#xff09;<br \/>\n    output &#061; transformer(src_data, tgt_data[:, :-1])  <\/p>\n<p>    # \u8ba1\u7b97\u635f\u5931\u65f6&#xff0c;\u76ee\u6807\u5e8f\u5217\u4ece\u7b2c\u4e8c\u4e2a\u8bcd\u5f00\u59cb&#xff08;\u5373\u9884\u6d4b\u4e0b\u4e00\u4e2a\u8bcd&#xff09;<br \/>\n    # output\u5f62\u72b6: (batch_size, seq_length-1, tgt_vocab_size)<br \/>\n    # \u76ee\u6807\u5f62\u72b6: (batch_size, seq_length-1)<br \/>\n    loss &#061; criterion(<br \/>\n        output.contiguous().view(-1, tgt_vocab_size),<br \/>\n        tgt_data[:, 1:].contiguous().view(-1)<br \/>\n    )<\/p>\n<p>    loss.backward()        # \u53cd\u5411\u4f20\u64ad<br \/>\n    optimizer.step()       # \u66f4\u65b0\u53c2\u6570<br \/>\n    print(f&#034;Epoch: {epoch&#043;1}, Loss: {loss.item()}&#034;) <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"909\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174222-6987797e5f462.png\" width=\"1148\" \/><\/p>\n<h2 id=\"5.%E6%A8%A1%E5%9E%8B%E8%AF%84%E4%BC%B0%E6%80%A7%E8%83%BD\" style=\"background-color:transparent\">5.\u6a21\u578b\u8bc4\u4f30\u6027\u80fd<\/h2>\n<ul>\n<li>\u5728\u9a8c\u8bc1\u6570\u636e\u4e0a\u8ba1\u7b97\u635f\u5931&#xff0c;\u8bc4\u4f30\u6a21\u578b\u6027\u80fd<\/li>\n<\/ul>\n<h3 id=\"5.1%E6%A8%A1%E5%9E%8B%E8%AF%84%E4%BC%B0%E6%80%A7%E8%83%BD%E5%85%B7%E4%BD%93%E7%A4%BA%E4%BE%8B%EF%BC%9A\">5.1\u6a21\u578b\u8bc4\u4f30\u6027\u80fd\u5177\u4f53\u793a\u4f8b&#xff1a;<\/h3>\n<p>transformer.eval()<br \/>\n# \u751f\u6210\u9a8c\u8bc1\u6570\u636e<br \/>\nval_src_data &#061; torch.randint(1, src_vocab_size, (64, max_seq_length))<br \/>\nval_tgt_data &#061; torch.randint(1, tgt_vocab_size, (64, max_seq_length))<br \/>\n# \u5047\u8bbe\u8f93\u5165\u4e3a\u4e00\u6279\u82f1\u6587\u548c\u5bf9\u5e94\u7684\u4e2d\u6587\u7ffb\u8bd1&#xff08;\u5df2\u8f6c\u6362\u4e3a\u7d22\u5f15&#xff09;<br \/>\n# \u793a\u4f8b\u6570\u636e&#xff1a;<br \/>\n# src_data: [[3, 14, 25, &#8230;, 0, 0], &#8230;]  # \u82f1\u6587\u53e5\u5b50&#xff08;0\u4e3a\u586b\u5145\u7b26&#xff09;<br \/>\n# tgt_data: [[5, 20, 36, &#8230;, 0, 0], &#8230;]  # \u4e2d\u6587\u7ffb\u8bd1&#xff08;0\u4e3a\u586b\u5145\u7b26&#xff09;<br \/>\n# \u6ce8\u610f&#xff1a;\u5b9e\u9645\u5e94\u7528\u4e2d\u9700\u5bf9\u6587\u672c\u8fdb\u884c\u5206\u8bcd\u3001\u7f16\u7801\u3001\u586b\u5145\u7b49\u9884\u5904\u7406<br \/>\nwith torch.no_grad():<br \/>\n    val_output &#061; transformer(val_src_data, val_tgt_data[:, :-1])<br \/>\n    val_loss &#061; criterion(val_output.contiguous().view(-1, tgt_vocab_size), val_tgt_data[:, 1:].contiguous().view(-1))<br \/>\n    print(f&#034;Validation Loss: {val_loss.item()}&#034;) <\/p>\n<h2 id=\"6.PyTorch%20%E6%9E%84%E5%BB%BA%2B%E8%AF%84%E4%BC%B0%C2%A0Transformer%20%E6%A8%A1%E5%9E%8B%E4%BB%A3%E7%A0%81%E4%BC%98%E5%8C%96%E6%B1%87%E6%80%BB\">6.PyTorch \u6784\u5efa&#043;\u8bc4\u4f30\u00a0Transformer \u6a21\u578b\u4ee3\u7801\u4f18\u5316\u6c47\u603b<\/h2>\n<p>import torch<br \/>\nimport torch.nn as nn<br \/>\nimport torch.optim as optim<br \/>\nimport math<\/p>\n<p>class PositionalEncoding(nn.Module):<br \/>\n    &#034;&#034;&#034;\u4f4d\u7f6e\u7f16\u7801\u5c42&#034;&#034;&#034;<br \/>\n    def __init__(self, d_model, max_seq_length&#061;100):<br \/>\n        super(PositionalEncoding, self).__init__()<\/p>\n<p>        pe &#061; torch.zeros(max_seq_length, d_model)<br \/>\n        position &#061; torch.arange(0, max_seq_length, dtype&#061;torch.float).unsqueeze(1)<br \/>\n        div_term &#061; torch.exp(torch.arange(0, d_model, 2).float() *<br \/>\n                            (-math.log(10000.0) \/ d_model))<\/p>\n<p>        pe[:, 0::2] &#061; torch.sin(position * div_term)<br \/>\n        pe[:, 1::2] &#061; torch.cos(position * div_term)<\/p>\n<p>        pe &#061; pe.unsqueeze(0)  # [1, max_seq_length, d_model]<br \/>\n        self.register_buffer(&#039;pe&#039;, pe)<\/p>\n<p>    def forward(self, x):<br \/>\n        # x: [batch_size, seq_length, d_model]<br \/>\n        return x &#043; self.pe[:, :x.size(1), :]<\/p>\n<p>class PositionwiseFeedForward(nn.Module):<br \/>\n    &#034;&#034;&#034;\u4f4d\u7f6e\u524d\u9988\u7f51\u7edc&#034;&#034;&#034;<br \/>\n    def __init__(self, d_model, d_ff):<br \/>\n        super(PositionwiseFeedForward, self).__init__()<br \/>\n        self.fc1 &#061; nn.Linear(d_model, d_ff)  # \u7b2c\u4e00\u5c42\u5168\u8fde\u63a5<br \/>\n        self.fc2 &#061; nn.Linear(d_ff, d_model)  # \u7b2c\u4e8c\u5c42\u5168\u8fde\u63a5<br \/>\n        self.relu &#061; nn.ReLU()  # \u6fc0\u6d3b\u51fd\u6570<\/p>\n<p>    def forward(self, x):<br \/>\n        # \u524d\u9988\u7f51\u7edc\u7684\u8ba1\u7b97<br \/>\n        return self.fc2(self.relu(self.fc1(x)))<\/p>\n<p>class MultiHeadAttention(nn.Module):<br \/>\n    def __init__(self, model_dim, n_heads):<br \/>\n        super(MultiHeadAttention, self).__init__()<br \/>\n        assert model_dim % n_heads &#061;&#061; 0, &#034;d_model\u5fc5\u987b\u80fd\u88abnum_heads\u6574\u9664&#034;<\/p>\n<p>        self.model_dim &#061; model_dim  # \u6a21\u578b\u7ef4\u5ea6&#xff08;\u5982512&#xff09;<br \/>\n        self.n_heads &#061; n_heads  # \u6ce8\u610f\u529b\u5934\u6570&#xff08;\u59828&#xff09;<br \/>\n        self.d_k &#061; model_dim \/\/ n_heads  # \u6bcf\u4e2a\u5934\u7684\u7ef4\u5ea6&#xff08;\u598264&#xff09;<\/p>\n<p>        # \u5b9a\u4e49\u7ebf\u6027\u53d8\u6362\u5c42&#xff08;\u65e0\u9700\u504f\u7f6e&#xff09;<br \/>\n        self.w_q &#061; nn.Linear(model_dim, model_dim)  # \u67e5\u8be2\u53d8\u6362<br \/>\n        self.w_k &#061; nn.Linear(model_dim, model_dim)  # \u952e\u53d8\u6362<br \/>\n        self.w_v &#061; nn.Linear(model_dim, model_dim)  # \u503c\u53d8\u6362<br \/>\n        self.w_o &#061; nn.Linear(model_dim, model_dim)  # \u8f93\u51fa\u53d8\u6362<\/p>\n<p>    def scaled_dot_product_attention(self, query, key, value, mask&#061;None):<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u8ba1\u7b97\u7f29\u653e\u70b9\u79ef\u6ce8\u610f\u529b<br \/>\n        \u8f93\u5165\u5f62\u72b6&#xff1a;<br \/>\n            query: (batch_size, num_heads, seq_length, d_k)<br \/>\n            key, value: \u540cquery<br \/>\n        \u8f93\u51fa\u5f62\u72b6&#xff1a; (batch_size, num_heads, seq_length, d_k)<br \/>\n        &#034;&#034;&#034;<br \/>\n        # \u8ba1\u7b97\u6ce8\u610f\u529b\u5206\u6570&#xff08;query\u548ckey\u7684\u70b9\u79ef&#xff09;<br \/>\n        attention_scores &#061; torch.matmul(query, key.transpose(-2, -1)) \/ math.sqrt(self.d_k)<\/p>\n<p>        # \u5e94\u7528\u63a9\u7801&#xff08;\u5982\u586b\u5145\u63a9\u7801\u6216\u672a\u6765\u4fe1\u606f\u63a9\u7801&#xff09;<br \/>\n        if mask is not None:<br \/>\n            attention_scores &#061; attention_scores.masked_fill(mask &#061;&#061; 0, -1e9)<\/p>\n<p>        # \u8ba1\u7b97\u6ce8\u610f\u529b\u6743\u91cd&#xff08;softmax\u5f52\u4e00\u5316&#xff09;<br \/>\n        attention_probs &#061; torch.softmax(attention_scores, dim&#061;-1)<\/p>\n<p>        # \u5bf9\u503c\u5411\u91cf\u52a0\u6743\u6c42\u548c<br \/>\n        output &#061; torch.matmul(attention_probs, value)<br \/>\n        return output<\/p>\n<p>    def split_heads(self, x):<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u5c06\u8f93\u5165\u5f20\u91cf\u5206\u5272\u4e3a\u591a\u4e2a\u5934<br \/>\n        \u8f93\u5165\u5f62\u72b6: (batch_size, seq_length, model_dim)<br \/>\n        \u8f93\u51fa\u5f62\u72b6: (batch_size, num_heads, seq_length, d_k)<br \/>\n        &#034;&#034;&#034;<br \/>\n        batch_size, seq_length, model_dim &#061; x.size()<br \/>\n        return x.view(batch_size, seq_length, self.n_heads, self.d_k).transpose(1, 2)<\/p>\n<p>    def combine_heads(self, x):<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u5c06\u591a\u4e2a\u5934\u7684\u8f93\u51fa\u5408\u5e76\u56de\u539f\u59cb\u5f62\u72b6<br \/>\n        \u8f93\u5165\u5f62\u72b6: (batch_size, num_heads, seq_length, d_k)<br \/>\n        \u8f93\u51fa\u5f62\u72b6: (batch_size, seq_length, model_dim)<br \/>\n        &#034;&#034;&#034;<br \/>\n        batch_size, _, seq_length, d_k &#061; x.size()<br \/>\n        return x.transpose(1, 2).contiguous().view(batch_size, seq_length, self.model_dim)<\/p>\n<p>    def forward(self, query, key, value, mask&#061;None):<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u524d\u5411\u4f20\u64ad<br \/>\n        \u8f93\u5165\u5f62\u72b6: query\/key\/value: (batch_size, seq_length, model_dim)<br \/>\n        \u8f93\u51fa\u5f62\u72b6: (batch_size, seq_length, model_dim)<br \/>\n        &#034;&#034;&#034;<br \/>\n        # \u7ebf\u6027\u53d8\u6362\u5e76\u5206\u5272\u591a\u5934<br \/>\n        query &#061; self.split_heads(self.w_q(query))  # (batch, heads, seq_len, d_k)<br \/>\n        key &#061; self.split_heads(self.w_k(key))<br \/>\n        value &#061; self.split_heads(self.w_v(value))<\/p>\n<p>        # \u8ba1\u7b97\u6ce8\u610f\u529b<br \/>\n        attention_output &#061; self.scaled_dot_product_attention(query, key, value, mask)<\/p>\n<p>        # \u5408\u5e76\u591a\u5934\u5e76\u8f93\u51fa\u53d8\u6362<br \/>\n        output &#061; self.w_o(self.combine_heads(attention_output))<br \/>\n        return output<\/p>\n<p>class EncoderLayer(nn.Module):<br \/>\n    def __init__(self, model_dim, n_heads, ff_dim, dropout_rate):<br \/>\n        super(EncoderLayer, self).__init__()<br \/>\n        self.self_attention &#061; MultiHeadAttention(model_dim, n_heads)  # \u81ea\u6ce8\u610f\u529b\u673a\u5236<br \/>\n        self.feed_forward &#061; PositionwiseFeedForward(model_dim, ff_dim)  # \u524d\u9988\u7f51\u7edc<br \/>\n        self.norm1 &#061; nn.LayerNorm(model_dim)  # \u5c42\u5f52\u4e00\u5316<br \/>\n        self.norm2 &#061; nn.LayerNorm(model_dim)<br \/>\n        self.dropout &#061; nn.Dropout(dropout_rate)  # Dropout<\/p>\n<p>    def forward(self, x, mask):<br \/>\n        # \u81ea\u6ce8\u610f\u529b\u673a\u5236<br \/>\n        attention_output &#061; self.self_attention(x, x, x, mask)<br \/>\n        x &#061; self.norm1(x &#043; self.dropout(attention_output))  # \u6b8b\u5dee\u8fde\u63a5\u548c\u5c42\u5f52\u4e00\u5316<\/p>\n<p>        # \u524d\u9988\u7f51\u7edc<br \/>\n        ff_output &#061; self.feed_forward(x)<br \/>\n        x &#061; self.norm2(x &#043; self.dropout(ff_output))  # \u6b8b\u5dee\u8fde\u63a5\u548c\u5c42\u5f52\u4e00\u5316<br \/>\n        return x<\/p>\n<p>class DecoderLayer(nn.Module):<br \/>\n    def __init__(self, model_dim, n_heads, ff_dim, dropout_rate):<br \/>\n        super(DecoderLayer, self).__init__()<br \/>\n        self.self_attention &#061; MultiHeadAttention(model_dim, n_heads)  # \u81ea\u6ce8\u610f\u529b\u673a\u5236<br \/>\n        self.cross_attention &#061; MultiHeadAttention(model_dim, n_heads)  # \u4ea4\u53c9\u6ce8\u610f\u529b\u673a\u5236<br \/>\n        self.feed_forward &#061; PositionwiseFeedForward(model_dim, ff_dim)  # \u524d\u9988\u7f51\u7edc<br \/>\n        self.norm1 &#061; nn.LayerNorm(model_dim)  # \u5c42\u5f52\u4e00\u5316<br \/>\n        self.norm2 &#061; nn.LayerNorm(model_dim)<br \/>\n        self.norm3 &#061; nn.LayerNorm(model_dim)<br \/>\n        self.dropout &#061; nn.Dropout(dropout_rate)  # Dropout<\/p>\n<p>    def forward(self, x, encoder_output, source_mask, target_mask):<br \/>\n        # \u81ea\u6ce8\u610f\u529b\u673a\u5236<br \/>\n        attention_output &#061; self.self_attention(x, x, x, target_mask)<br \/>\n        x &#061; self.norm1(x &#043; self.dropout(attention_output))  # \u6b8b\u5dee\u8fde\u63a5\u548c\u5c42\u5f52\u4e00\u5316<\/p>\n<p>        # \u4ea4\u53c9\u6ce8\u610f\u529b\u673a\u5236<br \/>\n        cross_attention_output &#061; self.cross_attention(x, encoder_output, encoder_output, source_mask)<br \/>\n        x &#061; self.norm2(x &#043; self.dropout(cross_attention_output))  # \u6b8b\u5dee\u8fde\u63a5\u548c\u5c42\u5f52\u4e00\u5316<\/p>\n<p>        # \u524d\u9988\u7f51\u7edc<br \/>\n        ff_output &#061; self.feed_forward(x)<br \/>\n        x &#061; self.norm3(x &#043; self.dropout(ff_output))  # \u6b8b\u5dee\u8fde\u63a5\u548c\u5c42\u5f52\u4e00\u5316<br \/>\n        return x<\/p>\n<p>class Transformer(nn.Module):<br \/>\n    def __init__(self, src_vocab_size, tgt_vocab_size, model_dim, n_heads, n_layers, ff_dim, max_seq_length, dropout_rate):<br \/>\n        super(Transformer, self).__init__()<br \/>\n        self.encoder_embedding &#061; nn.Embedding(src_vocab_size, model_dim)  # \u7f16\u7801\u5668\u8bcd\u5d4c\u5165<br \/>\n        self.decoder_embedding &#061; nn.Embedding(tgt_vocab_size, model_dim)  # \u89e3\u7801\u5668\u8bcd\u5d4c\u5165<br \/>\n        self.positional_encoding &#061; PositionalEncoding(model_dim, max_seq_length)  # \u4f4d\u7f6e\u7f16\u7801<\/p>\n<p>        # \u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\u5c42<br \/>\n        self.encoder_layers &#061; nn.ModuleList(<br \/>\n            [EncoderLayer(model_dim, n_heads, ff_dim, dropout_rate) for _ in range(n_layers)])<br \/>\n        self.decoder_layers &#061; nn.ModuleList(<br \/>\n            [DecoderLayer(model_dim, n_heads, ff_dim, dropout_rate) for _ in range(n_layers)])<\/p>\n<p>        self.fc &#061; nn.Linear(model_dim, tgt_vocab_size)  # \u6700\u7ec8\u7684\u5168\u8fde\u63a5\u5c42<br \/>\n        self.dropout &#061; nn.Dropout(dropout_rate)  # Dropout<\/p>\n<p>    &#064;staticmethod<br \/>\n    def generate_mask(source, target):<br \/>\n        # \u6e90\u63a9\u7801&#xff1a;\u5c4f\u853d\u586b\u5145\u7b26&#xff08;\u5047\u8bbe\u586b\u5145\u7b26\u7d22\u5f15\u4e3a0&#xff09;<br \/>\n        # \u5f62\u72b6&#xff1a;(batch_size, 1, 1, seq_length)<br \/>\n        source_mask &#061; (source !&#061; 0).unsqueeze(1).unsqueeze(2)<\/p>\n<p>        # \u76ee\u6807\u63a9\u7801&#xff1a;\u5c4f\u853d\u586b\u5145\u7b26\u548c\u672a\u6765\u4fe1\u606f<br \/>\n        # \u5f62\u72b6&#xff1a;(batch_size, 1, seq_length, 1)<br \/>\n        target_mask &#061; (target !&#061; 0).unsqueeze(1).unsqueeze(3)<br \/>\n        seq_length &#061; target.size(1)<br \/>\n        # \u751f\u6210\u4e0a\u4e09\u89d2\u77e9\u9635\u63a9\u7801&#xff0c;\u9632\u6b62\u89e3\u7801\u65f6\u770b\u5230\u672a\u6765\u4fe1\u606f<br \/>\n        no_peak_mask &#061; (1 &#8211; torch.triu(torch.ones(1, seq_length, seq_length), diagonal&#061;1)).bool()<br \/>\n        target_mask &#061; target_mask &amp; no_peak_mask  # \u5408\u5e76\u586b\u5145\u63a9\u7801\u548c\u672a\u6765\u4fe1\u606f\u63a9\u7801<br \/>\n        return source_mask, target_mask<\/p>\n<p>    def forward(self, source, target):<br \/>\n        # \u751f\u6210\u63a9\u7801<br \/>\n        source_mask, target_mask &#061; self.generate_mask(source, target)<\/p>\n<p>        # \u7f16\u7801\u5668\u90e8\u5206<br \/>\n        source_embedded &#061; self.dropout(self.positional_encoding(self.encoder_embedding(source)))<br \/>\n        encoder_output &#061; source_embedded<br \/>\n        for encoder_layer in self.encoder_layers:<br \/>\n            encoder_output &#061; encoder_layer(encoder_output, source_mask)<\/p>\n<p>        # \u89e3\u7801\u5668\u90e8\u5206<br \/>\n        target_embedded &#061; self.dropout(self.positional_encoding(self.decoder_embedding(target)))<br \/>\n        decoder_output &#061; target_embedded<br \/>\n        for decoder_layer in self.decoder_layers:<br \/>\n            decoder_output &#061; decoder_layer(decoder_output, encoder_output, source_mask, target_mask)<\/p>\n<p>        # \u6700\u7ec8\u8f93\u51fa<br \/>\n        output &#061; self.fc(decoder_output)<br \/>\n        return output<\/p>\n<p># \u8d85\u53c2\u6570<br \/>\nsrc_vocab_size &#061; 5000  # \u6e90\u8bcd\u6c47\u8868\u5927\u5c0f<br \/>\ntgt_vocab_size &#061; 5000  # \u76ee\u6807\u8bcd\u6c47\u8868\u5927\u5c0f<br \/>\nmodel_dim &#061; 512  # \u6a21\u578b\u7ef4\u5ea6<br \/>\nn_heads &#061; 8  # \u6ce8\u610f\u529b\u5934\u6570\u91cf<br \/>\nn_layers &#061; 6  # \u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\u5c42\u6570<br \/>\nff_dim &#061; 2048  # \u524d\u9988\u7f51\u7edc\u5185\u5c42\u7ef4\u5ea6<br \/>\nmax_seq_length &#061; 100  # \u6700\u5927\u5e8f\u5217\u957f\u5ea6<br \/>\ndropout_rate &#061; 0.1  # Dropout \u6982\u7387<\/p>\n<p># \u521d\u59cb\u5316\u6a21\u578b<br \/>\ntransformer &#061; Transformer(src_vocab_size, tgt_vocab_size, model_dim, n_heads,<br \/>\n                         n_layers, ff_dim, max_seq_length, dropout_rate)<\/p>\n<p># \u751f\u6210\u968f\u673a\u6570\u636e<br \/>\nsrc_data &#061; torch.randint(1, src_vocab_size, (64, max_seq_length))  # \u6e90\u5e8f\u5217<br \/>\ntgt_data &#061; torch.randint(1, tgt_vocab_size, (64, max_seq_length))  # \u76ee\u6807\u5e8f\u5217<\/p>\n<p># \u5b9a\u4e49\u635f\u5931\u51fd\u6570\u548c\u4f18\u5316\u5668<br \/>\ncriterion &#061; nn.CrossEntropyLoss(ignore_index&#061;0)  # \u5ffd\u7565\u586b\u5145\u90e8\u5206\u7684\u635f\u5931<br \/>\noptimizer &#061; optim.Adam(transformer.parameters(), lr&#061;0.0001, betas&#061;(0.9, 0.98), eps&#061;1e-9)<\/p>\n<p># \u8bad\u7ec3\u5faa\u73af<br \/>\ntransformer.train()<br \/>\nfor epoch in range(10):<br \/>\n    optimizer.zero_grad()  # \u6e05\u7a7a\u68af\u5ea6&#xff0c;\u9632\u6b62\u7d2f\u79ef<\/p>\n<p>    # \u8f93\u5165\u76ee\u6807\u5e8f\u5217\u65f6\u53bb\u6389\u6700\u540e\u4e00\u4e2a\u8bcd&#xff08;\u7528\u4e8e\u9884\u6d4b\u4e0b\u4e00\u4e2a\u8bcd&#xff09;<br \/>\n    output &#061; transformer(src_data, tgt_data[:, :-1])<\/p>\n<p>    # \u8ba1\u7b97\u635f\u5931\u65f6&#xff0c;\u76ee\u6807\u5e8f\u5217\u4ece\u7b2c\u4e8c\u4e2a\u8bcd\u5f00\u59cb&#xff08;\u5373\u9884\u6d4b\u4e0b\u4e00\u4e2a\u8bcd&#xff09;<br \/>\n    # output\u5f62\u72b6: (batch_size, seq_length-1, tgt_vocab_size)<br \/>\n    # \u76ee\u6807\u5f62\u72b6: (batch_size, seq_length-1)<br \/>\n    loss &#061; criterion(<br \/>\n        output.contiguous().view(-1, tgt_vocab_size),<br \/>\n        tgt_data[:, 1:].contiguous().view(-1)<br \/>\n    )<\/p>\n<p>    loss.backward()  # \u53cd\u5411\u4f20\u64ad<br \/>\n    optimizer.step()  # \u66f4\u65b0\u53c2\u6570<br \/>\n    print(f&#034;Epoch: {epoch &#043; 1}, Loss: {loss.item()}&#034;)<\/p>\n<p># \u9a8c\u8bc1\u6a21\u5f0f<br \/>\ntransformer.eval()<br \/>\n# \u751f\u6210\u9a8c\u8bc1\u6570\u636e<br \/>\nval_src_data &#061; torch.randint(1, src_vocab_size, (64, max_seq_length))<br \/>\nval_tgt_data &#061; torch.randint(1, tgt_vocab_size, (64, max_seq_length))<\/p>\n<p>with torch.no_grad():<br \/>\n    val_output &#061; transformer(val_src_data, val_tgt_data[:, :-1])<br \/>\n    val_loss &#061; criterion(val_output.contiguous().view(-1, tgt_vocab_size),<br \/>\n                        val_tgt_data[:, 1:].contiguous().view(-1))<br \/>\n    print(f&#034;Validation Loss: {val_loss.item()}&#034;) <\/p>\n<h3 id=\"6.1%E6%A8%A1%E5%9E%8B%E6%80%A7%E8%83%BD%E8%AF%84%E4%BC%B0%E7%BB%93%E6%9E%9C%E5%A6%82%E4%B8%8B%EF%BC%9A%E2%80%8B%E7%BC%96%E8%BE%91\" style=\"background-color:transparent\">6.1\u6a21\u578b\u6027\u80fd\u8bc4\u4f30\u7ed3\u679c\u5982\u4e0b&#xff1a;<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"456\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174223-6987797f67bd1.png\" width=\"788\" \/><\/h3>\n<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174223-6987797f8ca3b.gif\" \/><\/p>\n<\/p>\n<h2 id=\"%E6%AC%A2%E8%BF%8E%E5%90%84%E4%BD%8D%E5%BD%A6%E7%A5%96%E4%B8%8E%E7%83%AD%E5%B7%B4%E7%95%85%E6%B8%B8%E6%9C%AC%E4%BA%BA%E4%B8%93%E6%A0%8F%E4%B8%8E%E5%8D%9A%E5%AE%A2\" style=\"text-align:center\">\u6b22\u8fce\u5404\u4f4d\u5f66\u7956\u4e0e\u70ed\u5df4\u7545\u6e38\u672c\u4eba\u4e13\u680f\u4e0e\u6280\u672f\u535a\u5ba2<\/h2>\n<h2 id=\"%E4%BD%A0%E7%9A%84%E4%B8%89%E8%BF%9E%E6%98%AF%E6%88%91%E6%9C%80%E5%A4%A7%E7%9A%84%E5%8A%A8%E5%8A%9B\" style=\"text-align:center\"><span style=\"color:#fe2c24\">\u4f60\u7684\u4e09\u8fde\u662f\u6211\u6700\u5927\u7684\u52a8\u529b<\/span><\/h2>\n<h3 id=\"%E4%BB%A5%E4%B8%8B%E5%9B%BE%E7%89%87%E4%BB%85%E4%BB%A3%E8%A1%A8%E4%B8%93%E6%A0%8F%E7%89%B9%E8%89%B2%20%5B%E7%82%B9%E5%87%BB%E7%AE%AD%E5%A4%B4%E6%8C%87%E5%90%91%E7%9A%84%E4%B8%93%E6%A0%8F%E5%90%8D%E5%8D%B3%E5%8F%AF%E9%97%AA%E7%8E%B0%5D\" style=\"background-color:transparent;text-align:center\"><span style=\"color:#fe2c24\">\u70b9\u51fb<\/span>\u27a1\ufe0f<span style=\"color:#fe2c24\">\u6307\u5411\u7684\u4e13\u680f\u540d\u5373\u53ef\u95ea\u73b0<\/span><\/h3>\n<p style=\"text-align:center\">\u27a1\ufe0f\u8ba1\u7b97\u673a\u7ec4\u6210\u539f\u7406<\/p>\n<p style=\"text-align:center\">\u27a1\ufe0f\u64cd\u4f5c\u7cfb\u7edf<\/p>\n<p id=\"%E2%9E%A1%EF%B8%8F%E7%BD%91%E7%BB%9C%E7%A9%BA%E9%97%B4%E5%AE%89%E5%85%A8%E2%80%94%E2%80%94%E5%85%A8%E6%A0%88%E5%89%8D%E6%B2%BF%E6%8A%80%E6%9C%AF%E6%8C%81%E7%BB%AD%E6%B7%B1%E5%85%A5%E5%AD%A6%E4%B9%A0%C2%A0\" style=\"text-align:center\">\u27a1\ufe0f\u6e17\u900f\u7ec8\u6781\u4e4b\u7ea2\u961f\u653b\u51fb\u884c\u52a8\u00a0<\/p>\n<p style=\"text-align:center\">\u27a1\ufe0f\u52a8\u753b\u53ef\u89c6\u5316\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5<\/p>\n<p id=\"%E4%B8%93%E6%A0%8F%E8%B7%91%E9%81%93%E4%BA%8C%E2%9E%A1%EF%B8%8F%C2%A024%20Network%20Security%20-LJS%C2%A0\" style=\"text-align:center\">\u27a1\ufe0f\u00a0\u6c38\u6052\u4e4b\u5fc3\u84dd\u961f\u8054\u7eb5\u5408\u6a2a\u9632\u5fa1<\/p>\n<p style=\"text-align:center\">\u27a1\ufe0f\u534e\u4e3a\u9ad8\u7ea7\u7f51\u7edc\u5de5\u7a0b\u5e08<\/p>\n<p style=\"text-align:center\">\u27a1\ufe0f\u534e\u4e3a\u9ad8\u7ea7\u9632\u706b\u5899\u9632\u5fa1\u96c6\u6210\u90e8\u7f72<\/p>\n<p style=\"text-align:center\">\u00a0\u27a1\ufe0f\u00a0\u672a\u6388\u6743\u8bbf\u95ee\u6f0f\u6d1e\u6a2a\u5411\u6e17\u900f\u5229\u7528<\/p>\n<p style=\"text-align:center\">\u00a0\u27a1\ufe0f\u9006\u5411\u8f6f\u4ef6\u7834\u89e3\u5de5\u7a0b<\/p>\n<p id=\"%E4%B8%93%E6%A0%8F%E8%B7%91%E9%81%93%E4%B8%89%E2%9E%A1%EF%B8%8F%C2%A0MYSQL%20REDIS%20Advance%20operation\" style=\"text-align:center\">\u27a1\ufe0fMYSQL REDIS \u8fdb\u9636\u5b9e\u64cd<\/p>\n<p id=\"%E4%B8%93%E6%A0%8F%E8%B7%91%E9%81%93%E4%BA%94%E2%9E%A1%EF%B8%8FRHCE-LJS%5BLinux%E9%AB%98%E7%AB%AF%E9%AA%9A%E6%93%8D%E4%BD%9C%E5%AE%9E%E6%88%98%E7%AF%87%5D%E2%80%8B\" style=\"text-align:center\">\u27a1\ufe0f\u7ea2\u5e3d\u9ad8\u7ea7\u5de5\u7a0b\u5e08\u200b<\/p>\n<p id=\"%E4%B8%93%E6%A0%8F%E8%B7%91%E9%81%93%E4%B8%83\" style=\"text-align:center\">\u27a1\ufe0f\u7ea2\u5e3d\u7cfb\u7edf\u7ba1\u7406\u5458<\/p>\n<p style=\"text-align:center\">\u00a0\u27a1\ufe0fHVV \u5168\u56fd\u5404\u5730\u9762\u8bd5\u9898\u6c47\u603b<\/p>\n<p style=\"text-align:center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"300\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174224-698779802f054.gif\" width=\"1200\" \/><\/p>\n<p style=\"text-align:center\">\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7bc7\u6280\u672f\u535a\u6587\u6458\u8981 &#x1f31f; \u6587\u7ae0\u5f00\u7bc7\u5373\u9610\u660e\u4f7f\u7528PyTorch\u6846\u67b6\u5b9e\u73b0Transformer\u7684\u5b9e\u9645\u4ef7\u503c\u4e0e\u91cd\u8981\u610f\u4e49&#xff0c;\u6307\u51fa\u5176\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\u7b49\u9886\u57df\u7684\u6838\u5fc3\u5730\u4f4d\u3002\u968f\u540e&#xff0c;\u6587\u7ae0\u4ee5\u6e05\u6670\u7684\u6b65\u9aa4\u5316\u7ed3\u6784\u5c55\u5f00&#xff1a;\u9996\u5148\u6307\u5bfc\u8bfb\u8005\u5bfc\u5165\u5fc5\u8981\u7684PyTorch\u5e93\u4e0e\u6a21\u5757&#xff0c;\u5960\u5b9a\u5f00\u53d1\u57fa\u7840&#xff1b;\u63a5\u7740\u6df1\u5165\u62c6\u89e3Transformer\u7684\u6838\u5fc3\u7ec4\u4ef6&#xff0c;\u5305\u62ec\u591a\u5934\u6ce8\u610f\u529b\u673a\u5236\u3001\u4f4d\u7f6e\u524d\u9988\u7f51\u7edc\u4e0e\u4f4d\u7f6e\u7f16\u7801\u7684\u539f\u7406\u4e0e\u5b9e\u73b0&#xff0c;\u5e76\u9010\u4e00\u63d0\u4f9b\u53ef\u8fd0\u884c\u7684\u4ee3\u7801\u793a\u4f8b<\/p>\n","protected":false},"author":2,"featured_media":73625,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[7739,7738,7737,7740,7741,841,86],"topic":[],"class_list":{"0":"post-73643","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-server","8":"tag-7739","9":"tag-7738","10":"tag-transformer","11":"tag-7740","14":"tag-86"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u300aTransformer\u6a21\u578bPyTorch\u5b9e\u73b0\u5168\u653b\u7565\uff1a\u67b6\u6784\u62c6\u89e3\u3001\u4ee3\u7801\u793a\u4f8b\u4e0e\u4f18\u5316\u6280\u5de7\u300b - \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\/73643.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u300aTransformer\u6a21\u578bPyTorch\u5b9e\u73b0\u5168\u653b\u7565\uff1a\u67b6\u6784\u62c6\u89e3\u3001\u4ee3\u7801\u793a\u4f8b\u4e0e\u4f18\u5316\u6280\u5de7\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u672c\u7bc7\u6280\u672f\u535a\u6587\u6458\u8981 &#x1f31f; \u6587\u7ae0\u5f00\u7bc7\u5373\u9610\u660e\u4f7f\u7528PyTorch\u6846\u67b6\u5b9e\u73b0Transformer\u7684\u5b9e\u9645\u4ef7\u503c\u4e0e\u91cd\u8981\u610f\u4e49&#xff0c;\u6307\u51fa\u5176\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\u7b49\u9886\u57df\u7684\u6838\u5fc3\u5730\u4f4d\u3002\u968f\u540e&#xff0c;\u6587\u7ae0\u4ee5\u6e05\u6670\u7684\u6b65\u9aa4\u5316\u7ed3\u6784\u5c55\u5f00&#xff1a;\u9996\u5148\u6307\u5bfc\u8bfb\u8005\u5bfc\u5165\u5fc5\u8981\u7684PyTorch\u5e93\u4e0e\u6a21\u5757&#xff0c;\u5960\u5b9a\u5f00\u53d1\u57fa\u7840&#xff1b;\u63a5\u7740\u6df1\u5165\u62c6\u89e3Transformer\u7684\u6838\u5fc3\u7ec4\u4ef6&#xff0c;\u5305\u62ec\u591a\u5934\u6ce8\u610f\u529b\u673a\u5236\u3001\u4f4d\u7f6e\u524d\u9988\u7f51\u7edc\u4e0e\u4f4d\u7f6e\u7f16\u7801\u7684\u539f\u7406\u4e0e\u5b9e\u73b0&#xff0c;\u5e76\u9010\u4e00\u63d0\u4f9b\u53ef\u8fd0\u884c\u7684\u4ee3\u7801\u793a\u4f8b\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/73643.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-07T17:42:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174215-698779770c6a7.gif\" \/>\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=\"13 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/73643.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/73643.html\",\"name\":\"\u300aTransformer\u6a21\u578bPyTorch\u5b9e\u73b0\u5168\u653b\u7565\uff1a\u67b6\u6784\u62c6\u89e3\u3001\u4ee3\u7801\u793a\u4f8b\u4e0e\u4f18\u5316\u6280\u5de7\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-07T17:42:25+00:00\",\"dateModified\":\"2026-02-07T17:42:25+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/73643.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/73643.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/73643.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u300aTransformer\u6a21\u578bPyTorch\u5b9e\u73b0\u5168\u653b\u7565\uff1a\u67b6\u6784\u62c6\u89e3\u3001\u4ee3\u7801\u793a\u4f8b\u4e0e\u4f18\u5316\u6280\u5de7\u300b\"}]},{\"@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":"\u300aTransformer\u6a21\u578bPyTorch\u5b9e\u73b0\u5168\u653b\u7565\uff1a\u67b6\u6784\u62c6\u89e3\u3001\u4ee3\u7801\u793a\u4f8b\u4e0e\u4f18\u5316\u6280\u5de7\u300b - \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\/73643.html","og_locale":"zh_CN","og_type":"article","og_title":"\u300aTransformer\u6a21\u578bPyTorch\u5b9e\u73b0\u5168\u653b\u7565\uff1a\u67b6\u6784\u62c6\u89e3\u3001\u4ee3\u7801\u793a\u4f8b\u4e0e\u4f18\u5316\u6280\u5de7\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u672c\u7bc7\u6280\u672f\u535a\u6587\u6458\u8981 &#x1f31f; \u6587\u7ae0\u5f00\u7bc7\u5373\u9610\u660e\u4f7f\u7528PyTorch\u6846\u67b6\u5b9e\u73b0Transformer\u7684\u5b9e\u9645\u4ef7\u503c\u4e0e\u91cd\u8981\u610f\u4e49&#xff0c;\u6307\u51fa\u5176\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\u7b49\u9886\u57df\u7684\u6838\u5fc3\u5730\u4f4d\u3002\u968f\u540e&#xff0c;\u6587\u7ae0\u4ee5\u6e05\u6670\u7684\u6b65\u9aa4\u5316\u7ed3\u6784\u5c55\u5f00&#xff1a;\u9996\u5148\u6307\u5bfc\u8bfb\u8005\u5bfc\u5165\u5fc5\u8981\u7684PyTorch\u5e93\u4e0e\u6a21\u5757&#xff0c;\u5960\u5b9a\u5f00\u53d1\u57fa\u7840&#xff1b;\u63a5\u7740\u6df1\u5165\u62c6\u89e3Transformer\u7684\u6838\u5fc3\u7ec4\u4ef6&#xff0c;\u5305\u62ec\u591a\u5934\u6ce8\u610f\u529b\u673a\u5236\u3001\u4f4d\u7f6e\u524d\u9988\u7f51\u7edc\u4e0e\u4f4d\u7f6e\u7f16\u7801\u7684\u539f\u7406\u4e0e\u5b9e\u73b0&#xff0c;\u5e76\u9010\u4e00\u63d0\u4f9b\u53ef\u8fd0\u884c\u7684\u4ee3\u7801\u793a\u4f8b","og_url":"https:\/\/www.wsisp.com\/helps\/73643.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-07T17:42:25+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260207174215-698779770c6a7.gif"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"13 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/73643.html","url":"https:\/\/www.wsisp.com\/helps\/73643.html","name":"\u300aTransformer\u6a21\u578bPyTorch\u5b9e\u73b0\u5168\u653b\u7565\uff1a\u67b6\u6784\u62c6\u89e3\u3001\u4ee3\u7801\u793a\u4f8b\u4e0e\u4f18\u5316\u6280\u5de7\u300b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-07T17:42:25+00:00","dateModified":"2026-02-07T17:42:25+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/73643.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/73643.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/73643.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u300aTransformer\u6a21\u578bPyTorch\u5b9e\u73b0\u5168\u653b\u7565\uff1a\u67b6\u6784\u62c6\u89e3\u3001\u4ee3\u7801\u793a\u4f8b\u4e0e\u4f18\u5316\u6280\u5de7\u300b"}]},{"@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\/73643","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=73643"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/73643\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/73625"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=73643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=73643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=73643"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=73643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}