Pytorch遇到的问题

1、loss保持不变,始终稳定在0.208

loss保持不变

1.1在计算loss时数据维数不匹配

from torch import nn 
loss = nn.CrossEntropyLoss(outputs, labels)

经过查看可发现outputs.shape为torch.Size([64, 64, 10])
而labels.shape为torch.Size([64, 10])

1.1.1使用view时报错:

RuntimeError: view size is not compatible with input tensor’s size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(…) instead.

更改:

将变量先转为contiguous ,再进行view:
targets.contiguous().view(targets.size(0)*targets.size(1),-1)
或直接
targets.contiguous()
target.view(target.size(0), -1)

1.1.2 更改完view错误后

Expected input batch_size (4096) to match target batch_size (64)

outputs.shape: torch.Size([4096, 10])
labels.shape: torch.Size([64, 10])

查看标签的过程中,发现pytorch和TensorFlow之间的计算差异
https://blog.csdn.net/weixin_39190382/article/details/114433884
TensorFlow
在这里插入图片描述
Pytorch
在这里插入图片描述
修改参数使得

outputs.shape: torch.Size([64, 10])
labels.shape: torch.Size([64])

至此代码能够正常运行,且loss能够发生变化

2、输出test准确率为0

2.1 解决方法1(未用上)

原因是gao的代码是pytorch3迁移过来的,很多地方使用了sum(pred_y== target)来求训练集的精度,但是没有转成numpy这里就会为0,因为torch数据本身不支持这种计算方式,所以改成sum(np.array(pred_y)== np.array(target.data.cpu()))即可使训练集正常运行

链接: 原文链接

2.2 更改predicted的计算方式

#_, predicted = torch.max(outputs.data, 1)
#correct += (predicted == labels).sum().item()
#print(predicted)
#print(labels)
predicted = torch.max(outputs.data,1)[1]
correct += (predicted == labels).sum()
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值