Exemplo n.º 1
0
def pre_train(pre_train_path):
    model = train.pre_train()
    loss, auc = train.test(model)
    with open('logs/log.txt', 'a') as f:
        f.write('# {}\n'.format(conf.model_type))
        f.write('base_loss={};\n'.format(loss))
        f.write('base_auc={};\n'.format(auc))
    torch.save(model.state_dict(), pre_train_path)
Exemplo n.º 2
0
def me_train(model, me_path):
    me_model = train.meta_train(model)
    torch.save(me_model.state_dict(), me_path)
    loss, auc = train.test(me_model, warm=False)
    me_warm = train.meta_test(me_path, model_name='me', with_normal=True)
    with open('logs/log.txt', 'a') as f:
        f.write('me_loss={};\n'.format(loss))
        f.write('me_auc={};\n'.format(auc))
        f.write('me_warm={};\n'.format(me_warm))
Exemplo n.º 3
0
def best_auc_func(params):
    model = train.new_model(pre_train_path)
    train_ctw.meta_train(model, params)
    loss, auc = train.test(model, warm=False)
    global best_auc
    global best_params
    if auc > best_auc:
        torch.save(model.state_dict(), ours_path)
        best_auc = auc
        best_params = params
        print('best:{}'.format(best_auc))
    return {'loss': -auc, 'status': STATUS_OK}
Exemplo n.º 4
0
def save_tables():
    for model_type in ['FM', 'DNN', 'WD', 'DeepFM']:
        # for model_type in ['DNN', 'WD', 'DeepFM']:
        conf.model_type = model_type

        pre_train_path1 = "models/pre-trained-{}-{}.pth".format(
            model_type, conf.cvr_flag)
        me_path1 = "models/me-{}-{}.pth".format(model_type, conf.cvr_flag)
        ours_path1 = "models/ours-{}-{}.pth".format(model_type, conf.cvr_flag)

        conf.table_path = 'logs/table_base_cold_{}.csv'.format(model_type)
        train.test(pre_train_path1, warm=False, save_table=True)

        conf.table_path = 'logs/table_me_cold_{}.csv'.format(model_type)
        train.test(me_path1, warm=False, save_table=True)

        conf.table_path = 'logs/table_ours_cold_{}.csv'.format(model_type)
        train.test(ours_path1, warm=False, save_table=True)

        conf.table_path = 'logs/table_base_warm_{}.csv'.format(model_type)
        train.meta_test(pre_train_path1,
                        with_normal=False,
                        save_table=True,
                        base=True)

        conf.table_path = 'logs/table_me_warm_{}.csv'.format(model_type)
        train.meta_test(me_path1, with_normal=False, save_table=True)

        conf.table_path = 'logs/table_ours_warm_{}.csv'.format(model_type)
        train.meta_test(ours_path1, with_normal=False, save_table=True)

        print('{}-------------saved!!!!!!!!!!!!!!!!!!!!!'.format(model_type))
Exemplo n.º 5
0
def ours_train(ours_path):
    params = models[conf.model_type]
    fmin(best_auc_func, params, algo=tpe.suggest, max_evals=max_evals, show_progressbar=False)
    loss, auc = train.test(ours_path, warm=False)
    ours_warm = train.meta_test(ours_path, model_name='ours', with_normal=False)
    with open('logs/log.txt', 'a') as f:
        f.write('ours_loss={};\n'.format(loss))
        f.write('ours_auc={};\n'.format(auc))
        f.write('ours_warm={};\n'.format(ours_warm))
    global best_params
    with open('logs/best.txt', 'a') as f:
        f.write('# {}\n'.format(conf.model_type))
        f.write('best_{}={};\n'.format(conf.model_type, best_params))
Exemplo n.º 6
0
def hyper_param_analysis(rhos, alphas, path):

    ps = {
        'alpha': None,
        'amsgrad': False,
        'batch_n_ID': 50,
        'gamma': 1.0,
        'lr': 0.01,
        'p_lr': 0.0001,
        'p_lr_decay': 1.0,
        'rho': None,
        'weight_decay': 1e-8
    }

    # if os.path.exists(path):
    #     os.remove(path)

    with open(path, 'a') as f:
        f.write('# {}\n'.format(conf.model_type))
        for rho in rhos:
            for alpha in alphas:
                ps['rho'] = rho
                ps['alpha'] = alpha
                # path
                cold_path = "models/mer-trained-rho({})-alpha({}).pth".format(
                    rho, alpha)
                warm_path = 'models/mer-tested-rho({})-alpha({}).pth'.format(
                    rho, alpha)
                # cold
                model = train.new_model(pre_train_path)
                model = train_ctw.meta_train(model, ps, const.train_n_epoch)
                torch.save(model.state_dict(), cold_path)
                cold_loss, cold_auc = train.test(cold_path, warm=False)
                # warm
                ours_warm = train.meta_test(cold_path, 'ours', False)
                warm_losses, warm_aucs = zip(*ours_warm['ours'])
                losses = ',' + ','.join(map(str, warm_losses))
                aucs = ',' + ','.join(map(str, warm_aucs))
                torch.save(model.state_dict(), warm_path)

                msg = ','.join(
                    [str(rho),
                     str(alpha),
                     str(cold_loss),
                     str(cold_auc)]) + losses + aucs + '\n'
                print(msg)
                f.write(msg)