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