{"id":1811,"date":"2025-02-17T15:17:42","date_gmt":"2025-02-17T07:17:42","guid":{"rendered":"https:\/\/www.forillusion.com\/?p=1811"},"modified":"2025-02-17T15:17:43","modified_gmt":"2025-02-17T07:17:43","slug":"4-2-parameters","status":"publish","type":"post","link":"https:\/\/www.forillusion.com\/index.php\/4-2-parameters\/","title":{"rendered":"4.2 \u6a21\u578b\u53c2\u6570\u7684\u8bbf\u95ee\u3001\u521d\u59cb\u5316\u548c\u5171\u4eab"},"content":{"rendered":"\n<p><div class=\"has-toc have-toc\"><\/div><\/p>\n\n\n\n<p>\u5bfc\u5165\u9700\u8981\u7684\u5e93\uff0c<code>init<\/code>\u6a21\u5757\u5305\u542b\u4e86\u591a\u79cd\u6a21\u578b\u521d\u59cb\u5316\u65b9\u6cd5\u3002\u5b9a\u4e49\u4e00\u4e2a\u542b\u5355\u9690\u85cf\u5c42\u7684\u591a\u5c42\u611f\u77e5\u673a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch\nfrom torch import nn\nfrom torch.nn import init\n\nnet = nn.Sequential(nn.Linear(4, 3), nn.ReLU(), nn.Linear(3, 1))  # pytorch\u5df2\u8fdb\u884c\u9ed8\u8ba4\u521d\u59cb\u5316\n\nprint(net)\nX = torch.rand(2, 4)\nY = net(X).sum()<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sequential(\n  (0): Linear(in_features=4, out_features=3, bias=True)\n  (1): ReLU()\n  (2): Linear(in_features=3, out_features=1, bias=True)\n)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u8bbf\u95ee\u6a21\u578b\u53c2\u6570<\/h2>\n\n\n\n<p>\u5bf9\u4e8e<code>Sequential<\/code>\u5b9e\u4f8b\u4e2d\u542b\u6a21\u578b\u53c2\u6570\u7684\u5c42\uff0c\u53ef\u4ee5\u901a\u8fc7<code>Module<\/code>\u7c7b\u7684<code>parameters()<\/code>\u6216\u8005<code>named_parameters<\/code>\u65b9\u6cd5\u6765\u8bbf\u95ee\u6240\u6709\u53c2\u6570\uff08\u4ee5\u8fed\u4ee3\u5668\u7684\u5f62\u5f0f\u8fd4\u56de\uff09\uff0c\u540e\u8005\u9664\u4e86\u8fd4\u56de\u53c2\u6570<code>Tensor<\/code>\u5916\u8fd8\u4f1a\u8fd4\u56de\u5176\u540d\u5b57\u3002\u4e0b\u9762\uff0c\u8bbf\u95ee\u591a\u5c42\u611f\u77e5\u673a<code>net<\/code>\u7684\u6240\u6709\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(type(net.named_parameters()))\nfor name, param in net.named_parameters():\n    print(name, param.size())<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;class 'generator'&gt;\n0.weight torch.Size(&#91;3, 4])\n0.bias torch.Size(&#91;3])\n2.weight torch.Size(&#91;1, 3])\n2.bias torch.Size(&#91;1])<\/code><\/pre>\n\n\n\n<p>\u53ef\u89c1\u8fd4\u56de\u7684\u540d\u5b57\u81ea\u52a8\u52a0\u4e0a\u4e86\u5c42\u6570\u7684\u7d22\u5f15\u4f5c\u4e3a\u524d\u7f00\u3002<br>\u518d\u6765\u8bbf\u95ee<code>net<\/code>\u4e2d\u5355\u5c42\u7684\u53c2\u6570\u3002\u5bf9\u4e8e\u4f7f\u7528<code>Sequential<\/code>\u7c7b\u6784\u9020\u7684\u795e\u7ecf\u7f51\u7edc\uff0c\u53ef\u4ee5\u901a\u8fc7\u65b9\u62ec\u53f7<code>[]<\/code>\u6765\u8bbf\u95ee\u7f51\u7edc\u7684\u4efb\u4e00\u5c42\u3002\u7d22\u5f150\u8868\u793a\u9690\u85cf\u5c42\u4e3a<code>Sequential<\/code>\u5b9e\u4f8b\u6700\u5148\u6dfb\u52a0\u7684\u5c42\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for name, param in net&#91;0].named_parameters():\n    print(name, param.size(), type(param))<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>weight torch.Size(&#91;3, 4]) &lt;class 'torch.nn.parameter.Parameter'&gt;\nbias torch.Size(&#91;3]) &lt;class 'torch.nn.parameter.Parameter'&gt;<\/code><\/pre>\n\n\n\n<p>\u56e0\u4e3a\u8fd9\u91cc\u662f\u5355\u5c42\u7684\u6240\u4ee5\u6ca1\u6709\u4e86\u5c42\u6570\u7d22\u5f15\u7684\u524d\u7f00\u3002\u53e6\u5916\u8fd4\u56de\u7684<code>param<\/code>\u7684\u7c7b\u578b\u4e3a<code>torch.nn.parameter.Parameter<\/code>\uff0c\u8fd9\u662f<code>Tensor<\/code>\u7684\u5b50\u7c7b\uff0c\u548c<code>Tensor<\/code>\u4e0d\u540c\u7684\u662f\u5982\u679c\u4e00\u4e2a<code>Tensor<\/code>\u662f<code>Parameter<\/code>\uff0c\u90a3\u4e48\u5b83\u4f1a\u81ea\u52a8\u88ab\u6dfb\u52a0\u5230\u6a21\u578b\u7684\u53c2\u6570\u5217\u8868\u91cc\uff0c\u6765\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyModel(nn.Module):\n    def __init__(self, **kwargs):\n        super(MyModel, self).__init__(**kwargs)\n        self.weight1 = nn.Parameter(torch.rand(20, 20))\n        self.weight2 = torch.rand(20, 20)\n    def forward(self, x):\n        pass\n\nn = MyModel()\nfor name, param in n.named_parameters():\n    print(name)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>weight1<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d<code>weight1<\/code>\u5728\u53c2\u6570\u5217\u8868\u4e2d\u4f46\u662f<code>weight2<\/code>\u5374\u6ca1\u5728\u53c2\u6570\u5217\u8868\u4e2d\u3002<\/p>\n\n\n\n<p>\u56e0\u4e3a<code>Parameter<\/code>\u662f<code>Tensor<\/code>\uff0c\u5373<code>Tensor<\/code>\u62e5\u6709\u7684\u5c5e\u6027\u5b83\u90fd\u6709\uff0c\u6bd4\u5982\u53ef\u4ee5\u6839\u636e<code>data<\/code>\u6765\u8bbf\u95ee\u53c2\u6570\u6570\u503c\uff0c\u7528<code>grad<\/code>\u6765\u8bbf\u95ee\u53c2\u6570\u68af\u5ea6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>weight_0 = list(net&#91;0].parameters())&#91;0]\nprint(weight_0.data)\nprint(weight_0.grad) # \u53cd\u5411\u4f20\u64ad\u524d\u68af\u5ea6\u4e3aNone\nY.backward()\nprint(weight_0.grad)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tensor(&#91;&#91; 0.2719, -0.0898, -0.2462,  0.0655],\n        &#91;-0.4669, -0.2703,  0.3230,  0.2067],\n        &#91;-0.2708,  0.1171, -0.0995,  0.3913]])\nNone\ntensor(&#91;&#91;-0.2281, -0.0653, -0.1646, -0.2569],\n        &#91;-0.1916, -0.0549, -0.1382, -0.2158],\n        &#91; 0.0000,  0.0000,  0.0000,  0.0000]])<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u521d\u59cb\u5316\u6a21\u578b\u53c2\u6570<\/h2>\n\n\n\n<p>PyTorch\u7684<code>init<\/code>\u6a21\u5757\u91cc\u63d0\u4f9b\u4e86\u591a\u79cd\u9884\u8bbe\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5c06\u6743\u91cd\u53c2\u6570\u521d\u59cb\u5316\u6210\u5747\u503c\u4e3a0\u3001\u6807\u51c6\u5dee\u4e3a0.01\u7684\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570\uff0c\u5e76\u4f9d\u7136\u5c06\u504f\u5dee\u53c2\u6570\u6e05\u96f6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for name, param in net.named_parameters():\n    if 'weight' in name:\n        init.normal_(param, mean=0, std=0.01)\n        print(name, param.data)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0.weight tensor(&#91;&#91; 0.0030,  0.0094,  0.0070, -0.0010],\n        &#91; 0.0001,  0.0039,  0.0105, -0.0126],\n        &#91; 0.0105, -0.0135, -0.0047, -0.0006]])\n2.weight tensor(&#91;&#91;-0.0074,  0.0051,  0.0066]])<\/code><\/pre>\n\n\n\n<p>\u4e0b\u9762\u4f7f\u7528\u5e38\u6570\u6765\u521d\u59cb\u5316\u6743\u91cd\u53c2\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for name, param in net.named_parameters():\n    if 'bias' in name:\n        init.constant_(param, val=0)\n        print(name, param.data)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0.bias tensor(&#91;0., 0., 0.])\n2.bias tensor(&#91;0.])<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u81ea\u5b9a\u4e49\u521d\u59cb\u5316\u65b9\u6cd5<\/h2>\n\n\n\n<p>\u6709\u65f6\u5019\u9700\u8981\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u5e76\u6ca1\u6709\u5728<code>init<\/code>\u6a21\u5757\u4e2d\u63d0\u4f9b\u3002\u8fd9\u65f6\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u4ece\u800c\u80fd\u591f\u50cf\u4f7f\u7528\u5176\u4ed6\u521d\u59cb\u5316\u65b9\u6cd5\u90a3\u6837\u4f7f\u7528\u5b83\u3002\u5728\u8fd9\u4e4b\u524d\u5148\u6765\u770b\u770bPyTorch\u662f\u600e\u4e48\u5b9e\u73b0\u8fd9\u4e9b\u521d\u59cb\u5316\u65b9\u6cd5\u7684\uff0c\u4f8b\u5982<code>torch.nn.init.normal_<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def normal_(tensor, mean=0, std=1):\n    with torch.no_grad():\n        return tensor.normal_(mean, std)<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u8fd9\u5c31\u662f\u4e00\u4e2ainplace\u6539\u53d8<code>Tensor<\/code>\u503c\u7684\u51fd\u6570\uff0c\u800c\u4e14\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u4e0d\u8bb0\u5f55\u68af\u5ea6\u7684\u3002<\/p>\n\n\n\n<p><code>torch.no_grad()<\/code> \u662f\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u7528\u4e8e\u7981\u7528\u68af\u5ea6\u8ba1\u7b97\u3002\u5728\u5b83\u7684\u4f5c\u7528\u8303\u56f4\u5185\uff0c\u6240\u6709\u5bf9\u5f20\u91cf\u7684\u64cd\u4f5c\u90fd\u4e0d\u4f1a\u88ab\u8bb0\u5f55\u5230\u8ba1\u7b97\u56fe\u4e2d\uff0c\u4e5f\u4e0d\u4f1a\u66f4\u65b0\u68af\u5ea6\u3002\u4e00\u65e6\u79bb\u5f00 <code>with<\/code> \u5757\uff0c<code>torch.no_grad()<\/code> \u7684\u4f5c\u7528\u7ed3\u675f\uff0c\u540e\u7eed\u7684\u4ee3\u7801\u4f1a\u6062\u590d\u5230\u6b63\u5e38\u7684\u68af\u5ea6\u8bb0\u5f55\u72b6\u6001\u3002<\/p>\n\n\n\n<p>\u7c7b\u4f3c\u7684\u6765\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u91cc\uff0c\u4ee4\u6743\u91cd\u6709\u4e00\u534a\u6982\u7387\u521d\u59cb\u5316\u4e3a0\uff0c\u6709\u53e6\u4e00\u534a\u6982\u7387\u521d\u59cb\u5316\u4e3a$[-10,-5]$\u548c$[5,10]$\u4e24\u4e2a\u533a\u95f4\u91cc\u5747\u5300\u5206\u5e03\u7684\u968f\u673a\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def init_weight_(tensor):\n    with torch.no_grad():\n        tensor.uniform_(-10, 10)\n        tensor *= (tensor.abs() &gt;= 5).float()\n\nfor name, param in net.named_parameters():\n    if 'weight' in name:\n        init_weight_(param)\n        print(name, param.data)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0.weight tensor(&#91;&#91; 7.0403,  0.0000, -9.4569,  7.0111],\n        &#91;-0.0000, -0.0000,  0.0000,  0.0000],\n        &#91; 9.8063, -0.0000,  0.0000, -9.7993]])\n2.weight tensor(&#91;&#91;-5.8198,  7.7558, -5.0293]])<\/code><\/pre>\n\n\n\n<p>\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6539\u53d8\u8fd9\u4e9b\u53c2\u6570\u7684<code>data<\/code>\u6765\u6539\u5199\u6a21\u578b\u53c2\u6570\u503c\u540c\u65f6\u4e0d\u4f1a\u5f71\u54cd\u68af\u5ea6:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for name, param in net.named_parameters():\n    if 'bias' in name:\n        param.data += 1\n        print(name, param.data)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0.bias tensor(&#91;1., 1., 1.])\n2.bias tensor(&#91;1.])<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5171\u4eab\u6a21\u578b\u53c2\u6570<\/h2>\n\n\n\n<p>\u5728\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5e0c\u671b\u5728\u591a\u4e2a\u5c42\u4e4b\u95f4\u5171\u4eab\u6a21\u578b\u53c2\u6570\u30024.1.3\u8282\u63d0\u5230\u4e86\u5982\u4f55\u5171\u4eab\u6a21\u578b\u53c2\u6570: <code>Module<\/code>\u7c7b\u7684<code>forward<\/code>\u51fd\u6570\u91cc\u591a\u6b21\u8c03\u7528\u540c\u4e00\u4e2a\u5c42\u3002\u6b64\u5916\uff0c\u5982\u679c\u4f20\u5165<code>Sequential<\/code>\u7684\u6a21\u5757\u662f\u540c\u4e00\u4e2a<code>Module<\/code>\u5b9e\u4f8b\u7684\u8bdd\u53c2\u6570\u4e5f\u662f\u5171\u4eab\u7684\uff0c\u4e0b\u9762\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>linear = nn.Linear(1, 1, bias=False)\nnet = nn.Sequential(linear, linear) \nprint(net)\nfor name, param in net.named_parameters():\n    init.constant_(param, val=3)\n    print(name, param.data)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sequential(\n  (0): Linear(in_features=1, out_features=1, bias=False)\n  (1): Linear(in_features=1, out_features=1, bias=False)\n)\n0.weight tensor(&#91;&#91;3.]])<\/code><\/pre>\n\n\n\n<p>\u5728\u5185\u5b58\u4e2d\uff0c\u8fd9\u4e24\u4e2a\u7ebf\u6027\u5c42\u5176\u5b9e\u4e00\u4e2a\u5bf9\u8c61:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(id(net&#91;0]) == id(net&#91;1]))\nprint(id(net&#91;0].weight) == id(net&#91;1].weight))<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>True\nTrue<\/code><\/pre>\n\n\n\n<p>\u56e0\u4e3a\u6a21\u578b\u53c2\u6570\u91cc\u5305\u542b\u4e86\u68af\u5ea6\uff0c\u6240\u4ee5\u5728\u53cd\u5411\u4f20\u64ad\u8ba1\u7b97\u65f6\uff0c\u8fd9\u4e9b\u5171\u4eab\u7684\u53c2\u6570\u7684\u68af\u5ea6\u662f\u7d2f\u52a0\u7684:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = torch.ones(1, 1)\ny = net(x).sum()\nprint(y)\ny.backward()\nprint(net&#91;0].weight.grad) # \u5355\u6b21\u68af\u5ea6\u662f3\uff0c\u4e24\u6b21\u6240\u4ee5\u5c31\u662f6<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tensor(9., grad_fn=&lt;SumBackward0&gt;)\ntensor(&#91;&#91;6.]])<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5bfc\u5165\u9700\u8981\u7684\u5e93\uff0cinit\u6a21\u5757\u5305\u542b\u4e86\u591a\u79cd\u6a21\u578b\u521d\u59cb\u5316\u65b9\u6cd5\u3002\u5b9a\u4e49\u4e00\u4e2a\u542b\u5355\u9690\u85cf\u5c42\u7684\u591a\u5c42\u611f\u77e5\u673a\u3002 \u8f93\u51fa\uff1a \u8bbf\u95ee\u6a21\u578b\u53c2\u6570 \u5bf9\u4e8eSequentia &#8230;<\/p>","protected":false},"author":1,"featured_media":1812,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,3],"tags":[45,44,12,22],"class_list":["post-1811","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-46","category-3","tag-45","tag-44","tag-12","tag-22"],"_links":{"self":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/comments?post=1811"}],"version-history":[{"count":1,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1811\/revisions"}],"predecessor-version":[{"id":1813,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1811\/revisions\/1813"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media\/1812"}],"wp:attachment":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media?parent=1811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/categories?post=1811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/tags?post=1811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}