Runtime error: expected scalar type Float but found Double

错误:Runtime error: expected scalar type Float but found Double

w_true=torch.tensor([2,-3.4]).T
b_true=4.2
feature = torch.from_numpy(np.random.normal(0,1,(num_input,num_example)))
#feature = torch.float32(feature)
labels = torch.matmul(w_true.T,feature)+b_true

问题:Runtime error: expected scalar type Float but found Double
原因:np.random.rand()生成的数据是float64的,而torch默认是float32,于是就出现了问题
解决办法

feature = torch.from_numpy(np.float32(np.random.normal(0,1,(num_input,num_example))))

你可能感兴趣的:(pytorch,深度学习,python)