torch1.7.1问题报错:RuntimeError: expected scalar type Half but found Double

先来一点code,关于 矩阵相乘

	v_b = np.array(list(vectors2.values()))
    b_feature = torch.from_numpy(v_b).to(device)
    b_feature /= b_feature.norm(dim=-1, keepdim=True)
    with torch.no_grad():
        w_features = model.encode(w_inputs)
    w_features /= w_features.norm(dim=-1, keepdim=True)
   res = w_features @ b_feature.T
   

大概意思就是从别的地方读取了一个数组,然后我用模型计算出来的矩阵和它进行运算,但是这里就报了一个错误:

RuntimeError: expected scalar type Half but found Double

我不知道是具体哪里出的问题,如果是b_feature@b_feature.T和w_features @w_features.T都是没有问题的。而且如果w_features 不是这种计算出来,而是和b_features一样是从一个array里读取出来的话,也不会有问题。
琢磨了一下,改成如下,问题得到解决。

	v_b = np.array(list(vectors2.values()))
    b_feature = torch.from_numpy(v_b).to(device)
    b_feature /= b_feature.norm(dim=-1, keepdim=True)
    b_feature = b_feature.float()
    with torch.no_grad():
        w_features = model.encode(w_inputs)
    w_features /= w_features.norm(dim=-1, keepdim=True)
   w_features = w_features.float()
   res = w_features @ b_feature.T
   

如果有遇到同类问题,知道是啥原因或者想探讨问题,可以私信我,不定期回复,谢谢。

你可能感兴趣的:(学习,分享,神经网络,深度学习,机器学习,pytorch,矩阵)