Hello. I have a deeply nested list of tensors. It’s returned by an external library which was previously numpy based, which I modified to convert numpy to torch:
a = list(self.nsgt.forward((x,)))
print(type(a))
print(len(a))
print(type(a[0]))
print(len(a[0]))
print(type(a[0][0]))
print(len(a[0][0]))
print(type(a[0][0][0]))
print(len(a[0][0][0]))
print(a[0][0][0].device)
Results in:
class 'list'
59
<class 'list'>
2
<class 'list'>
126
<class 'torch.Tensor'>
304
cuda:0
I would like to convert this into a tensor with the following shape:
(59, 2, 126, 304)
Previously, when this was an np ndarray, achieving what I needed was trivial:
A = np.asarray(a)
print(A.shape)
print(A.dtype)
This would result in the correct ndarray:
(59, 2, 126, 304)
float32
In torch, I’m having trouble achieving the same with torch.tensor
or torch.stack
.
torch.tensor issues:
A = torch.tensor(a)
ValueError: only one element tensors can be converted to Python scalars
torch.stack issue:
A = torch.stack((a))
TypeError: expected Tensor as element 0 in argument 0, but got list