model/train.py
def train_one_epoch(model, optimizer, train_loader, epoch, hp):
    """
    Train the model for 1 epoch of the training set.
    An epoch corresponds to one full pass through the entire
    training set in successive mini-batches.
    """
    batch_time = AverageMeter()
    losses = AverageMeter()
    accs = AverageMeter()

    tic = time.time()
    with tqdm(total=hp['NUM_TRAIN']) as pbar:
        for sample_index, (x, y) in enumerate(train_loader):
            x, y = x.cuda(), y.cuda()
            x, y = Variable(x), Variable(y)

            # initialize location vector and hidden state
            hp['BATCH_SIZE'] = x.shape[0]  # We need to set this to train on variable batch size
            h_t, l_t = reset(hp)

            # save images
            imgs = [x[0:9]]

            # extract the glimpses
            locs = []
            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
                locs.append(l_t[0:9])
                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)
            locs.append(l_t[0:9])

            # convert list to tensors and reshape
            baselines = torch.stack(baselines).transpose(1, 0)
            log_pi = torch.stack(log_pi).transpose(1, 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
            # summed over timesteps and averaged across batch
            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])

            # compute gradients and update SGD
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            # measure elapsed time
            toc = time.time()
            batch_time.update(toc-tic)

            pbar.set_description(
                (
                    "{:.1f}s - loss: {:.3f} - acc: {:.3f}".format(
                        (toc-tic), loss.data[0], acc.data[0]
                    )
                )
            )
            pbar.update(hp['BATCH_SIZE'])

            # dump the glimpses and locs
            plot = False
            if (epoch % hp['PLOT_FREQ'] == 0) and (sample_index == 0):
                plot = True

            if plot:
                imgs = [g.cpu().data.numpy().squeeze() for g in imgs]
                locs = [l.cpu().data.numpy() for l in locs]
                pickle.dump(
                    imgs, open(
                        hp['PLOT_DIR'] + "g_{}.p".format(epoch+1),
                        "wb"
                    )
                )
                pickle.dump(
                    locs, open(
                        hp['PLOT_DIR'] + "l_{}.p".format(epoch+1),
                        "wb"
                    )
                )

            # log to tensorboard
            iteration = epoch*len(train_loader) + sample_index
            log_value('train_loss', losses.avg, iteration)
            log_value('train_acc', accs.avg, iteration)

        return losses.avg, accs.avg

    Train the model for 1 epoch of the training set.
    An epoch corresponds to one full pass through the entire
    training set in successive mini-batches.
    
Train the model for 1 epoch of the training set.
An epoch corresponds to one full pass through the entire
training set in successive mini-batches.
Train the model for 1 epoch of the training set . An epoch corresponds to one full pass through the entire training set in successive mini - batches .
def train_one_epoch(model, optimizer, train_loader, epoch, hp):
    batch_time = AverageMeter()
    losses = AverageMeter()
    accs = AverageMeter()
    tic = time.time()
    with tqdm(total=hp['NUM_TRAIN']) as pbar:
        for sample_index, (x, y) in enumerate(train_loader):
            x, y = x.cuda(), y.cuda()
            x, y = Variable(x), Variable(y)
            hp['BATCH_SIZE'] = x.shape[0]  
            h_t, l_t = reset(hp)
            imgs = [x[0:9]]
            locs = []
            log_pi = []
            baselines = []
            for t in range(hp['NUM_GLIMPSES'] - 1):
                h_t, l_t, b_t, p = model(x, l_t, h_t)
                locs.append(l_t[0:9])
                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)
            locs.append(l_t[0:9])
            baselines = torch.stack(baselines).transpose(1, 0)
            log_pi = torch.stack(log_pi).transpose(1, 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])
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            toc = time.time()
            batch_time.update(toc-tic)
            pbar.set_description(
                (
                    "{:.1f}s - loss: {:.3f} - acc: {:.3f}".format(
                        (toc-tic), loss.data[0], acc.data[0]
                    )
                )
            )
            pbar.update(hp['BATCH_SIZE'])
            plot = False
            if (epoch % hp['PLOT_FREQ'] == 0) and (sample_index == 0):
                plot = True
            if plot:
                imgs = [g.cpu().data.numpy().squeeze() for g in imgs]
                locs = [l.cpu().data.numpy() for l in locs]
                pickle.dump(
                    imgs, open(
                        hp['PLOT_DIR'] + "g_{}.p".format(epoch+1),
                        "wb"
                    )
                )
                pickle.dump(
                    locs, open(
                        hp['PLOT_DIR'] + "l_{}.p".format(epoch+1),
                        "wb"
                    )
                )
            iteration = epoch*len(train_loader) + sample_index
            log_value('train_loss', losses.avg, iteration)
            log_value('train_acc', accs.avg, iteration)
        return losses.avg, accs.avg
def train_one_epoch ( model , optimizer , train_loader , epoch , hp ) : batch_time = AverageMeter ( ) losses = AverageMeter ( ) accs = AverageMeter ( ) tic = time . time ( ) with tqdm ( total = hp [ 'NUM_TRAIN' ] ) as pbar : for sample_index , ( x , y ) in enumerate ( train_loader ) : x , y = x . cuda ( ) , y . cuda ( ) x , y = Variable ( x ) , Variable ( y ) hp [ 'BATCH_SIZE' ] = x . shape [ 0 ] h_t , l_t = reset ( hp ) imgs = [ x [ 0 : 9 ] ] locs = [ ] log_pi = [ ] baselines = [ ] for t in range ( hp [ 'NUM_GLIMPSES' ] - 1 ) : h_t , l_t , b_t , p = model ( x , l_t , h_t ) locs . append ( l_t [ 0 : 9 ] ) 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 ) locs . append ( l_t [ 0 : 9 ] ) baselines = torch . stack ( baselines ) . transpose ( 1 , 0 ) log_pi = torch . stack ( log_pi ) . transpose ( 1 , 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 ] ) optimizer . zero_grad ( ) loss . backward ( ) optimizer . step ( ) toc = time . time ( ) batch_time . update ( toc - tic ) pbar . set_description ( ( "{:.1f}s - loss: {:.3f} - acc: {:.3f}" . format ( ( toc - tic ) , loss . data [ 0 ] , acc . data [ 0 ] ) ) ) pbar . update ( hp [ 'BATCH_SIZE' ] ) plot = False if ( epoch % hp [ 'PLOT_FREQ' ] == 0 ) and ( sample_index == 0 ) : plot = True if plot : imgs = [ g . cpu ( ) . data . numpy ( ) . squeeze ( ) for g in imgs ] locs = [ l . cpu ( ) . data . numpy ( ) for l in locs ] pickle . dump ( imgs , open ( hp [ 'PLOT_DIR' ] + "g_{}.p" . format ( epoch + 1 ) , "wb" ) ) pickle . dump ( locs , open ( hp [ 'PLOT_DIR' ] + "l_{}.p" . format ( epoch + 1 ) , "wb" ) ) iteration = epoch * len ( train_loader ) + sample_index log_value ( 'train_loss' , losses . avg , iteration ) log_value ( 'train_acc' , accs . avg , iteration ) return losses . avg , accs . avg
Train the model for 1 epoch of the training set.
Train the model for 1 epoch of the training set .
"""
    Train the model for 1 epoch of the training set.
    An epoch corresponds to one full pass through the entire
    training set in successive mini-batches.
    """ # initialize location vector and hidden state # We need to set this to train on variable batch size # save images # extract the glimpses # forward pass through model # store # last iteration # convert list to tensors and reshape # calculate reward # compute losses for differentiable modules # compute reinforce loss # summed over timesteps and averaged across batch # sum up into a hybrid loss # compute accuracy # store # compute gradients and update SGD # measure elapsed time # dump the glimpses and locs # log to tensorboard
model
optimizer
train_loader
epoch
hp