将python列表的python列表转换为pytorch张量

时间:2019-03-16 04:00:50

标签: python pytorch

将列表的python列表转换为PyTorch张量的常规方法是什么?

a = [0,0]
b = [1,1]
c = [2]
c = [a, b, c]

我希望将c转换为扁平的Torch张量,如下所示:

torch([ 0, 0, 1, 1, 2])

1 个答案:

答案 0 :(得分:1)

您可以先使用Python拼合列表:

flat_list = [item for sublist in c for item in sublist]

并创建您的张量:

flattened_tensor = torch.FloatTensor(flat_list)
相关问题