0

I would like to make 640 fc layer.

(in def init)

self.fc0 = nn.Linear(120, M)
self.fc1 = nn.Linear(120, M)
.....
self.fc638 = nn.Linear(120, M)
self.fc639 = nn.Linear(120, M)

(in def forward)

x[:,:,0,:] = self.fc0(x[:,:,0,:])
x[:,:,1,:] = self.fc0(x[:,:,1,:])
.......
x[:,:,639,:] = self.fc639(x[:,:,639,:])

How can I execute the code above in simpler way ?

Jenny I
  • 91
  • 1
  • 2
  • 7
  • related: https://stackoverflow.com/q/58097924/1714410 – Shai May 31 '21 at 07:05
  • Does this answer your question? [Pytorch dynamic amount of Layers?](https://stackoverflow.com/questions/62937388/pytorch-dynamic-amount-of-layers) – iacob May 31 '21 at 08:28

1 Answers1

0

Use containers:

class MultipleFC(nn.Module):
  def __init__(self, num_layers, M):
    self.layers = nn.ModuleList([nn.Linear(120, M) for _ in range(num_layers)])

  def forward(self, x):
    y = torch.empty_like(x)  # up to third dim should be M
    for i, fc in enumerate(self.layers):
      y[..., i, :] = fc(x[..., i, :])
    return y
Shai
  • 111,146
  • 38
  • 238
  • 371