Skip to Content
Deep Learning with PyTorch
book

Deep Learning with PyTorch

by Vishnu Subramanian
February 2018
Intermediate to advanced
262 pages
6h 59m
English
Packt Publishing
Content preview from Deep Learning with PyTorch

Training the model

Training the model is very similar to what we saw for building image classifiers, so we will be using the same functions. We pass batches of data through the model, calculate the outputs and losses, and then optimize the model weights, which includes the embedding weights. The following code does this:

def fit(epoch,model,data_loader,phase='training',volatile=False):    if phase == 'training':        model.train()    if phase == 'validation':        model.eval()        volatile=True    running_loss = 0.0    running_correct = 0    for batch_idx , batch in enumerate(data_loader):        text , target = batch.text , batch.label        if is_cuda:            text,target = text.cuda(),target.cuda()                if phase == 'training':            optimizer.zero_grad()        output = model(text) loss = F.nll_loss(output,target) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Deep Learning with PyTorch

Deep Learning with PyTorch

Eli Stevens, Thomas Viehmann, Luca Pietro Giovanni Antiga
Grokking Deep Learning

Grokking Deep Learning

Andrew W. Trask

Publisher Resources

ISBN: 9781788624336Supplemental Content