Files

1 line
18 KiB
JavaScript

"use strict";(globalThis.webpackChunkblog=globalThis.webpackChunkblog||[]).push([[5155],{24060:(n,e,r)=>{r.r(e),r.d(e,{assets:()=>l,contentTitle:()=>t,default:()=>h,frontMatter:()=>s,metadata:()=>a,toc:()=>i});const a=JSON.parse('{"id":"\u4e2a\u4eba\u77e5\u8bc6\u5e93/AI/PyTorch/\u7b2c\u4e8c\u7ae0/2.2 \u81ea\u52a8\u6c42\u5bfc","title":"2.2 \u81ea\u52a8\u6c42\u5bfc.md","description":"|\u4f5c\u8005|\u4fee\u8ba2\u65f6\u95f4|","source":"@site/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/10.AI/01.PyTorch/02.\u7b2c\u4e8c\u7ae0/2.2 \u81ea\u52a8\u6c42\u5bfc.md","sourceDirName":"\u4e2a\u4eba\u77e5\u8bc6\u5e93/10.AI/01.PyTorch/02.\u7b2c\u4e8c\u7ae0","slug":"/\u4e2a\u4eba\u77e5\u8bc6\u5e93/AI/PyTorch/\u7b2c\u4e8c\u7ae0/2.2 \u81ea\u52a8\u6c42\u5bfc","permalink":"/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/AI/PyTorch/\u7b2c\u4e8c\u7ae0/2.2 \u81ea\u52a8\u6c42\u5bfc","draft":false,"unlisted":false,"editUrl":"https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/10.AI/01.PyTorch/02.\u7b2c\u4e8c\u7ae0/2.2 \u81ea\u52a8\u6c42\u5bfc.md","tags":[],"version":"current","frontMatter":{},"sidebar":"tutorialSidebar","previous":{"title":"2.1 \u5f20\u91cf.md","permalink":"/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/AI/PyTorch/\u7b2c\u4e8c\u7ae0/2.1 \u5f20\u91cf"},"next":{"title":"2.3 \u5e76\u884c\u8ba1\u7b97\u7b80\u4ecb.md","permalink":"/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/AI/PyTorch/\u7b2c\u4e8c\u7ae0/2.3 \u5e76\u884c\u8ba1\u7b97\u7b80\u4ecb"}}');var d=r(74848),c=r(28453);const s={},t="2.2 \u81ea\u52a8\u6c42\u5bfc.md",l={},i=[{value:"Autograd\u7b80\u4ecb",id:"autograd\u7b80\u4ecb",level:2},{value:"2.2.1 \u68af\u5ea6",id:"221-\u68af\u5ea6",level:2}];function o(n){const e={code:"code",h1:"h1",h2:"h2",header:"header",img:"img",li:"li",p:"p",pre:"pre",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",ul:"ul",...(0,c.R)(),...n.components};return(0,d.jsxs)(d.Fragment,{children:[(0,d.jsx)(e.header,{children:(0,d.jsx)(e.h1,{id:"22-\u81ea\u52a8\u6c42\u5bfcmd",children:"2.2 \u81ea\u52a8\u6c42\u5bfc.md"})}),"\n\n\n\n\n\n\n\n\n\n\n\n\n\n",(0,d.jsxs)(e.table,{children:[(0,d.jsx)(e.thead,{children:(0,d.jsxs)(e.tr,{children:[(0,d.jsx)(e.th,{style:{textAlign:"left"},children:"\u4f5c\u8005"}),(0,d.jsx)(e.th,{style:{textAlign:"left"},children:"\u4fee\u8ba2\u65f6\u95f4"})]})}),(0,d.jsx)(e.tbody,{children:(0,d.jsxs)(e.tr,{children:[(0,d.jsx)(e.td,{style:{textAlign:"left"},children:(0,d.jsx)(e.img,{src:"https://img.shields.io/badge/wjlin0-%E4%B8%AA%E4%BA%BA%E7%9F%A5%E8%AF%86%E5%BA%93-green",alt:"wjlin0"})}),(0,d.jsx)(e.td,{style:{textAlign:"left"},children:"2026-06-07 20:53:13"})]})})]}),"\n\n\n\n\n\n\n\n\n\n\n\n\n\n",(0,d.jsxs)(e.table,{children:[(0,d.jsx)(e.thead,{children:(0,d.jsxs)(e.tr,{children:[(0,d.jsx)(e.th,{style:{textAlign:"left"},children:"\u4f5c\u8005"}),(0,d.jsx)(e.th,{style:{textAlign:"left"},children:"\u4fee\u8ba2\u65f6\u95f4"})]})}),(0,d.jsx)(e.tbody,{children:(0,d.jsxs)(e.tr,{children:[(0,d.jsx)(e.td,{style:{textAlign:"left"},children:(0,d.jsx)(e.img,{src:"https://img.shields.io/badge/wjlin0-%E4%B8%AA%E4%BA%BA%E7%9F%A5%E8%AF%86%E5%BA%93-green",alt:"wjlin0"})}),(0,d.jsx)(e.td,{style:{textAlign:"left"},children:"2025-10-31 15:44:29"})]})})]}),"\n",(0,d.jsx)(e.h1,{id:"22-\u81ea\u52a8\u6c42\u5bfc",children:"2.2 \u81ea\u52a8\u6c42\u5bfc"}),"\n",(0,d.jsxs)(e.p,{children:["PyTorch \u4e2d\uff0c\u6240\u6709\u795e\u7ecf\u7f51\u7edc\u7684\u6838\u5fc3\u662f ",(0,d.jsx)(e.code,{children:"autograd "}),"\u5305\u3002autograd\u5305\u4e3a\u5f20\u91cf\u4e0a\u7684\u6240\u6709\u64cd\u4f5c\u63d0\u4f9b\u4e86\u81ea\u52a8\u6c42\u5bfc\u673a\u5236\u3002\u5b83\u662f\u4e00\u4e2a\u5728\u8fd0\u884c\u65f6\u5b9a\u4e49 ( define-by-run \uff09\u7684\u6846\u67b6\uff0c\u8fd9\u610f\u5473\u7740\u53cd\u5411\u4f20\u64ad\u662f\u6839\u636e\u4ee3\u7801\u5982\u4f55\u8fd0\u884c\u6765\u51b3\u5b9a\u7684\uff0c\u5e76\u4e14\u6bcf\u6b21\u8fed\u4ee3\u53ef\u4ee5\u662f\u4e0d\u540c\u7684\u3002"]}),"\n",(0,d.jsx)(e.p,{children:"\u7ecf\u8fc7\u672c\u8282\u7684\u5b66\u4e60\uff0c\u4f60\u5c06\u6536\u83b7\uff1a"}),"\n",(0,d.jsxs)(e.ul,{children:["\n",(0,d.jsx)(e.li,{children:"autograd\u7684\u6c42\u5bfc\u673a\u5236"}),"\n",(0,d.jsx)(e.li,{children:"\u68af\u5ea6\u7684\u53cd\u5411\u4f20\u64ad"}),"\n"]}),"\n",(0,d.jsx)(e.h2,{id:"autograd\u7b80\u4ecb",children:"Autograd\u7b80\u4ecb"}),"\n",(0,d.jsxs)(e.p,{children:[(0,d.jsx)(e.code,{children:"torch.Tensor "}),"\u662f\u8fd9\u4e2a\u5305\u7684\u6838\u5fc3\u7c7b\u3002\u5982\u679c\u8bbe\u7f6e\u5b83\u7684\u5c5e\u6027",(0,d.jsx)(e.code,{children:" .requires_grad"})," \u4e3a ",(0,d.jsx)(e.code,{children:"True"}),"\uff0c\u90a3\u4e48\u5b83\u5c06\u4f1a\u8ffd\u8e2a\u5bf9\u4e8e\u8be5\u5f20\u91cf\u7684\u6240\u6709\u64cd\u4f5c\u3002\u5f53\u5b8c\u6210\u8ba1\u7b97\u540e\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528",(0,d.jsx)(e.code,{children:" .backward()"}),"\uff0c\u6765\u81ea\u52a8\u8ba1\u7b97\u6240\u6709\u7684\u68af\u5ea6\u3002\u8fd9\u4e2a\u5f20\u91cf\u7684\u6240\u6709\u68af\u5ea6\u5c06\u4f1a\u81ea\u52a8\u7d2f\u52a0\u5230",(0,d.jsx)(e.code,{children:".grad"}),"\u5c5e\u6027\u3002"]}),"\n",(0,d.jsx)(e.p,{children:"\u6ce8\u610f\uff1a\u5728 y.backward() \u65f6\uff0c\u5982\u679c y \u662f\u6807\uf97e\uff0c\u5219\u4e0d\u9700\u8981\u4e3a backward() \u4f20\u5165\u4efb\u4f55\u53c2\u6570\uff1b\u5426\u5219\uff0c\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u4e0e y \u540c\u5f62\u7684Tensor\u3002"}),"\n",(0,d.jsxs)(e.p,{children:["\u8981\u963b\u6b62\u4e00\u4e2a\u5f20\u91cf\u88ab\u8ddf\u8e2a\u5386\u53f2\uff0c\u53ef\u4ee5\u8c03\u7528",(0,d.jsx)(e.code,{children:".detach()"}),"\u65b9\u6cd5\u5c06\u5176\u4e0e\u8ba1\u7b97\u5386\u53f2\u5206\u79bb\uff0c\u5e76\u963b\u6b62\u5b83\u672a\u6765\u7684\u8ba1\u7b97\u8bb0\u5f55\u88ab\u8ddf\u8e2a\u3002\u4e3a\u4e86\u9632\u6b62\u8ddf\u8e2a\u5386\u53f2\u8bb0\u5f55(\u548c\u4f7f\u7528\u5185\u5b58\uff09\uff0c\u53ef\u4ee5\u5c06\u4ee3\u7801\u5757\u5305\u88c5\u5728 ",(0,d.jsx)(e.code,{children:"with torch.no_grad(): "}),"\u4e2d\u3002\u5728\u8bc4\u4f30\u6a21\u578b\u65f6\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u6a21\u578b\u53ef\u80fd\u5177\u6709 ",(0,d.jsx)(e.code,{children:"requires_grad = True"})," \u7684\u53ef\u8bad\u7ec3\u7684\u53c2\u6570\uff0c\u4f46\u662f\u6211\u4eec\u4e0d\u9700\u8981\u5728\u6b64\u8fc7\u7a0b\u4e2d\u5bf9\u4ed6\u4eec\u8fdb\u884c\u68af\u5ea6\u8ba1\u7b97\u3002"]}),"\n",(0,d.jsxs)(e.p,{children:["\u8fd8\u6709\u4e00\u4e2a\u7c7b\u5bf9\u4e8e",(0,d.jsx)(e.code,{children:"autograd"}),"\u7684\u5b9e\u73b0\u975e\u5e38\u91cd\u8981\uff1a",(0,d.jsx)(e.code,{children:"Function"}),"\u3002",(0,d.jsx)(e.code,{children:"Tensor "}),"\u548c",(0,d.jsx)(e.code,{children:" Function"})," \u4e92\u76f8\u8fde\u63a5\u751f\u6210\u4e86\u4e00\u4e2a\u65e0\u73af\u56fe (acyclic graph)\uff0c\u5b83\u7f16\u7801\u4e86\u5b8c\u6574\u7684\u8ba1\u7b97\u5386\u53f2\u3002\u6bcf\u4e2a\u5f20\u91cf\u90fd\u6709\u4e00\u4e2a",(0,d.jsx)(e.code,{children:".grad_fn"}),"\u5c5e\u6027\uff0c\u8be5\u5c5e\u6027\u5f15\u7528\u4e86\u521b\u5efa ",(0,d.jsx)(e.code,{children:"Tensor "}),"\u81ea\u8eab\u7684",(0,d.jsx)(e.code,{children:"Function"}),"(\u9664\u975e\u8fd9\u4e2a\u5f20\u91cf\u662f\u7528\u6237\u624b\u52a8\u521b\u5efa\u7684\uff0c\u5373\u8fd9\u4e2a\u5f20\u91cf\u7684",(0,d.jsx)(e.code,{children:"grad_fn"}),"\u662f ",(0,d.jsx)(e.code,{children:"None"})," )\u3002\u4e0b\u9762\u7ed9\u51fa\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f20\u91cf\u7531\u7528\u6237\u624b\u52a8\u521b\u5efa\uff0c\u56e0\u6b64grad_fn\u8fd4\u56de\u7ed3\u679c\u662fNone\u3002"]}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"from __future__ import print_function\nimport torch\nx = torch.randn(3,3,requires_grad=True)\nprint(x.grad_fn)\n"})}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"None\n"})}),"\n",(0,d.jsxs)(e.p,{children:["\u5982\u679c\u9700\u8981\u8ba1\u7b97\u5bfc\u6570\uff0c\u53ef\u4ee5\u5728 ",(0,d.jsx)(e.code,{children:"Tensor"})," \u4e0a\u8c03\u7528 ",(0,d.jsx)(e.code,{children:".backward()"}),"\u3002\u5982\u679c",(0,d.jsx)(e.code,{children:" Tensor"})," \u662f\u4e00\u4e2a\u6807\u91cf(\u5373\u5b83\u5305\u542b\u4e00\u4e2a\u5143\u7d20\u7684\u6570\u636e\uff09\uff0c\u5219\u4e0d\u9700\u8981\u4e3a ",(0,d.jsx)(e.code,{children:"backward() "}),"\u6307\u5b9a\u4efb\u4f55\u53c2\u6570\uff0c\u4f46\u662f\u5982\u679c\u5b83\u6709\u66f4\u591a\u7684\u5143\u7d20\uff0c\u5219\u9700\u8981\u6307\u5b9a\u4e00\u4e2a",(0,d.jsx)(e.code,{children:"gradient"}),"\u53c2\u6570\uff0c\u8be5\u53c2\u6570\u662f\u5f62\u72b6\u5339\u914d\u7684\u5f20\u91cf\u3002"]}),"\n",(0,d.jsxs)(e.p,{children:["\u521b\u5efa\u4e00\u4e2a\u5f20\u91cf\u5e76\u8bbe\u7f6e",(0,d.jsx)(e.code,{children:"requires_grad=True"}),"\u7528\u6765\u8ffd\u8e2a\u5176\u8ba1\u7b97\u5386\u53f2"]}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"x = torch.ones(2, 2, requires_grad=True)\nprint(x)\n"})}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"tensor([[1., 1.],\n [1., 1.]], requires_grad=True)\n"})}),"\n",(0,d.jsx)(e.p,{children:"\u5bf9\u8fd9\u4e2a\u5f20\u91cf\u505a\u4e00\u6b21\u8fd0\u7b97\uff1a"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"y = x**2\nprint(y)\n"})}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"tensor([[1., 1.],\n [1., 1.]], grad_fn=<PowBackward0>)\n"})}),"\n",(0,d.jsxs)(e.p,{children:[(0,d.jsx)(e.code,{children:"y"}),"\u662f\u8ba1\u7b97\u7684\u7ed3\u679c\uff0c\u6240\u4ee5\u5b83\u6709",(0,d.jsx)(e.code,{children:"grad_fn"}),"\u5c5e\u6027\u3002"]}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"print(y.grad_fn)\n"})}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"<PowBackward0 object at 0x000001CB45988C70>\n"})}),"\n",(0,d.jsx)(e.p,{children:"\u5bf9 y \u8fdb\u884c\u66f4\u591a\u64cd\u4f5c"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"z = y * y * 3\nout = z.mean()\n\nprint(z, out)\n"})}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"tensor([[3., 3.],\n [3., 3.]], grad_fn=<MulBackward0>) tensor(3., grad_fn=<MeanBackward0>)\n"})}),"\n",(0,d.jsxs)(e.p,{children:[(0,d.jsx)(e.code,{children:".requires_grad_(...) "}),"\u539f\u5730\u6539\u53d8\u4e86\u73b0\u6709\u5f20\u91cf\u7684",(0,d.jsx)(e.code,{children:"requires_grad"}),"\u6807\u5fd7\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u7684\u8bdd\uff0c\u9ed8\u8ba4\u8f93\u5165\u7684\u8fd9\u4e2a\u6807\u5fd7\u662f",(0,d.jsx)(e.code,{children:" False"}),"\u3002"]}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"a = torch.randn(2, 2) # \u7f3a\u5931\u60c5\u51b5\u4e0b\u9ed8\u8ba4 requires_grad = False\na = ((a * 3) / (a - 1))\nprint(a.requires_grad)\na.requires_grad_(True)\nprint(a.requires_grad)\nb = (a * a).sum()\nprint(b.grad_fn)\n"})}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"False\nTrue\n<SumBackward0 object at 0x000001CB4A19FB50>\n"})}),"\n",(0,d.jsx)(e.h2,{id:"221-\u68af\u5ea6",children:"2.2.1 \u68af\u5ea6"}),"\n",(0,d.jsxs)(e.p,{children:["\u73b0\u5728\u5f00\u59cb\u8fdb\u884c\u53cd\u5411\u4f20\u64ad\uff0c\u56e0\u4e3a",(0,d.jsx)(e.code,{children:" out"})," \u662f\u4e00\u4e2a\u6807\u91cf\uff0c\u56e0\u6b64",(0,d.jsx)(e.code,{children:"out.backward()"}),"\u548c",(0,d.jsx)(e.code,{children:" out.backward(torch.tensor(1.))"})," \u7b49\u4ef7\u3002"]}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"out.backward()\n"})}),"\n",(0,d.jsxs)(e.p,{children:["\u8f93\u51fa\u5bfc\u6570",(0,d.jsx)(e.code,{children:" d(out)/dx"})]}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"print(x.grad)\n"})}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"tensor([[3., 3.],\n [3., 3.]])\n"})}),"\n",(0,d.jsxs)(e.p,{children:["\u6570\u5b66\u4e0a\uff0c\u82e5\u6709\u5411\u91cf\u51fd\u6570$\\vec{y}=f(\\vec{x})$\uff0c\u90a3\u4e48 $\\vec{y}$ \u5173\u4e8e $\\vec{x}$ \u7684\u68af\u5ea6\u5c31\u662f\u4e00\u4e2a\u96c5\u53ef\u6bd4\u77e9\u9635\uff1a\n$\nJ=\\left(\\begin{array}{ccc}\\frac{\\partial y_{1}}{\\partial x_{1}} & \\cdots & \\frac{\\partial y_{1}}{\\partial x_{n}} \\ \\vdots & \\ddots & \\vdots \\ \\frac{\\partial y_{m}}{\\partial x_{1}} & \\cdots & \\frac{\\partial y_{m}}{\\partial x_{n}}\\end{array}\\right)\n$\n\u800c ",(0,d.jsx)(e.code,{children:"torch.autograd"})," \u8fd9\u4e2a\u5305\u5c31\u662f\u7528\u6765\u8ba1\u7b97\u4e00\u4e9b\u96c5\u53ef\u6bd4\u77e9\u9635\u7684\u4e58\u79ef\u7684\u3002\uf9b5\u5982\uff0c\u5982\u679c $v$ \u662f\u4e00\u4e2a\u6807\uf97e\u51fd\u6570 $l = g(\\vec{y})$ \u7684\u68af\u5ea6\uff1a\n$\nv=\\left(\\begin{array}{lll}\\frac{\\partial l}{\\partial y_{1}} & \\cdots & \\frac{\\partial l}{\\partial y_{m}}\\end{array}\\right)\n$\n\u7531\u94fe\u5f0f\u6cd5\u5219\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\uff1a\n$\nv J=\\left(\\begin{array}{lll}\\frac{\\partial l}{\\partial y_{1}} & \\cdots & \\frac{\\partial l}{\\partial y_{m}}\\end{array}\\right)\\left(\\begin{array}{ccc}\\frac{\\partial y_{1}}{\\partial x_{1}} & \\cdots & \\frac{\\partial y_{1}}{\\partial x_{n}} \\ \\vdots & \\ddots & \\vdots \\ \\frac{\\partial y_{m}}{\\partial x_{1}} & \\cdots & \\frac{\\partial y_{m}}{\\partial x_{n}}\\end{array}\\right)=\\left(\\begin{array}{lll}\\frac{\\partial l}{\\partial x_{1}} & \\cdots & \\frac{\\partial l}{\\partial x_{n}}\\end{array}\\right)\n$"]}),"\n",(0,d.jsx)(e.p,{children:"\u6ce8\u610f\uff1agrad\u5728\u53cd\u5411\u4f20\u64ad\u8fc7\u7a0b\u4e2d\u662f\u7d2f\u52a0\u7684(accumulated)\uff0c\u8fd9\u610f\u5473\u7740\u6bcf\u4e00\u6b21\u8fd0\ufa08\u53cd\u5411\u4f20\u64ad\uff0c\u68af\u5ea6\u90fd\u4f1a\u7d2f\u52a0\u4e4b\u524d\u7684\u68af\u5ea6\uff0c\u6240\u4ee5\u4e00\u822c\u5728\u53cd\u5411\u4f20\u64ad\u4e4b\u524d\u9700\u628a\u68af\u5ea6\u6e05\u96f6\u3002"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"# \u518d\u6765\u53cd\u5411\u4f20\u64ad\u2f00\u4e00\u6b21\uff0c\u6ce8\u610fgrad\u662f\u7d2f\u52a0\u7684\nout2 = x.sum()\nout2.backward()\nprint(x.grad)\n\nout3 = x.sum()\nx.grad.data.zero_()\nout3.backward()\nprint(x.grad)\n"})}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"tensor([[4., 4.],\n [4., 4.]])\ntensor([[1., 1.],\n [1., 1.]])\n"})}),"\n",(0,d.jsx)(e.p,{children:"\u73b0\u5728\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u96c5\u53ef\u6bd4\u5411\u91cf\u79ef\u7684\u4f8b\u5b50\uff1a"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"x = torch.randn(3, requires_grad=True)\nprint(x)\n\ny = x * 2\ni = 0\nwhile y.data.norm() < 1000:\n y = y * 2\n i = i + 1\nprint(y)\nprint(i)\n"})}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"tensor([-0.9332, 1.9616, 0.1739], requires_grad=True)\ntensor([-477.7843, 1004.3264, 89.0424], grad_fn=<MulBackward0>)\n8\n"})}),"\n",(0,d.jsxs)(e.p,{children:["\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c",(0,d.jsx)(e.code,{children:"y "}),"\u4e0d\u518d\u662f\u6807\u91cf\u3002",(0,d.jsx)(e.code,{children:"torch.autograd"})," \u4e0d\u80fd\u76f4\u63a5\u8ba1\u7b97\u5b8c\u6574\u7684\u96c5\u53ef\u6bd4\u77e9\u9635\uff0c\u4f46\u662f\u5982\u679c\u6211\u4eec\u53ea\u60f3\u8981\u96c5\u53ef\u6bd4\u5411\u91cf\u79ef\uff0c\u53ea\u9700\u5c06\u8fd9\u4e2a\u5411\u91cf\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9 ",(0,d.jsx)(e.code,{children:"backward\uff1a"})]}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"v = torch.tensor([0.1, 1.0, 0.0001], dtype=torch.float)\ny.backward(v)\n\nprint(x.grad)\n"})}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"tensor([5.1200e+01, 5.1200e+02, 5.1200e-02])\n"})}),"\n",(0,d.jsxs)(e.p,{children:["\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5c06\u4ee3\u7801\u5757\u5305\u88c5\u5728",(0,d.jsx)(e.code,{children:" with torch.no_grad():"})," \u4e2d\uff0c\u6765\u963b\u6b62 autograd \u8ddf\u8e2a\u8bbe\u7f6e\u4e86",(0,d.jsx)(e.code,{children:".requires_grad=True"}),"\u7684\u5f20\u91cf\u7684\u5386\u53f2\u8bb0\u5f55\u3002"]}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"print(x.requires_grad)\nprint((x ** 2).requires_grad)\n\nwith torch.no_grad():\n print((x ** 2).requires_grad)\n"})}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"True\nTrue\nFalse\n"})}),"\n",(0,d.jsx)(e.p,{children:"\u5982\u679c\u6211\u4eec\u60f3\u8981\u4fee\u6539 tensor \u7684\u6570\u503c\uff0c\u4f46\u662f\u53c8\uf967\u5e0c\u671b\u88ab autograd \u8bb0\u5f55(\u5373\uf967\u4f1a\u5f71\u54cd\u53cd\u5411\u4f20\u64ad)\uff0c \u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5bf9 tensor.data \u8fdb\ufa08\u64cd\u4f5c\u3002"}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"x = torch.ones(1,requires_grad=True)\n\nprint(x.data) # \u8fd8\u662f\u4e00\u4e2atensor\nprint(x.data.requires_grad) # \u4f46\u662f\u5df2\u7ecf\u662f\u72ec\u7acb\u4e8e\u8ba1\u7b97\u56fe\u4e4b\u5916\n\ny = 2 * x\nx.data *= 100 # \u53ea\u6539\u53d8\u4e86\u503c\uff0c\u4e0d\u4f1a\u8bb0\u5f55\u5728\u8ba1\u7b97\u56fe\uff0c\u6240\u4ee5\u4e0d\u4f1a\u5f71\u54cd\u68af\u5ea6\u4f20\u64ad\n\ny.backward()\nprint(x) # \u66f4\u6539data\u7684\u503c\u4e5f\u4f1a\u5f71\u54cdtensor\u7684\u503c \nprint(x.grad)\n"})}),"\n",(0,d.jsx)(e.pre,{children:(0,d.jsx)(e.code,{className:"language-python",children:"tensor([1.])\nFalse\ntensor([100.], requires_grad=True)\ntensor([2.])\n"})})]})}function h(n={}){const{wrapper:e}={...(0,c.R)(),...n.components};return e?(0,d.jsx)(e,{...n,children:(0,d.jsx)(o,{...n})}):o(n)}},28453:(n,e,r)=>{r.d(e,{R:()=>s,x:()=>t});var a=r(96540);const d={},c=a.createContext(d);function s(n){const e=a.useContext(c);return a.useMemo(function(){return"function"==typeof n?n(e):{...e,...n}},[e,n])}function t(n){let e;return e=n.disableParentContext?"function"==typeof n.components?n.components(d):n.components||d:s(n.components),a.createElement(c.Provider,{value:e},n.children)}}}]);