Converting a list to tensor

Any ways for converting a list as follows to tensor?

a = [[1,2,3],[4,5,6],[1]]
b = torch.tensor(a)

For this one, I am getting this error:

ValueError: expected sequence of length 3 at dim 1 (got 1)
1 Like

This won’t work, as your input has varying shapes in dim1.
You could pad the last row with some values:

a = [[1,2,3],[4,5,6],[1, 0, 0]]
b = torch.tensor(a)
5 Likes

I see. Thank you very much for your help @ptrblck.

Actually I’m stuck in same trouble but, in my case my list are so large that don’t know how to change that.
My errors like this.


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
C:\Users\HILAB_~1\AppData\Local\Temp/ipykernel_14512/3355998112.py in <module>
      1 for epoch in range(EPOCH):
----> 2         X_train_ten, y_train_ten = Variable(torch.tensor(X_train)), Variable(torch.tensor(y_train, dtype=torch.float32, requires_grad=True))
      3         print(X_train_ten.size())
      4 
      5         # X_train_ten = X_train_ten.view(428, -1)

ValueError: expected sequence of length 1889 at dim 1 (got 2300)