【问题标题】:Convert a python list of python lists to pytorch tensor将 python 列表的 python 列表转换为 pytorch 张量
【发布时间】:2019-08-07 03:59:10
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: python pytorch


    【解决方案1】:

    你可以先在 Python 中展平你的列表:

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

    然后创建你的张量:

    flattened_tensor = torch.FloatTensor(flat_list)
    

    【讨论】:

      猜你喜欢
      • 2019-05-23
      • 2022-01-10
      • 2019-07-29
      • 2022-01-01
      • 2020-08-05
      • 2021-12-23
      • 2022-10-17
      • 2020-04-07
      相关资源
      最近更新 更多