Files

1 line
14 KiB
JavaScript

"use strict";(globalThis.webpackChunkblog=globalThis.webpackChunkblog||[]).push([[6496],{28453:(n,e,t)=>{t.d(e,{R:()=>l,x:()=>c});var i=t(96540);const r={},s=i.createContext(r);function l(n){const e=i.useContext(s);return i.useMemo(function(){return"function"==typeof n?n(e):{...e,...n}},[e,n])}function c(n){let e;return e=n.disableParentContext?"function"==typeof n.components?n.components(r):n.components||r:l(n.components),i.createElement(s.Provider,{value:e},n.children)}},53956:(n,e,t)=>{t.r(e),t.d(e,{assets:()=>d,contentTitle:()=>c,default:()=>a,frontMatter:()=>l,metadata:()=>i,toc:()=>o});const i=JSON.parse('{"id":"\u4e2a\u4eba\u77e5\u8bc6\u5e93/AI/PyTorch/\u7b2c\u4e09\u7ae0/3.5 \u6a21\u578b\u521d\u59cb\u5316","title":"3.5 \u6a21\u578b\u521d\u59cb\u5316.md","description":"|\u4f5c\u8005|\u4fee\u8ba2\u65f6\u95f4|","source":"@site/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/10.AI/01.PyTorch/03.\u7b2c\u4e09\u7ae0/3.5 \u6a21\u578b\u521d\u59cb\u5316.md","sourceDirName":"\u4e2a\u4eba\u77e5\u8bc6\u5e93/10.AI/01.PyTorch/03.\u7b2c\u4e09\u7ae0","slug":"/\u4e2a\u4eba\u77e5\u8bc6\u5e93/AI/PyTorch/\u7b2c\u4e09\u7ae0/3.5 \u6a21\u578b\u521d\u59cb\u5316","permalink":"/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/AI/PyTorch/\u7b2c\u4e09\u7ae0/3.5 \u6a21\u578b\u521d\u59cb\u5316","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/03.\u7b2c\u4e09\u7ae0/3.5 \u6a21\u578b\u521d\u59cb\u5316.md","tags":[],"version":"current","frontMatter":{},"sidebar":"tutorialSidebar","previous":{"title":"3.4 \u6a21\u578b\u6784\u5efa.md","permalink":"/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/AI/PyTorch/\u7b2c\u4e09\u7ae0/3.4 \u6a21\u578b\u6784\u5efa"},"next":{"title":"3.6 \u635f\u5931\u51fd\u6570.md","permalink":"/docs/\u4e2a\u4eba\u77e5\u8bc6\u5e93/AI/PyTorch/\u7b2c\u4e09\u7ae0/3.6 \u635f\u5931\u51fd\u6570"}}');var r=t(74848),s=t(28453);const l={},c="3.5 \u6a21\u578b\u521d\u59cb\u5316.md",d={},o=[{value:"torch.nn.init\u5185\u5bb9",id:"torchnninit\u5185\u5bb9",level:2},{value:"torch.nn.init\u4f7f\u7528",id:"torchnninit\u4f7f\u7528",level:2},{value:"\u521d\u59cb\u5316\u51fd\u6570\u7684\u5c01\u88c5",id:"\u521d\u59cb\u5316\u51fd\u6570\u7684\u5c01\u88c5",level:2}];function h(n){const e={a:"a",code:"code",h1:"h1",h2:"h2",header:"header",img:"img",li:"li",p:"p",pre:"pre",strong:"strong",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",ul:"ul",...(0,s.R)(),...n.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(e.header,{children:(0,r.jsx)(e.h1,{id:"35-\u6a21\u578b\u521d\u59cb\u5316md",children:"3.5 \u6a21\u578b\u521d\u59cb\u5316.md"})}),"\n\n\n\n\n\n\n\n\n\n\n\n\n\n",(0,r.jsxs)(e.table,{children:[(0,r.jsx)(e.thead,{children:(0,r.jsxs)(e.tr,{children:[(0,r.jsx)(e.th,{style:{textAlign:"left"},children:"\u4f5c\u8005"}),(0,r.jsx)(e.th,{style:{textAlign:"left"},children:"\u4fee\u8ba2\u65f6\u95f4"})]})}),(0,r.jsx)(e.tbody,{children:(0,r.jsxs)(e.tr,{children:[(0,r.jsx)(e.td,{style:{textAlign:"left"},children:(0,r.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,r.jsx)(e.td,{style:{textAlign:"left"},children:"2026-06-07 20:53:15"})]})})]}),"\n\n\n\n\n\n\n\n\n\n\n\n\n\n",(0,r.jsxs)(e.table,{children:[(0,r.jsx)(e.thead,{children:(0,r.jsxs)(e.tr,{children:[(0,r.jsx)(e.th,{style:{textAlign:"left"},children:"\u4f5c\u8005"}),(0,r.jsx)(e.th,{style:{textAlign:"left"},children:"\u4fee\u8ba2\u65f6\u95f4"})]})}),(0,r.jsx)(e.tbody,{children:(0,r.jsxs)(e.tr,{children:[(0,r.jsx)(e.td,{style:{textAlign:"left"},children:(0,r.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,r.jsx)(e.td,{style:{textAlign:"left"},children:"2025-10-31 15:44:29"})]})})]}),"\n",(0,r.jsx)(e.h1,{id:"35-\u6a21\u578b\u521d\u59cb\u5316",children:"3.5 \u6a21\u578b\u521d\u59cb\u5316"}),"\n",(0,r.jsxs)(e.p,{children:["\u5728\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u7684\u8bad\u7ec3\u4e2d\uff0c\u6743\u91cd\u7684\u521d\u59cb\u503c\u6781\u4e3a\u91cd\u8981\u3002\u4e00\u4e2a\u597d\u7684\u521d\u59cb\u503c\uff0c\u4f1a\u4f7f\u6a21\u578b\u6536\u655b\u901f\u5ea6\u63d0\u9ad8\uff0c\u4f7f\u6a21\u578b\u51c6\u786e\u7387\u66f4\u7cbe\u786e\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e0d\u4f7f\u7528\u51680\u521d\u59cb\u503c\u8bad\u7ec3\u7f51\u7edc\u3002\u4e3a\u4e86\u5229\u4e8e\u8bad\u7ec3\u548c\u51cf\u5c11\u6536\u655b\u65f6\u95f4\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6a21\u578b\u8fdb\u884c\u5408\u7406\u7684\u521d\u59cb\u5316\u3002PyTorch\u4e5f\u5728",(0,r.jsx)(e.code,{children:"torch.nn.init"}),"\u4e2d\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u5e38\u7528\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u3002\n\u901a\u8fc7\u672c\u7ae0\u5b66\u4e60\uff0c\u4f60\u5c06\u5b66\u4e60\u5230\u4ee5\u4e0b\u5185\u5bb9\uff1a"]}),"\n",(0,r.jsxs)(e.ul,{children:["\n",(0,r.jsx)(e.li,{children:"\u5e38\u89c1\u7684\u521d\u59cb\u5316\u51fd\u6570"}),"\n",(0,r.jsx)(e.li,{children:"\u521d\u59cb\u5316\u51fd\u6570\u7684\u4f7f\u7528"}),"\n"]}),"\n",(0,r.jsx)(e.h2,{id:"torchnninit\u5185\u5bb9",children:"torch.nn.init\u5185\u5bb9"}),"\n",(0,r.jsxs)(e.p,{children:["\u901a\u8fc7\u8bbf\u95eetorch.nn.init\u7684\u5b98\u65b9\u6587\u6863",(0,r.jsx)(e.a,{href:"https://pytorch.org/docs/stable/nn.init.html",children:"\u94fe\u63a5"})," \uff0c\u6211\u4eec\u53d1\u73b0",(0,r.jsx)(e.code,{children:"torch.nn.init"}),"\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u521d\u59cb\u5316\u65b9\u6cd5\uff1a\n1 . ",(0,r.jsx)(e.code,{children:"torch.nn.init.uniform_"}),"(tensor, a=0.0, b=1.0)\n2 . ",(0,r.jsx)(e.code,{children:"torch.nn.init.normal_"}),"(tensor, mean=0.0, std=1.0)\n3 . ",(0,r.jsx)(e.code,{children:"torch.nn.init.constant_"}),"(tensor, val)\n4 . ",(0,r.jsx)(e.code,{children:"torch.nn.init.ones_"}),"(tensor)\n5 . ",(0,r.jsx)(e.code,{children:"torch.nn.init.zeros_"}),"(tensor)\n6 . ",(0,r.jsx)(e.code,{children:"torch.nn.init.eye_"}),"(tensor)\n7 . ",(0,r.jsx)(e.code,{children:"torch.nn.init.dirac_"}),"(tensor, groups=1)\n8 . ",(0,r.jsx)(e.code,{children:"torch.nn.init.xavier_uniform_"}),"(tensor, gain=1.0)\n9 . ",(0,r.jsx)(e.code,{children:"torch.nn.init.xavier_normal_"}),"(tensor, gain=1.0)\n10 . ",(0,r.jsx)(e.code,{children:"torch.nn.init.kaiming_uniform_"}),"(tensor, a=0, mode='fan__in', nonlinearity='leaky_relu')\n11 . ",(0,r.jsx)(e.code,{children:"torch.nn.init.kaiming_normal_"}),"(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu')\n12 . ",(0,r.jsx)(e.code,{children:"torch.nn.init.orthogonal_"}),"(tensor, gain=1)\n13 . ",(0,r.jsx)(e.code,{children:"torch.nn.init.sparse_"}),"(tensor, sparsity, std=0.01)\n14 . ",(0,r.jsx)(e.code,{children:"torch.nn.init.calculate_gain"}),"(nonlinearity, param=None)\n\u5173\u4e8e\u8ba1\u7b97\u589e\u76ca\u5982\u4e0b\u8868\uff1a"]}),"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n",(0,r.jsxs)(e.table,{children:[(0,r.jsx)(e.thead,{children:(0,r.jsxs)(e.tr,{children:[(0,r.jsx)(e.th,{children:"nonlinearity"}),(0,r.jsx)(e.th,{children:"gain"})]})}),(0,r.jsxs)(e.tbody,{children:[(0,r.jsxs)(e.tr,{children:[(0,r.jsx)(e.td,{children:"Linear/Identity"}),(0,r.jsx)(e.td,{children:"1"})]}),(0,r.jsxs)(e.tr,{children:[(0,r.jsx)(e.td,{children:"Conv{1,2,3}D"}),(0,r.jsx)(e.td,{children:"1"})]}),(0,r.jsxs)(e.tr,{children:[(0,r.jsx)(e.td,{children:"Sigmod"}),(0,r.jsx)(e.td,{children:"1"})]}),(0,r.jsxs)(e.tr,{children:[(0,r.jsx)(e.td,{children:"Tanh"}),(0,r.jsx)(e.td,{children:"5/3"})]}),(0,r.jsxs)(e.tr,{children:[(0,r.jsx)(e.td,{children:"ReLU"}),(0,r.jsx)(e.td,{children:"sqrt(2)"})]}),(0,r.jsxs)(e.tr,{children:[(0,r.jsx)(e.td,{children:"Leaky Relu"}),(0,r.jsx)(e.td,{children:"sqrt(2/1+neg_slop^2)"})]})]})]}),"\n",(0,r.jsxs)(e.p,{children:["\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\u8fd9\u4e9b\u51fd\u6570\u9664\u4e86",(0,r.jsx)(e.code,{children:"calculate_gain"}),"\uff0c\u6240\u6709\u51fd\u6570\u7684\u540e\u7f00\u90fd\u5e26\u6709\u4e0b\u5212\u7ebf\uff0c\u610f\u5473\u7740\u8fd9\u4e9b\u51fd\u6570\u5c06\u4f1a\u76f4\u63a5\u539f\u5730\u66f4\u6539\u8f93\u5165\u5f20\u91cf\u7684\u503c\u3002"]}),"\n",(0,r.jsx)(e.h2,{id:"torchnninit\u4f7f\u7528",children:"torch.nn.init\u4f7f\u7528"}),"\n",(0,r.jsxs)(e.p,{children:["\u6211\u4eec\u901a\u5e38\u4f1a\u6839\u636e\u5b9e\u9645\u6a21\u578b\u6765\u4f7f\u7528",(0,r.jsx)(e.code,{children:"torch.nn.init"}),"\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u901a\u5e38\u4f7f\u7528",(0,r.jsx)(e.code,{children:"isinstance()"}),"\u6765\u8fdb\u884c\u5224\u65ad\u6a21\u5757\uff08\u56de\u987e3.4\u6a21\u578b\u6784\u5efa\uff09\u5c5e\u4e8e\u4ec0\u4e48\u7c7b\u578b\u3002"]}),"\n",(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-python",children:"import torch\nimport torch.nn as nn\n\nconv = nn.Conv2d(1,3,3)\nlinear = nn.Linear(10,1)\n\nisinstance(conv,nn.Conv2d) # \u5224\u65adconv\u662f\u5426\u662fnn.Conv2d\u7c7b\u578b\nisinstance(linear,nn.Conv2d) # \u5224\u65adlinear\u662f\u5426\u662fnn.Conv2d\u7c7b\u578b\n"})}),"\n",(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-python",children:"True\nFalse\n"})}),"\n",(0,r.jsx)(e.p,{children:"\u5bf9\u4e8e\u4e0d\u540c\u7684\u7c7b\u578b\u5c42\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8bbe\u7f6e\u4e0d\u540c\u7684\u6743\u503c\u521d\u59cb\u5316\u7684\u65b9\u6cd5\u3002"}),"\n",(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-python",children:"# \u67e5\u770b\u968f\u673a\u521d\u59cb\u5316\u7684conv\u53c2\u6570\nconv.weight.data\n# \u67e5\u770blinear\u7684\u53c2\u6570\nlinear.weight.data\n"})}),"\n",(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-python",children:"tensor([[[[ 0.1174, 0.1071, 0.2977],\n [-0.2634, -0.0583, -0.2465],\n [ 0.1726, -0.0452, -0.2354]]],\n [[[ 0.1382, 0.1853, -0.1515],\n [ 0.0561, 0.2798, -0.2488],\n [-0.1288, 0.0031, 0.2826]]],\n [[[ 0.2655, 0.2566, -0.1276],\n [ 0.1905, -0.1308, 0.2933],\n [ 0.0557, -0.1880, 0.0669]]]])\n\ntensor([[-0.0089, 0.1186, 0.1213, -0.2569, 0.1381, 0.3125, 0.1118, -0.0063, -0.2330, 0.1956]])\n"})}),"\n",(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-python",children:"# \u5bf9conv\u8fdb\u884ckaiming\u521d\u59cb\u5316\ntorch.nn.init.kaiming_normal_(conv.weight.data)\nconv.weight.data\n# \u5bf9linear\u8fdb\u884c\u5e38\u6570\u521d\u59cb\u5316\ntorch.nn.init.constant_(linear.weight.data,0.3)\nlinear.weight.data\n"})}),"\n",(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-python",children:"tensor([[[[ 0.3249, -0.0500, 0.6703],\n [-0.3561, 0.0946, 0.4380],\n [-0.9426, 0.9116, 0.4374]]],\n [[[ 0.6727, 0.9885, 0.1635],\n [ 0.7218, -1.2841, -0.2970],\n [-0.9128, -0.1134, -0.3846]]],\n [[[ 0.2018, 0.4668, -0.0937],\n [-0.2701, -0.3073, 0.6686],\n [-0.3269, -0.0094, 0.3246]]]])\ntensor([[0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000, 0.3000,0.3000]])\n"})}),"\n",(0,r.jsx)(e.h2,{id:"\u521d\u59cb\u5316\u51fd\u6570\u7684\u5c01\u88c5",children:"\u521d\u59cb\u5316\u51fd\u6570\u7684\u5c01\u88c5"}),"\n",(0,r.jsxs)(e.p,{children:["\u4eba\u4eec\u5e38\u5e38\u5c06\u5404\u79cd\u521d\u59cb\u5316\u65b9\u6cd5\u5b9a\u4e49\u4e3a\u4e00\u4e2a",(0,r.jsx)(e.code,{children:"initialize_weights()"}),"\u7684\u51fd\u6570\u5e76\u5728\u6a21\u578b\u521d\u59cb\u540e\u8fdb\u884c\u4f7f\u7528\u3002"]}),"\n",(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-python",children:"def initialize_weights(model):\n\tfor m in model.modules():\n\t\t# \u5224\u65ad\u662f\u5426\u5c5e\u4e8eConv2d\n\t\tif isinstance(m, nn.Conv2d):\n\t\t\ttorch.nn.init.zeros_(m.weight.data)\n\t\t\t# \u5224\u65ad\u662f\u5426\u6709\u504f\u7f6e\n\t\t\tif m.bias is not None:\n\t\t\t\ttorch.nn.init.constant_(m.bias.data,0.3)\n\t\telif isinstance(m, nn.Linear):\n\t\t\ttorch.nn.init.normal_(m.weight.data, 0.1)\n\t\t\tif m.bias is not None:\n\t\t\t\ttorch.nn.init.zeros_(m.bias.data)\n\t\telif isinstance(m, nn.BatchNorm2d):\n\t\t\tm.weight.data.fill_(1) \t\t \n\t\t\tm.bias.data.zeros_()\t\n"})}),"\n",(0,r.jsx)(e.p,{children:"\u8fd9\u6bb5\u4ee3\u7801\u6d41\u7a0b\u662f\u904d\u5386\u5f53\u524d\u6a21\u578b\u7684\u6bcf\u4e00\u5c42\uff0c\u7136\u540e\u5224\u65ad\u5404\u5c42\u5c5e\u4e8e\u4ec0\u4e48\u7c7b\u578b\uff0c\u7136\u540e\u6839\u636e\u4e0d\u540c\u7c7b\u578b\u5c42\uff0c\u8bbe\u5b9a\u4e0d\u540c\u7684\u6743\u503c\u521d\u59cb\u5316\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u4f8b\u7a0b\u8fdb\u884c\u4e00\u4e2a\u7b80\u77ed\u7684\u6f14\u793a\uff1a"}),"\n",(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-python",children:'# \u6a21\u578b\u7684\u5b9a\u4e49\nclass MLP(nn.Module):\n # \u58f0\u660e\u5e26\u6709\u6a21\u578b\u53c2\u6570\u7684\u5c42\uff0c\u8fd9\u91cc\u58f0\u660e\u4e86\u4e24\u4e2a\u5168\u8fde\u63a5\u5c42\n def __init__(self, **kwargs):\n # \u8c03\u7528MLP\u7236\u7c7bBlock\u7684\u6784\u9020\u51fd\u6570\u6765\u8fdb\u884c\u5fc5\u8981\u7684\u521d\u59cb\u5316\u3002\u8fd9\u6837\u5728\u6784\u9020\u5b9e\u4f8b\u65f6\u8fd8\u53ef\u4ee5\u6307\u5b9a\u5176\u4ed6\u51fd\u6570\n super(MLP, self).__init__(**kwargs)\n self.hidden = nn.Conv2d(1,1,3)\n self.act = nn.ReLU()\n self.output = nn.Linear(10,1)\n \n # \u5b9a\u4e49\u6a21\u578b\u7684\u524d\u5411\u8ba1\u7b97\uff0c\u5373\u5982\u4f55\u6839\u636e\u8f93\u5165x\u8ba1\u7b97\u8fd4\u56de\u6240\u9700\u8981\u7684\u6a21\u578b\u8f93\u51fa\n def forward(self, x):\n o = self.act(self.hidden(x))\n return self.output(o)\n\nmlp = MLP()\nprint(mlp.hidden.weight.data)\nprint("-------\u521d\u59cb\u5316-------")\n\nmlp.apply(initialize_weights)\n# \u6216\u8005initialize_weights(mlp)\nprint(mlp.hidden.weight.data)\n'})}),"\n",(0,r.jsx)(e.pre,{children:(0,r.jsx)(e.code,{className:"language-python",children:'tensor([[[[ 0.3069, -0.1865, 0.0182],\n [ 0.2475, 0.3330, 0.1352],\n [-0.0247, -0.0786, 0.1278]]]])\n"-------\u521d\u59cb\u5316-------"\ntensor([[[[0., 0., 0.],\n [0., 0., 0.],\n [0., 0., 0.]]]])\n'})}),"\n",(0,r.jsxs)(e.p,{children:[(0,r.jsx)(e.strong,{children:"\u6ce8\u610f\uff1a"}),"\n\u6211\u4eec\u5728\u521d\u59cb\u5316\u65f6\uff0c\u6700\u597d\u4e0d\u8981\u5c06\u6a21\u578b\u7684\u53c2\u6570\u521d\u59cb\u5316\u4e3a0\uff0c\u56e0\u4e3a\u8fd9\u6837\u4f1a\u5bfc\u81f4\u68af\u5ea6\u6d88\u5931\uff0c\u4ece\u800c\u5f71\u54cd\u6a21\u578b\u7684\u8bad\u7ec3\u6548\u679c\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5728\u521d\u59cb\u5316\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u521d\u59cb\u5316\u65b9\u6cd5\u6216\u8005\u5c06\u6a21\u578b\u521d\u59cb\u5316\u4e3a\u4e00\u4e2a\u5f88\u5c0f\u7684\u503c\uff0c\u59820.01\uff0c0.1\u7b49\u3002"]})]})}function a(n={}){const{wrapper:e}={...(0,s.R)(),...n.components};return e?(0,r.jsx)(e,{...n,children:(0,r.jsx)(h,{...n})}):h(n)}}}]);