pytorch Optimizer as a Module code example
Example 1: import optimizer pytorch
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
A = 3.1415926
b = 2.7189351
error = 0.1
N = 100
X = Variable(torch.randn(N, 1))
t = A * X + b + Variable(torch.randn(N, 1) * error)
model = nn.Linear(1, 1)
optimizer = optim.SGD(model.parameters(), lr=0.05)
loss_fn = nn.MSELoss()
niter = 50
for _ in range(0, niter):
optimizer.zero_grad()
predictions = model(X)
loss = loss_fn(predictions, t)
loss.backward()
optimizer.step()
print("-" * 50)
print("error = {}".format(loss.data[0]))
print("learned A = {}".format(list(model.parameters())[0].data[0, 0]))
print("learned b = {}".format(list(model.parameters())[1].data[0]))
Example 2: implement custom optimizer pytorch
optimizer = MySOTAOptimizer(my_model.parameters(), lr=0.001)
for epoch in epochs:
for batch in epoch:
outputs = my_model(batch)
loss = loss_fn(outputs, true_values)
loss.backward()
optimizer.step()