I want to try a toy case in pytorch. I want to use a simple full-connected neural network to fit a sin curve (the dataset is 100 points in the sin curve).
the architecture of the neural network is below:
Traceback (most recent call last):
File “main.py”, line 93, in
train(epoch)
File “main.py”, line 63, in train
output = model(data)
File “/usr/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 325, in call
result = self.forward(*input, **kwargs)
File “/home/hdeping/complexNetwork/00_CCodes/20_pytorchFitContinuousTarget/01_v1/mymodel.py”, line 13, in forward
x = F.relu(self.fc1(x))
File “/usr/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 325, in call
result = self.forward(*input, **kwargs)
File “/usr/lib/python3.6/site-packages/torch/nn/modules/linear.py”, line 55, in forward
return F.linear(input, self.weight, self.bias)
File “/usr/lib/python3.6/site-packages/torch/nn/functional.py”, line 837, in linear
output = input.matmul(weight.t())
File “/usr/lib/python3.6/site-packages/torch/autograd/variable.py”, line 386, in matmul
return torch.matmul(self, other)
File “/usr/lib/python3.6/site-packages/torch/functional.py”, line 168, in matmul
return torch.mm(tensor1.unsqueeze(0), tensor2).squeeze_(0) RuntimeError: Expected object of type Variable[torch.cuda.DoubleTensor] but found type Variable[torch.cuda.FloatTensor] for argument #1 ‘mat2’
I am sure that the type of the input data is torch.cuda.DoubleTensor.
What is wrong? I am doubting that something is wrong with the single input.
Thanks for your reply.
Yeah!That is it
.float should be added in the first layer!
thanks so much!
but , something new happens.
Traceback (most recent call last):
File “main.py”, line 93, in
train(epoch)
File “main.py”, line 63, in train
output = model(data)
File “/usr/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 325, in call
result = self.forward(*input, **kwargs)
File “/home/hdeping/complexNetwork/00_CCodes/20_pytorchFitContinuousTarget/02_v2/mymodel.py”, line 13, in forward
x = F.relu(self.fc1(x.float))
File “/usr/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 325, in call
result = self.forward(*input, **kwargs)
File “/usr/lib/python3.6/site-packages/torch/nn/modules/linear.py”, line 55, in forward
return F.linear(input, self.weight, self.bias)
File “/usr/lib/python3.6/site-packages/torch/nn/functional.py”, line 833, in linear if input.dim() == 2 and bias is not None: AttributeError: ‘function’ object has no attribute 'dim’
File “/home/hdeping/complexNetwork/00_CCodes/20_pytorchFitContinuousTarget/02_v2/mymodel.py”, line 13, in forward
x = F.relu(self.fc1(x.float()))
File “/usr/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 325, in call
result = self.forward(*input, kwargs)
File “/usr/lib/python3.6/site-packages/torch/nn/modules/linear.py”, line 55, in forward
return F.linear(input, self.weight, self.bias)
File “/usr/lib/python3.6/site-packages/torch/nn/functional.py”, line 837, in linear
output = input.matmul(weight.t())
File “/usr/lib/python3.6/site-packages/torch/autograd/variable.py”, line 386, in matmul
return torch.matmul(self, other)
File “/usr/lib/python3.6/site-packages/torch/functional.py”, line 168, in matmul
** return torch.mm(tensor1.unsqueeze(0), tensor2).squeeze_(0) RuntimeError: size mismatch at /build/python-pytorch/src/pytorch-0.3.0-py3-cuda/torch/lib/THC/generic/THCTensorMathBlas.cu:243
The size mismatch error comes from self.fc1, so I assume your x has an invalid shape. x should have the dimensions [batch_size, *, 1], in the simplest case just [batch_size, 1].
Have a look at this example:
batch_size = 10
x = Variable(torch.randn(batch_size, 1))
fc1 = nn.Linear(1, 10)
out = fc1(x)
Also, in your first post, you mentioned, that you would like to fit a sin curve.
Based on the loss you are using (CrossEntropyLoss), if seems you would like to perform a classification?
Maybe you should change the loss function to MSELoss and remove the log_softmax in your model.
As an activation function you could try F.tanh.
File “”, line 74, in
File “”, line 51, in train File “/usr/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 366, in getattr
** type(self).name, name))** AttributeError: ‘MSELoss’ object has no attribute 'data’