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 and validating the model

We need to make some minor changes to the fit method to accommodate the three input-values generated from the data loader. The following code implements the new fit function:

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 , (data1,data2,data3,target) in enumerate(data_loader):        if is_cuda:            data1,data2,data3,target = data1.cuda(),data2.cuda(),data3.cuda(),target.cuda()        data1,data2,data3,target = Variable(data1,volatile),Variable(data2,volatile),Variable(data3,volatile),Variable(target)        if phase == 'training':            optimizer.zero_grad() output = model(data1,data2,data3) ...
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