def train_step(model, train_loader, criterion, optimizer, epoch, step, valid_loader = None, assignment = None, loss_vector = None, loss_count=None): batch_time = AverageMeter() data_time = AverageMeter() losses = AverageMeter() top1 = AverageMeter() top5 = AverageMeter() stdev = AverageMeter() # switch to train mode model.train() end = time.time() for i, (input, target, inst_indices) in enumerate(train_loader): # measure data loading time data_time.update(time.time() - end) input_var = torch.autograd.Variable(input) # compute output output = model(input_var) loss, c_loss_vector, c_loss_count, c_stdev = criterion(output, list(inst_indices.numpy()), assignment, loss_vector, loss_count, input, model) stdev.update(c_stdev, 1) loss_vector *= 0.9 loss_count *= 0.9 loss_vector += (c_loss_vector/100.) loss_count += c_loss_count # measure accuracy and record loss # prec1, prec5 = accuracy(output.data, target, topk=(1, 5)) losses.update(loss.data[0], input.size(0)) # top1.update(prec1[0], input.size(0)) # top5.update(prec5[0], input.size(0)) # compute gradient and do SGD step model.zero_grad() loss.backward() optimizer.step() # measure elapsed time del loss, output batch_time.update(time.time() - end) if i % args.log_rate == 0 and i > 0: print('Epoch: [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t' # 'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t' # 'Prec@5 {top5.val:.3f} ({top5.avg:.3f})\t' .format( epoch, i, len(train_loader), batch_time=batch_time, data_time=data_time, loss=losses, top1=top1, top5=top5)) # Log items logger.add_scalar("Misc/batch time (s)", batch_time.avg, step + i) logger.add_scalar("Misc/Train_%", 1.0 - (data_time.avg/batch_time.avg), step + i) logger.add_scalar("Misc/epoch time (min)", batch_time.sum / 60., step + i) logger.add_scalar("Misc/time left (min)", ((len(train_loader) - i) * batch_time.avg) / 60., step + i) logger.add_scalar(args.dataset + "/Loss train (avg)", losses.avg, step + i) # logger.add_scalar(args.dataset + "/Perc5 train (avg)", top5.avg, step + i) # logger.add_scalar(args.dataset + "/Perc1 train (avg)", top1.avg, step + i) end = time.time() if valid_loader != None and i % args.eval_step == 0 and i > 0: _, _ = eval_step( model = model, data_loader = valid_loader, criterion = criterion, step = step + i, datasplit = "valid") model.train() return loss_vector, loss_count, stdev.avg
def train_step(model, train_loader, criterion, optimizer, epoch, step, valid_loader = None): batch_time = AverageMeter() data_time = AverageMeter() losses = AverageMeter() top1 = AverageMeter() top5 = AverageMeter() # switch to train mode model.train() end = time.time() for i, (input, target) in enumerate(train_loader): # measure data loading time data_time.update(time.time() - end) target = target.cuda(async=True) # input = input input_var = torch.autograd.Variable(input) target_var = torch.autograd.Variable(target) # compute output output = model(input_var) loss = criterion(output, target_var) # loss = F.nll_loss(output, target_var) # measure accuracy and record loss losses.update(loss.data[0], input.size(0)) # compute gradient and do SGD step model.zero_grad() loss.backward() optimizer.step() # measure elapsed time del loss, output batch_time.update(time.time() - end) if i % args.log_rate == 0 and i > 0: # print('Epoch: [{0}][{1}/{2}]\t' # 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' # 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' # 'Loss {loss.val:.4f} ({loss.avg:.4f})'.format( # epoch, i, len(train_loader), batch_time=batch_time, # data_time=data_time, loss=losses)) print('Epoch: [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t' # 'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t' # 'Prec@5 {top5.val:.3f} ({top5.avg:.3f})' .format( epoch, i, len(train_loader), batch_time=batch_time, data_time=data_time, loss=losses, top1=top1, top5=top5)) # Log items logger.add_scalar("Misc/batch time (s)", batch_time.avg, step + i) logger.add_scalar("Misc/Train_%", 1.0 - (data_time.avg/batch_time.avg), step + i) logger.add_scalar("Misc/epoch time (min)", batch_time.sum / 60., step + i) logger.add_scalar("Misc/time left (min)", ((len(train_loader) - i) * batch_time.avg) / 60., step + i) logger.add_scalar(args.dataset + "/Loss train (avg)", losses.avg, step + i) # logger.add_scalar(args.dataset + "/Perc5 train (avg)", top5.avg, step + i) # logger.add_scalar(args.dataset + "/Perc1 train (avg)", top1.avg, step + i) end = time.time()