我想將熊貓的列轉換為 PyTorch 張量。該列的每個單元格都有一個 300 暗淡的 NumPy 向量(一個嵌入)。
我試過這個:
torch.from_numpy(g_list[1]['sentence_vector'].to_numpy())
但它拋出了這個錯誤:
TypeError:無法轉換 numpy.object_ 型別的 np.ndarray。唯一支持的型別是:float64、float32、float16、complex64、complex128、int64、int32、int16、int8、uint8 和 bool。
uj5u.com熱心網友回復:
如果您有此資料框,其中每列是 2 個數字的向量:
import torch
import pandas as pd
df = pd.DataFrame({'a': [[ 3, 29],[ 3, 29]],
'b': [[94, 170],[ 3, 29]],
'c': [[31, 115],[ 3, 29]]})

要將此資料幀轉換為 pytorch 張量,只需將資料幀的值轉換為串列,然后轉換為張量:
t = torch.Tensor(list(df.values))
#output
tensor([[[ 3., 29.],
[ 94., 170.],
[ 31., 115.]],
[[ 3., 29.],
[ 3., 29.],
[ 3., 29.]]])
的形狀t為 [2,3,2],每個串列中有 2 行 3 列 2 個元素。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/453063.html
