【PyTorch】RuntimeError: expected scalar type Float but found Long 已解决

主要问题:矩阵乘法时类型不一致。先看下面代码:

	A = torch.arange(20,dtype=torch.float32).reshape(5,4)
	a = torch.arange(4)
	A.shape,a.shape,torch.mv(A, a)

请添加图片描述

出现该问题的主要原因是在A矩阵中已经设置了类型dtype=torch.float32,但在a向量中未设置,从下图可以看到,a的默认类型为int64,从而出现类型不一致的问题。

请添加图片描述

	A = torch.arange(20,dtype=torch.float32).reshape(5,4)
	a = torch.arange(4,dtype=torch.float32)
	A.shape,a.shape,torch.mv(A, a)

请添加图片描述

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

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

(0)
社会演员多的头像社会演员多普通用户
上一篇 2022年5月18日
下一篇 2022年5月19日

相关推荐