import torch
tmp=[torch.rand(2,4),torch.rand(2,4)]
print(type(tmp))
print(type(tmp[0]))
print(type(tmp[0][0]))
# The full tmp
print(tmp)
# After a stack, you convert the list to a dimensions
torch.stack(tmp)

@ptrblck I was trying something with subclasses. Can you check whether it is in the right direction

class nestedTensor(torch.Tensor):
def __new__(cls, x, *args, **kwargs):
print(super(nestedTensor,cls))
return([super(torch.Tensor,cls).__new__(cls, y, *args, **kwargs) for y in x])
def __init__(self, x):
print("Created nestedTensor")
tmp=nestedTensor([torch.rand(2,4),torch.rand(2,3),torch.rand(4,2)])
tmp

# %%
import torch
# trying to convert a list of tensors to a torch.tensor
x = torch.randn(3)
xs = [x.numpy(), x.numpy()]
# xs = torch.tensor(xs)
xs = torch.as_tensor(xs)
print(xs)
print(xs.size())
# %%
import torch
# trying to convert a list of tensors to a torch.tensor
x = torch.randn(3)
xs = [x.numpy(), x.numpy(), x.numpy()]
xs = [xs, xs]
# xs = torch.tensor(xs)
xs = torch.as_tensor(xs)
print(xs)
print(xs.size())

whats wrong with this solution…?

output:

import torch
# trying to convert a list of tensors to a torch.tensor
x = torch.randn(3)
xs = [x.numpy(), x.numpy(), x.numpy()]
xs = [xs, xs]
# xs = torch.tensor(xs)
xs = torch.as_tensor(xs)
print(xs)
print(xs.size())
tensor([[[0.3423, 1.6793, 0.0863],
[0.3423, 1.6793, 0.0863],
[0.3423, 1.6793, 0.0863]],
[[0.3423, 1.6793, 0.0863],
[0.3423, 1.6793, 0.0863],
[0.3423, 1.6793, 0.0863]]])
torch.Size([2, 3, 3])

I don’t see anything wrong with your approach, but as described in the other topic, you could use torch.stack instead of transforming the tensors to numpy arrays and call torch.as_tensor.

Nested tensors would allow you to create a tensor object containing tensors with different shapes, which doesn’t seem to be the use case you are working on.

I wish that torch.tensor(nested_list_of_tensors) gave you the corresponding tensors with many dimensions that respected the original list of tensors. Anyway, I have a small code that might be helpful here: Best way to convert a list to a tensor? - #8 by Brando_Miranda