iterations = 0 start = time.time() best_dev_acc = -1 train_iter.repeat = False header = ' Time Epoch Iteration Progress (%Epoch) Loss Dev/Loss Accuracy Dev/Accuracy' dev_log_template = ' '.join('{:>6.0f},{:>5.0f},{:>9.0f},{:>5.0f}/{:<5.0f} {:>7.0f}%,{:>8.6f},{:8.6f},{:12.4f},{:12.4f}'.split(',')) log_template = ' '.join('{:>6.0f},{:>5.0f},{:>9.0f},{:>5.0f}/{:<5.0f} {:>7.0f}%,{:>8.6f},{},{:12.4f},{}'.split(',')) os.makedirs(args.save_path, exist_ok=True) print(header) for epoch in range(args.epochs): train_iter.init_epoch() n_correct, n_total = 0, 0 for batch_idx, batch in enumerate(train_iter): model.train(); opt.zero_grad() iterations += 1 answer = model(batch) n_correct += (torch.max(answer, 1)[1].view(batch.label.size()).data == batch.label.data).sum() n_total += batch.batch_size train_acc = 100. * n_correct/n_total loss = criterion(answer, batch.label) loss.backward(); opt.step() if iterations % args.save_every == 0: snapshot_prefix = os.path.join(args.save_path, 'snapshot') snapshot_path = snapshot_prefix + '_acc_{:.4f}_loss_{:.6f}_iter_{}_model.pt'.format(train_acc, loss.data[0], iterations) torch.save(model, snapshot_path) for f in glob.glob(snapshot_prefix + '*'): if f != snapshot_path: os.remove(f) if iterations % args.dev_every == 0:
dev_log_template = ' '.join( '{:>6.0f},{:>5.0f},{:>9.0f},{:>5.0f}/{:<5.0f} {:>7.0f}%,{:>8.6f},{:8.6f},{:12.4f},{:12.4f}' .split(',')) log_template = ' '.join( '{:>6.0f},{:>5.0f},{:>9.0f},{:>5.0f}/{:<5.0f} {:>7.0f}%,{:>8.6f},{},{:12.4f},{}' .split(',')) makedirs(args.save_path) print(header) for epoch in range(args.epochs): train_iter.init_epoch() n_correct, n_total = 0, 0 for batch_idx, batch in enumerate(train_iter): # switch model to training mode, clear gradient accumulators model.train() opt.zero_grad() iterations += 1 # forward pass answer = model(batch) # calculate accuracy of predictions in the current batch n_correct += (torch.max(answer, 1)[1].view( batch.label.size()) == batch.label).sum().item() n_total += batch.batch_size train_acc = 100. * n_correct / n_total # calculate loss of the network output with respect to training labels loss = criterion(answer, batch.label)
iterations = 0 start = time.time() best_dev_acc = -1 header = ' Time Epoch Iteration Progress (%Epoch) Loss Dev/Loss Accuracy Dev/Accuracy' dev_log_template = ' '.join('{:>6.0f},{:>5.0f},{:>9.0f},{:>5.0f}/{:<5.0f} {:>7.0f}%,{:>8.6f},{:8.6f},{:12.4f},{:12.4f}'.split(',')) log_template = ' '.join('{:>6.0f},{:>5.0f},{:>9.0f},{:>5.0f}/{:<5.0f} {:>7.0f}%,{:>8.6f},{},{:12.4f},{}'.split(',')) makedirs(args.save_path) print(header) for epoch in range(args.epochs): train_iter.init_epoch() n_correct, n_total = 0, 0 for batch_idx, batch in enumerate(train_iter): # switch model to training mode, clear gradient accumulators model.train(); opt.zero_grad() iterations += 1 # forward pass answer = model(batch) # calculate accuracy of predictions in the current batch n_correct += (torch.max(answer, 1)[1].view(batch.label.size()) == batch.label).sum().item() n_total += batch.batch_size train_acc = 100. * n_correct/n_total # calculate loss of the network output with respect to training labels loss = criterion(answer, batch.label) # backpropagate and update optimizer learning rate