Created
October 9, 2019 14:55
-
-
Save kabirahuja2431/a446247b69f09c3455b457ccb852f2a7 to your computer and use it in GitHub Desktop.
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
def train(net, criterion, opti, train_loader, val_loader, args): | |
for ep in range(args.max_eps): | |
for it, (seq, attn_masks, labels) in enumerate(train_loader): | |
#Clear gradients | |
opti.zero_grad() | |
#Converting these to cuda tensors | |
seq, attn_masks, labels = seq.cuda(args.gpu), attn_masks.cuda(args.gpu), labels.cuda(args.gpu) | |
#Obtaining the logits from the model | |
logits = net(seq, attn_masks) | |
#Computing loss | |
loss = criterion(logits.squeeze(-1), labels.float()) | |
#Backpropagating the gradients | |
loss.backward() | |
#Optimization step | |
opti.step() | |
if (it + 1) % args.print_every == 0: | |
acc = get_accuracy_from_logits(logits, labels) | |
print("Iteration {} of epoch {} complete. Loss : {} Accuracy : {}".format(it+1, ep+1, loss.item(), acc)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment