def hybrDescent(Network, numIter, gamma, beta, delta, eta, eps, nhat, batchSize): j = 0 mu = 0 lastUpdate = 0 numBatches = Network.m/batchSize alpha = stepSize(mu, gamma, eta) while calcAccuracy(Network) < .95 and j < numIter: j += 1 randvec = range(Network.m) shuffle(randvec) oldInputWeight = Network.inputWeight.copy() oldHiddenWeight = Network.hiddenWeight.copy() gInput = np.zeros(Network.inputWeight.shape) hInput = np.zeros(Network.inputWeight.shape) gHidden = np.zeros(Network.hiddenWeight.shape) hHidden = np.zeros(Network.hiddenWeight.shape) for i in range(numBatches-1): obs = randvec[i*batchSize:(i+1)*batchSize] prop.calcDeriv(Network, Network.train[obs], obs) xi = 1/sum(np.power(mu,range(numBatches-i))) gInput += xi*Network.inputDeriv gHidden += xi*Network.hiddenDeriv hInput = mu*hInput + gInput hHidden = mu*hHidden + gHidden Network.inputWeight = oldInputWeight - alpha*hInput Network.hiddenWeight = oldHiddenWeight - alpha*hHidden lastUpdate += 1 if lastUpdate > nhat or np.linalg.norm(Network.inputWeight-oldInputWeight) < eps: mu = beta*mu + delta alpha = stepSize(mu, gamma, eta) lastUpdate = 0 print "Training set accuracy: %.3f" %calcAccuracy(Network)
def gradDescent(Network, numIter, alpha): j = 0 while calcAccuracy(Network) < .95 and j < numIter: j += 1 prop.calcDeriv(Network) Network.inputWeight -= alpha*Network.inputDeriv Network.hiddenWeight -= alpha*Network.hiddenDeriv print "Training set accuracy: %.3f" %calcAccuracy(Network)
def incrDescent(Network, numIter, alpha): j = 0 while calcAccuracy(Network) < .95 and j < numIter: j += 1 randvec = range(Network.m) shuffle(randvec) for i in randvec: prop.calcDeriv(Network, Network.train[i], i) Network.inputWeight -= alpha*Network.inputDeriv Network.hiddenWeight -= alpha*Network.hiddenDeriv print "Training set accuracy: %.3f" %calcAccuracy(Network)
def trainNetwork(self, method, *params): if method == 'GD': method = descent.gradDescent elif method == 'ID': method = descent.incrDescent else: method = descent.hybrDescent method(self, *params) if self.test is not None: print "Testing set accuracy" print utils.calcAccuracy(self, self.test, self.testDigits)
def genPOMDP(filename, reward, cost, gammas, numberOfWorkerPools): difficulties = getDifficulties(0.1) numDiffs = len(difficulties) #Add one absorbing state numberOfStates = ((numDiffs) * 2) + 1 numberOfActions = numberOfWorkerPools + 2 file = open(filename, 'w') file.write('discount: 0.9999\n') file.write('values: reward\n') file.write('states: %d\n' % numberOfStates) file.write('actions: %d\n' % numberOfActions) SUBMITZERO = numberOfWorkerPools SUBMITONE = numberOfWorkerPools + 1 file.write('observations: Zero One None\n') for i in range(0, numberOfStates): for k in range(0, numberOfWorkerPools): file.write('T: %d : %d : %d %f\n' % (k, i, i, 1.0)) #Add transitions to absorbing state file.write('T: %d : * : %d %f\n' % (SUBMITZERO, numberOfStates-1, 1.0)) file.write('T: %d : * : %d %f\n' % (SUBMITONE, numberOfStates-1, 1.0)) #Add observations in absorbing state file.write('O: * : %d : None %f\n' % (numberOfStates-1, 1.0)) for v in range(0, 2): for diffState in range(0,numDiffs): #for diffState in product(range(numDiffs), repeat = numberOfWorkerPools): state = v * numDiffs + diffState """for k in range(0, numberOfWorkerPools): state += (diffState[k] * (numDiffs ** (numberOfWorkerPools - (k+1))))""" file.write('O: %d: %d : None %f\n' % (SUBMITZERO, state, 1.0)) file.write('O: %d: %d : None %f\n' % (SUBMITONE, state, 1.0)) if v == 0: #if the answer is 0 for k in range(0, numberOfWorkerPools): file.write('O: %d : %d : Zero %f\n' % (k, state, calcAccuracy(gammas[k], difficulties[diffState]))) file.write('O: %d : %d : One %f\n' % (k, state, 1.0 - calcAccuracy(gammas[k], difficulties[diffState]))) else: # if the answer is 1 for k in range(0, numberOfWorkerPools): file.write('O: %d : %d : Zero %f\n' % (k, state, 1.0 - calcAccuracy(gammas[k], difficulties[diffState]))) file.write('O: %d : %d : One %f\n' % (k, state, calcAccuracy(gammas[k], difficulties[diffState]))) for v in range(0,numberOfWorkerPools): file.write('R: %d : * : * : * %f\n' % (v, -1 * cost[v])) for i in range(0, numberOfStates-1): if i < (numberOfStates-1) / 2: file.write('R: %d : %d : %d : * %f\n' % (SUBMITZERO, i, numberOfStates-1, 1)) file.write('R: %d : %d : %d : * %f\n' % (SUBMITONE, i, numberOfStates-1, reward)) else: file.write('R: %d : %d : %d : * %f\n' % (SUBMITONE, i, numberOfStates-1, 1)) file.write('R: %d : %d : %d : * %f\n' % (SUBMITZERO, i, numberOfStates-1, reward)) #Add rewards in absorbing state file.write('R: * : %d : %d : * %f\n' % (numberOfStates-1, numberOfStates-1, 0)) file.close()