class MyModel(nn.Module):
def __init__(self, input_dim, output_dim):
self.input_dim = input_dim
self.output_dim = output_dim
super().__init__()
self.linear1 = nn.Linear(input_dim, 3)
self.linear2 = nn.Linear(3, 3)
self.linear3 = nn.Linear(3, output_dim)
self.act = nn.ReLU()
def forward(self, x):
# |x| = (batch_size, input_dim)
h = self.act(self.linear1(x)) # |h| = (batch_size, 3)
h = self.act(self.linear2(h))
y = self.linear3(h)
# |y| = (batch_size, output_dim)
return y
model = MyModel(x.size(-1), y.size(-1))
model = nn.Sequential(
nn.Linear(x.size(-1), 3),
nn.LeakyReLU(),
nn.Linear(3, 3),
nn.LeakyReLU(),
nn.Linear(3, 3),
nn.LeakyReLU(),
nn.Linear(3, 3),
nn.LeakyReLU(),
nn.Linear(3, 3),
nn.LeakyReLU(),
nn.Linear(3, y.size(-1)),
)
optimizer = optim.SGD(model.parameters(),
lr=learning_rate)
for i in range(n_epochs):
y_hat = model(x)
loss = F.mse_loss(y_hat, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i + 1) % print_interval == 0:
print('Epoch %d: loss=%.4e' % (i + 1, loss))