AttributeError: ‘torch.dtype’ object has no attribute ‘type’

np.mean()改为torch.mean(torch.stack(my_list))即可
loss是一个tensor
np.mean()输入list可以,array也可以
torch.mean()必须输入tensor,最后得到的结果也是tensor
torch.stack()沿着某个维度进行堆叠数据
所以np.mean()会报错
本文探讨了在使用PyTorch框架时,遇到的AttributeError错误:'torch.dtype' object has no attribute 'type'。详细解释了np.mean()与torch.mean()的区别,前者可以处理list和array,而后者专门用于tensor,并给出了正确的解决方案。
AttributeError: ‘torch.dtype’ object has no attribute ‘type’

np.mean()改为torch.mean(torch.stack(my_list))即可
loss是一个tensor
np.mean()输入list可以,array也可以
torch.mean()必须输入tensor,最后得到的结果也是tensor
torch.stack()沿着某个维度进行堆叠数据
所以np.mean()会报错
您可能感兴趣的与本文相关的镜像
PyTorch 2.5
PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理
1553
1万+

被折叠的 条评论
为什么被折叠?