Example #1
0
 def resume_policy_from(self, policy_cpt_path):
     print('Resume policy from {}'.format(policy_cpt_path))
     resume_checkpoint(self.policy_net,
                       policy_cpt_path,
                       strict=True,
                       resume_key='policy',
                       print_keys=('epoch', ))
Example #2
0
    def load_pretrain_weights(self):
        """Loading weights from trained MLP model & GMF model"""
        config = self.config
        config['latent_dim'] = config['latent_dim_mlp']
        mlp_model = MLP(config)
        if config['use_cuda'] is True:
            mlp_model.cuda()
        resume_checkpoint(mlp_model,
                          model_dir=config['pretrain_mlp'],
                          device_id=config['device_id'])

        self.embedding_user_mlp.weight.data = mlp_model.embedding_user.weight.data
        self.embedding_item_mlp.weight.data = mlp_model.embedding_item.weight.data
        for idx in range(len(self.fc_layers)):
            self.fc_layers[idx].weight.data = mlp_model.fc_layers[
                idx].weight.data

        config['latent_dim'] = config['latent_dim_mf']
        gmf_model = GMF(config)
        if config['use_cuda'] is True:
            gmf_model.cuda()
        resume_checkpoint(gmf_model,
                          model_dir=config['pretrain_mf'],
                          device_id=config['device_id'])
        self.embedding_user_mf.weight.data = gmf_model.embedding_user.weight.data
        self.embedding_item_mf.weight.data = gmf_model.embedding_item.weight.data

        self.affine_output.weight.data = 0.5 * torch.cat([
            mlp_model.affine_output.weight.data,
            gmf_model.affine_output.weight.data
        ],
                                                         dim=-1)
        self.affine_output.bias.data = 0.5 * (
            mlp_model.affine_output.bias.data +
            gmf_model.affine_output.bias.data)
Example #3
0
 def resume_default_model(self, strict=False, resume_key='model'):
     model_file_path = os.path.join(self.config['model_dir'],
                                    self.config['model_resume_name'])
     resume_checkpoint(self.net,
                       model_file_path,
                       strict=strict,
                       resume_key=resume_key)
    def load_pretrain_weights(self):
        """Loading weights from trained MLP model & GMF model"""
        config = self.config
        mlp_model = MLP(config)
        device_id = -1
        if config['use_cuda'] is True:
            mlp_model.cuda()
            device_id = config['device_id']
        resume_checkpoint(mlp_model,
                          model_dir=config['pretrain_mlp'],
                          device_id=device_id)

        self.embedding_account_mlp.weight.data = mlp_model.embedding_account.weight.data
        self.embedding_location_mlp.weight.data = mlp_model.embedding_location.weight.data

        for idx in range(len(self.fc_layers)):
            self.fc_layers[idx].weight.data = mlp_model.fc_layers[
                idx].weight.data

        config['latent_dim'] = config['latent_dim_mf']
        gmf_model = GMF(config)
        if config['use_cuda'] is True:
            gmf_model.cuda()
        resume_checkpoint(gmf_model,
                          model_dir=config['pretrain_mf'],
                          device_id=device_id)
        self.embedding_account_mf.weight.data = gmf_model.embedding_account.weight.data
        self.embedding_location_mf.weight.data = gmf_model.embedding_location.weight.data

        self.embedding_account_mlp.require = False
        self.embedding_location_mlp.require = False
        self.embedding_account_mf.require = False
        self.embedding_location_mf.require = False
 def load(self, alias, epoch_id, hit_ratio, ndcg):
     model_dir = self.config['model_dir'].format(alias, epoch_id, hit_ratio,
                                                 ndcg)
     device_id = 0
     if self.config['use_cuda'] is True:
         device_id = self.config['device_id']
     resume_checkpoint(self.model, model_dir=model_dir, device_id=device_id)
     return self.model
Example #6
0
 def resume_model_from(self,
                       model_file_path,
                       strict=False,
                       resume_key='model'):
     resume_checkpoint(self.net,
                       model_file_path,
                       strict=strict,
                       resume_key=resume_key)
 def load_pretrain_weights(self):
     """Loading weights from trained GMF model"""
     config = self.config
     gmf_model = GMF(config)
     if config['use_cuda'] is True:
         gmf_model.cuda()
     resume_checkpoint(gmf_model, model_dir=config['pretrain_mf'], device_id=config['device_id'])
     self.embedding_user.weight.data = gmf_model.embedding_user.weight.data
     self.embedding_item.weight.data = gmf_model.embedding_item.weight.data
 def __init__(self, config):
     self.model = MLP(config)
     if config['use_cuda'] is True:
         use_cuda(True, config['device_id'])
         self.model.cuda()
     if config['pretrain']:
         #self.model.load_pretrain_weights()
         resume_checkpoint(self.model,
                           model_dir=config['pretrain_mlp'],
                           device_id=config['device_id'])
     super(MLPEngine, self).__init__(config)
     print(self.model)
Example #9
0
    def load_pretrain_weights(self):
        """Loading weights from trained MLP model & GMF model"""

        config = self.config
        config['latent_dim'] = config['latent_dim_mlp']
        mlp_model = MLP(config)

        # if config['use_cuda'] is True:
        #     mlp_model.cuda()

        # resume_checkpoint(mlp_model, model_dir = config['pretrain_mlp'], device_id = config['device_id'])
        resume_checkpoint(mlp_model, model_dir=config['pretrain_mlp'])

        # Get the user and item weights from the trained MLP model
        self.embedding_user_mlp.weight.data = mlp_model.embedding_user.weight.data
        self.embedding_item_mlp.weight.data = mlp_model.embedding_item.weight.data

        for idx in range(len(self.fc_layers)):
            self.fc_layers[idx].weight.data = mlp_model.fc_layers[
                idx].weight.data

        config['latent_dim'] = config['latent_dim_mf']
        gmf_model = GMF(config)

        # if config['use_cuda'] is True:
        #     gmf_model.cuda()

        # resume_checkpoint(gmf_model, model_dir = config['pretrain_mf'], device_id = config['device_id'])
        resume_checkpoint(gmf_model, model_dir=config['pretrain_mf'])

        # Get the user and item weights from the trained GMF model
        self.embedding_user_mf.weight.data = gmf_model.embedding_user.weight.data
        self.embedding_item_mf.weight.data = gmf_model.embedding_item.weight.data

        # Perform linear transformation to get the final weight and bias values from both MLP and GMF weights
        self.affine_output.weight.data = 0.5 * torch.cat([
            mlp_model.affine_output.weight.data,
            gmf_model.affine_output.weight.data
        ],
                                                         dim=-1)
        self.affine_output.bias.data = 0.5 * (
            mlp_model.affine_output.bias.data +
            gmf_model.affine_output.bias.data)
Example #10
0
    def load_pretrain_weights(self):
        """Loading weights from trained GMF model"""
        config = self.config
        gmf_model = GMF(config)
        if config['use_cuda'] is True:
            gmf_model.cuda()
        resume_checkpoint(gmf_model, model_dir=config['pretrain_mf'], device_id=config['device_id'])
        self.embedding_user.weight.data = gmf_model.embedding_user.weight.data
        self.embedding_item.weight.data = gmf_model.embedding_item.weight.data


# class MLPEngine(Engine):
#     """Engine for training & evaluating GMF model"""
#     def __init__(self, config):
#         self.model = MLP(config)
#         if config['use_cuda'] is True:
#             use_cuda(True, config['device_id'])
#             self.model.cuda()
#         super(MLPEngine, self).__init__(config)
#         print(self.model)

#         if config['pretrain']:
#             self.model.load_pretrain_weights()
Example #11
0
    def load_pretrain_grouping(self):
        """Loading weights from trained GMF model"""
        config = self.config
        grouping_module = Grouping(config)
        if config['use_cuda'] is True:
            grouping_module.cuda()
        resume_checkpoint(grouping_module,
                          model_dir=config['pretrain_grouping_module'],
                          device_id=config['device_id'])

        self.attention_user.weight.data[:grouping_module.
                                        num_users] = grouping_module.embedding_user.weight.data[:
                                                                                                grouping_module
                                                                                                .
                                                                                                num_users]
        self.attention_item.weight.data[:grouping_module.
                                        num_items] = grouping_module.embedding_item.weight.data[:
                                                                                                grouping_module
                                                                                                .
                                                                                                num_items]

        self.attention_fc.weight = grouping_module.fc.weight
        self.attention_fc.bias = grouping_module.fc.bias
Example #12
0
    logger.info("Selected target: {}".format(target))
    logger.info("=" * 100)
    ## ==========================
    # Initialize MDAN model
    ## ==========================
    mdan = load_model('mdan', class_number, len(sources), extractor).to(device)
    #optimizer = optim.Adadelta(mdan.parameters(), lr=learning_rate)
    optimizer = optim.SGD(mdan.parameters(), lr=learning_rate, momentum=0.9)
    scheduler = lr_scheduler.CyclicLR(optimizer,
                                      base_lr=learning_rate,
                                      max_lr=0.009)
    # Decay LR by a factor of 0.1 every 7 epochs
    #scheduler = lr_scheduler.StepLR(optimizer, step_size=25, gamma=0.1)
    resume_epoch = 0
    if constant.resume_train is True:
        resume_epoch, model_state_dict, optimizer_state_dict = resume_checkpoint(
            test_case_place, file_name='best_model.pt')
        mdan.load_state_dict(model_state_dict)
        optimizer.load_state_dict(optimizer_state_dict)
        mdan.eval()
        logger.info("Retain training from epoch {}".format(resume_epoch))
    else:
        mdan.train()

    #scheduler_plateau = lr_scheduler.ReduceLROnPlateau(optimizer, patience=3, verbose=True)
    #scheduler_warmup = GradualWarmupScheduler(optimizer, multiplier=8, total_epoch=5, after_scheduler=scheduler_plateau)

    ## ==========================
    #  MDAN Training
    ## ==========================
    max_iter = int(constant.sampling_size / batch_size) + 1
    best_acc = 0
Example #13
0
        model = model.cuda()

    # optimizer
    optimizer = optim.Adam(model.parameters(), lr=0.01)

    # loss function
    criterion = nn.CrossEntropyLoss()
    if torch.cuda.is_available():
        criterion = criterion.cuda()

    # resume model from last epoch
    last_epoch = 0
    best_acc = 0
    RESUME = True
    if RESUME:
        model, optimizer, last_epoch, best_acc = resume_checkpoint(
            model, optimizer)

    # Epoch iter
    epochs = 50
    for epoch in range(last_epoch + 1, epochs + 1):
        # train
        avg_loss, train_acc = train(model,
                                    optimizer,
                                    criterion,
                                    train_data_loader,
                                    epoch,
                                    epochs,
                                    print_freq=10)

        is_best = best_acc < train_acc.avg
        if is_best:
Example #14
0
def main():
    # Data Loader (Input Pipeline)
    print('===>Model Type:', args.model_type)
    print('loading dataset...')
    train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                               batch_size=args.batch_size, 
                                               num_workers=args.num_workers,
                                               drop_last=True,
                                               shuffle=True)
    
    test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                              batch_size=args.test_batch_size, 
                                              num_workers=args.num_workers,
                                              drop_last=True,
                                              shuffle=False)
    # Define models
    print('building model...')
    clf = CNN(input_channel=input_channel, n_outputs=num_classes).cuda()
    print(clf.parameters)

    # Optimizer
    if args.optim == 'adam':
        optimizer = torch.optim.Adam(clf.parameters(), lr=args.lr, \
                         betas=(args.beta1, args.momentum), weight_decay=args.weight_decay)
    if args.optim == 'sgd_mom':
        optimizer = torch.optim.SGD(clf.parameters(), lr=args.lr, \
                                    momentum=args.momentum, weight_decay=args.weight_decay)

    # learning rate schedule
    if args.lr_scheduler == 'linear':
        adjust_lr_linear = True
    else:
        scheduler = learning_rate_scheduler(optimizer, args)
        adjust_lr_linear = False
    print('Adjust lr linear [True/False]:', adjust_lr_linear)
    
    best_test_acc=0
    mean_pure_ratio=0
    mean_pure_ratio1=0


    if not args.resume:
        epoch=0
        train_acc=0
        
        # evaluate models with random weights
        test_acc, test_loss = evaluate(test_loader, clf, epoch)
        print('Epoch [%d/%d] Test Accuracy on the 10000 test data: %.4f %%, Pure Ratio %.4f %%' % \
                 (epoch+1, args.n_epoch, test_acc, mean_pure_ratio))
        # save results
        with open(txtfile, "a") as myfile:
            myfile.write(str(int(epoch)) + ': ' + str(train_acc) +' ' + str(test_acc) + ' ' \
                          + str(mean_pure_ratio) + "\n")
    else:
        args.start_epoch, best_test_acc, optimizer, clf = \
                               resume_checkpoint(optimizer, clf, model_dir, model_str)

    # training
    for epoch in range(args.start_epoch, args.n_epoch):
        # train models
        clf.train()

        # learning rate scheduler step
        if adjust_lr_linear:
            if args.model_type=='sigua_sl':
                if args.optim == 'adam':  
                    print('adjust learning rate adam sl')
                    adjust_learning_rate_adam_sl(optimizer, epoch)
                else:
                    adjust_learning_rate_sgd(optimizer, epoch)
            if args.model_type=='sigua_bc':
                if args.optim == 'adam':  
                    adjust_learning_rate_adam_bc(optimizer, epoch)
                else:
                    adjust_learning_rate_sgd(optimizer, epoch)
        else: 
            scheduler.step()

        print('Training %s...' % model_str)
        train_acc, pure_ratio_list = train(train_loader, epoch, clf, optimizer, args)
        # evaluate models
        print('Evaluating %s...' % model_str)
        test_acc, test_loss = evaluate(test_loader, clf, epoch)
        
        # save results
        if args.model_type=='sigua_sl':
            print('Epoch [%d/%d] Test Accuracy on the %s test data: %.4f %%, Pure Ratio %.4f %%' % \
                              (epoch+1, args.n_epoch, len(test_dataset), test_acc, mean_pure_ratio))
            mean_pure_ratio = sum(pure_ratio_list)/len(pure_ratio_list)
            with open(txtfile, "a") as myfile:
                myfile.write(str(int(epoch)) + ': ' + str(train_acc) + ' ' + str(test_acc) + ' ' \
                              + str(mean_pure_ratio) + "\n")
        else:
            print('Epoch [%d/%d] Test Accuracy on the %s test data: %.4f %%' % \
                         (epoch+1, args.n_epoch, len(test_dataset), test_acc))
            with open(txtfile, "a") as myfile:
                myfile.write(str(int(epoch)) + ': ' + str(train_acc) +' ' + str(test_acc) + "\n")

        # remember best prec@1 and save checkpoint
        is_best=test_acc > best_test_acc
        best_test_acc=max(test_acc, best_test_acc)
        if args.save_model:
            save_checkpoint({
            'epoch': epoch+1,
            'state_dict': clf.state_dict(),
            'best_prec1': best_test_acc,
            'optimizer': optimizer.state_dict(),
            }, is_best, model_dir, model_str)

    print('Predicting %s...' % model_str)
    preds, true=predict(test_loader, clf)
    for p, t in zip(preds, true):
        with open(txtfile.replace('.txt', '_pred.txt'), "a") as myfile:
            myfile.write(str(p) +' ' + str(t) + "\n")