FDD/label_distillation_or2.py
def validate(val_loader, model, criterion, epoch, fixed_input, labels, num_steps):
    """Validation phase. This involves training a model from scratch."""
    losses_eval = AverageMeter()
    top1_eval = AverageMeter()
    start = time.time()

    print('Number of steps for retraining during validation: ' + str(num_steps))

    # initialize a new model
    if args.target == "cifar10" or args.target == "cifar100":
        if args.resnet:
            model_eval = M.ResNetMeta(dataset=args.target, depth=18, num_classes=num_classes,
                                      bottleneck=False, device=device).to(device=device)
        else:
            model_eval = M.AlexCifarNetMeta(args).to(device=device)
    else:
        model_eval = M.LeNetMeta(args).to(device=device)
    optimizer = torch.optim.Adam(model_eval.parameters())
    model_eval.train()

    # train a model from scratch for the given number of steps
    # using only the base examples and their synthetic labels
    for ITER in range(num_steps):
        # sample a minibatch of n_i examples from x~, y~: x~', y~'
        perm = torch.randperm(fixed_input.size(0))
        idx = perm[:args.inner_batch_size]
        fi_i = fixed_input[idx]
        lb_i = labels[idx].detach()

        fi_o = model_eval(fi_i)
        loss = soft_cross_entropy(fi_o, lb_i)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    # evaluate the validation error
    # switch to evaluate mode
    model_eval.eval()

    for i, (input_, target) in enumerate(val_loader):
        input_ = input_.to(device=device)
        target = target.to(device=device)
        output = model_eval(input_)
        loss = criterion(output, target)

        # measure error and record loss
        err1 = compute_error_rate(output.data, target, topk=(1,))[0]
        top1_eval.update(err1.item(), input_.size(0))
        losses_eval.update(loss.item(), input_.size(0))

    val_time = time.time() - start
    print('* Epoch: [{0}/{1}]\t Top 1-err {top1.avg:.3f} Val Loss {loss.avg:.3f} Time {val_time:.3f}'.format(
        epoch, args.epochs, top1=top1_eval, loss=losses_eval, val_time=val_time))

    return top1_eval.avg, losses_eval.avg
Validation phase. This involves training a model from scratch.
Validation phase. This involves training a model from scratch.
Validation phase . This involves training a model from scratch .
def validate(val_loader, model, criterion, epoch, fixed_input, labels, num_steps):
    losses_eval = AverageMeter()
    top1_eval = AverageMeter()
    start = time.time()
    print('Number of steps for retraining during validation: ' + str(num_steps))
    if args.target == "cifar10" or args.target == "cifar100":
        if args.resnet:
            model_eval = M.ResNetMeta(dataset=args.target, depth=18, num_classes=num_classes,
                                      bottleneck=False, device=device).to(device=device)
        else:
            model_eval = M.AlexCifarNetMeta(args).to(device=device)
    else:
        model_eval = M.LeNetMeta(args).to(device=device)
    optimizer = torch.optim.Adam(model_eval.parameters())
    model_eval.train()
    for ITER in range(num_steps):
        perm = torch.randperm(fixed_input.size(0))
        idx = perm[:args.inner_batch_size]
        fi_i = fixed_input[idx]
        lb_i = labels[idx].detach()
        fi_o = model_eval(fi_i)
        loss = soft_cross_entropy(fi_o, lb_i)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    model_eval.eval()
    for i, (input_, target) in enumerate(val_loader):
        input_ = input_.to(device=device)
        target = target.to(device=device)
        output = model_eval(input_)
        loss = criterion(output, target)
        err1 = compute_error_rate(output.data, target, topk=(1,))[0]
        top1_eval.update(err1.item(), input_.size(0))
        losses_eval.update(loss.item(), input_.size(0))
    val_time = time.time() - start
    print('* Epoch: [{0}/{1}]\t Top 1-err {top1.avg:.3f} Val Loss {loss.avg:.3f} Time {val_time:.3f}'.format(
        epoch, args.epochs, top1=top1_eval, loss=losses_eval, val_time=val_time))
    return top1_eval.avg, losses_eval.avg
def validate ( val_loader , model , criterion , epoch , fixed_input , labels , num_steps ) : losses_eval = AverageMeter ( ) top1_eval = AverageMeter ( ) start = time . time ( ) print ( 'Number of steps for retraining during validation: ' + str ( num_steps ) ) if args . target == "cifar10" or args . target == "cifar100" : if args . resnet : model_eval = M . ResNetMeta ( dataset = args . target , depth = 18 , num_classes = num_classes , bottleneck = False , device = device ) . to ( device = device ) else : model_eval = M . AlexCifarNetMeta ( args ) . to ( device = device ) else : model_eval = M . LeNetMeta ( args ) . to ( device = device ) optimizer = torch . optim . Adam ( model_eval . parameters ( ) ) model_eval . train ( ) for ITER in range ( num_steps ) : perm = torch . randperm ( fixed_input . size ( 0 ) ) idx = perm [ : args . inner_batch_size ] fi_i = fixed_input [ idx ] lb_i = labels [ idx ] . detach ( ) fi_o = model_eval ( fi_i ) loss = soft_cross_entropy ( fi_o , lb_i ) optimizer . zero_grad ( ) loss . backward ( ) optimizer . step ( ) model_eval . eval ( ) for i , ( input_ , target ) in enumerate ( val_loader ) : input_ = input_ . to ( device = device ) target = target . to ( device = device ) output = model_eval ( input_ ) loss = criterion ( output , target ) err1 = compute_error_rate ( output . data , target , topk = ( 1 , ) ) [ 0 ] top1_eval . update ( err1 . item ( ) , input_ . size ( 0 ) ) losses_eval . update ( loss . item ( ) , input_ . size ( 0 ) ) val_time = time . time ( ) - start print ( '* Epoch: [{0}/{1}]\t Top 1-err {top1.avg:.3f} Val Loss {loss.avg:.3f} Time {val_time:.3f}' . format ( epoch , args . epochs , top1 = top1_eval , loss = losses_eval , val_time = val_time ) ) return top1_eval . avg , losses_eval . avg
Validation phase.
Validation phase .
"""Validation phase. This involves training a model from scratch.""" # initialize a new model # train a model from scratch for the given number of steps # using only the base examples and their synthetic labels # sample a minibatch of n_i examples from x~, y~: x~', y~' # evaluate the validation error # switch to evaluate mode # measure error and record loss
val_loader
model
criterion
epoch
fixed_input
labels
num_steps