Exemple #1
0
def train_model(train_X, train_Y, metadata, dataset):
    """
    loads the saved model if exist in the mdoels directory else make new and save
    """
    try:
        f = open('models/nn_model_'+ metadata['hidden_activation'] + '_' +\
                 str(metadata['iterations']) + '.pkl', 'rb')
        model = pickle.load(f)
        f.close()
        print ('\nModel found................')
    except:
        print ('\nModel not found............')
        model = NN.train_model(train_X, train_Y, metadata)
        f = open('models/nn_model_'+ metadata['hidden_activation'] + '_' +\
                 str(metadata['iterations']) + '.pkl', 'wb')
        pickle.dump(model, f)
        f.close()
    return model
Exemple #2
0
def hyperparameter_search():
    X, Y = neural_network.generate_dataset(8)
    layers_shape = (8, 3, 8)
    learning_performance = []
    for learning_rate in [0, 0.03, 0.03, 0.1, 0.3, 1, 3, 10, 30, 100]:
        for iterations in [0, 3, 10, 30, 100, 300, 1000, 3000, 10000]:
            print("learning rate", learning_rate, "iterations", iterations)
            # `parameters` are the weights and biases
            parameters = neural_network.train_model(
                X,
                Y,
                layers_shape,
                learning_rate=learning_rate,
                iterations=iterations,
                log_costs=False,
                save_graph=False)

            numCorrect = 0
            for x in X:
                x = np.array([x]).T
                h, predicted_label = neural_network.predict(x, parameters)
                if np.array_equiv(x, predicted_label):
                    numCorrect += 1
            learning_performance.append(
                [learning_rate, iterations, numCorrect / x.size])

    # create diagram

    frame = pd.DataFrame(learning_performance,
                         columns=["Learning rate", "#Iterations", "Accuracy"])
    wide_frame = pd.pivot_table(frame,
                                values="Accuracy",
                                index=["Learning rate"],
                                columns="#Iterations")
    sns.heatmap(wide_frame, cmap="viridis", annot=True)

    plt.savefig("learning_performance.png")
    plt.close()
def train_action(window_size, hidden_layers):
    print("Fetching data...", file=sys.stderr)
    data = data_manager.fetch_protein_data("../data/training_data")
    print("Done! Preparing for training...", file=sys.stderr)
    input_data, signal_data = data_manager.prepare_dataset(data, window_size, loading_screen)
    print("Done! Train and test splitting...", file=sys.stderr)
    train_input, test_input = data_manager.train_test_split(input_data, signal_data)
    print("Done", file=sys.stderr)

    print("Building signal model", file=sys.stderr)

    # Input layer
    architecture = [input_data[0].shape[0]]
    # Hidden layer
    architecture.extend(hidden_layers)
    # Output layer
    architecture.append(2)

    model = nn.build_model(architecture)

    directory = MODEL_PATH + get_directory_name()
    try:
        os.makedirs(directory)
    except:
        print("There was an error while creating model's sub-directory.", file=sys.stderr)
        exit(1)

    start_time = time.time()
    history = nn.train_model(model, train_input[0], train_input[1],
                             filename=directory + MODEL_NAME)
    exec_time = time.time() - start_time

    test_eval = model.evaluate(test_input[0], test_input[1])

    results, precision = test_sequences(data, model, window_size, eval=True)
    save_history(history, directory + HISTORY_NAME)
    save_log(window_size, architecture, test_eval, precision, exec_time, directory + LOG_NAME)
    print("Done.", file=sys.stderr)
print('\n\n')

print('Check gradients')
check_nn_gradients()

print('\nChecking Backpropagation (w/ Regularization) ... \n')

#  Check gradients by running checkNNGradients
_lambda = 3
check_nn_gradients(_lambda)

# Also output the costFunction debugging values

debug_J = compute_cost(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y_test, _lambda)

print('\n\nCost at (fixed) debugging parameters (w/ lambda = 3): ', debug_J,
      '\n(this value should be about 0.576051)')

print('\nPress enter to start training:')
input()
print('\nTraining Neural Network... \n')

theta1, theta2 = train_model(X, y_test, input_layer_size, hidden_layer_size, num_labels)

print('\nVisualizing Neural Network... \n')
#display_data(theta1[:, 1:])

# predict using trained parameters
pred = predict(theta1, theta2, X)
print('\nTraining Set Accuracy: ', np.mean(pred == y_test) * 100)
Exemple #5
0
with open(gen_fp) as gen_file:
    neg_tokenizes_sentences = [ast.literal_eval(x.strip()) for x in gen_file.readlines()]

pruned_pos_tokenizes_sentences = []
for sentence in pos_tokenizes_sentences:
    if sentence != []:
        pruned_pos_tokenizes_sentences.append(sentence)

pruned_neg_tokenizes_sentences = []
for sentence in neg_tokenizes_sentences:
    if sentence != []:
        pruned_neg_tokenizes_sentences.append(sentence)

neg_tokenizes_sentences_10 = list(filter(lambda x: len(x) <= LENGTH_TWEETS, pruned_neg_tokenizes_sentences))
pos_tokenizes_sentences_10 = list(filter(lambda x: len(x) <= LENGTH_TWEETS, pruned_pos_tokenizes_sentences))
print('Filtered tweets longer than 10 words')

word2vec_model = Word2Vec.load("word2vec-1573008850.model")
neg_data, neg_labels = create_training_data(neg_tokenizes_sentences_10, word2vec_model, 0, LENGTH_TWEETS)
pos_data, pos_labels = create_training_data(pos_tokenizes_sentences_10, word2vec_model, 1, LENGTH_TWEETS)
print('Generated training and evaluation data')

training_tuple, eval_tuple = segment_data(pos_data, neg_data)
training_tuple_sep, eval_tuple_sep = list(map(list, zip(*training_tuple))), list(map(list, zip(*eval_tuple)))
training_data, training_labels = training_tuple_sep[0], training_tuple_sep[1]
eval_training_data, eval_training_labels = eval_tuple_sep[0], eval_tuple_sep[1]
print('Training Model')
train_model(training_data[:int(len(training_data)/4)], training_labels[:int(len(training_labels)/4)], eval_training_data, eval_training_labels)
print('Finished training model')