【发布时间】:2020-04-01 16:37:02
【问题描述】:
我试图找到答案,但我找不到。
我使用 pytorch 制作了一个自定义深度学习模型。例如,
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.nn_layers = nn.ModuleList()
self.layer = nn.Linear(2,3).double()
torch.nn.init.xavier_normal_(self.layer.weight)
self.bias = torch.nn.Parameter(torch.randn(3))
self.nn_layers.append(self.layer)
def forward(self, x):
activation = torch.tanh
output = activation(self.layer(x)) + self.bias
return output
如果我打印
model = Net()
print(list(model.parameters()))
它不包含model.bias,所以 optimizer = optimizer.Adam(model.parameters()) 不更新 model.bias。 我怎么能通过这个? 谢谢!
【问题讨论】:
-
如果您是子类化模块,请参阅此处发布的解决方案。 discuss.pytorch.org/t/…。使用 print(list(model.parameters())) 进行测试。
标签: deep-learning pytorch