def ex_2_2(input1, target1, input2, target2):
    ## TODO
    scores = []
    scores_train = []
    classifiers = []
    for i in range(10):
        classifier = MLPClassifier(hidden_layer_sizes=(20, ),
                                   solver="adam",
                                   max_iter=1000,
                                   activation="tanh",
                                   random_state=i)
        classifier.fit(input1, target1[:, 0])
        scores.append(classifier.score(input2, target2[:, 0]))
        classifiers.append(classifier)
        scores_train.append(classifier.score(input1, target1[:, 0]))

    conf_mat = confusion_matrix(target2[:, 0],
                                classifiers[np.argmax(scores)].predict(input2))

    plot_histogram_of_acc(scores_train, scores)
    #plot_histogram_of_acc(classifiers[np.argmax(scores)], classifier.score(input2, target2[:, 0]))
    #plot_histogram_of_acc(classifier.score(input1, target1[:,0]), classifier.score(input2, target2[:,0]))
    predected_target = classifier.predict(input2)
    misclassified_images = []
    for i in range(len(target2[:, 0])):
        if target2[:, 0][i] != predected_target[i]:
            misclassified_images.append(input2[i])

    for i in range(len(misclassified_images)):
        plot_image(misclassified_images[i])
    pass
def ex_2_2(input1, target1, input2, target2):
    individualTarget1 = target1[:, 0]
    individualTarget2 = target2[:, 0]

    classifiers = []
    training_scores = []
    test_scores = []
    for i in range(10):
        classifiers.append(MLPClassifier(activation='tanh', hidden_layer_sizes=20, max_iter=1000, random_state=i))

    for mlp in classifiers:
        mlp.fit(input1, individualTarget1)
        training_scores.append(mlp.score(input1, individualTarget1))
        test_scores.append(mlp.score(input2, individualTarget2))

    plot_histogram_of_acc(training_scores, test_scores)
    best_index = test_scores.index(max(test_scores))
    best_prediction = classifiers[best_index].predict(input2)

    cnf_matrix = confusion_matrix(individualTarget2, best_prediction)
    print(cnf_matrix)

    misclassified_images = []
    for i, pred in enumerate(best_prediction):
        if pred != individualTarget2[i]:
            misclassified_images.append(i)

    plot_images(input2, misclassified_images)
Exemplo n.º 3
0
def ex_2_2(input1, target1, input2, target2):
    """
    Solution for exercise 2.2
    :param input1: The input from dataset1
    :param target1: The target from dataset1
    :param input2: The input from dataset2
    :param target2: The target from dataset2
    :return:
    """

    #declaring variables used for MLPClassifier
    hidden_layers = 20
    solver_mode = 'adam'
    activation_mode = 'tanh'
    max_iter = 1000

    max_accuracy = 0.0

    train_accuracy = []
    test_accuracy = []
    cfn = []

    m = 0

    for m in range(10):
        cf = MLPClassifier(hidden_layer_sizes=(hidden_layers, ),
                           activation=activation_mode,
                           solver=solver_mode,
                           random_state=m,
                           max_iter=max_iter)
        cf.fit(input1, target1[:, 0])

        train_accuracy.append(cf.score(input1, target1[:, 0]))

        current_test_accuracy = cf.score(input2, target2[:, 0])

        test_accuracy.append(current_test_accuracy)

        plot_histogram_of_acc(train_accuracy[m], test_accuracy[m])

        if current_test_accuracy > max_accuracy:
            cfn = confusion_matrix(target2[:, 0], cf.predict(input2))
            max_accuracy = current_test_accuracy

    print(cfn)

    #plot_histogram_of_acc(train_accuracy, test_accuracy)
    #plot_random_images(input2)

    pass
def ex_2_2(input1, target1, input2, target2):
    """
    Solution for exercise 2.2
    :param input1: The input from dataset1
    :param target1: The target from dataset1
    :param input2: The input from dataset2
    :param target2: The target from dataset2
    :return:
    """
    n = 10

    train_acc = np.zeros(n)
    test_acc = np.zeros(n)
    pred_test = np.zeros((n, 564))
    coefs = np.zeros((n, 960, 20))

    #print(min(target1[:,0]), max(target1[:,0]))
    # we have 20 person

    for i in range(n):
        classifier = MLPClassifier(hidden_layer_sizes=(20, ),
                                   activation='tanh',
                                   solver='adam',
                                   max_iter=5000,
                                   random_state=i)
        classifier.fit(input1, target1[:, 0])
        pred_test[i] = classifier.predict(input2)
        coefs[i] = classifier.coefs_[0]
        train_acc[i] = classifier.score(input1, target1[:, 0])
        test_acc[i] = classifier.score(input2, target2[:, 0])

    error = pred_test[1] - target2[:, 0]
    for j in range(len(error)):
        if (error[j] != 0):
            print(j)
    plot_random_images(np.row_stack((input2[175, :], input2[184, :])))
    plot_random_images(np.row_stack((input2[210, :], input2[134, :])))
    plot_random_images(np.row_stack((input2[223, :], input2[177, :])))
    plot_random_images(np.row_stack((input2[179, :], input2[186, :])))

    plot_histogram_of_acc(train_acc, test_acc)

    # best network with seed i=1
    confmat = confusion_matrix(target2[:, 0], pred_test[1])
    print(confmat)

    pass
Exemplo n.º 5
0
def main():
    data = load_data()
    target1, input1, target2, input2 = \
        data['target1'], normalize(data['input1']), data['target2'], normalize(data['input2'])

    ## Plot some random images
    plot_random_images(input2)
    ## End plot some random images

    ## 2.1
    ## ex_2_1(input2, target2)
    ## End 2.1

    ## 2.2
    train_acc, test_acc, y_pred, C = ex_2_2(input1, target1, input2, target2)
    plot_histogram_of_acc(train_acc, test_acc) 
    print(C)
def ex_2_2(input1, target1, input2, target2):
    """
    Solution for exercise 2.2
    :param input1: The input from dataset1
    :param target1: The target from dataset1
    :param input2: The input from dataset2
    :param target2: The target from dataset2
    :return:
    """
    train = input1
    test = input2
    target_train = target1[:, 1]
    target_test = target2[:, 1]


    ## TODO
    n_hidden_neurons = 20

    accu_list_train = np.zeros((10,1))
    accu_list_test = np.zeros((10, 1))

# Find the best seed
    for seed in range(10):
        nn = MLPClassifier(activation='tanh', solver='adam', max_iter=1000, hidden_layer_sizes=(n_hidden_neurons,), random_state=seed)
        nn.fit(train, target_train)
        accu_list_train[seed] = nn.score(train, target_train)
        accu_list_test[seed] = nn.score(test, target_test)

    print(accu_list_train)
    print(accu_list_test)
# Compute NN weights with the best seed
    best_seed = np.argmax(accu_list_train)
    best_nn = nn = MLPClassifier(activation='tanh', solver='adam', max_iter=1000, hidden_layer_sizes=(n_hidden_neurons,),random_state=best_seed)
    best_nn.fit(train, target_train)

# Evaluate the confusion matrix with best NN
    predictions = nn.predict(test)
    C = confusion_matrix(target_test, predictions)
    print(C)

# Plot results
    plot_histogram_of_acc(accu_list_train, accu_list_test)
    print(accu_list_test)
# Find misclassified images
    comp_array = target_test - predictions
    comp_vector2 = np.nonzero(comp_array)
Exemplo n.º 7
0
def ex_2_2(input1, target1, input2, target2):

    target1 = np.transpose(target1)
    target1 = target1[0]
    target2 = np.transpose(target2)
    target2 = target2[0]

    acc_train = np.zeros((10, ))
    acc_test = np.zeros((10, ))
    max = -1
    for i in range(10):
        nn = MLPClassifier(random_state=i,
                           hidden_layer_sizes=(20, ),
                           activation='tanh',
                           solver='adam',
                           max_iter=1000)

        model = nn.fit(input1, target1)
        acc_train[i] = model.score(input1, target1)
        acc_test[i] = model.score(input2, target2)
        if acc_test[i] > max:
            max = acc_test[i]
            y_predict = model.predict(input2)
            C = confusion_matrix(target2, y_predict)
    k = 0
    for i, a in enumerate(target2):
        if a != y_predict[i] and k < 20:
            plot_image(input2[i])
            k = k + 1

    hidden_layer_weights = model.coefs_
    plot_hidden_layer_weights(hidden_layer_weights[0])

    plot_histogram_of_acc(acc_train, acc_test)
    print(C)

    pass
Exemplo n.º 8
0
def ex_2_2(input1, target1, input2, target2):
    '''
    • Write code to train a feed-forward neural network with 1 hidden layer containing 20 hidden units
      for recognising the individuals. Use dataset1 for training, ‘adam’ as the training solver and train for
      1000 iterations. Use dataset2 as the test set.
    • Repeat the process 10 times starting from a different initial weight vector and plot the histogram
      for the resulting accuracy on the training and on the test set (the accuracy is proportion of correctly
      classified samples and it is computed with the method score of the classifier).
    • Use the best network (with maximal accuracy on the test set) to calculate the confusion matrix for
      the test set.
    • Plot a few misclassified images.
    '''
    x_train = input1
    y_train = target1[:, 0]
    x_test = input2
    y_test = target2[:, 0]
    seeds = np.array(range(1, 11))
    train_accs = []
    test_accs = []
    max_acc = -1

    for index_seed, seed in np.ndenumerate(seeds):
        nn = MLPClassifier(solver='adam',
                           activation='tanh',
                           max_iter=1000,
                           hidden_layer_sizes=(20, ),
                           random_state=seed)
        nn.fit(x_train, y_train)
        train_acc = accuracy_score(y_train, nn.predict(x_train))
        train_accs.append(train_acc)
        test_acc = accuracy_score(y_test, nn.predict(x_test))
        test_accs.append(test_acc)
        if test_acc > max_acc:
            max_acc = test_acc
            best_nn = nn

    plot_histogram_of_acc(train_accs, test_accs)

    cm = confusion_matrix(y_test, best_nn.predict(x_test))
    prediction = best_nn.predict(x_test)
    misclassified = np.where(y_test != prediction)
    print(cm)
    limit = 8
    i = 0
    for mc_index in misclassified[0]:
        if i < limit:
            fig, plts = plt.subplots(1, 2)
            plts[0].set_title("Predicted Person " + str(prediction[mc_index]))
            plts[0].imshow(input2[prediction[mc_index]].reshape(*IMAGE_DIM).T,
                           cmap=plt.cm.gray)
            plts[0].set_xticks(())
            plts[0].set_yticks(())
            plts[1].set_title("Should be Person " + str(y_test[mc_index]))
            plts[1].imshow(input2[y_test[mc_index]].reshape(*IMAGE_DIM).T,
                           cmap=plt.cm.gray)
            plts[1].set_xticks(())
            plts[1].set_yticks(())
            plt.show()
        i = i + 1

    pass