Pytorch:invalid index of a 0-dim tensor.Use tensor.item() to convert a 0-dim tensor to a Python的解决方法

Pytorch: invalid index of a 0-dim tensor.Use tensor.item() to convert a 0-dim tensor to a Python number

在学习“白话强化学习PyTorch”时遇到的错误(原代码如下):在这里插入图片描述
运行时系统报错:
在这里插入图片描述

解决方法:

将"loss.data[0]" 更改为"loss.data",问题就解决了!
在这里插入图片描述
这样程序就能正常执行了。

发布了4 篇原创文章 · 获赞 14 · 访问量 303

猜你喜欢

转载自blog.csdn.net/weixin_45914452/article/details/104023323