0

I am new to neural networks and I am using an example neural network I found online to attempt to approximate the sphere function(the addition of a set of numbers squared) using back propagation.

The initial code is:

class NeuralNetwork():
    def __init__(self):
        #Seed the random number generator, so it generates the same numbers
        #every time the program is run.
        #random.seed(1)

        #Model a single neuron, with 3 input connections and 1 output connection.
        #We assign random weights to a 3 x 1 matrix, with values in the range -1 to 1
        #and mean 0
        self.synaptic_weights = 2 * random.random((2,1)) - 1

    #The Sigmoid function, which describes an S shaped curve.
    #We pass the weighted sum of thle inputs through this function to
    #normalise them between 0 and 1.
    def __sigmoid(self, x):
        return 1 / (1 + exp(-x))


    #The derivative of the sigmoid function
    #This is the gradient of the sigmoid curve.
    #It indicates how confident we are about existing weight.
    def __sigmoid_derivative(self, x):
        return x * (1 -x)


    #Train the network through a process of trial and error.
    #Adjusting the synaptic weights each time.
    def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations):
        for iteration in xrange(10000):
            #Pass the training set through our neural network(a single neuron)
            output = self.think(training_set_inputs)

            #Calculate the error(Difference between the desired output and predicted output).
            error = training_set_outputs - output


            #Multiply the error by the input and again by the gradient of the Sigmoid curve.
            #This means less confident weights are adjusted more.
            #This means inputs, which are zero, do not cause changes to the weights.
            adjustment = dot(training_set_inputs.T, error * self.__sigmoid_derivative(output))

            #Adjust the weights
            self.synaptic_weights += adjustment

    #The neural network thinks.
    def think(self, inputs):
        #Pass inputs through our neural network(OUR SINGLE NEURON).
        return self.__sigmoid(dot(inputs, self.synaptic_weights))


if __name__ == "__main__":

    #Initialise a single neuron neural network.
    neural_network = NeuralNetwork()

    print"Random starting synaptic weights: "
    print neural_network.synaptic_weights

    #The training set. We have 4 examples, each consisting of 3 input values and 1 output value

    training_set_inputs = array([[0, 1], [1,0], [0,0]])
    training_set_outputs = array([[1,1,0]]).T

    #Train the neural network using a training set.
    #Do it 10,000 times and make small adjustments each time.
    neural_network.train(training_set_inputs, training_set_outputs, 10000)

    print "New synaptic weights after training: "
    print neural_network.synaptic_weights

    #Test the neural network with a new situation.
    print "Considering new situation [1,1] -> ?: "
    print neural_network.think(array([1,1]))

My aim is to input training data(the sphere function input and outputs) into the neural network to train it and meaningfully adjust the weights. After continuous training the weights should reach a point where reasonably accurate results are given from the training inputs.

I imagine an example of some training sets for the sphere function would be something like:

training_set_inputs = array([[2,1], [3,2], [4,6], [8,3]])
training_set_outputs = array([[5, 13, 52, 73]])

The example I found online can successfully approximate the XOR operation, but when given sphere function inputs it only gives me an output of 1 when tested on a new example(for example, [6,7] which should ideally return an approximation around 85)

From what I have read about neural networks I suspect this is because I need to normalize the inputs but I am not entirely sure how to do this. Any help on this or something to point me on the right track would be appreciated a lot, thank you.

hezzag
  • 23
  • 5
  • Am I reading this code correctly? Your NN has only a single hidden neuron? That is not enough to produce the results you want. You need to scale the topology (neurons and layers) to the information complexity of the job at hand. A single neuron with linear weights can handle only a linear relationship. – Prune Oct 28 '16 at 01:04
  • I assumed because XOR could be approximated then the network met the criteria for satisfying the universal approximation theorem, and thus the sphere function could also be approximated. In that case I will add more neurons and layers, would if also stil be meaningful to normalise input too? – hezzag Oct 28 '16 at 01:13
  • Normalizing input will not help in this case: your two inputs should have the same range and distribution. – Prune Oct 28 '16 at 16:36
  • XOR is a binary classification task with only three degrees of freedom in the input. The sphere function is a mapping RxR => R; it's more complex in information theory. – Prune Oct 28 '16 at 16:38

0 Answers0