【问题标题】:Error while executing RNN code on pyTorch?在 pyTorch 上执行 RNN 代码时出错?
【发布时间】:2019-03-28 17:57:33
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: pytorch rnn google-colaboratory


    【解决方案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 调整为适当的形状,那么它应该适合您。

    【讨论】:

      猜你喜欢
      • 2018-10-28
      • 2021-06-06
      • 1970-01-01
      • 2019-11-10
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多