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

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

                                 batch_size=64, shuffle=True, **kwargs)

test_data = torch.utils.data.DataLoader(datasets.MNIST(‘data’, train=False,
                                                transform = transforms.Compose([transforms.ToTensor(),

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

                                 batch_size=64, shuffle=True, **kwargs)

class Netz(nn.Module):
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()

model = Netz()
list(model.parameters())
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
def train(epoch):
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]))

for epoch in range(1, 30):
train(epoch)