python - converting list of tensors to tensors pytorch ...
stackoverflow.com › questions › 55050717Mar 08, 2019 · import torch a = torch.arange (8).reshape (2, 2, 2) b = torch.arange (12).reshape (2, 2, 3) my_list = [a, b] my_tensor = torch.cat ( [a, b], dim=2) print (my_tensor.shape) #torch.Size ( [2, 2, 5]) you haven't explained your goal so another option is to use pad_sequence like this:
Best way to convert a list to a tensor? - PyTorch Forums
discuss.pytorch.org › t › best-way-to-convert-a-listNov 04, 2019 · 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])