I was trying to implement a simple neural network from scratch using python. This neural network has only two neurons and the task is to match the input to output. (i.e. x = 0 --> output = 0, x = 1 --> output = 1)
I have used partial derivatives and try to maximize negative loss using gradient ascent. (Complete code is shown below) Even after training for more than 10000 of iterations, the output is not good enough. (I think maybe the loss is stuck at a local maxima perhaps.) Can anyone help me figuring out what's wrong with my implementation.
import random
import numpy as np
import math
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def error(d,z):
return -0.5 * np.sum(np.power(d-z, 2))
# x = input
##x = np.random.choice((0,1),10000)
x = np.array([0, 1])
# y = desired output
d = np.copy(x)
# weights of two neurons
w = np.random.rand(2)
# now training using backprop
gradient = np.random.rand(2)
iterations = 800
rate = 5
k = 1
for i in xrange(1, iterations + 1):
y = sigmoid(w[0] * x)
z = sigmoid(w[1] * y)
gradient[0] = np.sum(z * w[1] * y * x * (d-z) * (1-y) * (1-z))
gradient[1] = np.sum(y * z * (d-z) * (1-z))
w[0] += gradient[0] * rate
w[1] += gradient[1] * rate
print "Iteration %d, Error %f, Change %f" % (i, error(d,z), ((gradient[0] * rate) ** 2 + (gradient[1] * rate) ** 2)**0.5)
change = ((gradient[0] * rate) ** 2 + (gradient[1] * rate) ** 2)**0.5
if change < 0.00001:
break
## now test
print "1",
x = 1
y = sigmoid(w[0]*x)
z = sigmoid(w[1]*y)
print z
print "0",
x = 0
y = sigmoid(w[0]*x)
z = sigmoid(w[1]*y)
print z