February 2018
Intermediate to advanced
262 pages
6h 59m
English
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) ...