pytorch报错tensor.item()——IndexError: invalid index of a 0-dim tensor.

IndexError: invalid index of a 0-dim tensor. Use `tensor.item()` in Python or `tensor.item<T>()` in C++ to convert a 0-dim tensor to a number

pytorch报错tensor.item()——IndexError: invalid index of a 0-dim tensor.

pytorch在训练时发生如上报错,有其他大佬解释是pytorch版本问题造成的.data[0]和.item()的问题,但我在创建虚拟环境,安装其它版本pytorch后依旧出现同样的错误,因此排除了pytorch版本问题。

出错位置如下:

pytorch报错tensor.item()——IndexError: invalid index of a 0-dim tensor.    咨询大佬后,明白了是在dataloader部分出现了问题,比如我batchsize设置为8,而最后一个batch图片数目不足8张,造成了维度不同问题,在dataloader中加入drop_last=True,丢弃图片数量不足8张的最后一个batch即解决。

如下:

pytorch报错tensor.item()——IndexError: invalid index of a 0-dim tensor.

 

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2022年5月27日
下一篇 2022年5月27日

相关推荐