ValueError: optimizer got an empty parameter list!

5bc1886a0799b431b52a: <p>I am a  new Pytorch user and I meet a problem.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/b/bbbafdd38677f428501fa656cbd2ba61c4bbed56.png" data-download-href="https://discuss.pytorch.org/uploads/default/bbbafdd38677f428501fa656cbd2ba61c4bbed56" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/b/bbbafdd38677f428501fa656cbd2ba61c4bbed56_2_690x125.png" alt="image" data-base62-sha1="qMJUH5eNpMd4pqUMkWakQFBJ5Hg" width="690" height="125" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/b/bbbafdd38677f428501fa656cbd2ba61c4bbed56_2_690x125.png, https://discuss.pytorch.org/uploads/default/optimized/2X/b/bbbafdd38677f428501fa656cbd2ba61c4bbed56_2_1035x187.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/b/bbbafdd38677f428501fa656cbd2ba61c4bbed56_2_1380x250.png 2x" data-dominant-color="EDE9F6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1964×358 54.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>This is my code</p>
<pre><code class="lang-auto">#-*-coding:utf-8-*-

import torch
import torch.nn as nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms

# Hyper parameters
num_epochs = 20
batchsize = 100
lr = 0.001

# MNIST Dataset
train_dataset = dsets.MNIST(root='/Users/hushengyou/PycharmProjects/NJU_contest/homework3/',
                            train=True,
                            transform=transforms.ToTensor(),
                            download=False)
test_dataset = dsets.MNIST(root='/Users/hushengyou/PycharmProjects/NJU_contest/homework3/',
                           train=False,
                           transform=transforms.ToTensor())

# Data Loader (Input Pipeline)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batchsize,
                                           shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                          batch_size=batchsize,
                                          shuffle=False)

# CNN model
class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        nn.conlayer1 = nn.Sequential(
            nn.Conv2d(1,6,3),
            nn.Sigmoid(),
            nn.MaxPool2d(2))
        nn.conlayer2 = nn.Sequential(
            nn.Conv2d(6,16,3),
            nn.Sigmoid(),
            nn.MaxPool2d(2))
        nn.fc = nn.Sequential(
            nn.Linear(400,120),
            nn.Linear(120,84),
            nn.Linear(84,10))

    def forward(self, x):
        out = self.conlayer1(x)
        out = self.conlayer2(out)
        out = out.view(out.size(0),-1)
        out = self.fc(out)
        return out


cnn = CNN()

# Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(cnn.parameters(), lr=lr)

# Train the Model
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        # Forward + Backward + Optimize
        optimizer.zero_grad()
        outputs = cnn(images)
        loss = criterion(outputs,labels)
        loss.backward()
        optimizer.step()

        if (i+1)%100 == 0:
            print('Epoch [%d/%d], Iter [%d/%d] Loss: %.4f' %
                  (epoch+1,num_epochs,i+1,len(train_dataset)//batchsize,loss.data[0]))

# Test the Model
cnn.eval()  # Change model to 'eval' mode (BN uses moving mean/var)
correct = 0
total = 0
for images, labels in test_loader:
    outputs = cnn(images)
    _, predicted = torch.max(outputs.data, 1)
    total += labels.size(0)
    correct += (predicted == labels).sum()
print('Test Accuracy of the model on test images: %.6f%%' % (100.0*correct/total))

#Save the Trained Model
torch.save(cnn.state_dict(),'cnn.pkl')
</code></pre>
<p>Can anybody solve my problem？ Thank you very much!!!</p>

John_Smith: <p>You were not defining your layers correctly.</p>
<p>Change:</p>
<pre><code>nn.conlayer1, nn.conlayer2, nn.fc
</code></pre>
<p>to:</p>
<pre><code>self.conlayer1, self.conlayers, slef.fc
</code></pre>
<p>Should work.</p>
<p>something like:</p>
<pre><code class="lang-auto">class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conlayer1 = nn.Sequential(
            nn.Conv2d(1,6,3),
            nn.Sigmoid(),
            nn.MaxPool2d(2))
        self.conlayer2 = nn.Sequential(
            nn.Conv2d(6,16,3),
            nn.Sigmoid(),
            nn.MaxPool2d(2))
        self.fc = nn.Sequential(
            nn.Linear(400,120),
            nn.Linear(120,84),
            nn.Linear(84,10))

    def forward(self, x):
        out = self.conlayer1(x)
        out = self.conlayer2(out)
        out = out.view(out.size(0),-1)
        out = self.fc(out)
        return out
</code></pre>

5bc1886a0799b431b52a: <p>The problem is solved.</p>
<p>Thanks a lot!!!</p>
<p>I am stupid…</p>

John_Smith: <p>You are welcome. Glad it helped.</p>

exowanderer: <p>That was exactly my problem! Thank you!</p>