0%

pytorch RuntimeError-expected-backend-CPU-and-dtype-Double-but-got-backend-CPU-and-dtype-Float

自动摘要: date:2019012503:38:57 pytorchexpectedbackendCPUanddtypeDoublebutgotbackendCPUan ……..

date: 2019-01-25 03:38:57

pytorch expected backend CPU and dtype Double but got backend CPU and dtype Float

背景:

RuntimeError: expected backend CPU and dtype Double but got backend CPU and dtype Float


报错在transform.ToTensor () ,原因是因为``pytorch计算时要求类型一致,有的数据类型在转换时默认为float64/float32`了,转换成匹配类型即可。

方法:

  1. A fix would be to call .double() on your model (or .float() on the input)
    https://github.com/pytorch/pytorch/issues/2138

  2. _numpy().float()

  3. astype('float')

  4. 您的输入和目标张量是DoubleTensors,但您的模型参数是FloatTensors。您必须转换输入或参数。要将输入转换为float(推荐):

    1. inputs, labels = data
      inputs = inputs.float()
      labels = labels.float()
      inputs, labels = Variable(inputs), Variable(labels)
      
      # 将模型转换为double;
      # 建议使用float而不是double。它是PyTorch中的默认张量类型。在GPU上,float计算比double计算快得多。
      model = ColorizerNet()
      model.double()
      
      # https://discuss.pytorch.org/t/problems-with-weight-array-of-floattensor-type-in-loss-function/381
      

欢迎关注我的其它发布渠道