MNIST-dataset: optimizer got an empty parameter list

Alexander_Benz: <p>I was trying to do some work with MNIST-dataset. But received the following error:</p>
<p>ValueError                                Traceback (most recent call last)</p>
<p> in ()<br>
47<br>
48<br>
—&gt; 49 optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)<br>
50 “”"<br>
51 def train(epoch):</p>
<p>1 frames</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/optim/sgd.py in <strong>init</strong>(self, params, lr, momentum, dampening, weight_decay, nesterov)<br>
66         if nesterov and (momentum &lt;= 0 or dampening != 0):<br>
67             raise ValueError(“Nesterov momentum requires a momentum and zero dampening”)<br>
—&gt; 68         super(SGD, self).<strong>init</strong>(params, defaults)<br>
69<br>
70     def <strong>setstate</strong>(self, state):</p>
<p>/usr/local/lib/python3.6/dist-packages/torch/optim/optimizer.py in <strong>init</strong>(self, params, defaults)<br>
44         param_groups = list(params)<br>
45         if len(param_groups) == 0:<br>
—&gt; 46             raise ValueError(“optimizer got an empty parameter list”)<br>
47         if not isinstance(param_groups[0], dict):<br>
48             param_groups = [{‘params’: param_groups}]</p>
<p>ValueError: optimizer got an empty parameter list</p>
<p>Maybe you could help me with this problem. This is my code:</p>
<p>import torch</p>
<p>import torch.nn as nn</p>
<p>import torch.nn.functional as F</p>
<p>import torch.optim as optim</p>
<p>from torchvision import datasets, transforms</p>
<p>from torch.autograd import Variable</p>
<p>import numpy as np</p>
<p>import matplotlib.pyplot as plt</p>
<p>kwargs = {}</p>
<p>train_data = torch.utils.data.DataLoader(datasets.MNIST(‘data’, train=True, download=True,</p>
<pre><code>                                                transform = transforms.Compose([transforms.ToTensor(),

                                                transforms.Normalize((0.1307,),(0.3081,))])),

                                 batch_size=64, shuffle=True, **kwargs)
</code></pre>
<p>test_data = torch.utils.data.DataLoader(datasets.MNIST(‘data’, train=False,</p>
<pre><code>                                                transform = transforms.Compose([transforms.ToTensor(),

                                                transforms.Normalize((0.1307,),(0.3081,))])),

                                 batch_size=64, shuffle=True, **kwargs)
</code></pre>
<p>class Netz(nn.Module):</p>
<pre><code>def _init_(self):

  super(Netz, self)._init_()

  self.convl = nn.Conv2d(1, 10,kernel_size=5) 

  self.conv2 = nn.Conv2d(10,20,kernel_size=5)

  self.conv_dopout = nn.Dopout2d()

  self.fc1 = nn.Linear(320, 60)

  self.fc2 = nn.Linear(60, 10)

def forward(self, x):

  x = self.conv1(x)

  x = F.max_pool2d(x, 2)

  x = F.relu(x)

  x = self.conv2(x)

  x = self.conv_dropout

  x = F.max_pool2d(x, 2)

  x = F.relu(x)

  print(x.size())

  exit()
</code></pre>
<p>model = Netz()</p>
<p>list(model.parameters())</p>
<p>optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)</p>
<p>def train(epoch):</p>
<pre><code>model.train()

for batch_id, (data, target) in enumerate(train_data):

    data = Variable(data)

    target = Variable(target)

    optimizer.zero_grad()

    out = model(data)

    criterion = F.nll_loss

    loss = criterion(out, target)

    loss.backward()

    optimizer.step()

    print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(

      epoch, batch_id * len(data), len(train_data.dataset),

       100. * batch_id / len(train_data), loss.data[0]))
</code></pre>
<p>for epoch in range(1, 30):</p>
<pre><code>train(epoch)</code></pre>

chetan_patil: <p>Could you change the <code>_init_</code> to <code>__init__</code> here.</p>
<pre><code class="lang-auto">def _init_(self):
  super(Netz, self)._init_()
</code></pre>

Alexander_Benz: <p>Thank you very much for your quick response. I obviously wasn’t looking good enough on this line, now it is running.</p>