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

Defining the train and evaluate functions

The training of the model is very similar to what we saw in all the previous examples in this book. There are a few important changes that we need to make so that the trained model works better. Let's look at the code and its key parts:

criterion = nn.CrossEntropyLoss()def trainf():    # Turn on training mode which enables dropout.    lstm.train()    total_loss = 0    start_time = time.time()    hidden = lstm.init_hidden(batch_size)    for i,batch in enumerate(train_iter):        data, targets = batch.text,batch.target.view(-1)        # Starting each batch, we detach the hidden state from how it was previously produced.        # If we didn't, the model would try backpropagating all the way to start of the dataset. hidden = repackage_hidden(hidden) ...
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