当前位置: 代码迷 >> 综合 >> Message=Expected object of scalar type Long but got scalar type Float for argument #3 ‘mat2‘ in c
  详细解决方案

Message=Expected object of scalar type Long but got scalar type Float for argument #3 ‘mat2‘ in c

热度:98   发布时间:2024-02-12 11:19:06.0

在深度学习框架pytorch入门与实践的代码中 

因为版本问题在   y_pred =x.mm(w)+b.expand_as(y)这句话出现报错

 Message=Expected object of scalar type Long but got scalar type Float for argument #3 'mat2' in call to _th_addmm_out   Source=K:\exp\PythonApplication1\PythonApplication1\PythonApplication1.py   StackTrace:   File "K:\exp\PythonApplication1\PythonApplication1\PythonApplication1.py", line 37, in <module>     y=x.mm(w)+b.expand_as(x)

刚开始按字面分析结合网上的博客,将所有的传入用.long()把float改成long程序虽然能够运行但结果不对,w变成了0维向量

采用visual stduio的断点分析发现

y_pred =x.mm(w)+b.expand_as(y)中

而x是int tensor,而y由这句话传入

x=t.arange(0,20).view(-1,1)

因此可得将20改为20.0可自动变成浮点型

变为 x=t.arange(0,20.0).view(-1,1)

成功运行

结论:pytorch的版本问题带来的坑,另报错语句不一定是真的要你float改long

而是多想想为什么会这样报错

  相关解决方案