{"id":44535,"date":"2025-06-19T09:28:08","date_gmt":"2025-06-19T01:28:08","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/44535.html"},"modified":"2025-06-19T09:28:08","modified_gmt":"2025-06-19T01:28:08","slug":"%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0%ef%bc%9apytorch%e8%87%aa%e5%8a%a8%e5%be%ae%e5%88%86%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/44535.html","title":{"rendered":"\u6df1\u5ea6\u5b66\u4e60\uff1aPyTorch\u81ea\u52a8\u5fae\u5206\u6a21\u5757"},"content":{"rendered":"<\/p>\n<h4>\u672c\u6587\u76ee\u5f55&#xff1a;<\/h4>\n<ul>\n<li>\u4e00\u3001\u81ea\u52a8\u5fae\u5206\u76f8\u5173\u6982\u5ff5<\/li>\n<li>\u4e8c\u3001\u68af\u5ea6\u57fa\u672c\u8ba1\u7b97<\/li>\n<li>\n<ul>\n<li>&#xff08;\u4e00&#xff09;\u6807\u91cf\u5f20\u91cf\u68af\u5ea6\u8ba1\u7b97<\/li>\n<li>&#xff08;\u4e8c&#xff09;\u5411\u91cf\u5f20\u91cf\u68af\u5ea6\u8ba1\u7b97<\/li>\n<\/ul>\n<\/li>\n<li>\u4e09\u3001\u68af\u5ea6\u4e0b\u964d\u6cd5\u6c42\u6700\u4f18\u89e3<\/li>\n<li>\u56db\u3001\u68af\u5ea6\u8ba1\u7b97\u6ce8\u610f\u70b9<\/li>\n<li>\u4e94\u3001\u81ea\u52a8\u5fae\u5206\u6a21\u5757\u5e94\u7528<\/li>\n<li>\u516d\u3001PyTorch\u6784\u5efa\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/li>\n<li>\n<ul>\n<li>&#xff08;\u4e00&#xff09;\u6d41\u7a0b<\/li>\n<li>&#xff08;\u4e8c&#xff09;\u4ee3\u7801\u5b9e\u73b0<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u4e00\u3001\u81ea\u52a8\u5fae\u5206\u76f8\u5173\u6982\u5ff5<\/h2>\n<p>\u81ea\u52a8\u5fae\u5206\u5c31\u662f\u81ea\u52a8\u8ba1\u7b97\u68af\u5ea6\u503c&#xff0c;\u4e5f\u5c31\u662f\u8ba1\u7b97\u5bfc\u6570\u3002 <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619012806-685367a629f69.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u65f6&#xff0c;\u6700\u5e38\u7528\u7684\u7b97\u6cd5\u5c31\u662f\u53cd\u5411\u4f20\u64ad\u3002\u5728\u8be5\u7b97\u6cd5\u4e2d&#xff0c;\u53c2\u6570&#xff08;\u6a21\u578b\u6743\u91cd&#xff09;\u4f1a\u6839\u636e\u635f\u5931\u51fd\u6570\u5173\u4e8e\u5bf9\u5e94\u53c2\u6570\u7684\u68af\u5ea6\u8fdb\u884c\u8c03\u6574\u3002\u4e3a\u4e86\u8ba1\u7b97\u8fd9\u4e9b\u68af\u5ea6&#xff0c;PyTorch\u5185\u7f6e\u4e86\u540d\u4e3a torch.autograd \u7684\u5fae\u5206\u6a21\u5757\u3002\u5b83\u652f\u6301\u4efb\u610f\u8ba1\u7b97\u56fe\u7684\u81ea\u52a8\u68af\u5ea6\u8ba1\u7b97&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619012806-685367a665213.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h2>\u4e8c\u3001\u68af\u5ea6\u57fa\u672c\u8ba1\u7b97<\/h2>\n<ul>\n<li>pytorch\u4e0d\u652f\u6301\u5411\u91cf\u5f20\u91cf\u5bf9\u5411\u91cf\u5f20\u91cf\u7684\u6c42\u5bfc,\u53ea\u652f\u6301\u6807\u91cf\u5f20\u91cf\u5bf9\u5411\u91cf\u5f20\u91cf\u7684\u6c42\u5bfc<\/li>\n<li>x\u5982\u679c\u662f\u5f20\u91cf,y\u5fc5\u987b\u662f\u6807\u91cf(\u4e00\u4e2a\u503c)\u624d\u53ef\u4ee5\u8fdb\u884c\u6c42\u5bfc&#xff1b;<\/li>\n<li>\u8ba1\u7b97\u68af\u5ea6: y.backward(), y\u662f\u4e00\u4e2a\u6807\u91cf&#xff0c;\u5b9e\u9645\u5e38\u5e38\u4f7f\u7528y.sum()\u4ee5\u6539\u53d8y\u7684\u5f20\u91cf\u6027\u8d28&#xff1b;<\/li>\n<li>\u83b7\u53d6x\u70b9\u7684\u68af\u5ea6\u503c: x.grad, \u4f1a\u7d2f\u52a0\u4e0a\u4e00\u6b21\u7684\u68af\u5ea6\u503c\u3002<\/li>\n<li><\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619012806-685367a6842ff.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>&#xff08;\u4e00&#xff09;\u6807\u91cf\u5f20\u91cf\u68af\u5ea6\u8ba1\u7b97<\/h3>\n<p># \u5b9a\u4e49\u4e00\u4e2a\u6807\u91cf\u5f20\u91cf(\u70b9)<br \/>\n# requires_grad&#061;:\u9ed8\u8ba4\u4e3aFalse,\u4e0d\u4f1a\u81ea\u52a8\u8ba1\u7b97\u68af\u5ea6;\u4e3aTrue\u7684\u8bdd\u662f\u5c06\u81ea\u52a8\u8ba1\u7b97\u7684\u68af\u5ea6\u503c\u4fdd\u5b58\u5230grad\u4e2d<br \/>\nx &#061; torch.tensor(10, requires_grad&#061;True, dtype&#061;torch.float32)<br \/>\nprint(&#034;x&#8211;&gt;&#034;, x)<\/p>\n<p># \u5b9a\u4e49\u4e00\u4e2a\u66f2\u7ebf<br \/>\ny &#061; 2 * x ** 2<br \/>\nprint(&#034;y&#8211;&gt;&#034;, y)<br \/>\n# \u67e5\u770b\u68af\u5ea6\u51fd\u6570\u7c7b\u578b,\u5373\u66f2\u7ebf\u51fd\u6570\u7c7b\u578b&#xff0c;\u7b80\u5355\u6765\u8bf4\u4e86\u89e3y\u662f\u600e\u4e48\u6765\u7684&#xff0c;\u6bd4\u5982y&#061;sum(x)&#xff0c;\u5c31\u8fd4\u56desum\u76f8\u5173\u64cd\u4f5c<br \/>\nprint(y.grad_fn)<\/p>\n<p># \u8ba1\u7b97x\u70b9\u7684\u68af\u5ea6<br \/>\n# \u6b64\u65f6y\u662f\u4e00\u4e2a\u6807\u91cf,\u53ef\u4ee5\u4e0d\u7528\u4f7f\u7528y.sum()\u8f6c\u6362\u6210\u6807\u91cf<br \/>\nprint(&#034;y.sum()&#8211;&gt;&#034;, y.sum())<br \/>\n# y&#039;|(x&#061;10) &#061; (2*x**2)&#039;|(x&#061;10) &#061; 4x|(x&#061;10) &#061; 40<br \/>\ny.backward()<\/p>\n<p># \u6253\u5370x\u7684\u68af\u5ea6\u503c<br \/>\nprint(&#034;x\u7684\u68af\u5ea6\u503c\u662f:&#034;, x.grad)<\/p>\n<h3>&#xff08;\u4e8c&#xff09;\u5411\u91cf\u5f20\u91cf\u68af\u5ea6\u8ba1\u7b97<\/h3>\n<p># \u5b9a\u4e49\u4e00\u4e2a\u5411\u91cf\u5f20\u91cf(\u70b9)<br \/>\nx &#061; torch.tensor([10, 20], requires_grad&#061;True, dtype&#061;torch.float32)<br \/>\nprint(&#034;x&#8211;&gt;&#034;, x)<\/p>\n<p># \u5b9a\u4e49\u4e00\u4e2a\u66f2\u7ebf<br \/>\ny &#061; 2 * x ** 2<br \/>\nprint(&#034;y&#8211;&gt;&#034;, y)<\/p>\n<p># \u8ba1\u7b97\u68af\u5ea6<br \/>\n# x\u548cy\u90fd\u662f\u5411\u91cf\u5f20\u91cf,\u4e0d\u80fd\u8fdb\u884c\u6c42\u5bfc,\u9700\u8981\u5c06y\u8f6c\u6362\u6210\u6807\u91cf\u5f20\u91cf&#8211;&gt;y.sum()<br \/>\n# y&#039;|(x&#061;10) &#061; (2*x**2)&#039;|(x&#061;10) &#061; 4x|(x&#061;10) &#061; 40<br \/>\n# y&#039;|(x&#061;20) &#061; (2*x**2)&#039;|(x&#061;20) &#061; 4x|(x&#061;20) &#061; 80<br \/>\ny.sum().backward()<\/p>\n<p># \u6253\u5370x\u7684\u68af\u5ea6<br \/>\nprint(&#034;x.grad&#8211;&gt;&#034;, x.grad)<\/p>\n<h2>\u4e09\u3001\u68af\u5ea6\u4e0b\u964d\u6cd5\u6c42\u6700\u4f18\u89e3<\/h2>\n<p>\u68af\u5ea6\u4e0b\u964d\u6cd5\u516c\u5f0f: w &#061; w &#8211; r * grad (r\u662f\u5b66\u4e60\u7387, grad\u662f\u68af\u5ea6\u503c) \u6e05\u7a7a\u4e0a\u4e00\u6b21\u7684\u68af\u5ea6\u503c: x.grad.zero_()<\/p>\n<p># \u9700\u6c42&#xff1a;\u6c42 y &#061; x**2 &#043; 20 \u7684\u6781\u5c0f\u503c\u70b9 \u5e76\u6253\u5370y\u662f\u6700\u5c0f\u503c\u65f6 w\u7684\u503c(\u68af\u5ea6)<br \/>\n# 1 \u5b9a\u4e49\u70b9 &#xff1a;x&#061;10 requires_grad&#061;True  dtype&#061;torch.float32<br \/>\n# 2 \u5b9a\u4e49\u51fd\u6570&#xff1a; y &#061; x**2 &#043; 20<br \/>\n# 3 \u5229\u7528\u68af\u5ea6\u4e0b\u964d\u6cd5 \u5faa\u73af\u8fed\u4ee31000 \u6c42\u6700\u4f18\u89e3<br \/>\n# 3-1 \u6b63\u5411\u8ba1\u7b97(\u524d\u5411\u4f20\u64ad)<br \/>\n# 3-2 \u68af\u5ea6\u6e05\u96f6 &#xff1a;x.grad.zero_()<br \/>\n# 3-3 \u53cd\u5411\u4f20\u64ad<br \/>\n# 3-4 \u68af\u5ea6\u66f4\u65b0&#xff1a; x.data &#061; x.data &#8211; 0.01 * x.grad<\/p>\n<p># 1 \u5b9a\u4e49\u70b9&#xff1a;x&#061;10 requires_grad&#061;True  dtype&#061;torch.float32<br \/>\nx &#061; torch.tensor(10, requires_grad&#061;True, dtype&#061;torch.float32)<\/p>\n<p># 2 \u5b9a\u4e49\u51fd\u6570 y &#061; x ** 2 &#043; 20<br \/>\ny &#061; x ** 2 &#043; 20<br \/>\nprint(&#039;\u5f00\u59cb \u6743\u91cdx\u521d\u59cb\u503c:%.2f (0.01 * x.grad):\u65e0 y:%.2f&#039; % (x, y))<\/p>\n<p># 3 \u5229\u7528\u68af\u5ea6\u4e0b\u964d\u6cd5 \u5faa\u73af\u8fed\u4ee31000 \u6c42\u6700\u4f18\u89e3<br \/>\nfor i in range(1, 1001):<\/p>\n<p>    # 3-1 \u6b63\u5411\u8ba1\u7b97(\u524d\u5411\u4f20\u64ad)<br \/>\n    y &#061; x ** 2 &#043; 20<\/p>\n<p>    # 3-2 \u68af\u5ea6\u6e05\u96f6&#xff1a;x.grad.zero_()<br \/>\n    # \u5f20\u91cf\u7684 grad \u5c5e\u6027\u9ed8\u8ba4\u4f1a\u7d2f\u52a0\u5386\u53f2\u68af\u5ea6\u503c&#xff0c;\u9700\u624b\u5de5\u6e05\u96f6\u4e0a\u4e00\u6b21\u7684\u63d0\u53d6<br \/>\n    # \u4e00\u5f00\u59cb\u68af\u5ea6\u4e0d\u5b58\u5728, \u9700\u8981\u505a\u5224\u65ad<br \/>\n    if x.grad is not None:<br \/>\n        x.grad.zero_()  #\u68af\u5ea6\u6e05\u96f6<\/p>\n<p>    # 3-3 \u53cd\u5411\u4f20\u64ad<br \/>\n    y.sum().backward()   #\u4e5f\u53ef\u4e0d\u52a0sum()<\/p>\n<p>    # 3-4 \u68af\u5ea6\u66f4\u65b0 x.data &#061; x.data &#8211; 0.01 * x.grad<br \/>\n    # x.data\u662f\u4fee\u6539\u539f\u59cbx\u5185\u5b58\u4e2d\u7684\u6570\u636e,\u524d\u540ex\u7684\u5185\u5b58\u7a7a\u95f4\u4e00\u6837;\u5982\u679c\u4f7f\u7528x,\u6b64\u65f6\u4fee\u6539\u524d\u540ex\u7684\u5185\u5b58\u7a7a\u95f4\u4e0d\u540c<br \/>\n    x.data &#061; x.data &#8211; 0.01 * x.grad  # \u6ce8&#xff1a;\u4e0d\u80fd x &#061; x &#8211; 0.01 * x.grad \u8fd9\u6837\u5199<\/p>\n<p>    print(&#039;\u6b21\u6570:%d \u6743\u91cdx: %.6f, (0.01 * x.grad):%.6f y:%.6f&#039; % (i, x, 0.01 * x.grad, y))<\/p>\n<p>print(&#039;x&#xff1a;&#039;, x, x.grad, &#039;y\u6700\u5c0f\u503c:&#039;, y)<\/p>\n<h2>\u56db\u3001\u68af\u5ea6\u8ba1\u7b97\u6ce8\u610f\u70b9<\/h2>\n<p>\u82e5\u5f20\u91cf\u8bbe\u7f6e\u4e86\u68af\u5ea6\u8ddf\u8e2a&#xff0c;\u4fbf\u4e0d\u53ef\u76f4\u63a5\u8f6c\u6362\u6210numpy\u6570\u7ec4&#xff0c;\u4f1a\u53d1\u751f\u62a5\u9519&#xff0c;\u9700\u8981\u901a\u8fc7detach()\u65b9\u6cd5\u5148\u201c\u5f15\u7528\u4f20\u9012\u201d\u6570\u636e&#xff0c;\u518d\u8f6c\u6362\u3002<\/p>\n<p># \u5b9a\u4e49\u4e00\u4e2a\u5f20\u91cf<br \/>\nx1 &#061; torch.tensor([10, 20], requires_grad&#061;True, dtype&#061;torch.float64)  #\u8bbe\u7f6e\u4e86\u68af\u5ea6\u8ddf\u8e2a&#xff1a;requires_grad&#061;True<\/p>\n<p># \u5c06x\u5f20\u91cf\u8f6c\u6362\u6210numpy\u6570\u7ec4<br \/>\n# \u53d1\u751f\u62a5\u9519&#xff1a;RuntimeError: Can&#039;t call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.<br \/>\n# \u4e0d\u80fd\u5c06\u81ea\u52a8\u5fae\u5206\u7684\u5f20\u91cf\u8f6c\u6362\u6210numpy\u6570\u7ec4<br \/>\n# print(x1.numpy())<\/p>\n<p># \u901a\u8fc7detach()\u65b9\u6cd5\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u5f20\u91cf,\u4f5c\u4e3a\u53f6\u5b50\u7ed3\u70b9<br \/>\nx2 &#061; x1.detach()   #\u76f8\u5f53\u4e8ex1\u6570\u636e\u5f15\u7528\u4f20\u9012\u5230x2<br \/>\n# x1\u548cx2\u5f20\u91cf\u5171\u4eab\u6570\u636e,\u4f46\u662fx2\u4e0d\u4f1a\u81ea\u52a8\u5fae\u5206<br \/>\nprint(x1.requires_grad)   #\u7ed3\u679c\u4e3aTrue<br \/>\nprint(x2.requires_grad)   #\u7ed3\u679c\u4e3aFalse<br \/>\n# x1\u548cx2\u5f20\u91cf\u7684\u503c\u4e00\u6837,\u5171\u7528\u4e00\u4efd\u5185\u5b58\u7a7a\u95f4\u7684\u6570\u636e<br \/>\nprint(x1.data)<br \/>\nprint(x2.data)     #x1\u548cx2\u6570\u636e\u4e00\u6837<br \/>\nprint(id(x1.data))<br \/>\nprint(id(x2.data))  #x1\u548cx2\u7684id\u4e00\u6837&#xff0c;\u8bf4\u660e\u5171\u7528\u540c\u4e00\u4efd\u5185\u5b58\u6570\u636e<\/p>\n<p># \u5c06x2\u5f20\u91cf\u8f6c\u6362\u6210numpy\u6570\u7ec4<br \/>\nprint(x2.numpy())   #\u53ef\u884c<\/p>\n<h2>\u4e94\u3001\u81ea\u52a8\u5fae\u5206\u6a21\u5757\u5e94\u7528<\/h2>\n<p>import torch<br \/>\n#\u4e0b\u9762\u5b9a\u4e49x&#xff0c;y&#xff0c;w&#xff0c;b&#xff0c;\u9700\u8981\u6ce8\u610fx&#xff0c;y&#xff0c;w\u548cb\u884c\u5217\u6570\u7b26\u5408\u77e9\u9635\u8fd0\u7b97\u548c\u635f\u5931\u51fd\u6570\u8fd0\u7b97\u6807\u51c6<br \/>\n# \u8f93\u5165\u5f20\u91cf 2*5<br \/>\nx &#061; torch.ones(2, 5)<br \/>\n# \u76ee\u6807\u503c\u662f 2*3<br \/>\ny &#061; torch.zeros(2, 3)<br \/>\n# \u8bbe\u7f6e\u8981\u66f4\u65b0\u7684\u6743\u91cd\u548c\u504f\u7f6e\u7684\u521d\u59cb\u503c<br \/>\nw &#061; torch.randn(5, 3, requires_grad&#061;True)<br \/>\nb &#061; torch.randn(3, requires_grad&#061;True)<br \/>\n# \u8bbe\u7f6e\u7f51\u7edc\u7684\u8f93\u51fa\u503c<br \/>\nz &#061; torch.matmul(x, w) &#043; b  # \u77e9\u9635\u4e58\u6cd5<br \/>\n# \u8bbe\u7f6e\u635f\u5931\u51fd\u6570&#xff0c;\u5e76\u8fdb\u884c\u635f\u5931\u7684\u8ba1\u7b97<br \/>\nloss &#061; torch.nn.MSELoss()<br \/>\nloss &#061; loss(z, y)<br \/>\n# \u81ea\u52a8\u5fae\u5206<br \/>\nloss.backward()<br \/>\n# \u6253\u5370 w,b \u53d8\u91cf\u7684\u68af\u5ea6<br \/>\n# backward \u51fd\u6570\u8ba1\u7b97\u7684\u68af\u5ea6\u503c\u4f1a\u5b58\u50a8\u5728\u5f20\u91cf\u7684 grad \u53d8\u91cf\u4e2d<br \/>\nprint(&#034;W\u7684\u68af\u5ea6:&#034;, w.grad)<br \/>\nprint(&#034;b\u7684\u68af\u5ea6&#034;, b.grad)<\/p>\n<h2>\u516d\u3001PyTorch\u6784\u5efa\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/h2>\n<h3>&#xff08;\u4e00&#xff09;\u6d41\u7a0b<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619012806-685367a6c8b7e.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u603b\u5171\u56db\u4e2a\u6b65\u9aa4&#xff08;\u7ffb\u8bd1\u81ea\u4e0a\u6587&#xff09;&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619012807-685367a71a557.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>&#xff08;\u4e8c&#xff09;\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<p>import torch<br \/>\nimport torch.nn as nn<br \/>\nimport torch.optim as optim<br \/>\nfrom matplotlib import pyplot as plt<br \/>\nfrom sklearn.datasets import make_regression<br \/>\nfrom torch.utils.data import TensorDataset<\/p>\n<p>def create_dataset():<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u751f\u6210\u6570\u636e\u96c6<\/p>\n<p>    Returns:<br \/>\n        x (torch.Tensor): \u8f93\u5165\u7279\u5f81\u5f20\u91cf<br \/>\n        y (torch.Tensor): \u76ee\u6807\u503c\u5f20\u91cf<br \/>\n        coef (float): \u56de\u5f52\u7cfb\u6570<br \/>\n    &#034;&#034;&#034;<br \/>\n    x, y, coef &#061; make_regression(n_samples&#061;100, n_features&#061;1, bias&#061;14.5, noise&#061;5, coef&#061;True)<br \/>\n    x &#061; torch.tensor(x, dtype&#061;torch.float32)<br \/>\n    y &#061; torch.tensor(y, dtype&#061;torch.float32).unsqueeze(1)<br \/>\n    return x, y, coef<\/p>\n<p>def train_model(x, y, epochs):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u8bad\u7ec3\u6a21\u578b<\/p>\n<p>    Args:<br \/>\n        x (torch.Tensor): \u8f93\u5165\u7279\u5f81\u5f20\u91cf<br \/>\n        y (torch.Tensor): \u76ee\u6807\u503c\u5f20\u91cf<br \/>\n        epochs (int): \u8bad\u7ec3\u8f6e\u6570<br \/>\n    &#034;&#034;&#034;<br \/>\n    dataset &#061; TensorDataset(x, y)<br \/>\n    dataloader &#061; torch.utils.data.DataLoader(dataset, batch_size&#061;10, shuffle&#061;True)  #\u6570\u636e\u52a0\u8f7d\u5668<br \/>\n    model &#061; nn.Linear(1, 1)    #\u5b9a\u4e49\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u5168\u8fde\u63a5\u5c42&#xff08;\u7ebf\u6027\u5c42&#xff09;<br \/>\n    criterion &#061; nn.MSELoss()   #\u635f\u5931\u51fd\u6570<br \/>\n    optimizer &#061; optim.SGD(model.parameters(), lr&#061;0.01)    #\u4f18\u5316\u5668<br \/>\n    epoch_loss, total_loss, train_sample &#061; [], 0, 0   #\u521d\u59cb\u5316<br \/>\n    for epoch in range(epochs):    #\u5b9a\u4e49\u8f6e\u6b21<br \/>\n        for batch_x, batch_y in dataloader:   #\u6309\u6279\u6b21&#xff0c;\u4e00\u6279\u6b2110\u4e2a&#xff08;batch_size&#xff09;<br \/>\n            pre_y &#061; model(batch_x)<br \/>\n            loss &#061; criterion(pre_y, batch_y)<br \/>\n            total_loss &#043;&#061; loss.item()<br \/>\n            train_sample &#043;&#061; len(batch_x)<br \/>\n            optimizer.zero_grad()    #\u6bcf\u6279\u6b21\u68af\u5ea6\u6e05\u96f6&#xff0c;\u5426\u5219\u9ed8\u8ba4\u7d2f\u52a0<br \/>\n            loss.sum().backward()     #\u81ea\u52a8\u5fae\u5206<br \/>\n            optimizer.step()<br \/>\n        epoch_loss.append(total_loss \/ train_sample)<br \/>\n        print(f&#039;model.weights:{model.weight},model.bias:{model.bias}&#039;)<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u7ed8\u5236\u8bad\u7ec3\u635f\u5931\u548c\u9884\u6d4b\u56fe<\/p>\n<p>    Args:<br \/>\n        epochs (int): \u8bad\u7ec3\u8f6e\u6570<br \/>\n        epoch_loss (list): \u6bcf\u8f6e\u8bad\u7ec3\u7684\u635f\u5931<br \/>\n        x (torch.Tensor): \u8f93\u5165\u7279\u5f81\u5f20\u91cf<br \/>\n        y (torch.Tensor): \u76ee\u6807\u503c\u5f20\u91cf<br \/>\n        coef (float): \u56de\u5f52\u7cfb\u6570<br \/>\n        model (nn.Module): \u8bad\u7ec3\u597d\u7684\u6a21\u578b<br \/>\n    &#034;&#034;&#034;<br \/>\n    fig, ax &#061; plt.subplots(2, 1)<br \/>\n    ax[0].plot(range(epochs), epoch_loss)  #\u635f\u5931\u503c\u4e0e\u8f6e\u6b21\u5173\u7cfb<br \/>\n    ax[0].set_xlabel(&#039;epoch&#039;)<br \/>\n    ax[0].set_ylabel(&#039;loss&#039;)<br \/>\n    ax[0].set_title(&#039;loss&#039;)<\/p>\n<p>    x &#061; x.squeeze(1).numpy()<br \/>\n    y &#061; y.squeeze(1).numpy()<\/p>\n<p>    ax[1].scatter(x, y)  # x,y\u4e0d\u964d\u7ef4\u4e5f\u53ef\u4ee5\u753b\u56fe&#xff1a;\u89c2\u6d4b\u503c\u6563\u70b9\u56fe<\/p>\n<p>    x &#061; torch.linspace(-3, 3, 100)<br \/>\n    y_pred &#061; torch.tensor([model.weight * i &#043; model.bias for i in x])   #\u9884\u6d4b\u56de\u5f52\u7ebf<br \/>\n    y_true &#061; torch.tensor([coef * i &#043; 14.5 for i in x.numpy()])   #\u7406\u60f3\u56de\u5f52\u7ebf<br \/>\n    ax[1].plot(x, y_pred, label&#061;&#039;prediction&#039;, color&#061;&#039;red&#039;)<br \/>\n    ax[1].plot(x, y_true, label&#061;&#039;true&#039;, color&#061;&#039;green&#039;)<br \/>\n    ax[1].legend()  #\u56fe\u4f8b<br \/>\n    ax[1].grid(True) #\u7f51\u683c<br \/>\n    ax[1].set_title(&#039;prediction and true&#039;)  #\u6807\u9898<br \/>\n    plt.tight_layout()  # \u81ea\u52a8\u8c03\u6574\u5b50\u56fe\u548c\u6807\u7b7e\u95f4\u8ddd<br \/>\n    plt.show()<\/p>\n<p>if __name__ &#061;&#061; &#039;__main__&#039;:<br \/>\n    epochs &#061; 100<br \/>\n    x, y, coef &#061; create_dataset()<br \/>\n    train_model(x, y, epochs)<\/p>\n<p>\u8f93\u51fa\u56fe\u50cf&#xff1a; <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619012807-685367a7298b2.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u4eca\u65e5\u5206\u4eab\u5230\u6b64\u7ed3\u675f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb705\u6b21\uff0c\u70b9\u8d5e16\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u65f6\uff0c\u6700\u5e38\u7528\u7684\u7b97\u6cd5\u5c31\u662f\u53cd\u5411\u4f20\u64ad\u3002\u5728\u8be5\u7b97\u6cd5\u4e2d\uff0c\u53c2\u6570\uff08\u6a21\u578b\u6743\u91cd\uff09\u4f1a\u6839\u636e\u635f\u5931\u51fd\u6570\u5173\u4e8e\u5bf9\u5e94\u53c2\u6570\u7684\u68af\u5ea6\u8fdb\u884c\u8c03\u6574\u3002\u4e3a\u4e86\u8ba1\u7b97\u8fd9\u4e9b\u68af\u5ea6\uff0cPyTorch\u5185\u7f6e\u4e86\u540d\u4e3a torch.autograd \u7684\u5fae\u5206\u6a21\u5757\u3002(r\u662f\u5b66\u4e60\u7387, grad\u662f\u68af\u5ea6\u503c)\u5148\u201c\u5f15\u7528\u4f20\u9012\u201d\u6570\u636e\uff0c\u518d\u8f6c\u6362\u3002\u82e5\u5f20\u91cf\u8bbe\u7f6e\u4e86\u68af\u5ea6\u8ddf\u8e2a\uff0c\u4fbf\u3002\uff0c\u4f1a\u53d1\u751f\u62a5\u9519\uff0c\u9700\u8981\u901a\u8fc7\u3002<\/p>\n","protected":false},"author":2,"featured_media":44529,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[152,50,86],"topic":[],"class_list":["post-44535","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-pytorch","tag-50","tag-86"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u6df1\u5ea6\u5b66\u4e60\uff1aPyTorch\u81ea\u52a8\u5fae\u5206\u6a21\u5757 - \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\/44535.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u6df1\u5ea6\u5b66\u4e60\uff1aPyTorch\u81ea\u52a8\u5fae\u5206\u6a21\u5757 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb705\u6b21\uff0c\u70b9\u8d5e16\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u65f6\uff0c\u6700\u5e38\u7528\u7684\u7b97\u6cd5\u5c31\u662f\u53cd\u5411\u4f20\u64ad\u3002\u5728\u8be5\u7b97\u6cd5\u4e2d\uff0c\u53c2\u6570\uff08\u6a21\u578b\u6743\u91cd\uff09\u4f1a\u6839\u636e\u635f\u5931\u51fd\u6570\u5173\u4e8e\u5bf9\u5e94\u53c2\u6570\u7684\u68af\u5ea6\u8fdb\u884c\u8c03\u6574\u3002\u4e3a\u4e86\u8ba1\u7b97\u8fd9\u4e9b\u68af\u5ea6\uff0cPyTorch\u5185\u7f6e\u4e86\u540d\u4e3a torch.autograd \u7684\u5fae\u5206\u6a21\u5757\u3002(r\u662f\u5b66\u4e60\u7387, grad\u662f\u68af\u5ea6\u503c)\u5148\u201c\u5f15\u7528\u4f20\u9012\u201d\u6570\u636e\uff0c\u518d\u8f6c\u6362\u3002\u82e5\u5f20\u91cf\u8bbe\u7f6e\u4e86\u68af\u5ea6\u8ddf\u8e2a\uff0c\u4fbf\u3002\uff0c\u4f1a\u53d1\u751f\u62a5\u9519\uff0c\u9700\u8981\u901a\u8fc7\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/44535.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-19T01:28:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619012806-685367a629f69.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=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44535.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/44535.html\",\"name\":\"\u6df1\u5ea6\u5b66\u4e60\uff1aPyTorch\u81ea\u52a8\u5fae\u5206\u6a21\u5757 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-19T01:28:08+00:00\",\"dateModified\":\"2025-06-19T01:28:08+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/44535.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/44535.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/44535.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u6df1\u5ea6\u5b66\u4e60\uff1aPyTorch\u81ea\u52a8\u5fae\u5206\u6a21\u5757\"}]},{\"@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":"\u6df1\u5ea6\u5b66\u4e60\uff1aPyTorch\u81ea\u52a8\u5fae\u5206\u6a21\u5757 - \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\/44535.html","og_locale":"zh_CN","og_type":"article","og_title":"\u6df1\u5ea6\u5b66\u4e60\uff1aPyTorch\u81ea\u52a8\u5fae\u5206\u6a21\u5757 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb705\u6b21\uff0c\u70b9\u8d5e16\u6b21\uff0c\u6536\u85cf8\u6b21\u3002\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u65f6\uff0c\u6700\u5e38\u7528\u7684\u7b97\u6cd5\u5c31\u662f\u53cd\u5411\u4f20\u64ad\u3002\u5728\u8be5\u7b97\u6cd5\u4e2d\uff0c\u53c2\u6570\uff08\u6a21\u578b\u6743\u91cd\uff09\u4f1a\u6839\u636e\u635f\u5931\u51fd\u6570\u5173\u4e8e\u5bf9\u5e94\u53c2\u6570\u7684\u68af\u5ea6\u8fdb\u884c\u8c03\u6574\u3002\u4e3a\u4e86\u8ba1\u7b97\u8fd9\u4e9b\u68af\u5ea6\uff0cPyTorch\u5185\u7f6e\u4e86\u540d\u4e3a torch.autograd \u7684\u5fae\u5206\u6a21\u5757\u3002(r\u662f\u5b66\u4e60\u7387, grad\u662f\u68af\u5ea6\u503c)\u5148\u201c\u5f15\u7528\u4f20\u9012\u201d\u6570\u636e\uff0c\u518d\u8f6c\u6362\u3002\u82e5\u5f20\u91cf\u8bbe\u7f6e\u4e86\u68af\u5ea6\u8ddf\u8e2a\uff0c\u4fbf\u3002\uff0c\u4f1a\u53d1\u751f\u62a5\u9519\uff0c\u9700\u8981\u901a\u8fc7\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/44535.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-19T01:28:08+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/06\/20250619012806-685367a629f69.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/44535.html","url":"https:\/\/www.wsisp.com\/helps\/44535.html","name":"\u6df1\u5ea6\u5b66\u4e60\uff1aPyTorch\u81ea\u52a8\u5fae\u5206\u6a21\u5757 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-19T01:28:08+00:00","dateModified":"2025-06-19T01:28:08+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/44535.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/44535.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/44535.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u6df1\u5ea6\u5b66\u4e60\uff1aPyTorch\u81ea\u52a8\u5fae\u5206\u6a21\u5757"}]},{"@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\/44535","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=44535"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/44535\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/44529"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=44535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=44535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=44535"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=44535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}