Last active
June 15, 2017 21:56
-
-
Save kdubovikov/eb57c2abcdc665d5326ed15111287d20 to your computer and use it in GitHub Desktop.
PyTorch example 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from torch.autograd import Variable | |
import numpy as np | |
def rmse(y, y_hat): | |
"""Compute root mean squared error""" | |
return torch.sqrt(torch.mean((y - y_hat).pow(2))) | |
def forward(x, e): | |
"""Forward pass for our fuction""" | |
return x.pow(e.repeat(x.size(0))) | |
# Let's define some settings | |
n = 1000 # number of examples | |
learning_rate = 5e-10 | |
# Model definition | |
x = Variable(torch.rand(n) * 10, requires_grad=False) | |
y = forward(x, exp) | |
# Model parameters | |
exp = Variable(torch.FloatTensor([2.0]), requires_grad=False) | |
exp_hat = Variable(torch.FloatTensor([4]), requires_grad=True) | |
# Optimizer (NEW) | |
opt = torch.optim.SGD([exp_hat], lr=learning_rate, momentum=0.9) | |
loss_history = [] | |
exp_history = [] | |
# Training loop | |
for i in range(0, 10000): | |
opt.zero_grad() | |
print("Iteration %d" % i) | |
# Compute current estimate | |
y_hat = forward(x, exp_hat) | |
# Calculate loss function | |
loss = rmse(y, y_hat) | |
# Do some recordings for plots | |
loss_history.append(loss.data[0]) | |
exp_history.append(y_hat.data[0]) | |
# Update model parameters | |
loss.backward() | |
opt.step() | |
print("loss = %s" % loss.data[0]) | |
print("exp = %s" % exp_hat.data[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment