src/train.py
def train(opt, tr_dataloader, val_dataloader, model, optim, lr_scheduler):
    '''
    Train the model with the prototypical learning algorithm
    '''
    train_loss = []
    train_acc = []
    val_loss = []
    val_acc = []
    best_acc = 0

    for epoch in range(opt.epochs):
        print('=== Epoch: {} ==='.format(epoch))
        tr_iter = iter(tr_dataloader)
        for batch in tqdm(tr_iter):
            x, y = batch
            x, y = Variable(x), Variable(y)
            if opt.cuda:
                x, y = x.cuda(), y.cuda()
            model_output = model(x)
            l, acc = loss(model_output, target=y, n_support=opt.num_support_tr)
            l.backward()
            optim.step()
            train_loss.append(l.data[0])
            train_acc.append(acc.data[0])
        avg_loss = np.mean(train_loss[-opt.iterations:])
        avg_acc = np.mean(train_acc[-opt.iterations:])
        print('Train Loss: {}, Train Acc: {}'.format(avg_loss, avg_acc))
        val_iter = iter(val_dataloader)
        for batch in tqdm(val_iter):
            x, y = batch
            x, y = Variable(x), Variable(y)
            if opt.cuda:
                x, y = x.cuda(), y.cuda()
            model_output = model(x)
            l, acc = loss(model_output, target=y, n_support=opt.num_support_tr)
            val_loss.append(l.data[0])
            val_acc.append(acc.data[0])
        avg_loss = np.mean(val_loss[-opt.iterations:])
        avg_acc = np.mean(val_acc[-opt.iterations:])
        postfix = ' (Best)' if avg_acc > best_acc else ''
        print('Val Loss: {}, Val Acc: {}{}'.format(avg_loss, avg_acc, postfix))
        if avg_acc > best_acc:
            torch.save(model.fc.state_dict(), '../best_model.pth')
            best_acc = avg_acc

        lr_scheduler.step()

    return best_acc, train_loss, train_acc, val_loss, val_acc

    Train the model with the prototypical learning algorithm
    
Train the model with the prototypical learning algorithm
Train the model with the prototypical learning algorithm
def train(opt, tr_dataloader, val_dataloader, model, optim, lr_scheduler):
    train_loss = []
    train_acc = []
    val_loss = []
    val_acc = []
    best_acc = 0
    for epoch in range(opt.epochs):
        print('=== Epoch: {} ==='.format(epoch))
        tr_iter = iter(tr_dataloader)
        for batch in tqdm(tr_iter):
            x, y = batch
            x, y = Variable(x), Variable(y)
            if opt.cuda:
                x, y = x.cuda(), y.cuda()
            model_output = model(x)
            l, acc = loss(model_output, target=y, n_support=opt.num_support_tr)
            l.backward()
            optim.step()
            train_loss.append(l.data[0])
            train_acc.append(acc.data[0])
        avg_loss = np.mean(train_loss[-opt.iterations:])
        avg_acc = np.mean(train_acc[-opt.iterations:])
        print('Train Loss: {}, Train Acc: {}'.format(avg_loss, avg_acc))
        val_iter = iter(val_dataloader)
        for batch in tqdm(val_iter):
            x, y = batch
            x, y = Variable(x), Variable(y)
            if opt.cuda:
                x, y = x.cuda(), y.cuda()
            model_output = model(x)
            l, acc = loss(model_output, target=y, n_support=opt.num_support_tr)
            val_loss.append(l.data[0])
            val_acc.append(acc.data[0])
        avg_loss = np.mean(val_loss[-opt.iterations:])
        avg_acc = np.mean(val_acc[-opt.iterations:])
        postfix = ' (Best)' if avg_acc > best_acc else ''
        print('Val Loss: {}, Val Acc: {}{}'.format(avg_loss, avg_acc, postfix))
        if avg_acc > best_acc:
            torch.save(model.fc.state_dict(), '../best_model.pth')
            best_acc = avg_acc
        lr_scheduler.step()
    return best_acc, train_loss, train_acc, val_loss, val_acc
def train ( opt , tr_dataloader , val_dataloader , model , optim , lr_scheduler ) : train_loss = [ ] train_acc = [ ] val_loss = [ ] val_acc = [ ] best_acc = 0 for epoch in range ( opt . epochs ) : print ( '=== Epoch: {} ===' . format ( epoch ) ) tr_iter = iter ( tr_dataloader ) for batch in tqdm ( tr_iter ) : x , y = batch x , y = Variable ( x ) , Variable ( y ) if opt . cuda : x , y = x . cuda ( ) , y . cuda ( ) model_output = model ( x ) l , acc = loss ( model_output , target = y , n_support = opt . num_support_tr ) l . backward ( ) optim . step ( ) train_loss . append ( l . data [ 0 ] ) train_acc . append ( acc . data [ 0 ] ) avg_loss = np . mean ( train_loss [ - opt . iterations : ] ) avg_acc = np . mean ( train_acc [ - opt . iterations : ] ) print ( 'Train Loss: {}, Train Acc: {}' . format ( avg_loss , avg_acc ) ) val_iter = iter ( val_dataloader ) for batch in tqdm ( val_iter ) : x , y = batch x , y = Variable ( x ) , Variable ( y ) if opt . cuda : x , y = x . cuda ( ) , y . cuda ( ) model_output = model ( x ) l , acc = loss ( model_output , target = y , n_support = opt . num_support_tr ) val_loss . append ( l . data [ 0 ] ) val_acc . append ( acc . data [ 0 ] ) avg_loss = np . mean ( val_loss [ - opt . iterations : ] ) avg_acc = np . mean ( val_acc [ - opt . iterations : ] ) postfix = ' (Best)' if avg_acc > best_acc else '' print ( 'Val Loss: {}, Val Acc: {}{}' . format ( avg_loss , avg_acc , postfix ) ) if avg_acc > best_acc : torch . save ( model . fc . state_dict ( ) , '../best_model.pth' ) best_acc = avg_acc lr_scheduler . step ( ) return best_acc , train_loss , train_acc , val_loss , val_acc
Train the model with the prototypical learning algorithm
Train the model with the prototypical learning algorithm
'''
    Train the model with the prototypical learning algorithm
    '''
opt
tr_dataloader
val_dataloader
model
optim
lr_scheduler