PyTorch是如何自动知道我的模型参数是什么的?

huangapple go评论69阅读模式
英文:

How does pytorch automatically know what are my model's parameters?

问题

我已经定义了自定义类如下:

class MLPClassifier(nn.Module):
    """
    A basic multi-layer perceptron classifier with 3 layers.
    """
  
    def __init__(self, input_size, hidden_size, num_classes):
        """
        MLPClassifier类的构造函数。
        """
        super(MLPClassifier, self).__init__()

        self.fc1 = nn.Linear(input_size, hidden_size)  # 输入到隐藏层的权重和偏置
        self.ac1 = nn.ReLU()                           # 输入到隐藏层的非线性激活
        self.fc2 = nn.Linear(hidden_size, num_classes) # 隐藏到输出层的权重和偏置
        self.ac2 = nn.Softmax(dim=1)                   # 隐藏到输出层的非线性激活

当我运行以下脚本时,我得到这个结果:

hyper_param_input_size  = 4
hyper_param_hidden_size = 64
hyper_param_num_classes = 3

model = MLPClassifier(hyper_param_input_size, hyper_param_hidden_size, hyper_param_num_classes)

for p in model.parameters():
    print(p.shape)

>>> torch.Size([64, 4])
>>> torch.Size([64])
>>> torch.Size([3, 64])
>>> torch.Size([3])

PyTorch是如何自动知道我的内部定义属性的呢,即使我从未明确告诉它呢?它是不是遍历类中的所有内容,然后检查是否isinstance(self, nn.Layer)或类似的操作?

英文:

I have defined the custom class as follows:

class MLPClassifier(nn.Module):
    """
    A basic multi-layer perceptron classifier with 3 layers.
    """
  
    def __init__(self, input_size, hidden_size, num_classes):
        """
        The constructor for the MLPClassifier class.
        """
        super(MLPClassifier, self).__init__()

        self.fc1 = nn.Linear(input_size, hidden_size)  # weights & biases for the input-to-hidden layer
        self.ac1 = nn.ReLU()                           # non-linear activation for the input-to-hidden layer
        self.fc2 = nn.Linear(hidden_size, num_classes) # weights & biases for the hidden-to-output layer
        self.ac2 = nn.Softmax(dim=1)                   # non-linear activation for the hidden-to-output layer

When I run the following script I get this:

hyper_param_input_size  = 4
hyper_param_hidden_size = 64
hyper_param_num_classes = 3

model = MLPClassifier(hyper_param_input_size, hyper_param_hidden_size, hyper_param_num_classes)

for p in model.parameters():
    print(p.shape)

>>> torch.Size([64, 4])
>>> torch.Size([64])
>>> torch.Size([3, 64])
>>> torch.Size([3])

How on earth does PyTorch automatically know about my internally defined attributes when I never explicitly told it? Does it loop through everything in the class and check if isinstance(self, nn.Layer) or something?

答案1

得分: 3

nn.Module.parameters 函数将递归遍历父模块的所有子模块,并返回它们的所有参数。它与您的 MLPClassifier 模块的实际结构无关。在 __init__ 中定义新的子模块属性时,父模块会将其注册为子模块,这样它们的参数(如果有的话,例如您的 nn.ReLUnn.Softmax 没有参数...)稍后可以通过 parameters 调用访问。

英文:

The nn.Module.parameters function will recursively go through all child modules of the parent and return all its parameters. It does not have to do with the actual structure of your MLPClassifier module. When defining a new submodule attribute inside your __init__ the parent module will register it as a child module, this way their parameters (if any, eg. your nn.ReLU and nn.Softmax don't have any...) can later be accessed via the parameters call.

huangapple
  • 本文由 发表于 2023年2月6日 08:18:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定