forward_propagation function with digit classification does not work in Python code

Falcon Source

I have the following python code to classify digit numbers from MNIST

import numpy as np
from numpy import array, dot
import pandas as pd


def initialize_network():
    input_neurons = 784
    hidden_neurons = input_neurons+1
    output_neurons = 10    
    n_hidden_layers = 1

    net = list()

    for h in range(n_hidden_layers):
        if h!=0:
            input_neurons = len(net[-1])
        hidden_layer = [ {'weights': np.random.uniform(size=input_neurons)} for i in range(hidden_neurons)]
        net.append(hidden_layer)


    output_layer = [ { 'weights': np.random.uniform(size=hidden_neurons)} for i in range(output_neurons)]
    net.append(output_layer)


    return net


############################################################


def forward_propagation(net, input):
    row = input
    for layer in net:
        prev_input = np.array([])
        for neuron in layer:
            sum = neuron['weights'].T.dot(row)
            result = activate_sigmoid(sum)
            neuron['result'] = result
            prev_input = np.append(prev_input,[result])

        row = prev_input

    return row


#############################################################

def activate_sigmoid(sum):
    return (1/(1+np.exp(-sum)))


#############################################################

def back_propagation(net,row,expected):
    for i in reversed(range(len(net))):
        layer = net[i]
        errors = np.array([])
        if i == len(net)-1:
            results = [ neuron['result'] for neuron in layer]
            errors = expected - np.array(results)
        else:
            for j in range(len(layer)):
                herror = 0
                nextlayer = net[i+1]
                for neuron in nextlayer:
                    herror += ( neuron['weights'][j]*neuron['delta'])
                errors = np.append(errors , [herror])


        for j in range(len(layer)):
            neuron = layer[j]
            neuron['delta'] = errors[j]*sigmoidDerivative(neuron['result'])

##############################################################

def sigmoidDerivative(output):
    return output * (1.0 - output)


##############################################################

def updateWeights(net, input , lrate):
    for i in range(len(net)):
        inputs = input
        if i!=0:
            inputs = [neuron['result'] for neuron in net[i-1]]

        for neuron in net[i]:
            for j in range(len(inputs)):
                neuron['weights'][j] += lrate*neuron['delta']*inputs[j]

###############################################################

def training(net , epochs , lrate , n_outputs):
    errors = []
    for epoch in range(epochs):
        sum_error = 0
        for i,row in enumerate(X):
            outputs = forward_propagation(net , row)

            print(outputs)
            expected = [0.0 for i in range(n_outputs)]
            expected[y[i]] = 1

            sum_error += sum([(expected[j]-outputs[j])**2 for j in range(len(expected))])
            back_propagation(net,row,expected)
            updateWeights(net, row, lrate)

        if epoch%500 == 0:
            print('>epoch=%d, error=%.3f'%(epoch,sum_error))
            errors.append(sum_error)

    return errors


#################################################################

def predict(network,row):
    outputs = forward_propagation(net,row)
    return outputs




########################################################################   






data = pd.read_csv("dataset/train.csv").as_matrix()

#convert data to be 0 or 1
for i in range(len(data)):
    for j in range(1,len(data[i])):
        if data[i][j] > 50:
            data[i][j] = 1
        else:
            data[i][j] = 0 



X = data[0:1000 , 1:785]
y = data[0:1000, 0]




net = initialize_network()
errors = training(net , 10000 , 0.05 ,10)




pred = predict(net,[0,1])
output = np.argmax(pred)
print(output)

The problem with this code that forward_propagation function returns [1,1,1,1,1,1,1,1,1,1] for each instance. I applied the same code for XOR and it works fine. Any help would appreciable.

I would like to mention that I do not want to use any more libraries.

python-3.xneural-network

Answers

comments powered by Disqus