【发布时间】:2020-07-17 07:27:09
【问题描述】:
我得到一个pandas DataFrame,如下所示,并希望将其转换为torch.tensor 以进行嵌入。
# output first 5 rows examples
print(df['col'].head(5))
col
0 [a, bc, cd]
1 [d, ed, fsd, g, h]
2 [i, hh, ihj, gfw, hah]
3 [a, cb]
4 [sad]
train_tensor = torch.from_numpy(train)
但它得到一个错误:
TypeError:无法转换 numpy.str_ 类型的 np.ndarray。唯一支持的类型是:float64、float32、float16、int64、int32、int16、int8、uint8 和 bool。
from_numpy() 似乎不支持可变长度序列。
那么如果要初始化 tensor 形式,正确的方法是什么?
在得到相应的tensor 之后,我会尝试为可变长度序列添加填充并为其做嵌入层。
有人可以帮我吗?
提前致谢。
【问题讨论】:
-
train到底是什么?那 5 个文字数组是什么?能不能得到更精确的代码sn-ps?
标签: python pandas pytorch tensor