How to convert a matrix of torch.tensor to a larger tensor?
I meet a problem to convert a python matrix of torch.tensor to a torch.tensor

For example, M is an (n,m) matrix, with each element M[i][j] is a torch.tensor with same size (p, q, r, ...). How to convert python list of list M to a torch.tensor with size (n,m,p,q,r,...)
e.g. 

M = []
for i in range(5):
    row = []
    for j in range(10):
        row.append(torch.rand(3,4))
    M.append(row)


How to convert above M to a torch.tensor with size (5,10,3,4).

Try torch.stack() to stack a list of tensors on the first dimension.

import torch

M = []
for i in range(5):
    row = []
    for j in range(10):
        row.append(torch.rand(3,4))
    row = torch.stack(row)
    M.append(row)
M = torch.stack(M)

print(M.size())
# torch.Size([5, 10, 3, 4])

