def find_a_network(): # TODO notes from thesis update # # Look at and implement the quasi-random initialization stuff # from pga's paper. # # Try training networks on other chaotic functions # In particular try mu * ( x * (1 - x) )^(1/2) # pga -- it looks like the function that my network learned # # Try finding chaotic autonomous RNNs using my GA. # K = 80 pat = generate_pattern(200) network1 = NN(2, K, 1) d = shelve.open('shelved.networks.db') key = str(int(time.time())) d[key] = network1 d.close() print "Shelved pre-training under the key", key network1.train(pat, True, 0.010) print "Done training." d = shelve.open('shelved.networks.db') key = str(int(time.time())) d[key] = network1 d.close() print "Shelved post-training under the key", key all_diagrams(lambda x : network1.update([x,1]),f)
def main(): print 'Building neural network...' net = NN(4, 52, 26) print 'Training neural network' count = 0 traindata = [] for filename in listdir(CAPTCHA_DIR): if count % 100 == 0: print count count += 1 chdir(CAPTCHA_DIR) proc = Popen(['../split', filename, '../out.jpg', '1', '0', '210'], stdout=PIPE) vectors = proc.stdout.read() proc.wait() if proc.returncode != 0: continue for line in vectors.split('\n'): line = [x for x in line.split(' ') if x] if not line: continue letter = ord(line[0].lower()) - ord('a') line = line[1:] line = [int(x) for x in line] letters = [] for i in xrange(26): if i == letter: letters.append(100) else: letters.append(0) traindata.append((line, letters)) net.train(traindata, iterations=10) print 'Finished training network... Saving' network = { 'ni': net.ni, 'nh': net.nh, 'no': net.no, 'ai': net.ai, 'ah': net.ah, 'ao': net.ao, 'wi': net.wi, 'wo': net.wo, 'ci': net.ci, 'co': net.co } json.dump(network, file('../network2.json', 'w')) print 'Finished dumping to network2.json'
def train(self, session, doc): # modified bpnn to accept dict as sparse input (labels, vectors) = doc.get_raw(session) (nattrs, vectors) = self.renumber_train(session, vectors) labelSet = set(labels) lls = len(labelSet) if lls == 2: patts = [(vectors[x], [labels[x]]) for x in xrange(len(labels))] noutput = 1 else: if lls < 5: templ = ((0, 0), (1, 0), (0, 1), (1, 1)) elif lls < 9: templ = ((0, 0, 0), (1, 0, 0), (0, 1, 0), (1, 1, 0), (0, 0, 1), (1, 0, 1), (0, 1, 1), (1, 1, 1)) else: # hopefully not more than 16 classes! templ = ((0, 0, 0, 0), (1, 0, 0, 0), (0, 1, 0, 0), (1, 1, 0, 0), (0, 0, 1, 0), (1, 0, 1, 0), (0, 1, 1, 0), (1, 1, 1, 0), (0, 0, 0, 1), (1, 0, 0, 1), (0, 1, 0, 1), (1, 1, 0, 1), (0, 0, 1, 1), (1, 0, 1, 1), (0, 1, 1, 1), (1, 1, 1, 1)) rll = range(len(labels)) patts = [(vectors[x], templ[labels[x]]) for x in rll] noutput = len(templ[0]) # shuffle to ensure not all of class together r = random.Random() r.shuffle(patts) # only way this is at all usable is with small datasets run in psyco # but is at least fun to play with... if maxattr * len(labels) > 2000000: print "Training NN is going to take a LONG time..." print "Make sure you have psyco enabled..." n = NN(maxattr, self.hidden, noutput) self.model = n n.train(patts, self.iterations, self.learning, self.momentum) modStr = cPickle.dumps(n) f = file(mp, 'w') f.write(modStr) f.close() self.predicting = 1
def train(self, session, doc): # modified bpnn to accept dict as sparse input (labels, vectors) = doc.get_raw(session) (nattrs, vectors) = self.renumber_train(session, vectors) labelSet = set(labels) lls = len(labelSet) if lls == 2: patts = [(vectors[x], [labels[x]]) for x in xrange(len(labels))] noutput = 1 else: if lls < 5: templ = ((0, 0), (1,0), (0, 1), (1, 1)) elif lls < 9: templ = ((0, 0, 0), (1, 0, 0), (0, 1, 0), (1, 1, 0), (0, 0, 1), (1, 0, 1), (0, 1, 1), (1, 1, 1)) else: # hopefully not more than 16 classes! templ = ((0,0,0,0), (1,0,0,0), (0,1,0,0), (1,1,0,0), (0,0,1,0), (1,0,1,0), (0,1,1,0), (1,1,1,0), (0,0,0,1), (1,0,0,1), (0,1,0,1), (1,1,0,1), (0,0,1,1), (1,0,1,1), (0,1,1,1), (1,1,1,1)) rll = range(len(labels)) patts = [(vectors[x], templ[labels[x]]) for x in rll] noutput = len(templ[0]) # shuffle to ensure not all of class together r = random.Random() r.shuffle(patts) # only way this is at all usable is with small datasets run in psyco # but is at least fun to play with... if maxattr * len(labels) > 2000000: print "Training NN is going to take a LONG time..." print "Make sure you have psyco enabled..." n = NN(maxattr, self.hidden, noutput) self.model = n n.train(patts, self.iterations, self.learning, self.momentum) modStr = cPickle.dumps(n) f = file(mp, 'w') f.write(modStr) f.close() self.predicting = 1
def load(self, N=.03, M=.007, iterations=5000): ''' Trains the neural network with match statistics for the current heroid ''' patterns = self.data[self.heroid][:300] print("n = %d" % len(patterns)) NN.train(self, patterns, iterations, N, M)