1. 程式人生 > 其它 >torch1.7.1問題報錯:RuntimeError: expected scalar type Half but found Double

torch1.7.1問題報錯:RuntimeError: expected scalar type Half but found Double

技術標籤:學習分享神經網路深度學習機器學習pytorch矩陣

先來一點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

我不知道是具體哪裡出的問題,如果是[email protected]_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
   

如果有遇到同類問題,知道是啥原因或者想探討問題,可以私信我,不定期回覆,謝謝。