将字典转换为张量,然后返回

时间:2020-06-09 06:02:57

标签: python pytorch

我从一个字典对象开始:

window.data

然后我想在循环中为每个输入和输出对象添加填充。因此,我将每个输入和输出转换为张量,以便随后可以使用F.pad添加填充。第一次输入的结果:

{"train": [{"input": [[3, 1, 2], [3, 1, 2], [3, 1, 2]], "output": [[4, 5, 6], [4, 5, 6], [4, 5, 6]]}, {"input": [[2, 3, 8], [2, 3, 8], [2, 3, 8]], "output": [[6, 4, 9], [6, 4, 9], [6, 4, 9]]}]}

第一个输出的结果:

tensor([[0, 0, 0, 0, 0],
        [0, 3, 1, 2, 0],
        [0, 3, 1, 2, 0],
        [0, 3, 1, 2, 0],
        [0, 0, 0, 0, 0]]).

所以很好。现在,我想将生成的张量重构为与原始字典相同的形式,使其看起来像这样:

tensor([[0, 0, 0, 0, 0],
        [0, 4, 5, 6, 0],
        [0, 4, 5, 6, 0],
        [0, 4, 5, 6, 0],
        [0, 0, 0, 0, 0]])

我可以看到一种可能有效的字符串连接方式:

{"train": [{"input": [[0, 0, 0, 0, 0], [0, 3, 1, 2, 0], [0, 3, 1, 2, 0], [0, 3, 1, 2, 0], [0, 0, 0, 0, 0]], "output": [[0, 0, 0, 0, 0], [0, 4, 5, 6, 0], [0, 4, 5, 6, 0], [0, 4, 5, 6, 0], [0, 0, 0, 0, 0]]}, {"input": [[0, 0, 0, 0, 0], [0, 2, 3, 8, 0], [0, 2, 3, 8, 0], [0, 2, 3, 8, 0], [0, 0, 0, 0, 0]], "output": [[0, 0, 0, 0, 0], [0, 6, 4, 9, 0], [0, 6, 4, 9, 0], [0, 6, 4, 9, 0], [0, 0, 0, 0, 0]]}]}

或类似的东西。但是鉴于各种数组中元素的数量不同,这似乎是一场恶梦。我在想有一种更好的方法。有人知道这是什么吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

以下内容符合您的目的吗?

in_dict = {"train": [{"input": [[3, 1, 2], [3, 1, 2], [3, 1, 2]], "output": [[4, 5, 6], [4, 5, 6], [4, 5, 6]]}, {"input": [[2, 3, 8], [2, 3, 8], [2, 3, 8]], "output": [[6, 4, 9], [6, 4, 9], [6, 4, 9]]}]}

train_examples = []
for item in in_dict['train']:
    in_tensor = torch.Tensor(item['input'])
    out_tensor = torch.Tensor(item['output'])
    train_examples.append([in_tensor, out_tensor])

out_dict = {'train': []}
for item in train_examples:
    out_dict['train'].append({
        'input': item[0].tolist(),
        'output': item[1].tolist()
    })

print(out_dict)