2
votes

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
Do you expect us to look up those links? It would be nicer if you explained what was missing. The obvious One command is to wrap those individual commands in a function. What's so import an about "one command"? You might also want to explain why this is difficult, if for no other reason than to show your own command of the language and these packages.hpaulj

1 Answers

1
votes

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]