Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a list of numpy array to torch tensor list

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

like image 664
ChaosPredictor Avatar asked Nov 02 '25 15:11

ChaosPredictor


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]
like image 134
Dishin H Goyani Avatar answered Nov 04 '25 04:11

Dishin H Goyani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!