python - Neural Network not learning for any value of momentum, learning rate etc -
i'm building neural network play noughts , crosses (tic-tac-toe). takes in board configuration (e.g. [0, -1, 1, 1, 0, 1, 0, 0, -1] -1 opponent , 1 itself) , outputs optimum move on board, e.g. [0, 0, 0, 0, 0, 1, 0, 0, 0]. i'm doing demonstration on backpropagation in few days, need nn able @ least draw.
however, doesn't appear willing learn. rate of error decrease slows down extremely quickly, sticking @ same error. run training 10,000 times, seems happen regardless of iterations.
the higher learning rate (i've tried 0.01, 0.05, 0.1, 0.5, 0.9, 2), lower error gets stuck at, output still not related @ desired output.
changing momentum (i've tried 0.01, 0.1, 0.5, 0.9) makes absolutely no difference change in rate of error or correctness of output.
changing number of hidden nodes (i've tried 4, 5, 9, 10) same above: no change.
class network(object) : def __init__(self, input, desired, hiddennodes) : self.input = input self.desired = desired self.hiddenlayer = np.zeros(hiddennodes,) self.weights0 = np.random.random((9,hiddennodes)) self.weights1 = np.random.random((hiddennodes,9)) self.output = np.zeros(hiddennodes,) self.result = 0 def train(self, epsilon, outmom, hidmom) : momentum = 0.9 # forward propagation dotsum1 = self.input.dot(self.weights0) activateddotsum1 = nonlin(dotsum1) dotsum2 = activateddotsum1.dot(self.weights1) result = nonlin(dotsum2) self.result = result #calculate change in weights second set of weights outputlayererror = self.desired - result requiredchangeinoutput = nonlin(result, deriv=true).dot(outputlayererror) changeinweights1 = result.dot(requiredchangeinoutput) #calculate change in weights first set of weights requiredchangeinhidden = requiredchangeinoutput * (self.weights1) * nonlin(dotsum2, deriv=true) changeinweights0 = self.input * (requiredchangeinhidden) if % 100 == 0: #print error, see how it's decreasing print "error " + str(np.mean(np.abs(outputlayererror))) #update weights self.weights1 += epsilon * (changeinweights1 + outmom) self.weights0 += epsilon * (changeinweights0.t + hidmom) #update momentum outmom += requiredchangeinoutput * momentum hidmom += requiredchangeinhidden * momentum #run training outputlayer_mom = 0 hiddenlayer1_mom = 0 network = network(np.array([0, -1, 1, 1, 0, 1, 0, 0, -1]), np.array([0, 0 ,1 ,0, 0, 0, 0, 0, 1]), 5) in range(0, 10000) : network.train(0.1, outputlayer_mom, hiddenlayer1_mom) print network.result
i'm lost cause. when error didn't converge thought had got stuck in local minima , proper momentum + learning rate solve problem, seem have been wrong. can't see anywhere i've gone wrong in backpropagation. i'm using sigmoid activation function both hidden , output - why?
Comments
Post a Comment