Last active
March 25, 2020 21:10
-
-
Save ravishchawla/f4b75b775d34f5b8d54d4076c1bf5e77 to your computer and use it in GitHub Desktop.
Torch Model Training
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(nn_model, nn_optimizer, nn_criterion, data_loader, val_loader = None, num_epochs = 5, print_ratio = 0.1, verbose=True): | |
for epoch in range(num_epochs): | |
# Enable Training for the model | |
nn_model.train() | |
running_loss = 0; | |
for ite, (x, y, l) in enumerate(data_loader): | |
init_time = time.time(); | |
# Convert our tensors to GPU tensors | |
x = x.cuda() | |
y = y.cuda() | |
# Clear gradients | |
nn_optimizer.zero_grad() | |
# Forward Propagation and compute predictions | |
preds = nn_model.forward(x, l) | |
# Compute loss against actual values | |
loss = nn_criterion(preds, y) | |
# Back Propagation and Updating weights | |
loss.backward() | |
nn_optimizer.step() | |
running_loss = running_loss + loss.item(); | |
print('Epoch %d done in %.2f min'%(epoch+1, (time.time() - epoch_time)/60 )); | |
running_loss = 0.0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment