> 文章列表 > dtype = torch.float32到底有什么用

dtype = torch.float32到底有什么用

dtype = torch.float32到底有什么用

dtype = torch.float32到底有什么用

解决:RuntimeError: expected scalar type Long but found Float

先看一个例子

请添加图片描述
要计算 z = x0 + w1x1 + w2x2
其中w = [-0.2,0.15,0.15]

  • 于是你开始尝试
    在这里插入图片描述
  • 其中torch.mv用于矩阵*向量
    此时你发现他需要你提供float格式的数据
    你查看发现生成的x默认为int64,也就是说torch只支持同样的数据格式之间的计算,
    请添加图片描述
    此时把x定义的时候定义为float32就行了

定义torch.tensor时定义为float32

请添加图片描述

当然也可以x.float转换格式请添加图片描述

使用torch.Torch()大写T可以无脑生成float32(不推荐使用,太死版了)

请添加图片描述