4/10/2022

pytorch module list example

 

ex1)

linears = nn.ModuleList([nn.Linear(10, 10) for i in range(10)])

..


ex2)

linears=[]
for i in range(10):
linears.append( nn.Linear(10, 10) )
nn.ModuleList(linears)

..



print module list

>

ModuleList(
  (0): Linear(in_features=10, out_features=10, bias=True)
  (1): Linear(in_features=10, out_features=10, bias=True)
  (2): Linear(in_features=10, out_features=10, bias=True)
  (3): Linear(in_features=10, out_features=10, bias=True)
  (4): Linear(in_features=10, out_features=10, bias=True)
  (5): Linear(in_features=10, out_features=10, bias=True)
  (6): Linear(in_features=10, out_features=10, bias=True)
  (7): Linear(in_features=10, out_features=10, bias=True)
  (8): Linear(in_features=10, out_features=10, bias=True)
  (9): Linear(in_features=10, out_features=10, bias=True)
)