PyTorch:获取输入层大小

Tom*_*ale 5 pytorch

我想以编程方式找到输入层的大小。

如果我的第一层被称为fc1,我如何找到它的输入?

Tom*_*ale 4

假设您的模型被称为model,这将给出该层的输入特征的数量fc1

model.fc1.in_features
Run Code Online (Sandbox Code Playgroud)

这在.forward()方法内部很有用:

def forward(self, x):
    x = x.view(-1, self.fc1.in_features)  # resize the input to match the input layer
    ...
Run Code Online (Sandbox Code Playgroud)