pytorch中numpy矩阵转tensor,矩阵维度变换和维度拓展

np.transpose()将numpy矩阵的纬度按[2,0,1]矩阵交换,np.expand_dims()维度拓展一个维度,位置为axis = 1,维度。一般直接用.copy()进行矩阵复制,不要直接相等。

    rgb = np.expand_dims(np.transpose(rgb,[2,0,1]),axis=1).copy()
  1. “RuntimeError: Input type (torch.cuda.DoubleTensor) and weight type (torch.cuda.FloatTensor) should be the same”错误解决办法
    input = torch.from_numpy(rgb)
    target = torch.from_numpy(rad)
    input = input.cuda()
    target = target.cuda()

因为pytorch中的weight的类型存储为float32存储,而input为double(64)位,所以input和weight都为cuda()型的。并且类型要一样,但是numpy自动存储的类型为double(Float32),所以要将input的类型调整为double()型。

    input = torch.from_numpy(rgb)
    target = torch.from_numpy(rad)
    input = input.type(torch.FloatTensor)
    input = input.cuda()
    target = target.cuda()

将tensor转化为numpy,转化为数值。当转化的时候要先转化为Tensor.cpu(),在转化为Tensor.numpy()

testLoss = testLoss.cpu().numpy()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值