This question looks a bit like this:
List of 2D arrays with different size into 3D array
and even more like this:
Turning list of 2D tensors with different length to one 3D tensor,
but either I don't get it or the small differences that both dimensions in the 2d lists in my list can vary and that I have to pad my tensor to (1st dim, max 2nd dim, max 3rd) do change things enough to warrant another question.
Given a list of 2D elements I want to:
- find the max of each of these dimensions and create a 3D tensor with dim 0 = length of list, dim 1 = max length of dim 0 of each element in the list, dim 2 = max length of dim 1 of each element in the list.
- create a tensor from the list that is padded to these dimensions with zeros.
Example: Given:
[ [ [0, 1, 2] , [8, 0] ],
[ [12, 13, 6, 7], [12] , [23, 11] ] ]
Output:
[ [ [0, 1, 2, *0*], [8, 0, *0*, *0* ], [*0*, *0*, *0*, *0*] ],
[ [12, 13, 6, 7], [12, *0*, *0*, *0*], [23, 11, *0*, *0* ] ] ]
I was guessing this would be the algorithm, but it is missing a part I don't even have a guess for, I would like some certainty before I try to implement by guess, I have a suspicion there's some simpler way like some variation of cat() and reshape() that would do this.
- Do I use a loop to create a list with the length of each list in the list given?
- Get the max value of this list. Set this as the max of the 2nd dim, dim = 1.
- Create another list to hold the lengths of the lists/arrays forming the 3rd dim, i.e. the lists in the lists in the list.
- Assign this to the max of the 3rd dimension.
- Now I loop through the length of the list over these max of the 2nd and 3rd dim and in the 2nd dim somehow figure out that I am at the end of that list (???)
- add empty lists until the end of that loop, then
- loop through the max of the 3rd dim and assign the value if it exists, somehow figure out if I am at the end of the list even if the list is empty (???)
- assign
0
for the remainder.