Convert dictionary to tensors, and back

Question:

I started with a dictionary object:

{"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]]}]}

I then want to add padding to each input and output object in a loop. So I converted each input and output to a tensor so I could then use F.pad to add padding. Result of the first input:

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]]).

result of the first output:

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]])

So that works fine. Now, I want to reconstruct the generated tensors into the same form as the original dictionary, so that it will look like this:

{"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]]}]}

I can see a string concatenation way that might work:

composedString = '{"train": [{"input": ' + tensor1 + tensor2

or something like that. But given that there are different numbers of elements in the various arrays, it seems like a loop nightmare. I’m thinking there’s got to be a better way. Anyone know what it is?

Asked By: David

||

Answers:

Does the following serve your purpose?

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)
Answered By: Wasi Ahmad
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.