Search code examples
pythonmachine-learningneural-networkbackpropagationgradient-descent

Neural Network makes same predictions for different instances with different features


Out of interest, I created (or at least tried to create) an Artificial Neural Network with four layers as a classifier for the famous Iris flower data set. The target values vary from 0 to 2 as labels for the three different flowers. For simplicity´s sake, I left away biases.

The problem is: Even though the mean squared error actually gets reduced and seems to converge, the network ends up classifying all instances (both train and test) equally. Every time I run it, it´s "choosing" a label between 1 and 3, never below or higher. So it seems that the gradient descent is somewhat working.

Could it be due to the missing biases? Or did I missunderstand the algorithm? Or maybe the derivatives are incorrect?

I learned the mathematical theory behind back propagation here: https://google-developers.appspot.com/machine-learning/crash-course/backprop-scroll/

neuralnetwork.py

import numpy as np
import math


def sigmoid(x):
    return (math.e**x) / (math.e**x + 1)


def sigmoid_deriv(x):
    return sigmoid(x) * (1 - sigmoid(x))


def ReLU(x):
    return x * (x > 0)


def ReLU_deriv(x):
    if x > 0:
        return 1
    else:
        return 0


def mean_square_error(output_vector, correct_vector):
    error = 0
    for i in range(0, len(output_vector)):
        error += (output_vector[i][0] - correct_vector[i][0])**2
    return 1/len(output_vector) * error


def div_x_output(y, y_correct, nr_instances):
    return 2 / nr_instances * (y - y_correct) * ReLU_deriv(y)

def div_x(y):
    return sigmoid_deriv(y)


def partial_deriv_synapses_output(learning_rate, prediction, solution, nr_instances, x, i, hidden_layer_1):
    return learning_rate * div_x_output(prediction, solution, nr_instances) * hidden_layer_1[x][i]


def partial_deriv_synapses_1(learning_rate, y, i, j, hidden_layer_0):
    return learning_rate * div_x(y) * hidden_layer_0[j][i]


def partial_deriv_synapses_0(learning_rate, y, i, j, input_matrix):
    return learning_rate * div_x(y) * input_matrix[j][i]




class NeuralNetwork:

    def __init__(self, synapses_0, synapses_1, synapses_2):
        self.synapses_0 = synapses_0
        self.synapses_1 = synapses_1
        self.synapses_2 = synapses_2
        self.sigmoid = np.vectorize(sigmoid)
        self.ReLU = np.vectorize(ReLU)

    def fit(self, input_matrix, solutions, learning_rate, nr_instances):
        hidden_layer_0 = self.sigmoid(np.dot(input_matrix, self.synapses_0))
        hidden_layer_1 = self.sigmoid(np.dot(hidden_layer_0, self.synapses_1))
        output_layer = self.ReLU(np.dot(hidden_layer_1, self.synapses_2))

        while mean_square_error(output_layer, solutions) > 0.7:
            print(mean_square_error(output_layer, solutions))
            x = 0
            for prediction in output_layer:
                # back propagate synapses 2
                for i in range(0, len(self.synapses_2)):
                        self.synapses_2[i][0] -= partial_deriv_synapses_output(learning_rate, prediction[0], solutions[x][0], nr_instances, x, i, hidden_layer_1)

                # back propagate synapses 1
                y_deriv_vector_synapses_1 = np.array([1. for i in range(0, len(self.synapses_1[0]))])
                for i in range(0, len(self.synapses_1[0])):
                    y_deriv_vector_synapses_1[i] = div_x_output(prediction[0], solutions[x][0], nr_instances) * self.synapses_2[i][0]
                for i in range(0, len(self.synapses_1)):
                    for j in range(0, len(self.synapses_1[0])):
                        self.synapses_1[i][j] -= partial_deriv_synapses_1(learning_rate, y_deriv_vector_synapses_1[j], i, j, hidden_layer_0)

                # back propagate synapses 0
                y_deriv_vector_synapses_0 = np.array([1. for i in range(0, len(self.synapses_0[0]))])
                for i in range(0, len(self.synapses_0[0])):
                    y_deriv_vector_synapses_0[i] = sum([div_x(y_deriv_vector_synapses_1[k]) * self.synapses_1[i][k] for k in range(0, len(self.synapses_1[0]))])
                for i in range(0, len(self.synapses_0)):
                    for j in range(0, len(self.synapses_0[0])):
                        self.synapses_0[i][j] -= partial_deriv_synapses_0(learning_rate, y_deriv_vector_synapses_0[j], i, j, input_matrix)

                hidden_layer_0 = self.sigmoid(np.dot(input_matrix, self.synapses_0))
                hidden_layer_1 = self.sigmoid(np.dot(hidden_layer_0, self.synapses_1))
                output_layer = self.sigmoid(np.dot(hidden_layer_1, self.synapses_2))

                x += 1

    def predict(self, input_vector):
        hidden_layer_0 = self.sigmoid(np.dot(input_vector, self.synapses_0))
        hidden_layer_1 = self.sigmoid(np.dot(hidden_layer_0, self.synapses_1))
        output_layer = self.ReLU(np.dot(hidden_layer_1, self.synapses_2))

        return output_layer[0]

main.py

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import numpy as np
import random

from neuralnetwork import NeuralNetwork

iris = load_iris()

X = iris.data
y = iris.target


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .2)

network = NeuralNetwork(np.array([[random.random() for i in range(0, 8)] for j in range(0, 4)]),
                        np.array([[random.random() for i in range(0, 3)] for j in range(0, 8)]),
                        np.array([[random.random() for i in range(0, 1)] for j in range(0, 3)]))


network.fit(np.array([x for x in X_train]), np.array([[y] for y in y_train]), 0.1, len(X_train))

error_count = 0
counter = 0

for x in X_train:
    prediction = round(network.predict(x))
    print("prediction: "+ str(prediction) + ", actual: " + str(y_train[counter]))
    if prediction != y_train[counter]:
        error_count += 1
    counter += 1


print("The error count is: " + str(error_count))

I appreciate all kind of help or tips!


Solution

  • The issue is due to your loss function; mean squared error (MSE) is meaningful for regression problems, while here you face a classification one (3-class), hence your loss function should be Cross Entropy (also called log-loss).

    For multi-class classification, sigmoid is also not advisable; so, at a high-level, here are some other code modifications advisable for your problem:

    • One-hot encode your 3 classes
    • Use softmax activation for your last layer, which should have 3 units (i.e. as many as the number of your classes)