2

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

ChaosPredictor
  • 3,777
  • 1
  • 36
  • 46
  • 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 Oct 19 '20 at 23:10

1 Answers1

3

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]
Dishin H Goyani
  • 7,195
  • 3
  • 26
  • 37
  • 1
    Thank you, From you answer I can understand that pytorch doesn't have list data structure? I mean a numpy array turn to a tensor and a list stay a list – ChaosPredictor Oct 20 '20 at 08:14
  • 1
    "numpy array turn to a tensor and a list stay a list" Yes that is right. – Dishin H Goyani Oct 20 '20 at 08:35
  • 1
    @ChaosPredictor pytorch has list-like data structure. https://pytorch.org/docs/stable/generated/torch.nn.ModuleList.html?highlight=list#torch.nn.ModuleList – meowongac Oct 20 '20 at 09:26