Skip to content

Instantly share code, notes, and snippets.

@jojonki
Created December 7, 2017 15:17
Show Gist options
  • Select an option

  • Save jojonki/9f4e66f2cd98806e4f5bced34e195b80 to your computer and use it in GitHub Desktop.

Select an option

Save jojonki/9f4e66f2cd98806e4f5bced34e195b80 to your computer and use it in GitHub Desktop.
Pure SGD in PyTOrch
# https://github.com/jcjohnson/pytorch-examples
for t in range(500):
# Forward pass: compute predicted y by passing x to the model. Module objects
# override the __call__ operator so you can call them like functions. When
# doing so you pass a Variable of input data to the Module and it produces
# a Variable of output data.
y_pred = model(x)
# Compute and print loss. We pass Variables containing the predicted and true
# values of y, and the loss function returns a Variable containing the loss.
loss = loss_fn(y_pred, y)
print(t, loss.data[0])
# Zero the gradients before running the backward pass.
model.zero_grad()
# Backward pass: compute gradient of the loss with respect to all the learnable
# parameters of the model. Internally, the parameters of each Module are stored
# in Variables with requires_grad=True, so this call will compute gradients for
# all learnable parameters in the model.
loss.backward()
# Update the weights using gradient descent. Each parameter is a Variable, so
# we can access its data and gradients like we did before.
for param in model.parameters():
param.data -= learning_rate * param.grad.data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment