Exemple #1
0
def tune_neural_network():
    kfold = StratifiedKFold(n_splits=4,
                            shuffle=True)  # 4-fold cross-validation
    # neural net parameters
    units = [200, 200]  # neurons in each hidden layer
    dropout = 0.2  # dropout rate
    epochs = 5  # epochs
    batch_size = 150  # size in batch
    learn_rate = 0.001
    #momentum = 0.8  # to work with SGD
    kernel_initializer = 'normal'  # weight init
    bias_initializer = 'normal'  # bias initi
    activation_function = 'relu'

    scores = []

    for train, test in kfold.split(
            data, labels):  # train on 3 pieces evaluate on 1 (4 total runs)
        model = NN.generate_neural_network(total_features, units, dropout,
                                           learn_rate, kernel_initializer,
                                           bias_initializer,
                                           activation_function)

        NN.train_neural_network(model, epochs, batch_size, data[train],
                                labels[train])  # train neural network

        score = NN.evaluate_neural_network(model, data[test],
                                           labels[test])  # evaluate neural net
        scores.append(score)

    print("Average accuracy: ", np.mean(scores), "Standard Deviation:",
          np.std(scores))
Exemple #2
0
def train_neural_network():
    print("Training neural network")
    (data, labels) = neural_network.load_training_data(OUTPUT_FOLDER)
    (training_dataset, validation_dataset, training_lab,
     validation_lab) = neural_network.configure_training_data(data, labels)
    network = neural_network.create_neural_network()
    neural_network.train_neural_network(network, training_dataset,
                                        validation_dataset, training_lab,
                                        validation_lab)
    neural_network.save_neural_network(network)
def train_models():
    data, labels, test_data, test_labels = create_sets()

    model = NB.train_multi_naive_bayes_classifier(data, labels, save=False)
    NB.test_multi_naive_bayes_classifier(model, test_data, test_labels)

    model = DT.train_decision_tree_classifier(data, labels, save=False)
    DT.test_decision_tree_classifier(model, test_data, test_labels)

    model = RF.train_random_forest_classifier(data, labels, save=False)
    RF.test_random_forest_classifier(model, test_data, test_labels)

    model = KNN.train_knn_classifier(data, labels, save=False)
    KNN.test_knn_classifier(model, test_data, test_labels)

    model = LR.train_logistic_regression_classifier(data, labels, save=False)
    LR.test_logistic_regression_classifier(model, test_data, test_labels)

    model = SVM.train_svm_classifier(data, labels, save=False)
    SVM.test_svm_classifier(model, test_data, test_labels)

    # init the neural net
    model = NN.generate_neural_network(total_features, units, dropout,
                                       learn_rate, kernel_initializer,
                                       bias_initializer, activation_function)
    """
    train the neural network with the given model, epochs, batch size, train data-labels.
    Specify verbosity level, validation data, callbacks and plots if needed.
    Default parameters:
    verbose=0, validation=False, val_data=None, val_labels=None, callbacks=False, plot_history=False
    example:
    NN.train_neural_network(model, epochs, batch_size, data, labels, verbose=0,
                            validation=True, val_data=test_data, val_labels=test_labels,
                            callbacks=True, plot_history=True)
    This is the main training stage and thus we want to save the best models at the right times. This is done
    setting the callback to True. Keras will seek for the minimum validation loss and it saves the model with
    the highest validation accuracy.
    """
    NN.train_neural_network(model,
                            epochs,
                            batch_size,
                            data,
                            labels,
                            verbose=2,
                            validation=True,
                            val_data=test_data,
                            val_labels=test_labels,
                            callbacks=True)
    NN.test_neural_network(model, test_data, test_labels)
Exemple #4
0
def adversarial_training():
    NN.train_neural_network(trained_model, 4, 15, val_data, val_labels, verbose=2)
    trained_model.save('Adam_adversarial_training_adv_1500_0.3.h5')

    predictions = trained_model.predict(val_data)
    confusion = confusion_matrix(val_labels, np.argmax(predictions, axis=1))
    print(confusion)
    TP = confusion[1, 1]
    TN = confusion[0, 0]
    FP = confusion[0, 1]
    FN = confusion[1, 0]
    FNR = FN / float(FN + TP) * 100
    FPR = FP / float(FP + TN) * 100
    accuracy = ((TP + TN) / float(TP + TN + FP + FN)) * 100
    print("Adversarial  FP:", FP, "- FN:", FN, "- TP:", TP, "- TN", TN)
    print("Adversarial Accuracy:", accuracy, "- FPR:", FPR, "- FNR:", FNR)
Exemple #5
0
def par_tune(train_features, train_labels):
    #    for i in range(avg_run):
    #        alpha= 0.01 * 10**(-1*i)
    #        print("alpha= %f" % alpha)
    best_accuracy = -1.0

    if test_layers_combination:
        test_layers = (
            (50, ),
            (300, ),
            (500, ),
            (700, ),
            #(400), (600), (700), (800), (1200), (1600),
            #(400, 100), (800, 200), (1200, 300), (1600, 400),
            #(400, 100, 40), (800, 400, 200), (1600, 800, 200),
            #(400, 300, 200, 100), (400, 400, 400, 400), (800, 800, 800, 800)
        )
    else:
        test_layers = ((700, ), )

    for tlayer in test_layers:
        print("********** Layers:", tlayer, "**********")
        n_test_tot = 0
        n_test_cor = 0
        for i in range(avg_run):
            train_f_shuff, train_l_shuff, test_f_shuff, test_l_shuff = make_train_test(
                train_features, train_labels, test_split)

            trained_nn = nn.train_neural_network(train_f_shuff,
                                                 train_l_shuff,
                                                 rescale_base,
                                                 hidden_layer_sizes=tlayer,
                                                 tol=2e-5)
            n_cor_i, n_tot_i, wrong_f, wrong_l, wrong_p = nn.test_neural_network(
                trained_nn, test_f_shuff, test_l_shuff, rescale_base)

            n_test_cor += n_cor_i
            n_test_tot += n_tot_i

#        print("***** PCA *****")
#        trained_nn = nn.train_neural_network(reduced_features[:n_train], train_labels[:n_train], rescale_base)
#        wrong_f, wrong_l, wrong_p = nn.test_neural_network(trained_nn, reduced_features[n_train:n_train_data], train_labels[n_train:n_train_data], rescale_base)
        if best_accuracy < n_test_cor / n_test_tot:
            best_accuracy = n_test_cor / n_test_tot
            best_trained_nn = trained_nn
            best_trained_nn.name = tlayer
        print('--- Average accuracy: ', n_test_cor / n_test_tot, ' ---')
        print()


#    for i in range(10):
#        input_tol= 10**(-1*i)
#        print("Tol:", input_tol)
#        trained_nn = nn.train_neural_network(train_features[0:n_data-test_split], train_labels[0:n_data-test_split], rescale_base, hidden_layer_sizes = (700), tol= input_tol)
#        wrong_f, wrong_l, wrong_p = nn.test_neural_network(trained_nn, train_features[n_train:n_train_data], train_labels[n_train:n_train_data], rescale_base)
#        print()

    print('Best neural network is', best_trained_nn.name)
    return best_trained_nn
Exemple #6
0
    def test_can_learn_left_vs_right(self):
        data = np.array([
            [2.0, 0.0, 0],
            [0.0, -1.0, 1],
            [0.0, 6.0, 1],
            [-1.0, 0.0, 0],
        ])
        # Training uses random initialization, so seed the generator to ensure we get the same value
        # every time.
        np.random.seed(0)
        model = nn.train_neural_network(data, [2, 2, 2], epochs=500)

        X = np.array([
            [10.0, 0.0],
            [0.0, 5.0],
        ])
        prediction = model.predict(X)

        expected_prediction = np.array([0, 1])
        numpy.testing.assert_almost_equal(prediction,
                                          expected_prediction,
                                          decimal=3)
def random_sub_sampling(runs):
    score_gnb = []  # score for gaussian naive bayes
    score_mnb = []  # scores for multinomial naive bayes
    score_cnb = []  # scores for complement naive bayes
    score_bnb = []  # scores for bernoulli naive bayes
    score_dt = []  # scores for decision trees
    score_rf = []  # scores for random forest
    score_knn = []  # scores for k nearest neighbors
    score_lr = []  # scores for logistic regression
    score_svm = []  # scores for support vector machines
    score_nn = []  # scores for neural network

    for i in range(runs):

        data, labels, test_data, test_labels = create_random_sets(
        )  # choose random training and testing sets

        #model = GNB.train_gaussian_naive_bayes_classifier(data, labels)  # train Gaussian Naive Bayes
        #score_gnb = GNB.evaluate_gaussian_naive_bayes_classifier(model, test_data, test_labels)  # evaluate performance

        #model = MNB.train_multi_naive_bayes_classifier(data, labels)  # train Multinomial Naive Bayes
        #score_mnb = MNB.evaluate_multi_naive_bayes_classifier(model, test_data, test_labels)

        #model = CNB.train_complement_naive_bayes_classifier(data, labels)  # train Complement Naive Bayes
        #score_cnb = CNB.evaluate_complement_naive_bayes_classifier(model, test_data, test_labels)

        #model = BNB.train_bernoulli_naive_bayes_classifier(data, labels)  # train Bernoulli Naive Bayes
        #score_bnb = BNB.evaluate_bernoulli_naive_bayes_classifier(model, test_data, test_labels)

        #model = DT.train_decision_tree_classifier(data, labels)  # train Decision Tree Classifier
        #score_dt = DT.evaluate_decision_tree_classifier(model, test_data, test_labels)

        #model = RF.train_random_forest_classifier(data, labels)  # train Random Forest
        #score_rf = RF.evaluate_random_forest_classifier(model, test_data, test_labels)

        #model = KNN.train_knn_classifier(data, labels)  # train k-Nearest Neighbors Classifier
        #score_knn = KNN.evaluate_knn_classifier(model, test_data, test_labels)

        #model = LR.train_logistic_regression_classifier(data, labels)  # train logistic Regression
        #score_lr = LR.evaluate_logistic_regression_classifier(model, test_data, test_labels)

        #model = SVM.train_svm_classifier(data, labels)  # train Support Vector Machines
        #score_svm = SVM.evaluate_svm_classifier(model, test_data, test_labels)

        # init neural net
        model = NN.generate_neural_network(total_features, units, dropout,
                                           learn_rate, kernel_initializer,
                                           bias_initializer,
                                           activation_function)
        """
        this is not the actual training procedure and we don't want to save the models. To save models and implement
        the early stopping technique refer to train_models.py
        The goal of this operation is only to determine the behavior of models to random training sets and random
        testing sets!
        So, only train and evaluate models.
        """
        NN.train_neural_network(model,
                                epochs,
                                batch_size,
                                data,
                                labels,
                                verbose=2)
        score = NN.evaluate_neural_network(model, test_data, test_labels)
        score_nn.append(score)

    # get average accuracy and standard deviation for each model for each model
    #print("NB Average accuracy: ", np.mean(score_gnb), "Standard Deviation:", np.std(score_gnb))
    #print("MNB Average accuracy: ", np.mean(score_mnb), "Standard Deviation:", np.std(score_mnb))
    #print("CNB Average accuracy: ", np.mean(score_cnb), "Standard Deviation:", np.std(score_cnb))
    #print("BNB Average accuracy: ", np.mean(score_bnb), "Standard Deviation:", np.std(score_bnb))
    #print("DT Average accuracy: ", np.mean(score_dt), "Standard Deviation:", np.std(score_dt))
    #print("RF Average accuracy: ", np.mean(score_rf), "Standard Deviation:", np.std(score_rf))
    #print("kNN Average accuracy: ", np.mean(score_knn), "Standard Deviation:", np.std(score_knn))
    #print("LR Average accuracy: ", np.mean(score_lr), "Standard Deviation:", np.std(score_lr))
    #print("SVM Average accuracy: ", np.mean(score_svm), "Standard Deviation:", np.std(score_svm))
    print("NN Average accuracy: ", np.mean(score_nn), "Standard Deviation:",
          np.std(score_nn))
Exemple #8
0
def main():
    random.seed(142)
    np.random.seed(142)
    tf.random.set_seed(142)

    parser = ArgumentParser()
    parser.add_argument('-t', '--track', dest='track_name', type=str, required=True, choices=['nouns', 'verbs'],
                        help='A competition track name (nouns or verbs).')
    parser.add_argument('-f', '--fasttext', dest='fasttext_name', type=str, required=True,
                        help='A binary file with a Facebook-like FastText model (*.bin).')
    parser.add_argument('-w', '--wordnet', dest='wordnet_dir', type=str, required=True,
                        help='A directory with unarchived RuWordNet.')
    parser.add_argument('-i', '--input', dest='input_data_dir', type=str, required=True,
                        help='A directory with input data, i.e. lists of unseen hyponyms for public and private '
                             'submission.')
    parser.add_argument('-o', '--output', dest='output_data_dir', type=str, required=True,
                        help='A directory with output data, i.e. lists of unseen hyponyms and their hypernyms, found '
                             'as a result of this program execution, for public and private submission.')
    parser.add_argument('-c', '--cache_dir', dest='cache_dir', type=str, required=True,
                        help='A directory with cached data for training.')
    parser.add_argument('-n', '--number', dest='number_of_hypernyms', type=int, required=False, default=10,
                        help='A number of hypernyms generated for each unseen hyponym.')
    parser.add_argument('--conv', dest='conv_size', type=int, required=False, default=512,
                        help='A number of feature maps in a 1D convolution layer for each convolution window.')
    parser.add_argument('--n_hidden', dest='hidden_layers_number', type=int, required=False, default=1,
                        help='A number of hidden dense layers after the convolution layer.')
    parser.add_argument('--hidden_size', dest='hidden_layer_size', type=int, required=False, default=4096,
                        help='Size of each hidden dense layer.')
    parser.add_argument('--dropout', dest='dropout_rate', type=float, required=False, default=0.5,
                        help='A fraction of the input units to drop for the dropout technique.')
    parser.add_argument('--lr_max', dest='max_learning_rate', type=float, required=False, default=1e-3,
                        help='A maximal learning rate for the cyclical learning rate schedule.')
    parser.add_argument('--lr_min', dest='min_learning_rate', type=float, required=False, default=1e-5,
                        help='A minimal learning rate for the cyclical learning rate schedule.')
    parser.add_argument('--cycle_length', dest='training_cycle_length', type=int, required=False, default=11,
                        help='A period or cycle length for the cyclical learning rate schedule.')
    parser.add_argument('--epochs', dest='max_epochs', type=int, required=False, default=1000,
                        help='A maximal number of epochs to train the neural network.')
    parser.add_argument('--batch', dest='batch_size', type=int, required=False, default=128, help='A mini-batch size.')
    parser.add_argument('--bayesian', dest='bayesian_nn', action='store_true',
                        help='Must a Bayesian neural network be used?')
    parser.add_argument('--monte_carlo', dest='num_monte_carlo', type=int, required=False, default=20,
                        help='A sample number for the Monte Carlo inference in a bayesian neural network.')
    parser.add_argument('--kl_weight', dest='kl_weight', type=float, required=False, default=0.5,
                        help='Weight of the KL loss for Bayesian deep learning.')
    args = parser.parse_args()

    assert args.number_of_hypernyms >= 10, \
        '{0} is too small value for the hypernyms number!'.format(args.number_of_hypernyms)
    is_bayesian = args.bayesian_nn
    if is_bayesian:
        num_monte_carlo = args.num_monte_carlo
        assert num_monte_carlo > 1
    else:
        num_monte_carlo = 0
    cached_data_dir = os.path.normpath(args.cache_dir)
    assert os.path.isdir(cached_data_dir)
    nltk.download('punkt')
    wordnet_dir = os.path.normpath(args.wordnet_dir)
    assert os.path.isdir(wordnet_dir)
    wordnet_senses_name = os.path.join(wordnet_dir, 'senses.N.xml' if args.track_name == 'nouns' else 'senses.V.xml')
    wordnet_synsets_name = os.path.join(wordnet_dir, 'synsets.N.xml' if args.track_name == 'nouns' else 'synsets.V.xml')
    wordnet_relations_name = os.path.join(
        wordnet_dir,
        'synset_relations.N.xml' if args.track_name == 'nouns' else 'synset_relations.V.xml'
    )
    assert os.path.isfile(wordnet_senses_name)
    assert os.path.isfile(wordnet_synsets_name)
    assert os.path.isfile(wordnet_relations_name)
    fasttext_model_path = os.path.normpath(args.fasttext_name)
    assert os.path.isfile(fasttext_model_path)
    input_data_dir = os.path.normpath(args.input_data_dir)
    os.path.isdir(input_data_dir), 'Directory `{0}` does not exist!'.format(input_data_dir)
    output_data_dir = os.path.normpath(args.output_data_dir)
    os.path.isdir(output_data_dir), 'Directory `{0}` does not exist!'.format(output_data_dir)
    public_data_name = os.path.join(input_data_dir,
                                    '{0}_public.tsv'.format('nouns' if args.track_name == 'nouns' else 'verbs'))
    assert os.path.isfile(public_data_name), 'File `{0}` does not exist!'.format(public_data_name)
    public_submission_name = os.path.join(output_data_dir,
                                          'submitted_{0}_public.tsv'.format(('nouns' if args.track_name == 'nouns'
                                                                             else 'verbs')))
    private_data_name = os.path.join(input_data_dir,
                                     '{0}_private.tsv'.format('nouns' if args.track_name == 'nouns' else 'verbs'))
    assert os.path.isfile(private_data_name), 'File `{0}` does not exist!'.format(private_data_name)
    private_submission_name = os.path.join(output_data_dir,
                                           'submitted_{0}_private.tsv'.format(('nouns' if args.track_name == 'nouns'
                                                                               else 'verbs')))

    synsets = ruwordnet_parsing.load_synsets(senses_file_name=wordnet_senses_name,
                                             synsets_file_name=wordnet_synsets_name)
    data_for_public_submission = hyponyms_loading.load_terms_for_submission(public_data_name)
    print('Number of hyponyms for public submission is {0}.'.format(len(data_for_public_submission)))
    data_for_private_submission = hyponyms_loading.load_terms_for_submission(private_data_name)
    print('Number of hyponyms for private submission is {0}.'.format(len(data_for_private_submission)))
    print('')

    solver_name = os.path.join(cached_data_dir, 'fasttext_and_{0}_cnn.h5'.format(
        'bayesian' if is_bayesian else 'simple'))
    solver_params_name = os.path.join(cached_data_dir, 'fasttext_and_{0}_cnn_params.pkl'.format(
        'bayesian' if is_bayesian else 'simple'))
    if does_neural_network_exist(solver_name) and os.path.isfile(solver_params_name):
        with open(solver_params_name, 'rb') as fp:
            (max_hyponym_length, max_hypernym_length, all_tokens, embeddings_matrix, conv_size, hidden_layer_size,
             hidden_layers_number) = pickle.load(fp)
        assert isinstance(embeddings_matrix, np.ndarray)
        assert len(embeddings_matrix.shape) == 2
        assert embeddings_matrix.shape[0] == (len(all_tokens) + 1)
        if is_bayesian:
            solver = neural_network.build_bayesian_cnn(
                max_hyponym_length=max_hyponym_length, max_hypernym_length=max_hypernym_length,
                word_embeddings=embeddings_matrix, n_feature_maps=args.conv_size,
                hidden_layer_size=args.hidden_layer_size, n_hidden_layers=args.hidden_layers_number,
                n_train_samples=100, kl_weight=args.kl_weight
            )
        else:
            solver = neural_network.build_cnn(
                max_hyponym_length=max_hyponym_length, max_hypernym_length=max_hypernym_length,
                word_embeddings=embeddings_matrix, n_feature_maps=args.conv_size,
                hidden_layer_size=args.hidden_layer_size,
                n_hidden_layers=args.hidden_layers_number, dropout_rate=args.dropout_rate
            )
        solver.load_weights(solver_name)
        print('The neural network has been loaded from the `{0}`...'.format(solver_name))
    else:
        data_for_training, data_for_validation, data_for_testing = ruwordnet_parsing.prepare_data_for_training(
            senses_file_name=wordnet_senses_name, synsets_file_name=wordnet_synsets_name,
            relations_file_name=wordnet_relations_name
        )
        all_tokens = ruwordnet_parsing.tokens_from_synsets(synsets, additional_sources=[data_for_public_submission,
                                                                                        data_for_private_submission])
        print('Vocabulary size is {0}.'.format(len(all_tokens)))
        embeddings_matrix = trainset_preparing.calculate_word_embeddings(all_tokens, fasttext_model_path)
        print('All word embeddings are calculated...')
        max_hyponym_length, max_hypernym_length = trainset_preparing.get_maximal_lengths_of_texts(synsets)
        print('Maximal length of a single hyponym is {0}.'.format(max_hyponym_length))
        print('Maximal length of a single hypernym is {0}.'.format(max_hypernym_length))
        print('')

        X_train, y_train = trainset_preparing.build_dataset_for_training(
            data=data_for_training, synsets=synsets, tokens_dict=all_tokens,
            max_hyponym_length=max_hyponym_length, max_hypernym_length=max_hypernym_length
        )
        X_val, y_val = trainset_preparing.build_dataset_for_training(
            data=data_for_validation, synsets=synsets, tokens_dict=all_tokens,
            max_hyponym_length=max_hyponym_length, max_hypernym_length=max_hypernym_length
        )
        if is_bayesian:
            solver = neural_network.build_bayesian_cnn(
                max_hyponym_length=max_hyponym_length, max_hypernym_length=max_hypernym_length,
                word_embeddings=embeddings_matrix, n_feature_maps=args.conv_size,
                hidden_layer_size=args.hidden_layer_size, n_hidden_layers=args.hidden_layers_number,
                n_train_samples=y_train.shape[0], kl_weight=args.kl_weight
            )
        else:
            solver = neural_network.build_cnn(
                max_hyponym_length=max_hyponym_length, max_hypernym_length=max_hypernym_length,
                word_embeddings=embeddings_matrix, n_feature_maps=args.conv_size,
                hidden_layer_size=args.hidden_layer_size,
                n_hidden_layers=args.hidden_layers_number, dropout_rate=args.dropout_rate
            )
        solver = neural_network.train_neural_network(
            X_train=X_train, y_train=y_train, X_val=X_val, y_val=y_val,
            neural_network=solver, max_epochs=args.max_epochs, training_cycle_length=args.training_cycle_length,
            max_learning_rate=args.max_learning_rate, min_learning_rate=args.min_learning_rate,
            is_bayesian=is_bayesian, num_monte_carlo=num_monte_carlo, batch_size=args.batch_size
        )
        del X_train, y_train, X_val, y_val
        X_test, y_test = trainset_preparing.build_dataset_for_training(
            data=data_for_testing, synsets=synsets, tokens_dict=all_tokens,
            max_hyponym_length=max_hyponym_length, max_hypernym_length=max_hypernym_length
        )
        neural_network.evaluate_neural_network(
            X=X_test, y_true=y_test,
            neural_network=solver, batch_size=args.batch_size,
            num_monte_carlo=num_monte_carlo
        )
        del X_test, y_test
        with open(solver_params_name, 'wb') as fp:
            pickle.dump((max_hyponym_length, max_hypernym_length, all_tokens, embeddings_matrix, args.conv_size,
                         args.hidden_layer_size, args.hidden_layers_number), fp, protocol=pickle.HIGHEST_PROTOCOL)
        solver.save_weights(solver_name, overwrite=True)

    n_data_parts = 20
    print('Public submission is started...')
    if os.path.isfile(public_submission_name):
        predicted_hyponyms = load_existing_predictions(public_submission_name)
    else:
        predicted_hyponyms = set()
    with codecs.open(public_submission_name, mode='a', encoding='utf-8', errors='ignore') as fp:
        data_part_size = int(np.ceil(len(data_for_public_submission) / n_data_parts))
        data_part_counter = 0
        for idx, hyponym in enumerate(data_for_public_submission):
            if hyponym not in predicted_hyponyms:
                dataset, synset_IDs = trainset_preparing.build_dataset_for_submission(
                    unseen_hyponym=hyponym, synsets=synsets, tokens_dict=all_tokens,
                    max_hyponym_length=max_hyponym_length, max_hypernym_length=max_hypernym_length
                )
                probabilities = neural_network.apply_neural_network(
                    X=dataset, neural_network=solver,
                    batch_size=args.batch_size, num_monte_carlo=num_monte_carlo
                )
                del dataset
                best_synsets = list(map(lambda idx: (synset_IDs[idx], probabilities[idx]), range(len(synset_IDs))))
                best_synsets.sort(key=lambda it: (-it[1], it[0]))
                if len(best_synsets) > args.number_of_hypernyms:
                    n = args.number_of_hypernyms
                else:
                    n = len(best_synsets)
                for synset_id in map(lambda it: it[0], best_synsets[0:n]):
                    fp.write('{0}\t{1}\t{2}\n'.format(' '.join(hyponym).upper(), synset_id, synsets[synset_id][2]))
                del synset_IDs, best_synsets
            if (idx + 1) % data_part_size == 0:
                data_part_counter += 1
                print('  {0} % of public data have been processed...'.format(data_part_counter * 5))
        if data_part_counter < n_data_parts:
            print('  100 % of public data have been processed...')
    print('Public submission is finished...')
    print('')
    print('Private submission is started...')
    if os.path.isfile(private_submission_name):
        predicted_hyponyms = load_existing_predictions(private_submission_name)
    else:
        predicted_hyponyms = set()
    with codecs.open(private_submission_name, mode='a', encoding='utf-8', errors='ignore') as fp:
        data_part_size = int(np.ceil(len(data_for_private_submission) / n_data_parts))
        data_part_counter = 0
        for idx, hyponym in enumerate(data_for_private_submission):
            if hyponym not in predicted_hyponyms:
                dataset, synset_IDs = trainset_preparing.build_dataset_for_submission(
                    unseen_hyponym=hyponym, synsets=synsets, tokens_dict=all_tokens,
                    max_hyponym_length=max_hyponym_length, max_hypernym_length=max_hypernym_length
                )
                probabilities = neural_network.apply_neural_network(
                    X=dataset, neural_network=solver,
                    batch_size=args.batch_size, num_monte_carlo=num_monte_carlo
                )
                del dataset
                best_synsets = list(map(lambda idx: (synset_IDs[idx], probabilities[idx]), range(len(synset_IDs))))
                best_synsets.sort(key=lambda it: (-it[1], it[0]))
                if len(best_synsets) > args.number_of_hypernyms:
                    n = args.number_of_hypernyms
                else:
                    n = len(best_synsets)
                for synset_id in map(lambda it: it[0], best_synsets[0:n]):
                    fp.write('{0}\t{1}\t{2}\n'.format(' '.join(hyponym).upper(), synset_id, synsets[synset_id][2]))
                del synset_IDs, best_synsets
            if (idx + 1) % data_part_size == 0:
                data_part_counter += 1
                print('  {0} % of private data have been processed...'.format(data_part_counter * 5))
        if data_part_counter < n_data_parts:
            print('  100 % of private data have been processed...')
    print('Private submission is finished...')
    print('')
Exemple #9
0
    print(test_features.shape)
    #pd.display(train_features[1], length_x, length_y)

    ###### Perform PCA reduction ######
    if use_PCA:
        train_features, pca_model, n_rf = pr.pca_reduction(train_features)
        print(train_features.shape)

    ###### Tune pars of NN ######
    if tune_parameters:
        trained_nn = par_tune(train_features, train_labels)
    else:
        ## Training NN and get results ######
        trained_nn = nn.train_neural_network(train_features,
                                             train_labels,
                                             rescale_base,
                                             hidden_layer_sizes=(700),
                                             tol=1e-10)

    ###### Print out wrong cases ######
    if print_wrong_cases and not tune_parameters and not use_PCA:
        print('Wrong cases:')
        for i in range(min(10, len(wrong_l))):
            print("(i, label, predict)", i, ",", wrong_l[i], ",", wrong_p[i])
            pd.display(wrong_f[i], length_x, length_y)

    ###### Output predicts for test data ######
    if output_predict:
        print('--- Output predicts for test data ---')
        if use_PCA:
            test_features = pr.pca_transform(test_features, pca_model, n_rf)