Example #1
0
def invoke_model(parameters):
    model_params = load_parameters()
    model_name = model_params["MODEL_TYPE"]
    for parameter in parameters.keys():
        model_params[parameter] = parameters[parameter][0]
        logger.debug("Assigning to %s the value %s" % (str(parameter), parameters[parameter][0]))
        model_name += '_' + str(parameter) + '_' + str(parameters[parameter][0])
    model_params["SKIP_VECTORS_HIDDEN_SIZE"] = model_params["TARGET_TEXT_EMBEDDING_SIZE"]
    model_params["MODEL_NAME"] = model_name
    # models and evaluation results will be stored here
    model_params[
        "STORE_PATH"] = '/home/lvapeab/smt/software/egocentric-video-description/meta-optimizers/spearmint/trained_models/' + \
                        model_params["MODEL_NAME"] + '/'
    check_params(model_params)
    assert model_params['MODE'] == 'training', 'You can only launch Spearmint when training!'
    logging.info('Running training.')
    train_model(model_params)

    results_path = model_params['STORE_PATH'] + '/' + model_params['EVAL_ON_SETS'][0] + '.' + model_params['METRICS'][0]

    # Recover the highest metric score
    metric_pos_cmd = "head -n 1 " + results_path + \
                     " |awk -v metric=" + metric_name + \
                     " 'BEGIN{FS=\",\"}" \
                     "{for (i=1; i<=NF; i++) if ($i == metric) print i;}'"
    metric_pos = \
    subprocess.Popen(metric_pos_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True).communicate()[0][:-1]
    cmd = "tail -n +2 " + results_path + \
          " |awk -v m_pos=" + str(metric_pos) + \
          " 'BEGIN{FS=\",\"}{print $m_pos}'|sort -gr|head -n 1"
    ps = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, env=d)
    metric_value = float(ps.communicate()[0])
    print "Best %s: %f" % (metric_name, metric_value)

    return 1. - metric_value if maximize else metric_value  # Spearmint minimizes a function
Example #2
0
def test_NMT_Unidir_GRU_ConditionalGRU():
    params = load_tests_params()

    # Current test params: Single layered GRU - ConditionalGRU
    params['BIDIRECTIONAL_ENCODER'] = False
    params['N_LAYERS_ENCODER'] = 1
    params['BIDIRECTIONAL_DEEP_ENCODER'] = False
    params['ENCODER_RNN_TYPE'] = 'GRU'
    params['DECODER_RNN_TYPE'] = 'ConditionalGRU'
    params['N_LAYERS_DECODER'] = 1

    params['REBUILD_DATASET'] = True
    dataset = build_dataset(params)
    params['INPUT_VOCABULARY_SIZE'] = dataset.vocabulary_len[params['INPUTS_IDS_DATASET'][0]]
    params['OUTPUT_VOCABULARY_SIZE'] = dataset.vocabulary_len[params['OUTPUTS_IDS_DATASET'][0]]
    params['MODEL_NAME'] = \
        params['TASK_NAME'] + '_' + params['SRC_LAN'] + params['TRG_LAN'] + '_' + params['MODEL_TYPE'] + \
        '_src_emb_' + str(params['SOURCE_TEXT_EMBEDDING_SIZE']) + \
        '_bidir_' + str(params['BIDIRECTIONAL_ENCODER']) + \
        '_enc_' + params['ENCODER_RNN_TYPE'] + '_*' + str(params['N_LAYERS_ENCODER']) + '_' + str(
            params['ENCODER_HIDDEN_SIZE']) + \
        '_dec_' + params['DECODER_RNN_TYPE'] + '_*' + str(params['N_LAYERS_DECODER']) + '_' + str(
            params['DECODER_HIDDEN_SIZE']) + \
        '_deepout_' + '_'.join([layer[0] for layer in params['DEEP_OUTPUT_LAYERS']]) + \
        '_trg_emb_' + str(params['TARGET_TEXT_EMBEDDING_SIZE']) + \
        '_' + params['OPTIMIZER'] + '_' + str(params['LR'])
    params['STORE_PATH'] = K.backend() + '_test_train_models/' + params['MODEL_NAME'] + '/'

    # Test several NMT-Keras utilities: train, sample, sample_ensemble, score_corpus...
    print ("Training model")
    train_model(params)
    params['RELOAD'] = 1
    print ("Done")
    print ("Applying model")
    apply_NMT_model(params)
    print ("Done")

    parser = argparse.ArgumentParser('Parser for unit testing')
    parser.dataset = params['DATASET_STORE_PATH'] + '/Dataset_' + params['DATASET_NAME'] + '_' + params['SRC_LAN'] + params['TRG_LAN'] + '.pkl'

    parser.text = params['DATA_ROOT_PATH'] + '/' + params['TEXT_FILES']['val'] + params['SRC_LAN']
    parser.splits = ['val']
    parser.config = params['STORE_PATH'] + '/config.pkl'
    parser.models = [params['STORE_PATH'] + '/epoch_' + str(2)]
    parser.verbose = 0
    parser.dest = None
    parser.source = params['DATA_ROOT_PATH'] + '/' + params['TEXT_FILES']['val'] + params['SRC_LAN']
    parser.target = params['DATA_ROOT_PATH'] + '/' + params['TEXT_FILES']['val'] + params['TRG_LAN']
    parser.weights = []

    for n_best in [True, False]:
        parser.n_best = n_best
        print ("Sampling with n_best = %s "% str(n_best))
        sample_ensemble(parser, params)
        print ("Done")

    print ("Scoring corpus")
    score_corpus(parser, params)
    print ("Done")
def test_transformer():
    params = load_tests_params()

    # Current test params: Transformer
    params['N_LAYERS_ENCODER'] = 2
    params['N_LAYERS_DECODER'] = 2
    params['MULTIHEAD_ATTENTION_ACTIVATION'] = 'relu'
    params['MODEL_SIZE'] = 32
    params['FF_SIZE'] = MODEL_SIZE * 4
    params['N_HEADS'] = 2

    params['REBUILD_DATASET'] = True
    dataset = build_dataset(params)
    params['INPUT_VOCABULARY_SIZE'] = dataset.vocabulary_len[
        params['INPUTS_IDS_DATASET'][0]]
    params['OUTPUT_VOCABULARY_SIZE'] = dataset.vocabulary_len[
        params['OUTPUTS_IDS_DATASET'][0]]

    params['MODEL_NAME'] =\
        params['TASK_NAME'] + '_' + params['SRC_LAN'] + params['TRG_LAN'] + '_' + params['MODEL_TYPE'] + \
        '_model_size_' + str(params['MODEL_SIZE']) + \
        '_ff_size_' + str(params['FF_SIZE']) + \
        '_num_heads_' + str(params['N_HEADS']) + \
        '_encoder_blocks_' + str(params['N_LAYERS_ENCODER']) + \
        '_decoder_blocks_' + str(params['N_LAYERS_DECODER']) + \
        '_deepout_' + '_'.join([layer[0] for layer in params['DEEP_OUTPUT_LAYERS']]) + \
        '_' + params['OPTIMIZER'] + '_' + str(params['LR'])

    params['STORE_PATH'] = K.backend(
    ) + '_test_train_models/' + params['MODEL_NAME'] + '/'

    # Test several NMT-Keras utilities: train, sample, sample_ensemble, score_corpus...
    train_model(params)
    params['RELOAD'] = 2
    apply_NMT_model(params)
    parser = argparse.ArgumentParser('Parser for unit testing')
    parser.dataset = params['DATASET_STORE_PATH'] + '/Dataset_' + params[
        'DATASET_NAME'] + '_' + params['SRC_LAN'] + params['TRG_LAN'] + '.pkl'

    parser.text = params['DATA_ROOT_PATH'] + '/' + params['TEXT_FILES'][
        'val'] + params['SRC_LAN']
    parser.splits = ['val']
    parser.config = params['STORE_PATH'] + '/config.pkl'
    parser.models = [params['STORE_PATH'] + '/epoch_' + str(2)]
    parser.verbose = 0
    parser.dest = None
    parser.source = params['DATA_ROOT_PATH'] + '/' + params['TEXT_FILES'][
        'val'] + params['SRC_LAN']
    parser.target = params['DATA_ROOT_PATH'] + '/' + params['TEXT_FILES'][
        'val'] + params['TRG_LAN']
    parser.weights = []

    for n_best in [True, False]:
        parser.n_best = n_best
        sample_ensemble(parser, params)
    score_corpus(parser, params)
Example #4
0
def invoke_model(parameters):
    """
    Loads a model, trains it and evaluates it.
    :param parameters: Model parameters
    :return: Metric to minimize value.
    """

    model_params = load_parameters()
    model_name = model_params["MODEL_TYPE"]
    for parameter in list(parameters):
        model_params[parameter] = parameters[parameter][0]
        logger.debug("Assigning to %s the value %s" %
                     (str(parameter), parameters[parameter][0]))
        model_name += '_' + str(parameter) + '_' + str(
            parameters[parameter][0])
    model_params["MODEL_NAME"] = model_name
    # models and evaluation results will be stored here
    model_params["STORE_PATH"] = os.path.join('trained_models',
                                              model_params["MODEL_NAME"])
    check_params(model_params)
    assert model_params[
        'MODE'] == 'training', 'You can only launch Spearmint when training!'
    logger.info('Running training.')
    train_model(model_params)

    results_path = os.path.join(
        model_params['STORE_PATH'],
        model_params['EVAL_ON_SETS'][0] + '.' + model_params['METRICS'][0])

    # Recover the highest metric score
    metric_pos_cmd = "head -n 1 " + results_path + \
                     " |awk -v metric=" + metric_name + \
                     " 'BEGIN{FS=\",\"}" \
                     "{for (i=1; i<=NF; i++) if ($i == metric) print i;}'"
    metric_pos = subprocess.Popen(metric_pos_cmd,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.STDOUT,
                                  shell=True).communicate()[0][:-1]
    cmd = "tail -n +2 " + results_path + \
          " |awk -v m_pos=" + str(metric_pos) + \
          " 'BEGIN{FS=\",\"}{print $m_pos}'|sort -gr|head -n 1"
    ps = subprocess.Popen(cmd,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.STDOUT,
                          shell=True,
                          env=d)
    metric_value = float(ps.communicate()[0])
    print("Best %s: %f" % (metric_name, metric_value))

    return 1. - metric_value if maximize else metric_value  # Spearmint minimizes a function
Example #5
0
def main(arg):
    dataset_param = arg2dataset_param(arg[0])
    model_param = arg2model_param(arg[1])
    resume_flag = len(arg) > 2

    dataset_param.size = 100000
    dataset_param.min_num_node = 4
    dataset_param.num_num_node = 30
    general_param = get_general_param()
    general_param.save_freq = int(general_param.save_freq*10000/dataset_param.size)
    general_param.epoch_num = int(general_param.epoch_num*10000*2/dataset_param.size)
    general_param.resume_flag = resume_flag

    train_model(dataset_param, model_param, general_param)
Example #6
0
def traffic_model_func(data_type, img_size, img_depth, forecasting_horizon,
                       seq_len, model_type, batch_size, max_epoch, optimizer,
                       opt_learningrate, batchnorm_on, dropout_on, loss_func,
                       earlystop_on, n_conv_layers, conv1_depth, conv2_depth,
                       conv3_depth, conv4_depth, conv5_depth, conv_fc_units,
                       act_conv, pooling_on, pooling_size, conv_filter_size,
                       n_lstm_layers, lstm_units, act_lstm, lstm_fc_units,
                       n_fc_layers, last_fc_units, temp_type, traj_opt,
                       normalization_opt):

    hyperparams = pack_args_to_dict(
        data_type, img_size, img_depth, forecasting_horizon, seq_len,
        model_type, batch_size, max_epoch, optimizer, opt_learningrate,
        batchnorm_on, dropout_on, loss_func, earlystop_on, n_conv_layers,
        conv1_depth, conv2_depth, conv3_depth, conv4_depth, conv5_depth,
        conv_fc_units, act_conv, pooling_on, pooling_size, conv_filter_size,
        n_lstm_layers, lstm_units, act_lstm, lstm_fc_units, n_fc_layers,
        last_fc_units, temp_type, traj_opt, normalization_opt)

    train_model('test_datasize', 1, 0, hyperparams)
def train():
    try:
        if request.method == 'POST':
            result = train_model()
            print('result', result)
            return result
        else:
            return False

    except Exception as e:
        print(e)
        return e
Example #8
0
def train():
    if request.method == 'POST':
        algorithm = request.form['algorithm']
        table_dict = get_table_dict()
        saved_model_filename = "model/my_model.sav"
        table_dict['saved_model_filename'] = saved_model_filename
        save_table_dict(table_dict)

        score = train_model(table_dict['dataset_file'], table_dict['params'], saved_model_filename, algorithm,
                            table_dict['task_type'])
        avg_score = sum(score) / len(score)

        return render_template('./score.html', score=avg_score, task_type=table_dict['task_type'])
Example #9
0
 def test_reinforce_correct(self):
     records = [
         {
             'state':[0,0,0,0],
             'new_state':[0,0,0,0],
             'reward': 0,
             'action': 1,
             'done': False,
         },
     ]*5 + [
         {
             'state':[0,0,0,0],
             'new_state':[0,0,0,1],
             'reward': 1,
             'action': 0,
             'done': False,
         },
     ]* 5
     model = main.build_model(env)
     main.train_model( model, records, env, batch_size=64)
     prediction = main.predict(model,[0,0,0,0])
     assert np.argmax(prediction) == 0, prediction
Example #10
0
 def test_predict_future_reward(self):
     """When predicting future rewards, we want to see the network give correct directions"""
     good_sequence = [
         ([0,0,0,0],1,[0,0,0,1]),
         ([0,0,0,1],0,[1,0,1,0]),
         ([1,0,1,0],1,[1,1,1,1]),
     ]
     bad_sequence = [
         ([0,0,0,0],0,[1,0,0,1]),
         ([1,0,0,1],1,[0,0,1,0]),
         ([0,0,1,0],1,[0,1,1,1]),
     ]
     def expand(r, final_reward):
         results = []
         for i,(state,action,new_state) in enumerate(r):
             record = {
                 'state': np.array(state,'f'),
                 'new_state': np.array(new_state,'f'),
                 'action': action,
                 'done': i >= len(r),
                 'reward': final_reward
             }
             results.append(record)
         assert results[-1]['reward'] == final_reward
         return results 
     records = expand(good_sequence,1.0) + expand(bad_sequence,-1.0)
     print(records)
     records = records * 256
     model = main.build_model(env)
     main.train_model( model, records, env, batch_size=8)
     for (state,action,new_state) in good_sequence:
         prediction = main.predict(model,state)
         assert np.argmax(prediction) == action, (state,action,prediction)
     
     for (state,action,new_state) in bad_sequence:
         prediction = main.predict(model,state)
         assert np.argmax(prediction) != action, (state,action,prediction)
def main(args):
    # Learning rate is 0.01 for MNIST and 0.001 for CIFAR10
    train_args = {
        'stopcond': 0,
        'dataset': 'CIFAR10',
        'learningrate': 0.001,
        'nunits': 1024,
        'batchsize': 64,
        'momentum': 0.9,
        'no_cuda': False,
        'weightdecay': args["weightdecay"],
        'init_reg_strength': args["init_reg_strength"],
        'square_loss': args['square_loss'],
        'epochs': args["epochs"],
        'datadir': '/hdd0/datasets'
    }

    # vc, l1max, fro, spec_l1, spec_fro, our
    bounds_vals = [[], [], [], [], [], []]
    total_activations = []
    hidden_units = np.array([pow(2, n) for n in range(6, 16)])

    for n in range(6, 16):
        train_args["nunits"] = pow(2, n)

        measure, activations = train_model(train_args)
        bounds = list(measure.items())[-6:]

        for i, a in enumerate(activations):
            activations[i] = a.cpu().numpy()

        total_activations.append([a for a in activations])

        with open(args["name"] + ".txt", "a+") as f:
            f.write("Hidden units: 2^" + str(n) + "\n")
            for key, value in measure.items():
                val = float(value)
                f.write(key + ':\t %.3g' % val)
                f.write("\n")
            f.write("\n")

        for i, bound in enumerate(bounds):
            bounds_vals[i].append(float(bound[1]))

    print(avg_zero_activations)
    plt.plot(hidden_units,
             np.array(bounds_vals[0]),
             marker="+",
             label="(1) VC-dim",
             color="blue")
    plt.plot(hidden_units,
             np.array(bounds_vals[1]),
             marker="+",
             label="(2) l1,max",
             color="orange")
    plt.plot(hidden_units,
             np.array(bounds_vals[2]),
             marker="+",
             label="(3) Fro",
             color="green")
    plt.plot(hidden_units,
             np.array(bounds_vals[3]),
             marker="+",
             label="(4) spec-l2,1",
             color="black")
    plt.plot(hidden_units,
             np.array(bounds_vals[4]),
             marker="+",
             label="(5) spec-Fro",
             color="brown")
    plt.plot(hidden_units,
             np.array(bounds_vals[5]),
             marker="+",
             label="(6) ours",
             color="red")
    plt.xlabel("# Hidden Units")
    plt.ylabel("Capacity")
    plt.xscale("log", basex=2)
    plt.yscale("log")
    plt.xticks([pow(2, n) for n in range(6, 16, 3)])
    plt.title("CIFAR10 - Epochs: " + str(args["epochs"]) + " WD: " +
              str(args["weightdecay"]) + " Loss: MSE")
    plt.legend()
import argparse
import main

parser = argparse.ArgumentParser()
parser.add_argument("data_directory",
                    help="add a data directory",
                    default="flowers")
parser.add_argument("--arch", default="vgg19", type=str)
parser.add_argument("--learning_rate", default=0.001)
parser.add_argument("--hidden_units", default=2048)
parser.add_argument("--epochs", default=8, type=int)
parser.add_argument("--save_dir", default="checkpoint.pth")
args = parser.parse_args()

data_dir = args.data_directory
arch = args.arch
learning_rate = args.learning_rate
hidden_units = args.hidden_units
epochs = args.epochs
save_dir = args.save_dir

trainloader, validloader, testloader, train_data = main.load_data(data_dir)
model, criterion, optimizer = main.model_setup(arch, learning_rate,
                                               hidden_units)

main.train_model(model, criterion, optimizer, epochs, trainloader, validloader)

main.saving_checkpoint(model, save_dir, train_data, hidden_units, optimizer)

print("The model is trained")
Example #13
0
import main

#define input

args = main.process_inputs()

# load / transform data

dataloader, class_to_idx = sub.load_data(args.data_dir)

#build model

model = main.build_model(args.arch, args.hidden_units)

# set device = cpu/gpu

device = sub.set_device(args.gpu)

# train/validate model

criterion = nn.NLLLoss()
optimizer = optim.SGD(model.classifier.parameters(), lr=args.learnrate)
main.train_model(model, dataloader, args.epochs, args.learnrate, optimizer,
                 criterion, device)

# return final statistics (after printing during process)
# save

main.save_checkpoint(args.save_path, model, class_to_idx, args, optimizer,
                     criterion)
    data_dir = results.data_dir
    save_dir = results.save_dir
    droppercentage = results.dropout
    arch = results.arch
    LR = results.learning_rate
    hidden_units = results.hidden_units
    epochs = results.epochs
    gpu = results.gpu

    train_set, valid_set, test_set, train_loader, valid_loader, test_loader = load_data(
        data_dir)

    pretrained_model = results.arch
    pretrained_model = getattr(models, pretrained_model)(pretrained=True)

    input_layer = pretrained_model.classifier[0].in_features

    model, hls = build_model(arch, gpu, input_layer, droppercentage)

    model, criterion, optimizer = train_model(model, train_set, valid_set,
                                              train_loader, valid_loader,
                                              epochs, LR, gpu)

    accuracy = accuracy_test(model, test_loader)

    print("Accuracy of your trained model: {:.2f}%".format(accuracy * 100))

    save_checkpoint(model, hls, epochs, droppercentage, train_set, optimizer,
                    criterion, save_dir)
    print('Model has been saved!')
Example #15
0
val_losses = []
val_recalls = []
test_losses = []
test_recalls = []
f1_max = 0.0
loss_min = 10000

log_dir = 'seed_{}'.format(seed)
# if not os.path.exists(log_dir):
#     os.makedirs(log_dir)
########## train #################
writer = SummaryWriter(log_dir='runs/' + log_dir, comment=args.model_name)

for ep in range(epoch):
    avg_train_loss, avg_train_recall, avg_train_prec, avg_train_f1 = train_model(
        rec_sys_model, exec_device, data_type, config_param['batch_size'],
        loss_func, optimizer, A, train_loader, ep, top_k, train_display_step)
    # train_losses.append(avg_train_loss)
    # train_recalls.append(avg_train_recall)

    writer.add_scalar("Loss/train", avg_train_loss, ep)
    writer.add_scalar("Recall/train", avg_train_recall, ep)
    writer.add_scalar("Precision/train", avg_train_prec, ep)
    writer.add_scalar("F1/train", avg_train_f1, ep)

    avg_val_loss, avg_val_recall, avg_val_prec, avg_val_f1 = validate_model(
        rec_sys_model, exec_device, data_type, config_param['batch_size'],
        loss_func, A, valid_loader, ep, top_k, val_display_step)
    writer.add_scalar("Loss/val", avg_val_loss, ep)
    writer.add_scalar("Recall/val", avg_val_recall, ep)
    writer.add_scalar("Precision/val", avg_val_prec, ep)
Example #16
0
def train():
    categories, documents = load_corpus('corpus.txt')
    classifier, dictionary = train_model(documents, categories)
    joblib.dump((classifier, dictionary), 'model.pkl', compress=9)
    return "trained"