Skip to content

Instantly share code, notes, and snippets.

@pedrohbtp
Last active December 13, 2018 16:13
Show Gist options
  • Save pedrohbtp/eff5ddcdbadeef5d01f072c3497994ce to your computer and use it in GitHub Desktop.
Save pedrohbtp/eff5ddcdbadeef5d01f072c3497994ce to your computer and use it in GitHub Desktop.
Pytroch training example
import torch.optim as optim
import torch.nn as nn
# instantiate your network that should be defined by you
net = Net()
# create your optimizer
optimizer = optim.SGD(net.parameters(), lr=0.01)
# define your criterion for optimization
criterion = nn.MSELoss()
# dat_set comes from somewhere
for data in data_set:
# zero the gradient buffers
optimizer.zero_grad()
# Passes the data through your network
output = net.forward(data)
# calculates the loss
loss = criterion(output, target)
# Propagates the loss back
loss.backward()
# Updates all the weights of the network
optimizer.step()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment