Error while executing RNN code on pyTorch?
I am running a code on Binary Addition of two strings using PyTorch. 

However, while training the model I am getting the following error:

can't convert np.ndarray of type numpy.object. 
The only supported types are: double, float, float16, int64, int32, and uint8.  


Can anyone help me? Here is my code:

featDim=2 # two bits each from each of the String
outputDim=1 # one output node which would output a zero or 1

lstmSize=10

lossFunction = nn.MSELoss()
model =Adder(featDim, lstmSize, outputDim)
print ('model initialized')
#optimizer = optim.SGD(model.parameters(), lr=3e-2, momentum=0.8)
optimizer=optim.Adam(model.parameters(),lr=0.001)
epochs=500
### epochs ##
totalLoss= float("inf")
while totalLoss &gt; 1e-5:
  print(" Avg. Loss for last 500 samples = %lf"%(totalLoss))
  totalLoss=0
  for i in range(0,epochs): # average the loss over 200 samples

    stringLen=4
    testFlag=0
    x,y=getSample(stringLen, testFlag)

    model.zero_grad()


    x_var=autograd.Variable(torch.from_numpy(x).unsqueeze(1).float()) #convert to torch tensor and variable
    # unsqueeze() is used to add the extra dimension since
    # your input need to be of t*batchsize*featDim; you cant do away with the batch in pytorch
    seqLen=x_var.size(0)
    #print (x_var)
    x_var= x_var.contiguous()
    y_var=autograd.Variable(torch.from_numpy(y).float()) ##ERROR ON THIS LINE
    finalScores = model(x_var)
    #finalScores=finalScores.

    loss=lossFunction(finalScores,y_var)  
    totalLoss+=loss.data[0]
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()


  totalLoss=totalLoss/epochs


The main problem here is the type of your y. You haven't given any information about this, therefore this here will be more general:

But obviously your ndarray doesn't contain numeric data types. You have to use one of these mentioned in your error message:


  The only supported types are: double, float, float16, int64, int32,
  and uint8.


So here is a short example to demonstrate the issue:

If you use one of the previously mentioned data types it works just fine:

import torch
import numpy as np
a = np.ndarray(shape=(2,2), dtype=np.float) # data type np.float
print(a)
print(torch.autograd.Variable(torch.from_numpy(a).float()))


Output:

[[2.16641777e-314 2.16641777e-314]
 [2.16641777e-314 2.16641777e-314]]
Variable containing:
 0  0
 0  0
[torch.FloatTensor of size 2x2]



But if you use some other numpy data type (like np.object) you will get this error message:

import torch
import numpy as np
a = np.ndarray(shape=(2,2), dtype=np.object) # data type np.object
print(a)
print(torch.autograd.Variable(torch.from_numpy(a).float()))


This results in:

[[None None]
 [None None]]
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-27-01e1e4bec020&gt; in &lt;module&gt;()
      3 a = np.ndarray(shape=(2,2), dtype=np.object)
      4 print(a)
----&gt; 5 print(torch.autograd.Variable(torch.from_numpy(a).float()))

RuntimeError: can't convert a given np.ndarray to a tensor - it has an invalid type. The only supported types are: double, float, int64, int32, and uint8.


You probably haven't specified the data type np.object directly. I guess this is maybe a result of some nested arrays or so. 

But your need to bring your numpy array y into proper shape with a numeric data type then it should work for you.
