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

Once the network is created, we can train the model using the same code as seen in the previous examples. The following is the code for training the model:

model = IMDBRnn(n_vocab,n_hidden,3,bs=32)model = model.cuda()optimizer = optim.Adam(model.parameters(),lr=1e-3)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)         running_loss += F.nll_loss(output,target,size_average=False).data[0] ...
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