Convert a list of numpy array to torch tensor list

Viewed 6082

The numpy arrays in the list are 2D array that have different sizes, let's say:

1x1, 4x4, 8x8, etc

about 7 arrays in total.

I know how to convert each on of them, by:

torch.from_numpy(a1by1).type(torch.FloatTensor)
torch.from_numpy(a4by4).type(torch.FloatTensor)
etc..

Is there a way to convert the entire list in one command?

I found these 2 question:

How to convert a list or numpy array to a 1d torch tensor?

How to convert a list of tensors into a torch::Tensor?

but it's not what Im' looking for

1 Answers

If by one command you mean one-liner then

Here, We can use list-comprehension

lst = [a1by1, a4by4, a8by8]
lst = [torch.from_numpy(item).float() for item in lst]
Related