Trying to implement a basic neural net on the mnist dataset.
train_dataset = datasets.MNIST(root = ‘./mnist_data/’,train = True,transform = transforms.ToTensor(), download = True)
train_loader = torch.utils.data.DataLoader(dataset = train_dataset,
batch_size = batch_size,
shuffle = True)
My network is
class Net(nn.Module):
def init(self):
super(Net,self).init()
self.l1 = nn.Linear(784,520)
self.l2 = nn.Linear(520,320)
self.l3 = nn.Linear(320,240)
self.l4 = nn.Linear(240,120)
self.l5 = nn.Linear(120,10)
def forward(self,x):
x = x.view(-1,784)
x = F.relu(self.l1(x))
x = F.relu(self.l2(x))
x = F.relu(self.l3(x))
x = F.relu(self.l4(x))
return self.l5(x)
After that I had
model = Net()
model = model.cuda()
I thought this moved the network to memory, so why am i still getting the error.
This is the train function:
def train(epoch):
model.train()
for batch_idx, (data,target) in enumerate(train_loader):
optimizer.zero_grad()
data, target = Variable(data), Variable(target)
output = model(data)
loss = criterion(output, target)
optimizer.step()
if batch_idx%10==0:
print(‘Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}’.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.data[0]))
I am new to pytorch. Please help me in making the necessary changes.
This is the error i get,
RuntimeError Traceback (most recent call last)
in ()
1 for epoch in range(1,10):
----> 2 train(epoch)
3 test()
in train(epoch)
4 optimizer.zero_grad()
5 data, target = Variable(data), Variable(target)
----> 6 output = model(data)
7 loss = criterion(output, target)
8 optimizer.step()
~/anaconda2/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
355 result = self._slow_forward(*input, **kwargs)
356 else:
–> 357 result = self.forward(*input, **kwargs)
358 for hook in self._forward_hooks.values():
359 hook_result = hook(self, input, result)
in forward(self, x)
9 def forward(self,x):
10 x = x.view(-1,784)
—> 11 x = F.relu(self.l1(x))
12 x = F.relu(self.l2(x))
13 x = F.relu(self.l3(x))
~/anaconda2/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
355 result = self._slow_forward(*input, **kwargs)
356 else:
–> 357 result = self.forward(*input, **kwargs)
358 for hook in self._forward_hooks.values():
359 hook_result = hook(self, input, result)
~/anaconda2/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/linear.py in forward(self, input)
53
54 def forward(self, input):
—> 55 return F.linear(input, self.weight, self.bias)
56
57 def repr(self):
~/anaconda2/envs/fastai/lib/python3.6/site-packages/torch/nn/functional.py in linear(input, weight, bias)
833 if input.dim() == 2 and bias is not None:
834 # fused op is marginally faster
–> 835 return torch.addmm(bias, input, weight.t())
836
837 output = input.matmul(weight.t())
RuntimeError: Expected object of type Variable[torch.cuda.FloatTensor] but found type Variable[torch.FloatTensor] for argument #1 ‘mat1’