Beispiel #1
0
def main():
    train_x, train_y, val_x, val_y, test_x, test_y = load_data()

    train_x, train_y = preprocess_data(train_x, train_y)
    val_x, val_y = preprocess_data(val_x, val_y)
    test_x, test_y = preprocess_data(test_x, test_y)

    train_a_network(train_x, train_y, val_x, val_y, test_x, test_y)
Beispiel #2
0
def end2end(mcat_ratio: float, epochs: int, dim: int, nlayers: int, fname: str,
            gpu: bool, name: str, lr: float, batch_size: int,
            split_ratio: float, vectors: str, data_dir: str, pbar_width: int,
            wandb: bool, boom_dim: int, dropout: float, ema: float,
            overwrite_model: bool) -> str:
    ''' Loads the data, preprocesses it if needed, builds the SHARNN model,
        trains it and evaluates it. '''
    from data import load_data
    from modules import SHARNN

    pp_path = get_path(data_dir) / f'{fname}_pp.tsv'
    if not pp_path.is_file():
        from data import preprocess_data
        raw_path = get_path(data_dir) / f'{fname}.tsv'
        cats_path = get_path(data_dir) / 'cats.json'
        mcat_dict_path = get_path(data_dir) / 'mcat_dict.json'

        if not (raw_path.is_file() and cats_path.is_file()
                and mcat_dict_path.is_file()):
            from db import ArXivDatabase
            db = ArXivDatabase(data_dir=data_dir)
            db.get_mcat_dict()
            db.get_cats()
            if not raw_path.is_file():
                db.get_training_df()

        preprocess_data(data_dir=data_dir)

    train_dl, val_dl, vocab = load_data(tsv_fname=f'{fname}_pp',
                                        batch_size=batch_size,
                                        split_ratio=split_ratio,
                                        vectors=vectors,
                                        data_dir=data_dir)

    model = SHARNN(dim=dim,
                   nlayers=nlayers,
                   data_dir=data_dir,
                   pbar_width=pbar_width,
                   vocab=vocab,
                   boom_dim=boom_dim,
                   dropout=dropout)
    if gpu: model.cuda()

    model = model.fit(train_dl,
                      val_dl,
                      epochs=epochs,
                      lr=lr,
                      mcat_ratio=mcat_ratio,
                      name=name,
                      use_wandb=wandb,
                      ema=ema,
                      overwrite_model=overwrite_model)

    return model.evaluate(val_dl)
Beispiel #3
0
def run():
    model = GRU(FLAGS.input_dim, FLAGS.hidden_dim)
    print(model)
    optimizer = optim.SGD(model.parameters(), lr=0.001)

    data_paths = [FLAGS.data_path, FLAGS.eval_data_path]

    datasets, embeddings = preprocess_data(data_paths, FLAGS.embedding_path)

    training_data = Dataset(datasets[0])
    eval_data = Dataset(datasets[1], shuffle=False, truncate_final_batch=True)

    if FLAGS.extract:
        raise NotImplementedError

    def eval_fn():
        return evaluate(model, eval_data, embeddings)

    def ckpt_fn(ckpt_args):
        step = ckpt_args['step']
        best_dev_error = ckpt_args['best_dev_error']
        best = ckpt_args.get('best', False)
        save_dict = pack_checkpoint(step, best_dev_error, model, optimizer)
        filename = ckpt_path(FLAGS.save_path, filename=FLAGS.experiment_name, best=best)

        print("Checkpointing...")
        save(save_dict, filename)

    train_args = dict(step=0, best_dev_error=1.0)

    train(model, optimizer, training_data, embeddings, eval_fn, ckpt_fn, train_args)
Beispiel #4
0
def main():
    print('getting data...')
    df = get_data()
    X_train, X_test, y_train, y_test, enc = preprocess_data(df)

    print('training...')
    model = Net()
    target = to_tensor(y_train)
    loss_fn = torch.nn.MSELoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
    for i in range(100):
        y_pred = model(to_tensor(X_train))
        loss = loss_fn(y_pred, target)
        print(f'loss on pass {i}: {loss}')
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    print('trained!')
    y_pred = enc.inverse_transform(model.predict(to_tensor(X_test)))
    # print('y_test')
    y_test_deenc = enc.inverse_transform((y_test))
    # print(y_test_deenc)
    # print('y_pred')
    # print((y_pred))
    print(f'accuracy score: {balanced_accuracy_score(y_test_deenc,y_pred)}')
    return X_train, X_test, y_train, y_test, model
Beispiel #5
0
def _init():
    # Read data and shuffle it
    dataframe = pandas.read_csv('data/log.csv', sep=',', header=0)
    dataframe = dataframe.reindex(numpy.random.permutation(dataframe.index))

    # Generate features and targets
    processed_features, processed_targets = preprocess_data(dataframe)

    print('==========Feature Summary==========')
    print(processed_features.describe())
    print()
    print('==========Target Summary===========')
    print(processed_targets.describe())
    print()

    # ratio = training data / all data
    training_data_ratio = 0.8

    # Separate data to training and validation sets
    training_data_count = math.ceil(training_data_ratio * len(dataframe))
    training_examples = processed_features.head(training_data_count)
    training_targets = processed_targets.head(training_data_count)

    validation_data_count = len(dataframe) - training_data_count
    validation_examples = processed_features.tail(validation_data_count)
    validation_targets = processed_targets.tail(validation_data_count)

    # Train neural network
    model = NeuralNetworkModel(hidden_units=[10, 10])
    model.train(
        learning_rate=0.005,
        steps=750,
        batch_size=training_data_count,
        training_examples=training_examples,
        training_targets=training_targets,
        validation_examples=validation_examples,
        validation_targets=validation_targets
    )

    # Predict unlabeled data and display result
    unlabeled_dataframe = pandas.read_csv('data/log_unlabeled.csv', sep=',', header=0)
    unlabeled_features, unlabeled_targets = preprocess_data(unlabeled_dataframe)
    unlabeled_predictions = model.predict(unlabeled_features, unlabeled_targets)

    result_table = restore_category(unlabeled_features).assign(prediction=unlabeled_predictions)
    print('\n==========Unlabeled Data Predictions==========')
    print(result_table.to_string())
Beispiel #6
0
def extract_data(size=256):
    print("Extracting data..")
    X, y = data.extract_data(size=256)

    print("Preprocessing data..")
    X, y, nb_samples, num_categories = data.preprocess_data(X, y, save=True, subtract_mean=True)

    return X, y, nb_samples, num_categories
def extract_data(size=256):
    print("Extracting data..")
    X, y = data.extract_data(size=256)

    print("Preprocessing data..")
    X, y, nb_samples, num_categories = data.preprocess_data(X, y, save=True, subtract_mean=True)

    return X, y, nb_samples, num_categories
Beispiel #8
0
    def build_validation_data_loader(self) -> keras.InputData:
        if not self.data_downloaded:
            self.download_directory = download_cifar10_tf_sequence(
                download_directory=self.download_directory,
                url=self.context.get_data_config()["url"],
            )
            self.data_downloaded = True

        (_, _), (test_data, test_labels) = get_data(self.download_directory)

        return preprocess_data(test_data), preprocess_labels(test_labels)
Beispiel #9
0
def main():
    # Use the loading function from Assignment 1
    train_x, train_y, val_x, val_y, test_x, test_y = load_data(use_all=True,
                                                               val_size=5000)

    # Use the preprocessing function from Assignment 1
    train_x, train_y = preprocess_data(train_x, train_y)
    val_x, val_y = preprocess_data(val_x, val_y)
    test_x, test_y = preprocess_data(test_x, test_y)

    # Process the data so they have a zero mean
    mean, std = np.mean(train_x), np.std(
        train_x)  # Find mean and std of training data
    train_x = process_zero_mean(train_x, mean, std)
    val_x = process_zero_mean(val_x, mean, std)
    test_x = process_zero_mean(test_x, mean, std)

    # Testing gradients
    # test_grad_computations(train_x, train_y)

    # Testing model capabilities
    # overfit_test(train_x, train_y)

    # Test different model parameters
    # train_a_network(train_x, train_y, val_x, val_y, test_x, test_y)

    # Find a good regularisation value
    # lambda_search(train_x, train_y, val_x, val_y)

    # Train a network with optimal hyper-parameter values using almost all of the data
    #  use_all=True, val_size=1000 | cycles = 5 | n_s = 1000 | lambda = 0.00087
    # train_a_network(train_x, train_y, val_x, val_y, test_x, test_y, n_cycles=5)

    # Report the effect of adding noise to the training data
    # test_noise(train_x, train_y, val_x, val_y, test_x, test_y)

    # Report the effect of different number of nodes
    # test_nodes(train_x, train_y, val_x, val_y, test_x, test_y)

    # Report the effect of ensemble method
    test_ensemble(train_x, train_y, val_x, val_y, test_x, test_y)
Beispiel #10
0
def main():
    # Use the loading function from Assignment 1
    train_x, train_y, val_x, val_y, test_x, test_y = load_data(use_all=True,
                                                               val_size=5000)

    # Use the preprocessing function from Assignment 1
    train_x, train_y = preprocess_data(train_x, train_y)
    val_x, val_y = preprocess_data(val_x, val_y)
    test_x, test_y = preprocess_data(test_x, test_y)

    # Process the data so they have a zero mean
    mean, std = np.mean(train_x), np.std(
        train_x)  # Find mean and std of training data
    train_x = process_zero_mean(train_x, mean, std)
    val_x = process_zero_mean(val_x, mean, std)
    test_x = process_zero_mean(test_x, mean, std)

    # Testing gradients
    # test_grad_computations(train_x, train_y)

    # Training simple k-layer networks
    train_simple(train_x, train_y, val_x, val_y, test_x, test_y)
    # os.chdir(wpath)
    # print(os.getcwd())

    with open("train2012_MAX.txt", 'r', encoding="utf8", errors="ignore"
              ) as f:  # encoding='cp1252', encoding='utf8', errors="replace"
        data_train = f.readlines()

    # data_train = load_file('data/test2011')
    data_valid = load_file('data/dev2012')
    data_test = load_file('data/test2011')

    tokenizer = BertTokenizer.from_pretrained('bert-base-uncased',
                                              do_lower_case=True)

    print('PREPROCESSING DATA...')
    X_train, y_train = preprocess_data(data_train, tokenizer, punctuation_enc,
                                       segment_size)
    X_valid, y_valid = preprocess_data(data_valid, tokenizer, punctuation_enc,
                                       segment_size)

    print('INITIALIZING MODEL...')
    output_size = len(punctuation_enc)
    bert_punc = nn.DataParallel(
        BertPunc(segment_size, output_size, dropout).cuda())

    # print('TRAINING TOP LAYER...')
    # data_loader_train = create_data_loader(X_train, y_train, True, batch_size_top)
    # data_loader_valid = create_data_loader(X_valid, y_valid, False, batch_size_top)
    #  freeze all layers of bert and just train task based classifier so that the gradients are not computed in backward(). Parameters of newly constructed modules (below requires_grad=False statment) have requires_grad=True by default
    # for p in bert_punc.module.bert.parameters():
    #     p.requires_grad = False
def run(epochs=500, training_percentage=0.4, validation_percentage=0.1, extract=True, cont=True, size=256, top_k=5):
    '''Does the routine required to get the data, put them in needed format and start training the model
       saves weights whenever the model produces a better test result and keeps track of the best loss'''
    if extract:
        print("Extracting data..")
        X, y = data.extract_data(size=size)

        print("Preprocessing data..")
        X, y, nb_samples, num_categories = data.preprocess_data(X, y, save=True, subtract_mean=True)

    else:
        print("Loading data..")
        h5f = h5py.File('data.hdf5', 'r')
        nb_samples = h5f['nb_samples'].value
        num_categories = h5f['n_categories'].value
        h5f.close()

    print("Number of categories: {}".format(num_categories))
    print("Number of samples {}".format(nb_samples))

    data_ids = np.arange(start=0, stop=nb_samples)
    val_ids = data.produce_validation_indices(data_ids, nb_samples * validation_percentage)
    train_ids = data.produce_train_indices(dataset_indx=data_ids, number_of_samples=nb_samples * training_percentage,
                                           val_indx=val_ids)
    # X_train, y_train, X_test, y_test = data.split_data(X, y, split_ratio=split)
    X_train, y_train, X_val, y_val = data.load_dataset_bit_from_hdf5(train_ids, val_ids, only_train=False)
    X_val = X_val / 255

    print("Building and Compiling model..")
    model = m.get_model(n_outputs=num_categories, input_size=size)

    if cont:
        # model.load_weights_until_layer("pre_trained_weights/latest_model_weights.hdf5", 26)
        model.load_weights("pre_trained_weights/latest_model_weights.hdf5")
    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=["accuracy"])

    print("Training..")

    best_performance = np.inf
    for i in range(epochs):
        train_ids = data.produce_train_indices(dataset_indx=data_ids, number_of_samples=15000, val_indx=val_ids)

        X_train, y_train = data.load_dataset_bit_from_hdf5(train_ids, val_ids, only_train=True)

        X_train = X_train / 255
        X_train = data.augment_data(X_train)

        # fit the model on the batches generated by datagen.flow()
        metadata = model.fit(X_train, y_train, validation_data=[X_val, y_val], batch_size=64,
                             nb_epoch=1, verbose=1, shuffle=True, class_weight=None,
                             sample_weight=None)
        current_loss = metadata.history['loss'][-1]
        current_val_loss = metadata.history['val_loss'][-1]
        preds = model.predict_proba(X_val, batch_size=64)
        print("Loss: {}".format(current_loss))
        print("Val_loss: {}".format(current_val_loss))

        top_3_error = get_top_n_error(preds, y_val, top_k)
        print("Top 3 error: {}".format(top_3_error))
        if current_val_loss < best_performance:
            model.save_weights("pre_trained_weights/model_weights.hdf5", overwrite=True)
            best_performance = current_val_loss
            print("Saving weights..")
        model.save_weights("pre_trained_weights/latest_model_weights.hdf5", overwrite=True)
Beispiel #13
0
    axes[(0, 0)].set_title('Truth: 1', fontsize=15)
    axes[(0, 1)].set_title('Truth: 0', fontsize=15)
    axes[(0, 0)].set_ylabel('Pred: 1', fontsize=15)
    axes[(1, 0)].set_ylabel('Pred: 0', fontsize=15)

    # axes[(1,1)].y
    plt.savefig('uncertainty')

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Uncertainty estimation and evaluation')
    parser.add_argument("checkpoint", type=str)
    parser.add_argument("--datadir", type=str, default='dataset'),
    params, _ = parser.parse_known_args()

    print("preprocess")
    train_set, val_set, test_set, tay_set = data.preprocess_data(
        data_folder=params.datadir)
    model = torch.load(params.checkpoint, map_location=device)
    train_it, val_it, test_it, tay_it = data.get_batch_iterators(
            64, train_set, val_set, test_set, tay_set)

 
    ground_truth, predictions, dropout_preds, ids = predict(model,test_it, num_samples=100)
    np.savetxt('predictions.csv', predictions)
    np.savetxt('dropout_preds.csv', dropout_preds)
    np.savetxt('ground_truth.csv', ground_truth)


    sorted_variances = dropout_preds.var(axis=1).argsort(axis=0)
    max_indices, min_indices = sorted_variances[:5], sorted_variances[-5:] 
    # print(dropout_preds.var(axis=1).max(axis=0))
    # print(dropout_preds.var(axis=1).min(axis=0))
Beispiel #14
0
    # network training
    parser.add_argument("--batch_size", type=int, default=16, help="batch size")
    parser.add_argument("--unit_epoch", type=int, default=50, help="number of transition epochs")
    parser.add_argument("--lambda_gp", type=float, default=10., help="weight of gradient penalty in WGAN")
    parser.add_argument("--lambda_drift", type=float, default=0.001, help="weight of drift term in D_loss")
    parser.add_argument("--num_aug", type=int, default=5, help="times of data augmentation; \
        x5 when training unconditional gan (using only mel data); x1 when training conditional gan (using the whole training set)")
    parser.add_argument("--lr", type=float, default=0.001, help="initial learning rate")
    parser.add_argument("--outf", type=str, default="logs", help='path of log files')

    # inference
    parser.add_argument("--num", type=int, default=1000, help="number of images to generate")

    arg = parser.parse_args()

    if arg.preprocess:
        mel_only = False if arg.cond else True
        preprocess_data(root_dir='../data_2018', mel_only=mel_only)

    assert arg.mode == "train" or arg.mode == "test", "invalid argument!"
    if arg.mode == "train":
        if arg.cond:
            print("\ntrain conditional GAN ...\n")
            gan_trainer = CondTrainer(arg, device, device_ids)
        else:
            gan_trainer = Trainer(arg, device, device_ids)
        gan_trainer.train()
    if arg.mode == "test":
        gan_generator = ImageGenerator(arg, device)
        gan_generator.generate(arg.num)
Beispiel #15
0
                                                target_words_emb)
        word_sim = fluid.layers.reduce_sum(word_sim, dim=-1)
        word_sim = fluid.layers.reshape(word_sim, shape=[-1])
        pred = fluid.layers.sigmoid(word_sim)

        # 通过估计的输出概率定义损失函数,注意我们使用的是sigmoid_cross_entropy_with_logits函数
        # 将sigmoid计算和cross entropy合并成一步计算可以更好的优化,所以输入的是word_sim,而不是pred

        loss = fluid.layers.sigmoid_cross_entropy_with_logits(word_sim, label)
        loss = fluid.layers.reduce_mean(loss)

        # 返回前向计算的结果,飞桨会通过backward函数自动计算出反向结果。
        return pred, loss


corpus = preprocess_data()
word2id_dict, word2id_freq, id2word_dict = build_dict(corpus)
corpus = [word2id_dict[word] for word in corpus]
vocab_size = len(word2id_dict)
print(f"vocab size: {vocab_size}")
corpus = subsampling(corpus, word2id_freq)
print("after subsampling %d tokens in the corpus" % len(corpus))
# print(f"finish create dateset: {len(dataset)} sample")

step = 0
learning_rate = 0.001


# 定义一个使用word-embedding查询同义词的函数
# 这个函数query_token是要查询的词,k表示要返回多少个最相似的词,embed是我们学习到的word-embedding参数
# 我们通过计算不同词之间的cosine距离,来衡量词和词的相似度
Beispiel #16
0
                    I_val = utils.make_grid(fixed_batch, nrow=4, normalize=True, scale_each=True)
                    writer.add_image('val/image', I_val, epoch)
            if opt.log_images and (not opt.no_attention):
                print('\nlog attention maps ...\n')
                # training data
                __, a1, a2 = model(inputs[0:16,:,:,:])
                if a1 is not None:
                    attn1 = visualize_attn(I_train, a1, up_factor=opt.base_up_factor, nrow=4)
                    writer.add_image('train/attention_map_1', attn1, epoch)
                if a2 is not None:
                    attn2 = visualize_attn(I_train, a2, up_factor=2*opt.base_up_factor, nrow=4)
                    writer.add_image('train/attention_map_2', attn2, epoch)
                # val data
                __, a1, a2 = model(fixed_batch)
                if a1 is not None:
                    attn1 = visualize_attn(I_val, a1, up_factor=opt.base_up_factor, nrow=4)
                    writer.add_image('val/attention_map_1', attn1, epoch)
                if a2 is not None:
                    attn2 = visualize_attn(I_val, a2, up_factor=2*opt.base_up_factor, nrow=4)
                    writer.add_image('val/attention_map_2', attn2, epoch)
        # Append the row to the dataframes
        train_results = train_results.append(train_row, ignore_index=True)
        val_results = val_results.append(val_row, ignore_index=True)
    # write to csvs
    train_results.to_csv('train_scores.csv')
    val_results.to_csv('val_scores.csv')

if __name__ == "__main__":
    preprocess_data(root_dir='../ImageDataSet/Xray')
    main()
Beispiel #17
0
def run(epochs=500,
        training_percentage=0.4,
        validation_percentage=0.1,
        extract=True,
        cont=True,
        size=256,
        top_k=5):
    '''Does the routine required to get the data, put them in needed format and start training the model
       saves weights whenever the model produces a better test result and keeps track of the best loss'''
    if extract:
        print("Extracting data..")
        X, y = data.extract_data(size=size)

        print("Preprocessing data..")
        X, y, nb_samples, num_categories = data.preprocess_data(
            X, y, save=True, subtract_mean=True)

    else:
        print("Loading data..")
        h5f = h5py.File('data.hdf5', 'r')
        nb_samples = h5f['nb_samples'].value
        num_categories = h5f['n_categories'].value
        h5f.close()

    print("Number of categories: {}".format(num_categories))
    print("Number of samples {}".format(nb_samples))

    data_ids = np.arange(start=0, stop=nb_samples)
    val_ids = data.produce_validation_indices(
        data_ids, nb_samples * validation_percentage)
    train_ids = data.produce_train_indices(dataset_indx=data_ids,
                                           number_of_samples=nb_samples *
                                           training_percentage,
                                           val_indx=val_ids)
    # X_train, y_train, X_test, y_test = data.split_data(X, y, split_ratio=split)
    X_train, y_train, X_val, y_val = data.load_dataset_bit_from_hdf5(
        train_ids, val_ids, only_train=False)
    X_val = X_val / 255

    print("Building and Compiling model..")
    model = m.get_model(n_outputs=num_categories, input_size=size)

    if cont:
        # model.load_weights_until_layer("pre_trained_weights/latest_model_weights.hdf5", 26)
        model.load_weights("pre_trained_weights/latest_model_weights.hdf5")
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=["accuracy"])

    print("Training..")

    best_performance = np.inf
    for i in range(epochs):
        train_ids = data.produce_train_indices(dataset_indx=data_ids,
                                               number_of_samples=15000,
                                               val_indx=val_ids)

        X_train, y_train = data.load_dataset_bit_from_hdf5(train_ids,
                                                           val_ids,
                                                           only_train=True)

        X_train = X_train / 255
        X_train = data.augment_data(X_train)

        # fit the model on the batches generated by datagen.flow()
        metadata = model.fit(X_train,
                             y_train,
                             validation_data=[X_val, y_val],
                             batch_size=64,
                             nb_epoch=1,
                             verbose=1,
                             shuffle=True,
                             class_weight=None,
                             sample_weight=None)
        current_loss = metadata.history['loss'][-1]
        current_val_loss = metadata.history['val_loss'][-1]
        preds = model.predict_proba(X_val, batch_size=64)
        print("Loss: {}".format(current_loss))
        print("Val_loss: {}".format(current_val_loss))

        top_3_error = get_top_n_error(preds, y_val, top_k)
        print("Top 3 error: {}".format(top_3_error))
        if current_val_loss < best_performance:
            model.save_weights("pre_trained_weights/model_weights.hdf5",
                               overwrite=True)
            best_performance = current_val_loss
            print("Saving weights..")
        model.save_weights("pre_trained_weights/latest_model_weights.hdf5",
                           overwrite=True)
from utils import read_imagenet_classnames, display_results, run_inference, parse_base64
from torchvision import models

parser = argparse.ArgumentParser(description='Inference Trained Model')
parser.add_argument('--data', metavar='DIR', default='./data', help='default data path')
parser.add_argument('-bs', '--batch-size', metavar='BS', default=2, help='maximum batchsize')
parser.add_argument('-tp', '--top-predictions', metavar='NUMPRED',\
                     default=5, help='number of top predictions per sample')
parser.add_argument('-exp', '--export', action="store_true",help='export model to onnx')


def export_model(model):
    model_path = 'checkpoints/model.pt'
    sample_input = torch.randn((1, 3, 256, 256))
    model = model.cpu()
    model.eval()
    model = torch.jit.trace(model, sample_input)
    torch.jit.save(model, model_path)    


if __name__ == "__main__":
    args = parser.parse_args()
    model = models.resnet18(pretrained=True)
    if args.export:
        export_model(model)
    else:
        imagenet_classes = read_imagenet_classnames("cache/imagenet_classnames.txt")
        data = preprocess_data("cache")
        predictions = run_inference(model, data[0], args.top_predictions)
        display_results(data, predictions, imagenet_classes)
Beispiel #19
0
def train():
    # get batch iterator
    print("preprocess")
    train_set, val_set, test_set, _ = data.preprocess_data(
        data_folder=params.datadir)

    print(model_config)

    print("Training model...")
    model_config['num_embeddings'] = train_set.fields[
        'text'].vocab.vectors.shape[0]
    model_config['embedding_dim'] = train_set.fields[
        'text'].vocab.vectors.shape[1]

    model = Main(model_config, train_set.fields['text'].vocab)

    pytorch_total_params = sum(p.numel() for p in model.parameters())
    print(pytorch_total_params)

    model.to(device)
    grad_params = [p for p in model.parameters() if p.requires_grad]
    # weight = torch.FloatTensor(N_CLASSES).fill_(1)
    weight = torch.FloatTensor([0.3, 0.7])
    ce_loss = nn.CrossEntropyLoss(weight=weight).to(device)

    lr = model_config['learning_rate']
    epoch = 1
    prev_dev_accuracy = 0
    optimizer = optim.Adam(grad_params,
                           lr,
                           weight_decay=model_config['weight_decay'])
    best_epoch = 0
    while epoch < params.epochs:
        # writer.add_scalar(
        #     'Learning rate', optimizer.param_groups[0]['lr'], epoch)
        optimizer.param_groups[0]['lr'] = optimizer.param_groups[0]['lr'] * \
            LR_DECAY if epoch > 1 else optimizer.param_groups[0]['lr']
        train_it, val_it, test_it, _ = data.get_batch_iterators(
            MINIBATCH_SIZE, train_set, val_set, test_set)
        train_loss = 0
        train_loss_epoch = 0
        batches = 0
        for batch in train_it:
            optimizer.zero_grad()
            output = model.forward(batch.text)
            loss = ce_loss(output, batch.label_a)
            loss.backward()

            train_loss += loss
            train_loss_epoch += loss
            optimizer.step()
            batches += 1
            if batches % 50 == 0:
                train_loss = 0
                # writer.add_scalar('Loss', train_loss, batches / 50)
        print("Training Loss at epoch: {} is: {}".format(epoch, train_loss))
        # writer.add_scalar('Loss (Epoch)', train_loss_epoch, epoch)
        n_correct = 0
        n_tested = 0
        true_positive = [0, 0]
        true_negative = [0, 0]
        false_positive = [0, 0]
        false_negative = [0, 0]
        true_labels = [0, 0]
        for batch in val_it:
            output = model.forward(batch.text)
            scores, predictions = torch.max(output, dim=1)

            for i in range(2):
                true_labels = (batch.label_a == i).nonzero()
                false_labels = (batch.label_a != i).nonzero()
                true_positive[i] += (
                    predictions[true_labels] == i).sum().item()
                false_negative[i] += (predictions[true_labels] !=
                                      i).sum().item()
                false_positive[i] += (
                    predictions[false_labels] == i).sum().item()
                true_negative[i] += (predictions[false_labels] !=
                                     i).sum().item()

            n_correct += (batch.label_a == predictions).sum()
            n_tested += batch.label_a.shape[0]

        # writer.add_scalar('Validation accuracy', accuracy, epoch)
        macro_precision = 0.0
        macro_recall = 0.0
        macro_f1 = 0.0

        non_zero_devision = 1e-6
        for i in range(2):
            precision = (
                true_positive[i] /
                (true_positive[i] + false_positive[i] + non_zero_devision))
            macro_precision += precision
            recall = (
                true_positive[i] /
                (true_positive[i] + false_negative[i] + non_zero_devision))
            macro_recall += recall
            macro_f1 += 2 * (precision*recall) / \
                (precision+precision+ non_zero_devision)

        macro_precision /= 2
        macro_recall /= 2

        macro_f1 /= 2
        print(
            f'Precision: {macro_precision}\nRecall: {macro_recall}\nF1: {macro_f1}'
        )
        accuracy = n_correct.item() / n_tested

        if macro_f1 <= prev_dev_accuracy:
            # optimizer.param_groups[0]['lr'] /= 2
            lr = optimizer.param_groups[0]['lr']
            print(f"lr: {lr} and threshold: {THRESHOLD}")
        else:
            prev_dev_accuracy = macro_f1
            best_epoch = epoch
            if (params.save_model):
                torch.save(
                    model,
                    os.path.join(params.outputdir, params.model + "_epoch_" +
                                 str(epoch) + ".pt"))

        print("Validation accuracy at epoch: {} is: {}, f1 {}".format(
            epoch, accuracy, macro_f1))

        # Store model
        epoch += 1
    validate(test_it, best_epoch, train_set.fields['text'].vocab, model_config)
Beispiel #20
0
# Dataset parameters
img_rows, img_cols = 28, 28  #input image dimensions for the MNIST dataset
num_classes = 10  #number of classes in the MNIST dataset

# Load the data, and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Specify channel order
K.set_image_data_format('channels_last')

# Preprocess the data
input_shape, x_train, y_train, x_test, y_test = preprocess_data(
    x_train,
    y_train,
    x_test,
    y_test,
    img_rows,
    img_cols,
    num_classes,
    K.image_data_format(),
    print_shape=False)

model = ThreeLayersCNN(input_shape=input_shape,
                       nb_filters=num_filters,
                       nb_classes=num_classes,
                       print_model_summary=False)

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

model.fit(x_train,
Beispiel #21
0
####
####
#### Responsible for loading data as well as training and evaluating the model
####
####

import constants as C
from data import load_raw_data, preprocess_data, split_data_to_training_and_test_sets
from model import create_logistic_regression_model, create_SVM, calculate_accuracy

if __name__ == '__main__':
    # Loads the raw data
    dataset = load_raw_data()

    # Splits the data to training and test sets
    X, y = preprocess_data(dataset, C.FEATURES)
    X_train, X_test, y_train, y_test = split_data_to_training_and_test_sets(
        X, y)

    # Generate the LR model, train, and get the accuracy score
    lr_classifier = create_logistic_regression_model(X_train, y_train)
    accuracy = calculate_accuracy(lr_classifier, X_test, y_test)

    print()
    print('--------------')
    print('Accuracy of logistic regression model using all features:',
          accuracy)
    print('--------------')
    print()

    # Generate the SVC model, train, and get the accuracy score