Hello, I am a beginner in PyTorch.
I am trying to learn by building a toy example with custom data. Here is my code:
import torch
import torch.nn as nn
import torch.optim as optim
data={};
for iter in range(10):
data[iter]=[torch.tensor([1]).long().type(torch.LongTensor),torch.tensor([2]).long().type(torch.LongTensor)]
model=nn.Linear(1,1);
loss=nn.BCELoss();
adam = optim.Adam(model.parameters(), lr=1e-1);
for epoch in range(10):
adam.zero_grad();
x=data[epoch][0];
y=data[epoch][1];
x,y=x.type(torch.LongTensor),y.type(torch.LongTensor)
pred=model(x.type(torch.LongTensor));
l=loss(pred,y);
print(f'Epoch {epoch}: training loss: {l}');
loss.backward();
adam.step();
When I run this script this is the error I get:
Traceback (most recent call last):
File "c", line 15, in <module>
pred=model(x.type(torch.LongTensor));
File "/home/samarth/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl
return forward_call(*input, **kwargs)
File "/home/samarth/.local/lib/python3.6/site-packages/torch/nn/modules/linear.py", line 96, in forward
return F.linear(input, self.weight, self.bias)
File "/home/samarth/.local/lib/python3.6/site-packages/torch/nn/functional.py", line 1847, in linear
return torch._C._nn.linear(input, weight, bias)
RuntimeError: expected scalar type Long but found Float
I have told PyTorch to convert the data into LongTensor multiple times, but it still won’t work.
Can anyone help me?
Thank you.