Esempio n. 1
0
def find_best_num_of_hidden_units(X, Y):
    print("--------------K-Fold Training-------------------")
    scores = []
    best_alpha = 0
    max_scores = 0
    max_h = 1

    for i in range(1, 11):
        '''
        Find the best learning rate first
        '''
        clf = MLPClassifier(hidden_layer_sizes=(i,), max_iter=8000, solver='sgd', tol=0.000000001)
        parameters = {'alpha': 10.0 ** -np.arange(1, 10)}
        clf = GridSearchCV(clf, parameters, n_jobs=-1, cv=3, verbose=True)
        reg = clf.fit(X, Y)
        '''
        Cross validation to find out the mean score
        '''
        print("Best Alpha: ", clf.best_params_['alpha'])
        score = np.mean(cross_val_score(reg, X, Y, cv=5, n_jobs=-1))
        scores.append(score)
        print("Hidden Layer:", i, " scores = ", score)
        if score > max_scores:
            best_alpha = clf.best_params_['alpha']
            max_h = i
            max_scores = score
    print("The best number of hidden units used is ", max_h, "with max score : ", max_scores)
    print("with alpha", best_alpha)
    print("--------------------------------------------------")
    best_classifier = MLPClassifier(hidden_layer_sizes=(max_h,), tol=0.000000001)
    best_classifier.alpha = best_alpha
    return best_classifier
def neural_network(df_images, df_labels, no_of_neurons):
    '''This function build the neural network and then delegate the accuracy calculation. It also plots the graphs'''
    classifier = MLPClassifier(solver="adam",
                               verbose=True,
                               early_stopping=False,
                               max_iter=1000)
    classifier.alpha = 0.05
    classifier.hidden_layer_sizes = (no_of_neurons, )
    classifier.activation = "relu"
    classifier.learning_rate_init = 0.1

    # splitting the dataset
    train_X, test_X, train_y, test_y = train_test_split(
        df_images,
        df_labels,
        test_size=0.2,
        random_state=1,
    )

    # fit the model
    classifier.fit(train_X, train_y)

    # for calculating accuracy manually
    print('\n------------------------accuracies for ' + str(no_of_neurons) +
          ' neurons----------------------------\n')
    train_accuracy, test_accuracy = calculate_accuracy(classifier, test_X,
                                                       test_y, train_X,
                                                       train_y)

    print(
        '\n---------------------Class wise accuracies-------------------------\n'
    )
    classwise_accuracy(classifier, test_X, test_y, train_X, train_y)

    # plotting the loss curve vs iterations
    loss_values = classifier.loss_curve_
    plt.plot(loss_values)
    plt.title('loss vs iterations for : ' +
              str(classifier.learning_rate_init) +
              ' learning rate and no of neurons ' + str(no_of_neurons))
    plt.xlabel('iterations')
    plt.ylabel('loss')
    plt.show()

    # save the model to disk
    current_date_time = time.strftime("%d/%m/%Y") + '_' + time.strftime(
        "%H:%M:%S")
    filename = 'model_for_' + str(
        no_of_neurons) + '_' + current_date_time + '.sav'
    joblib.dump(classifier, filename.replace(
        '/', '_'))  # saving the classifier using joblib library
    print('\nmodel saved in file ' + filename + '\n')

    # writing on csv file for reporting
    row_to_write = []
    row_to_write.append([
        filename,
        str(no_of_neurons),
        str(train_accuracy),
        str(test_accuracy)
    ])
    with open('report_regul_mnist.csv', 'a') as writeFile:
        writer = csv.writer(writeFile)
        writer.writerows(row_to_write)

    # printing train set classwise accuracy
    # classwise_accuracy(classifier, test_X, test_y, train_X, train_y)

    compute_confusion_matrix(classifier, df_labels, test_X, test_y)
def penalty_l2(X, y, l):
    clf = MLPClassifier().set_params(**params)
    clf.alpha = l
    clf.penalty = 'l2'
    clf.fit(X, y)
    return clf