What is wrong bellow?
import torch
from torch import autograd
import torch.nn as nn
all = [
âCNNâ, âcnnâ,
]
class CNN(nn.Module):
def init(self, dataset):
super(CNN, self).init()
self.threshold = autograd.Variable(torch.rand(1), requires_grad=True)
self.threshold.data.fill_(0.05)
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=2, stride=None, padding=0)
self.fc1 = nn.Linear(320, 50)
self.fc2 = nn.Linear(50, 10)
def forward(self, x):
x = self.conv1(x)
x = self.maxpool(x)
#x = x + self.threshold.expand_as(x)
x = x + self.threshold
x = self.relu(x)
x = self.conv2(x)
x = self.conv2_drop(x)
x = self.maxpool(x)
x = self.relu(x)
x = x.view(-1, 320)
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.relu(x)
return x
def cnn(dataset):
model = CNN(dataset)
return model
Error:
Traceback (most recent call last):
File âtrain.pyâ, line 390, in
main()
File âtrain.pyâ, line 165, in main
cnn(args.epochs, train_loader, val_loader, model, criterion, optimizer, experiment)
File âtrain.pyâ, line 200, in cnn
training_time += train(train_loader, model, criterion, optimizer, epoch)
File âtrain.pyâ, line 260, in train
output = model(input_var)
File â/home/dlm/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.pyâ, line 206, in call
result = self.forward(*input, **kwargs)
File â/home/dlm/anaconda3/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.pyâ, line 59, in forward
return self.module(*inputs[0], **kwargs[0])
File â/home/dlm/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.pyâ, line 206, in call
result = self.forward(*input, **kwargs)
File â/home/dlm/code/deeplearninglab/sem/models/cnn.pyâ, line 27, in forward
x = x + self.threshold
File â/home/dlm/anaconda3/lib/python3.6/site-packages/torch/autograd/variable.pyâ, line 745, in add
return self.add(other)
File â/home/dlm/anaconda3/lib/python3.6/site-packages/torch/autograd/variable.pyâ, line 283, in add
return self._add(other, False)
File â/home/dlm/anaconda3/lib/python3.6/site-packages/torch/autograd/variable.pyâ, line 277, in _add
return Add(inplace)(self, other)
File â/home/dlm/anaconda3/lib/python3.6/site-packages/torch/autograd/_functions/basic_ops.pyâ, line 20, in forward
return a.add(b)
TypeError: add received an invalid combination of arguments - got (torch.FloatTensor), but expected one of:
- (float value)
didnât match because some of the arguments have invalid types: (torch.FloatTensor)
- (torch.cuda.FloatTensor other)
didnât match because some of the arguments have invalid types: (torch.FloatTensor)
- (torch.cuda.sparse.FloatTensor other)
didnât match because some of the arguments have invalid types: (torch.FloatTensor)
- (float value, torch.cuda.FloatTensor other)
- (float value, torch.cuda.sparse.FloatTensor other)