def import_and_test(): import nnpy print dir(nnpy) for a in dir(nnpy): print getattr(nnpy, a) try: print getattr(nnpy, a)() except TypeError: pass set1 = [([1, 1], [1]), ([1, 0], [0]), ([0, 1], [0]), ([0, 0], [1])] n = test_net([2, 10, 1], set1) for a in dir(n): print "n.%s: " % a print getattr(n, a) try: print getattr(n, a).__doc__ except AttributeError: pass print "trying 4 layer net" test_net([2,4,4,1], set1) set2 = [] for x in range(16): ins = (x >> 3, x >> 2 & 1, x >> 1 & 1, x & 1) outs = (sum(ins)>>1 &1, sum(ins)&1) set2.append((ins, outs)) #test_net([4,9,9,2], set2) net = test_net([4, 11, 2], set2) print "refcount is %s " % sys.getrefcount(net) net.save_weights('/tmp/test.net') net2 = nnpy.Network([4, 11, 2])#, 0.3, 0.3, 1.0) print "loading weights" net2.load_weights('/tmp/test.net') #print_results(set2, net2) print "trying 2 layer net" net = nnpy.Network((3, 2)) print "trying 1 layer net -- should fail" try: net = nnpy.Network((2,)) except ValueError, e: print e
def make_net(n_teams): net = nnpy.Network([ 16 + 16 * n_teams + config.EXTRA_INPUTS + config.FEEDBACK_NODES, config.LAYER2_NODES, config.OUTPUT_NODES + config.FEEDBACK_NODES ], bias=1) return net
def memtest(): from resource import getrusage, RUSAGE_SELF #x =[] for z in range(50): t = time.time() for y in range(100000): net = nnpy.Network([5, 13, 3], weight_range=(-1,1)) net.opinion((0,0,0,0,0)) #x.append(net) print getrusage(RUSAGE_SELF)[2:8], time.time() - t
def get_net(team): inputs = (team.inputs_per_type * (team.game.n_teams + 1) + config.EXTRA_INPUTS + config.FEEDBACK_NODES) outputs = len(team.directions) + team.fires + config.FEEDBACK_NODES if config.LAYER2_NODES: shape = [inputs, config.LAYER2_NODES, outputs] else: shape = [inputs, outputs] return nnpy.Network(shape, bias=1)
def test_net(dimensions, set, momentum=0.3, learn_rate=0.2, scale=1, r=(-0.5,0.5), count=10000,threshold=0.00001): import nnpy net = nnpy.Network(dimensions, momentum=momentum, learn_rate=learn_rate, scale=scale, weight_range=r) print "in test_net, refcount is %s " % sys.getrefcount(net) #net.randomise(r) net.learn_rate = 0.23 net.momentum = 0.321 print "set learn_rate and momentum " t = time.time() print "got time" net.train_set(set, count, threshold, 0) print "took %s seconds" % (time.time() - t) #print_results(set, net) w = net.get_weights() print "copied net via python:" net2 = nnpy.Network(dimensions) net2.set_weights(w) #print_results(set, net2) return net
def seed_nets(entropy): """seed the nnpy generator. once per game works for all nets""" random_holding_net = nnpy.Network([2, 3, 1]) random_holding_net.seed_random(entropy[1]) return random_holding_net