小编JaM*_*oin的帖子

pytorch 中的 apply(fn) 函数如何与没有 return 语句作为参数的函数一起工作?

我对以下代码片段有一些疑问:

>>> def init_weights(m):
        print(m)
        if type(m) == nn.Linear:
            m.weight.data.fill_(1.0)
            print(m.weight)

>>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2))
>>> net.apply(init_weights)
Run Code Online (Sandbox Code Playgroud)

apply() 是 pytorch.nn 包的一部分。您可以在此包的文档中找到代码。最后的问题: 1. 为什么这个代码示例可以工作,尽管在将它提供给 apply() 时没有在 init_weights() 中添加参数或括号?2. init_weights(m) 函数从哪里得到它的参数 m,当它作为参数提供给函数 apply() 而不带括号和 m 时?

python-3.x pytorch

3
推荐指数
1
解决办法
5458
查看次数

标签 统计

python-3.x ×1

pytorch ×1