model/train.py
def validate(model, valid_loader, epoch, hp):
    """
    Evaluate the model on the validation set.
    """
    losses = AverageMeter()
    accs = AverageMeter()

    for i, (x, y) in enumerate(valid_loader):
        x, y = x.cuda(), y.cuda()
        x, y = Variable(x), Variable(y)

        # duplicate 10 times
        x = x.repeat(hp['M'], 1, 1, 1)

        # initialize location vector and hidden state
        hp['VALIDATE_BATCH_SIZE'] = x.shape[0]
        h_t, l_t = reset(hp)

        # extract the glimpses
        log_pi = []
        baselines = []
        for t in range(hp['NUM_GLIMPSES'] - 1):
            # forward pass through model
            h_t, l_t, b_t, p = model(x, l_t, h_t)

            # store
            baselines.append(b_t)
            log_pi.append(p)

        # last iteration
        h_t, l_t, b_t, log_probas, p = model(x, l_t, h_t, last=True)
        log_pi.append(p)
        baselines.append(b_t)

        # convert list to tensors and reshape
        baselines = torch.stack(baselines).transpose(1, 0)
        log_pi = torch.stack(log_pi).transpose(1, 0)

        # average
        log_probas = log_probas.view(
            hp['M'], -1, log_probas.shape[-1]
        )
        log_probas = torch.mean(log_probas, dim=0)

        baselines = baselines.contiguous().view(
            hp['M'], -1, baselines.shape[-1]
        )
        baselines = torch.mean(baselines, dim=0)

        log_pi = log_pi.contiguous().view(
            hp['M'], -1, log_pi.shape[-1]
        )
        log_pi = torch.mean(log_pi, dim=0)

        # calculate reward
        predicted = torch.max(log_probas, 1)[1]
        R = (predicted.detach() == y).float()
        R = R.unsqueeze(1).repeat(1, hp['NUM_GLIMPSES'])

        # compute losses for differentiable modules
        loss_action = F.nll_loss(log_probas, y)
        loss_baseline = F.mse_loss(baselines, R)

        # compute reinforce loss
        adjusted_reward = R - baselines.detach()
        loss_reinforce = torch.sum(-log_pi*adjusted_reward, dim=1)
        loss_reinforce = torch.mean(loss_reinforce, dim=0)

        # sum up into a hybrid loss
        loss = loss_action + loss_baseline + loss_reinforce

        # compute accuracy
        correct = (predicted == y).float()
        acc = 100 * (correct.sum() / len(y))

        # store
        losses.update(loss.data[0], x.size()[0])
        accs.update(acc.data[0], x.size()[0])

        # log to tensorboard
        iteration = epoch*len(valid_loader) + i
        log_value('valid_loss', losses.avg, iteration)
        log_value('valid_acc', accs.avg, iteration)

    return losses.avg, accs.avg

    Evaluate the model on the validation set.
    
Evaluate the model on the validation set.
Evaluate the model on the validation set .
def validate(model, valid_loader, epoch, hp):
    losses = AverageMeter()
    accs = AverageMeter()
    for i, (x, y) in enumerate(valid_loader):
        x, y = x.cuda(), y.cuda()
        x, y = Variable(x), Variable(y)
        x = x.repeat(hp['M'], 1, 1, 1)
        hp['VALIDATE_BATCH_SIZE'] = x.shape[0]
        h_t, l_t = reset(hp)
        log_pi = []
        baselines = []
        for t in range(hp['NUM_GLIMPSES'] - 1):
            h_t, l_t, b_t, p = model(x, l_t, h_t)
            baselines.append(b_t)
            log_pi.append(p)
        h_t, l_t, b_t, log_probas, p = model(x, l_t, h_t, last=True)
        log_pi.append(p)
        baselines.append(b_t)
        baselines = torch.stack(baselines).transpose(1, 0)
        log_pi = torch.stack(log_pi).transpose(1, 0)
        log_probas = log_probas.view(
            hp['M'], -1, log_probas.shape[-1]
        )
        log_probas = torch.mean(log_probas, dim=0)
        baselines = baselines.contiguous().view(
            hp['M'], -1, baselines.shape[-1]
        )
        baselines = torch.mean(baselines, dim=0)
        log_pi = log_pi.contiguous().view(
            hp['M'], -1, log_pi.shape[-1]
        )
        log_pi = torch.mean(log_pi, dim=0)
        predicted = torch.max(log_probas, 1)[1]
        R = (predicted.detach() == y).float()
        R = R.unsqueeze(1).repeat(1, hp['NUM_GLIMPSES'])
        loss_action = F.nll_loss(log_probas, y)
        loss_baseline = F.mse_loss(baselines, R)
        adjusted_reward = R - baselines.detach()
        loss_reinforce = torch.sum(-log_pi*adjusted_reward, dim=1)
        loss_reinforce = torch.mean(loss_reinforce, dim=0)
        loss = loss_action + loss_baseline + loss_reinforce
        correct = (predicted == y).float()
        acc = 100 * (correct.sum() / len(y))
        losses.update(loss.data[0], x.size()[0])
        accs.update(acc.data[0], x.size()[0])
        iteration = epoch*len(valid_loader) + i
        log_value('valid_loss', losses.avg, iteration)
        log_value('valid_acc', accs.avg, iteration)
    return losses.avg, accs.avg
def validate ( model , valid_loader , epoch , hp ) : losses = AverageMeter ( ) accs = AverageMeter ( ) for i , ( x , y ) in enumerate ( valid_loader ) : x , y = x . cuda ( ) , y . cuda ( ) x , y = Variable ( x ) , Variable ( y ) x = x . repeat ( hp [ 'M' ] , 1 , 1 , 1 ) hp [ 'VALIDATE_BATCH_SIZE' ] = x . shape [ 0 ] h_t , l_t = reset ( hp ) log_pi = [ ] baselines = [ ] for t in range ( hp [ 'NUM_GLIMPSES' ] - 1 ) : h_t , l_t , b_t , p = model ( x , l_t , h_t ) baselines . append ( b_t ) log_pi . append ( p ) h_t , l_t , b_t , log_probas , p = model ( x , l_t , h_t , last = True ) log_pi . append ( p ) baselines . append ( b_t ) baselines = torch . stack ( baselines ) . transpose ( 1 , 0 ) log_pi = torch . stack ( log_pi ) . transpose ( 1 , 0 ) log_probas = log_probas . view ( hp [ 'M' ] , - 1 , log_probas . shape [ - 1 ] ) log_probas = torch . mean ( log_probas , dim = 0 ) baselines = baselines . contiguous ( ) . view ( hp [ 'M' ] , - 1 , baselines . shape [ - 1 ] ) baselines = torch . mean ( baselines , dim = 0 ) log_pi = log_pi . contiguous ( ) . view ( hp [ 'M' ] , - 1 , log_pi . shape [ - 1 ] ) log_pi = torch . mean ( log_pi , dim = 0 ) predicted = torch . max ( log_probas , 1 ) [ 1 ] R = ( predicted . detach ( ) == y ) . float ( ) R = R . unsqueeze ( 1 ) . repeat ( 1 , hp [ 'NUM_GLIMPSES' ] ) loss_action = F . nll_loss ( log_probas , y ) loss_baseline = F . mse_loss ( baselines , R ) adjusted_reward = R - baselines . detach ( ) loss_reinforce = torch . sum ( - log_pi * adjusted_reward , dim = 1 ) loss_reinforce = torch . mean ( loss_reinforce , dim = 0 ) loss = loss_action + loss_baseline + loss_reinforce correct = ( predicted == y ) . float ( ) acc = 100 * ( correct . sum ( ) / len ( y ) ) losses . update ( loss . data [ 0 ] , x . size ( ) [ 0 ] ) accs . update ( acc . data [ 0 ] , x . size ( ) [ 0 ] ) iteration = epoch * len ( valid_loader ) + i log_value ( 'valid_loss' , losses . avg , iteration ) log_value ( 'valid_acc' , accs . avg , iteration ) return losses . avg , accs . avg
Evaluate the model on the validation set.
Evaluate the model on the validation set .
"""
    Evaluate the model on the validation set.
    """ # duplicate 10 times # initialize location vector and hidden state # extract the glimpses # forward pass through model # store # last iteration # convert list to tensors and reshape # average # calculate reward # compute losses for differentiable modules # compute reinforce loss # sum up into a hybrid loss # compute accuracy # store # log to tensorboard
model
valid_loader
epoch
hp