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