Issue
I tried to find the answer but I can't.
I make a custom deep learning model using pytorch. For example,
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
If I print
model = Net()
print(list(model.parameters()))
it does not contains model.bias, so optimizer = optimizer.Adam(model.parameters()) does not update model.bias. How can I go through this? Thanks!
Solution
You need to register your parameters:
self.register_parameter(name='bias', param=torch.nn.Parameter(torch.randn(3)))
Answered By - Shai
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.