在pyTorch上执行RNN代码时出错?

时间:2018-10-24 10:42:55

标签: pytorch rnn google-colaboratory

我正在使用PyTorch在两个字符串的二进制加法上运行代码。

但是,在训练模型时,出现以下错误:

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

有人可以帮助我吗?这是我的代码:

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 > 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

1 个答案:

答案 0 :(得分:1)

这里的主要问题是您的y的类型。您尚未提供任何有关此信息,因此这里的信息将更为笼统:

但是显然您的ndarray不包含数字数据类型。您必须使用错误消息中提到的其中之一:

  

仅支持以下类型:double,float,float16,int64,int32,   和uint8。

因此,这是一个简短的示例来演示该问题:

如果使用前面提到的数据类型之一,它就可以正常工作:

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()))

输出:

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


但是,如果您使用其他一些numpy数据类型(例如np.object),则会收到以下错误消息:

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()))

结果是:

[[None None]
 [None None]]
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-27-01e1e4bec020> in <module>()
      3 a = np.ndarray(shape=(2,2), dtype=np.object)
      4 print(a)
----> 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.

您可能没有直接指定数据类型np.object。我想这可能是一些嵌套数组的结果。

但是您需要将numpy数组y设置为具有数字数据类型的适当形状,然后它应该对您有用。