Search code examples
pythonmachine-learningpytorchneural-network

Cannot send decimal input in Pytorch as it leads to RuntimeError: expected scalar type Long but found Float


I am trying to train a basic neural network which takes an integer as an input, however we can not send the integer directly hence I tried to convert it into a tensor. This is the code that I am currently using

class Net(nn.Module):
  def __init__(self,dim_input,hidden_dim,dim_output):
    super(Net, self).__init__()
    self.inp = dim_input
    self.oup = dim_output
    self.l1 = nn.Linear(dim_input,hidden_dim);
    self.l2 = nn.Linear(hidden_dim,dim_output);
    #self.l3 = nn.Linear(hidden_dim,dim_output);
  def forward(self,x):
    # x = self.converter(x)
    #x = torch.tensor(x)
    x = F.sigmoid(self.l1(x))
    x = self.l2(x)
    return x
  def converter(self,x):
    n = x;
    n = bin(n).replace("0b", "")
    a = []
    for i in n:
      a.append(int(i))
    a.reverse()
    while(len(a) < self.inp):
      a.append(0)
    a.reverse()
    a = torch.tensor(a)
    a = a.to(dtype=torch.int64)
    return a

I then run the following code in order to prepare the data, here net is the model and primes is a list of primes. This is to prepare the dataset.

def prepare():
  global primes,batch_size,net
  inp = []
  oup = []
  for i in range(batch_size):
    a = random.choice(primes)
    b = random.choice(primes)
    inp.append(net.converter(a*b))
    oup.append(net.converter(min(a,b)))
  print(inp[0])
  return inp,oup;

Finally I train the model.

loss_fn = nn.BCELoss()  # binary cross entropy
optimizer = optim.Adam(net.parameters(), lr=0.001)

for i in range(n_epochs):
  inp,oup = prepare()
  for j in range(0,batch_size):
    y_pred = net(inp[j]);
    y_out = net(oup[j]);
    loss = loss_fn(y_pred,y_out)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
  print(f'Finished epoch {i}, latest loss {loss}')

This however leads to the above mentioned error.

I am not sure what or how to try solving this problem, I am new to pytorch. Thanks for your assistance.

I also know that some answers have been posted for other questions, however I was not able to comprehend them.


Solution

  • The model has to accept a float tensor. You're explicitly ensuring it is an int64 tensor with a = a.to(dtype=torch.int64). The dtype you'll want to convert to is torch.float32 or torch.float.

    e.g.

    linear = torch.nn.Linear(5,5)
    linear(torch.tensor([1,2,3,4,5], dtype=torch.int)) # <-- this will fail
    linear(torch.tensor([1,2,3,4,5], dtype=torch.float)) # <-- this won't