import torch
import torch.nn as nn
class MyModel(nn.Module):
def init(self, inp, out, hidden_size = 19):
super().init()
self.layers = nn.ModuleList()
for i in range(10):
self.layers.add_module(f'layer_{i}',nn.Linear(inp, hidden_size))
self.layers.add_module(f'act{i}',nn.ReLU())
inp = hidden_size
hidden_size -= 1
self.layers.add_module(f'layer_out',nn.Linear(inp, out))
def forward(self, x):
outputs = []
for i, layer in enumerate(self.layers):
x = layer(x)
outputs.append(x)
return outputs
model = MyModel(inp = 20, out = 10)
import torch
import torch.nn as nn
class MyModel(nn.Module):
def __init__(self, inp, out, hidden_size=19):
super().__init__()
layers = []
for i in range(1, 10):
layers.append(nn.Linear(inp, hidden_size))
layers.append(nn.ReLU())
inp = hidden_size
hidden_size -= 1
layers.append(nn.Linear(inp, out))
self.model = nn.Sequential(*layers)
def forward(self, x):
out self.model(x)
return out
model = MyModel(inp=20, out=10)
а это правильный код только без ModuleList, а надо только с ним