Skip to content

Instantly share code, notes, and snippets.

@omarsar
Created August 24, 2019 17:59
Show Gist options
  • Save omarsar/fd8b6886746c9e42aa8d3c7b7c497623 to your computer and use it in GitHub Desktop.
Save omarsar/fd8b6886746c9e42aa8d3c7b7c497623 to your computer and use it in GitHub Desktop.
learning_rate = 0.001
num_epochs = 5
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = MyModel()
model = model.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
## compute accuracy
def get_accuracy(logit, target, batch_size):
''' Obtain accuracy for training round '''
corrects = (torch.max(logit, 1)[1].view(target.size()).data == target.data).sum()
accuracy = 100.0 * corrects/batch_size
return accuracy.item()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment