Pytorch optimizer returns empty

Viewed 29

I already check relevant questions but can not figure out what is wrong with my code. I build a cnn and add residual value but, it returns

"ValueError: optimizer got an empty parameter list"

In below you can see some of my code

 def _init_(self):
        super()._init_()
        self.conv1 = nn.Conv2d(3, 6, 3, 1, 1)
        self.pool1 = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 10, 3, 1, 1)
        self.conv3 = nn.Conv2d(10, 5, 3, 1, 1)
        self.conv4 = nn.Conv2d(5, 10, 3, 1, 1)
        self.conv5 = nn.Conv2d(10, 10, 3, 1, 1)
        self.fc1 = nn.Linear(40960, 15)
        self.dropout = nn.Dropout(0.15)

    def forward(self, x):

        x = (F.relu(self.conv1(x)))
        x = (F.relu(self.conv2(x)))
        x = (F.relu(self.conv3(x)))
        residual = x.clone()
        x = (F.relu(self.conv4(x)))
        x += residual
        x = F.relu(x)
        x = (F.relu(self.conv5(x)))
        x = torch.flatten(x, 1)
        x = F.relu(self.fc1(x))

        return x

and it returns error in here:

net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=learn_rate, momentum=0.9)

thank you for your helps in advance

2 Answers

What is the type of net.parameters()? optim.SGD() is expecting an "iterable of parameters...". Try this:

net = Net()
criterion = nn.CrossEntropyLoss()
parameters = list(net.parameters())
optimizer = optim.SGD(parameters, lr=learn_rate, momentum=0.9)

Your Net class needs to extend torch.nn.Module

class Net(nn.Module):
    def __init__(self):
        ...

    def forward(self, x):
        ...
Related