With Pytorch I am attempting to use ModuleList to ensure model parameters are detected, and can be optimized. When calling the SGD optimizer I get the following error:
ValueError: optimizer got an empty parameter list
Can you please review the code below and advise?
JavaScript
x
13
13
1
class LR(nn.Module):
2
def ___init___(self):
3
super(LR, self).___init___()
4
self.linear = nn.ModuleList()
5
self.linear.append(nn.Linear(in_features=28*28, out_features=128, bias=True))
6
7
def forward(self, x):
8
y_p = torch.sigmoid(self.linear(x))
9
return y_p
10
11
LR_model = LR()
12
optimizer = torch.optim.SGD(params = LR_model.parameters(), lr=learn_rate)
13
Advertisement
Answer
This seems to be a copy-paste issue: your __init__
has 3 underscores instead of 2, both at __init__(self)
and super(LR, self).__init__()
. Thus the init
itself failed. Delete the extra underscores and try again or try the below code:
JavaScript
1
16
16
1
class LR(nn.Module):
2
def __init__(self):
3
super(LR, self).__init__()
4
self.linear = nn.ModuleList()
5
self.linear.append(nn.Linear(in_features=28*28,
6
out_features=128,
7
bias=True))
8
9
def forward(self, x):
10
y_p = torch.sigmoid(self.linear(x))
11
return y_p
12
13
LR_model = LR()
14
optimizer = torch.optim.SGD(params = list(LR_model.parameters()),
15
lr=learn_rate)
16