Pytorch:了解nn.Module类在内部如何工作 [英] Pytorch: Understand how nn.Module class internally work
问题描述
通常, nn.Module
可以由如下的子类继承.
Generally, a nn.Module
can be inherited by a subclass as below.
def init_weights(m):
if type(m) == nn.Linear:
torch.nn.init.xavier_uniform(m.weight) #
class LinearRegression(nn.Module):
def __init__(self):
super(LinearRegression, self).__init__()
self.fc1 = nn.Linear(20, 1)
self.apply(init_weights)
def forward(self, x):
x = self.fc1(x)
return x
我的第一个问题是,为什么即使我的 __ init __
没有 training_signals
的任何正弦参数,我也可以简单地运行下面的代码,看起来像training_signals
传递给 forward()
方法.如何运作?
My 1st question is, why I can simply run the code below even my __init__
doesn't have any positinoal arguments for training_signals
and it looks like that training_signals
is passed to forward()
method. How does it work?
model = LinearRegression()
training_signals = torch.rand(1000,20)
model(training_signals)
第二个问题是 self.apply(init_weights)
在内部如何工作?它是在调用 forward
方法之前执行的吗?
The second question is that how does self.apply(init_weights)
internally work? Is it executed before calling forward
method?
推荐答案
Q1:为什么即使我的
__ init __
没有training_signals
的任何位置参数,我也可以简单地运行下面的代码,看起来像training_signals
传递给forward()
方法.如何运作?
Q1: Why I can simply run the code below even my
__init__
doesn't have any positional arguments fortraining_signals
and it looks like thattraining_signals
is passed toforward()
method. How does it work?
首先,运行此行时会调用 __ init __
:
First, the __init__
is called when you run this line:
model = LinearRegression()
如您所见,您不传递任何参数,也不应该传递任何参数. __ init __
的签名与基类的签名相同(您在运行 super(LinearRegression,self).__ init __()
时调用该基类).如您所见,此处, nn.Module
的初始签名只是 def __init __(self)
(就像您的签名一样).
As you can see, you pass no parameters, and you shouldn't. The signature of your __init__
is the same as the one of the base class (which you call when you run super(LinearRegression, self).__init__()
). As you can see here, nn.Module
's init signature is simply def __init__(self)
(just like yours).
第二,模型
现在是一个对象.当您运行以下行时:
Second, model
is now an object. When you run the line below:
model(training_signals)
您实际上是在调用 __ call __
方法,并传递 training_signals
作为位置参数.如您所见,此处,除其他外, __ call __
方法调用 forward
方法:
You are actually calling the __call__
method and passing training_signals
as a positional parameter. As you can see here, among many other things, the __call__
method calls the forward
method:
result = self.forward(*input, **kwargs)
将 __ call __
的所有参数(位置和名称)传递到 forward
.
passing all parameters (positional and named) of the __call__
to the forward
.
Q2:
self.apply(init_weights)
在内部如何工作?是在调用forward方法之前执行的吗?
Q2: How does
self.apply(init_weights)
internally work? Is it executed before calling forward method?
PyTorch是开源的,因此您只需转到源代码并进行检查.如您所见,此处,实现非常简单:
PyTorch is Open Source, so you can simply go to the source-code and check it. As you can see here, the implementation is quite simple:
def apply(self, fn):
for module in self.children():
module.apply(fn)
fn(self)
return self
引用函数的文档:它"递归地将 fn
应用于每个子模块(由 .children()
返回)和自我
".根据实现,您还可以了解要求:
Quoting the documentation of the function: it "applies fn
recursively to every submodule (as returned by .children()
) as well as self
". Based on the implementation, you can also understand the requirements:
-
fn
必须是可调用的; -
fn
仅接收Module
对象作为输入;
fn
must be a callable;fn
receives as input only aModule
object;
这篇关于Pytorch:了解nn.Module类在内部如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!