def nn_controller(filename, activation, keras=False):
    """
    Return the network controller function
    """
    if not keras:
        filename = 'nn_retrained/' + filename
        # Obtain the trained parameters and assign the value to res
        with open(filename) as inputfile:
            lines = inputfile.readlines()
        length = len(lines)
        res = np.zeros(length)
        for i, text in enumerate(lines):
            res[i] = eval(text)

        # Set the controller
        NN_controller = NN(res, activation)
        controller = NN_controller.controller
    else:
        # load json and create model
        json_file = open('model.json', 'r')
        loaded_model_json = json_file.read()
        json_file.close()
        loaded_model = model_from_json(loaded_model_json)
        # load weights into new model
        loaded_model.load_weights('model.h5')
        print("Loaded kera model from disk.")
        NN_controller = NN(keras=True, model=loaded_model)
        controller = NN_controller.keras_model
    return controller
class testactivate(unittest.TestCase):
    def setUp(self):
        weights_in = -2 + np.random.rand(NN.ni, NN.nh) * 4
        weights_out = -2 + np.random.rand(NN.nh, NN.no) * 4
        self.net1 = NN()
        self.net1.wi = weights_in.copy()
        self.net1.wo = weights_out.copy()
        self.net2 = NN()
        self.net2.wi = weights_in.copy()
        self.net2.wo = weights_out.copy()
        inputs = -2 + np.random.rand(4) * 8
        for i in range(NN.ni):
            self.net1.ai[i] = np.tanh(inputs[i])
        for j in range(NN.nh):
            self.net1.ah[j] = sigmoid(
                sum([
                    self.net1.ai[i] * self.net1.wi[i][j] for i in range(NN.ni)
                ]))
        for k in range(NN.no):
            self.net1.ao[k] = sum(
                [self.net1.ah[j] * self.net1.wo[j][k] for j in range(NN.nh)])
        self.net1.ao = np.where(self.net1.ao > 0.5, 1.0, 0.0)
        self.net2.activate(inputs)

    def testactivationfunctions(self):
        npt.assert_array_equal(self.net1.ai, self.net2.ai)
        npt.assert_array_equal(self.net1.ah, self.net2.ah)
        npt.assert_array_equal(self.net1.ao, self.net2.ao)

    def testOutput(self):
        self.assertEqual(bincount(self.net2.ao.astype('int'))[0], 2)
        self.assertEqual(bincount(self.net2.ao.astype('int'))[1], 1)
class testactivate(unittest.TestCase):
	def setUp(self):
		weights_in = -2 + np.random.rand(NN.ni, NN.nh) * 4
		weights_out = -2 + np.random.rand(NN.nh, NN.no) * 4
		self.net1 = NN()
		self.net1.wi = weights_in.copy()
		self.net1.wo = weights_out.copy()
		self.net2 = NN()
		self.net2.wi = weights_in.copy()
		self.net2.wo = weights_out.copy()
		inputs = -2 + np.random.rand(4) * 8
		for i in range(NN.ni):
			self.net1.ai[i] = np.tanh(inputs[i])
		for j in range(NN.nh):
			self.net1.ah[j] = sigmoid(sum([self.net1.ai[i] * self.net1.wi[i][j] for i in range(NN.ni)]))
		for k in range(NN.no):
			self.net1.ao[k] = sum([self.net1.ah[j] * self.net1.wo[j][k] for j in range(NN.nh)])
		self.net1.ao = np.where(self.net1.ao > 0.5, 1.0, 0.0)
		self.net2.activate(inputs)

	def testactivationfunctions(self):
		npt.assert_array_equal(self.net1.ai,
			self.net2.ai)
		npt.assert_array_equal(self.net1.ah,
			self.net2.ah)
		npt.assert_array_equal(self.net1.ao,
			self.net2.ao)

	def testOutput(self):
		self.assertEqual(bincount(self.net2.ao.astype('int'))[0],
			2)
		self.assertEqual(bincount(self.net2.ao.astype('int'))[1],
			1)
Ejemplo n.º 4
0
class testUpdate(unittest.TestCase):
    def setUp(self):
        self.nn = NN()
        self.in_to_hidden = self.nn.wi
        self.hidAstroL = AstrocyteLayer(self.nn.ah, self.in_to_hidden)

    def testUpdateNeuronCounters(self):
        self.hidAstroL.neur_activs = linspace(-1, 1, NN.nh)
        self.hidAstroL.updateNeuronCounters()
        npt.assert_array_equal(self.hidAstroL.neur_counters,
                               array([-1, -1, -1, 1, 1, 1]))

    def testUpdateAstrocyteActivations(self):
        result = map(self.hidAstroL._checkIfAstroActive,
                     map(int, linspace(-3, 3, 8)))
        trgt = array([-1, -1, -1, 0, 0, 1, 1, 1])
        npt.assert_array_equal(result, trgt)

    def testPerformAstrocyteActions(self):
        self.hidAstroL.neur_in_ws[:] = ones(24).reshape(NN.ni, NN.nh)
        """Assuming we are at the fourth iter of minor iters and that astro 
        parameters are reset after each input pattern, neur counters can only 
        be in [4, -4, 2, -2, 0]"""
        self.hidAstroL.neur_counters[:] = array([4, -4, 2, -2, 0, 0])
        self.hidAstroL.remaining_active_durs[:] = array([3, -3, 2, -2, 0, 0])
        self.hidAstroL.astro_statuses[:] = array([1, -1, 1, -1, 0, 0])
        self.hidAstroL.performAstroActions()
        target_weights = empty([NN.ni, NN.nh])
        target_weights[:, 0] = 1.25
        target_weights[:, 1] = .5
        target_weights[:, 2] = 1.25
        target_weights[:, 3] = .5
        target_weights[:, 4] = 1.
        target_weights[:, 5] = 1.
        npt.assert_array_equal(self.in_to_hidden, target_weights)
        target_activations = array([1, -1, 1, -1, 0, 0])
        npt.assert_array_equal(target_activations,
                               self.hidAstroL.astro_statuses)
        target_remaining_active_durs = array([2, -2, 1, -1, 0, 0])
        npt.assert_array_equal(target_remaining_active_durs,
                               self.hidAstroL.remaining_active_durs)

    def testObjectActivationsAreUpdated(self):
        self.nn.activate(rand(4) * 4)
        a = self.hidAstroL.neur_in_ws
        npt.assert_array_equal(self.in_to_hidden, a)

    def testObjectWeightsAreUpdated(self):
        # don't understand this test don't think i need it
        # it is a bit random lol
        target = empty([NN.ni, NN.nh])
        for j in range(len(self.nn.ah)):
            self.hidAstroL.neur_in_ws[:, j] = 10**j
            target[:, j] = 10**j
        npt.assert_array_equal(self.in_to_hidden, self.hidAstroL.neur_in_ws)
        npt.assert_array_equal(self.in_to_hidden, target)
Ejemplo n.º 5
0
def main ():
  """
  Fresh population of NN individuals are evalutated, paired with 
  their fitness scores and ordered by fitness in descending order (fittest first)
  """
  # Rank first random population
  pop = createPop()
  pairedPop = pairPop(pop)
  # ordered by fitness in descending order
  rankedPop = sorted(pairedPop, key=itemgetter(-1), reverse=True) 
  # tst.correctlyranked(rankedPop)
  # Keep iterating new pops until max_iterations
  iters = 0
  tops, avgs = [], []
  while iters != max_iterations:
    if iters%1 == 0:
      print 'Iteration'.rjust(150), iters
    newpopW = evolveNewPop(rankedPop)
    rankedPop, toperr, avgerr = rankPop(newpopW,pop)
    tops.append(toperr)
    avgs.append(avgerr)
    iters+=1
  
  # test a NN with the fittest weights
  tester = NN ()
  fittestWeights = [ x[0] for x in rankedPop ]
  tester.assignWeights(fittestWeights, 0)
  results, targets = tester.test(testpat)
  x = np.arange(0,150)
  title2 = 'Test after '+str(iters)+' iterations'
  plt.title(title2)
  plt.ylabel('Node output')
  plt.xlabel('Instances')
  plt.plot(results, 'xr', linewidth=1.5, label='Results')
  plt.plot(targets, 's', color='black', linewidth=3, label='Targets')
  plt.legend(loc='lower right')

  plt.figure(2)
  plt.subplot(121)
  plt.title('Top individual error evolution')
  plt.ylabel('Inverse error')
  plt.xlabel('Iterations')
  plt.plot( tops, '-g', linewidth=1)
  plt.subplot(122)
  plt.plot( avgs, '-g', linewidth=1)
  plt.title('Population average error evolution')
  plt.ylabel('Inverse error')
  plt.xlabel('Iterations')
  
  plt.show()
  
  print 'max_iterations',max_iterations,'\tpop_size', \
      pop_size,'pop_size*0.15', \
      int(pop_size*0.15),'\tmutation_rate', \
      NN.mutation_rate,'crossover_rate', \
      NN.crossover_rate,'ni, nh, no', NN.ni, NN.nh, NN.no
class testUpdate(unittest.TestCase):
    def setUp(self):
        self.nn = NN()
        self.in_to_hidden = self.nn.wi
        self.hidAstroL = AstrocyteLayer(self.nn.ah, self.in_to_hidden)

    def testUpdateNeuronCounters(self):
        self.hidAstroL.neur_activs = linspace(-1, 1, NN.nh)
        self.hidAstroL.updateNeuronCounters()
        npt.assert_array_equal(self.hidAstroL.neur_counters,
            array([-1, -1, -1, 1, 1, 1]))

    def testUpdateAstrocyteActivations(self):
        result = map(self.hidAstroL._checkIfAstroActive, 
            map(int, linspace(-3, 3, 8)))
        trgt = array([-1, -1, -1, 0, 0, 1, 1, 1])
        npt.assert_array_equal(result, trgt)

    def testPerformAstrocyteActions(self):
        self.hidAstroL.neur_in_ws[:] = ones(24).reshape(NN.ni, NN.nh)
        """Assuming we are at the fourth iter of minor iters and that astro 
        parameters are reset after each input pattern, neur counters can only 
        be in [4, -4, 2, -2, 0]"""
        self.hidAstroL.neur_counters[:] = array([4, -4, 2, -2, 0, 0])
        self.hidAstroL.remaining_active_durs[:] = array([3, -3, 2, -2, 0, 0])
        self.hidAstroL.astro_statuses[:] = array([1, -1, 1, -1, 0, 0])
        self.hidAstroL.performAstroActions()
        target_weights = empty([NN.ni, NN.nh])
        target_weights[:,0] = 1.25
        target_weights[:,1] = .5
        target_weights[:,2] = 1.25
        target_weights[:,3] = .5
        target_weights[:,4] = 1.
        target_weights[:,5] = 1.
        npt.assert_array_equal(self.in_to_hidden, target_weights)
        target_activations = array([1, -1, 1, -1, 0, 0])
        npt.assert_array_equal(target_activations, self.hidAstroL.astro_statuses)
        target_remaining_active_durs = array([2, -2, 1, -1, 0, 0])
        npt.assert_array_equal(target_remaining_active_durs, 
            self.hidAstroL.remaining_active_durs)

    def testObjectActivationsAreUpdated(self):
        self.nn.activate(rand(4)*4)
        a = self.hidAstroL.neur_in_ws
        npt.assert_array_equal(self.in_to_hidden, a) 

    def testObjectWeightsAreUpdated(self):
        # don't understand this test don't think i need it
        # it is a bit random lol
        target = empty([NN.ni, NN.nh])  
        for j in range(len(self.nn.ah)):
            self.hidAstroL.neur_in_ws[:,j] = 10 ** j
            target[:,j] = 10 ** j
        npt.assert_array_equal(self.in_to_hidden, self.hidAstroL.neur_in_ws)
        npt.assert_array_equal(self.in_to_hidden, target)
Ejemplo n.º 7
0
def main():
    """
    Fresh population of NN individuals are evalutated, paired with 
    their fitness scores and ordered by fitness in descending order (fittest first)
    """
    # Rank first random population
    pop = createPop()
    pairedPop = pairPop(pop)
    # ordered by fitness in descending order
    rankedPop = sorted(pairedPop, key=itemgetter(-1), reverse=True)
    # tst.correctlyranked(rankedPop)
    # Keep iterating new pops until max_iterations
    iters = 0
    tops, avgs = [], []
    while iters != max_iterations:
        if iters % 1 == 0:
            print 'Iteration'.rjust(150), iters
        newpopW = evolveNewPop(rankedPop)
        rankedPop, toperr, avgerr = rankPop(newpopW, pop)
        tops.append(toperr)
        avgs.append(avgerr)
        iters += 1

    # test a NN with the fittest weights
    tester = NN()
    fittestWeights = rankedPop[0][0]
    tester.assignWeights(fittestWeights)
    results, targets = tester.test(testpat)
    title2 = 'Test after ' + str(iters) + ' iterations'
    plt.title(title2)
    plt.ylabel('Node output')
    plt.xlabel('Instances')
    plt.plot(results, 'xr', linewidth=1.5, label='Results')
    plt.plot(targets, 's', color='black', linewidth=3, label='Targets')
    plt.legend(loc='lower right')

    plt.figure(2)
    plt.subplot(121)
    plt.title('Top individual error evolution')
    plt.ylabel('Inverse error')
    plt.xlabel('Iterations')
    plt.plot(tops, '-g', linewidth=1)
    plt.subplot(122)
    plt.plot(avgs, '-g', linewidth=1)
    plt.title('Population average error evolution')
    plt.ylabel('Inverse error')
    plt.xlabel('Iterations')

    plt.show()

    print 'max_iterations',max_iterations,'\tpop_size', \
        NN.pop_size,'pop_size*0.15', \
        int(NN.pop_size*0.15),'\tmutation_rate', \
        NN.mutation_rate,'crossover_rate', \
        NN.crossover_rate,'ni, nh, no', NN.ni, NN.nh, NN.no
Ejemplo n.º 8
0
def testFittestInd(rankedPop):
    tester = NN ()
    fittestWeights = rankedPop[0][0]
    tester.assignWeights(fittestWeights)
    count = 0
    for i, t in tester.test_pat:
        o = tester.activate(i)
        print i, o, t, '++' if list(o) == list(t) else '----'
        count += 1 if list(o) == list(t) else -1
    print float(count) / len(tester.test_pat)
    err, per = tester.sumErrors(test_data=True)
    print per
    return err, per
Ejemplo n.º 9
0
def testFittestInd(rankedPop):
    tester = NN ()
    fittestWeights = rankedPop[0][0]
    tester.assignWeights(fittestWeights)
    right = 0
    for i, t, c, l in tester.test_pat:
        o = tester.activate(i)
        print i, o, t, '++' if list(o) == list(t) else '----'
        right += 1 if list(o) == list(t) else 0
    print right, len(tester.test_pat)
    print 100 * (float(right) / len(tester.test_pat))
    err, per = tester.sumErrors(test_data=True)
    print per
    return err, per
Ejemplo n.º 10
0
	def testWeightsAndActivationsEquivalent(self):
		pyb_ws = self.net.params
		nn = NN()
		nn.wi = pyb_ws[:nn.wi.size].reshape(NN.nh, NN.ni).T
		nn.wo = pyb_ws[nn.wi.size:].reshape(NN.no, NN.nh).T
		for i, x in enumerate(self.trn_d['input']):
			nn.activate(x)
			out = self.net.activate(x)
			npt.assert_array_equal(nn.ai, self.net['in'].outputbuffer[0])
			# self.assertItemsEqual(list(nn.ah), list(self.net['hidden0'].outputbuffer[0]))
			for j, pb_ah in enumerate(self.net['hidden0'].outputbuffer[0]):
				self.assertAlmostEqual(nn.ah[j], pb_ah)
			for k, pb_ao in enumerate(out):
				self.assertAlmostEqual(nn.ao[k], pb_ao)
Ejemplo n.º 11
0
def main(_):
    pp.pprint(flags.FLAGS.__flags)

    #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
    run_config = tf.ConfigProto()
    #run_config.gpu_options.allow_growth=True
    
    
    # Data initialisation
    data_path = os.path.join(FLAGS.data_dir, FLAGS.dataset, FLAGS.input_fname_pattern)
    data = ImageDataset(regex_file = data_path, input_height = FLAGS.input_height, input_width = FLAGS.input_width)
       
    with tf.Session() as sess :
        
        net = NN(FLAGS.input_height*FLAGS.input_width*3, 2, hidden_layers =  [60,10, 30],
                 learning_rate = FLAGS.learning_rate, build = True, name = 'FCNeuralNet')
        model = Classifier1D(sess, [net] ,dataset,
                             checkpoint_dir= FLAGS.checkpoint_dir,
                             model_name = FLAGS.model_name)
        
        old, step = model.load(FLAGS.checkpoint_dir)
        print("(*) Already train : {} {}".format(old, ("--> step "+str(step) if old else "")))
        
        if FLAGS.train:
            model.train(FLAGS)
        else:
            if not model.load(FLAGS.checkpoint_dir)[0]:
                raise Exception("(!) Train a model first, then run test mode")
Ejemplo n.º 12
0
 def testPercentErrorIsSame(self):
     NN.pat = zip(self.trn_d['input'], self.trn_d['target'])
     pyb_ws = self.net.params.copy()
     nn = NN()
     nn.wi = pyb_ws[:nn.wi.size].reshape(NN.nh, NN.ni).T
     nn.wo = pyb_ws[nn.wi.size:].reshape(NN.no, NN.nh).T
     correct = 0
     wrong = 0
     argmax_cor = 0
     argmax_wng = 0
     all_aos = []
     for i, x in enumerate(self.trn_d['input']):
         nn.activate(x)
         out = self.net.activate(x)
         # print 'ga bp trg', nn.ao, out, self.trn_d['target'][i], '++++' if not (out - self.trn_d['target'][i]).any() else '-'
         all_aos.append(nn.ao.copy())
         if not (out - self.trn_d['target'][i]).any():
             correct += 1
         else:
             wrong += 1
         if argmax(out) == argmax(self.trn_d['target'][i]):
             argmax_cor += 1
         else:
             argmax_wng += 1
     print 'actual', wrong, 'wrong', correct, 'correct', float(wrong) / (
         wrong + correct) * 100
     print 'using argmax', argmax_wng, 'wrong', argmax_cor, 'correct', float(
         argmax_wng) / (argmax_wng + argmax_cor) * 100
     argmax_perc_err = float(argmax_wng) / (argmax_wng + argmax_cor) * 100
     res = nn.sumErrors()
     nn_perc_err = 100 - res[1]
     pb_nn_perc_err = percentError(self.trainer.testOnClassData(),
                                   self.trn_d['class'])
     self.assertAlmostEqual(nn_perc_err, pb_nn_perc_err)
     self.assertAlmostEqual(nn_perc_err, pb_nn_perc_err, argmax_perc_err)
Ejemplo n.º 13
0
 def objective(x, v):
     controller = NN(x).controller
     trackers = []
     for i in range(N_TRAIN_DIRECTIONS):
         for traj in paths:
             trackers.append(
                 [x, v, i * 2 * pi / N_TRAIN_DIRECTIONS, traj])
     costs = pool.map(function_x, trackers)
     return max(costs)
	def setUp(self):
		weights_in = -2 + np.random.rand(NN.ni, NN.nh) * 4
		weights_out = -2 + np.random.rand(NN.nh, NN.no) * 4
		self.net1 = NN()
		self.net1.wi = weights_in.copy()
		self.net1.wo = weights_out.copy()
		self.net2 = NN()
		self.net2.wi = weights_in.copy()
		self.net2.wo = weights_out.copy()
		inputs = -2 + np.random.rand(4) * 8
		for i in range(NN.ni):
			self.net1.ai[i] = np.tanh(inputs[i])
		for j in range(NN.nh):
			self.net1.ah[j] = sigmoid(sum([self.net1.ai[i] * self.net1.wi[i][j] for i in range(NN.ni)]))
		for k in range(NN.no):
			self.net1.ao[k] = sum([self.net1.ah[j] * self.net1.wo[j][k] for j in range(NN.nh)])
		self.net1.ao = np.where(self.net1.ao > 0.5, 1.0, 0.0)
		self.net2.activate(inputs)
Ejemplo n.º 15
0
class testactivate(unittest.TestCase):
	def setUp(self):
		self.nn = NN()
		self.inputs = [1, 2, 3, 4]
		self.nn.activate(self.inputs)

	def testInputActivation(self):
		targets = self.nn.activate_input(self.inputs)
		npt.assert_array_equal(self.nn.in_activations,
			targets)

	def testHiddenActivation(self):
		# targets = NN.activate_hidden([1, 2, 3, 4])
		# npt.assert_array_equal(self.nn.in_activations,
		# 	targets)
		pass

	def testShapesUnchanged(self):
		pass
Ejemplo n.º 16
0
	def testPercentErrorIsSame(self):
		NN.pat = zip(self.trn_d['input'], self.trn_d['target'])		
		pyb_ws = self.net.params.copy()
		nn = NN()
		nn.wi = pyb_ws[:nn.wi.size].reshape(NN.nh, NN.ni).T
		nn.wo = pyb_ws[nn.wi.size:].reshape(NN.no, NN.nh).T
		correct = 0
		wrong = 0
		argmax_cor = 0
		argmax_wng = 0
		all_aos = []
		for i, x in enumerate(self.trn_d['input']):
			nn.activate(x)
			out = self.net.activate(x)
			# print 'ga bp trg', nn.ao, out, self.trn_d['target'][i], '++++' if not (out - self.trn_d['target'][i]).any() else '-'
			all_aos.append(nn.ao.copy())
			if not (out - self.trn_d['target'][i]).any():
				correct += 1
			else:
				wrong += 1
			if argmax(out) == argmax(self.trn_d['target'][i]):
				argmax_cor += 1
			else:
				argmax_wng += 1
		print 'actual', wrong, 'wrong', correct, 'correct', float(wrong) / (wrong + correct) * 100
		print 'using argmax', argmax_wng, 'wrong', argmax_cor, 'correct', float(argmax_wng) / (argmax_wng + argmax_cor) * 100
		argmax_perc_err = float(argmax_wng) / (argmax_wng + argmax_cor) * 100
		res = nn.sumErrors()
		nn_perc_err = 100 - res[1]
		pb_nn_perc_err = percentError(self.trainer.testOnClassData(), self.trn_d['class'])
		self.assertAlmostEqual(nn_perc_err, pb_nn_perc_err)
		self.assertAlmostEqual(nn_perc_err, pb_nn_perc_err, argmax_perc_err)
Ejemplo n.º 17
0
 def testDataAssignedCorrectly(self):
     NN.pat = zip(self.trn_d['input'], self.trn_d['target'])
     pyb_ws = self.net.params.copy()
     nn = NN()
     nn.wi = pyb_ws[:nn.wi.size].reshape(NN.nh, NN.ni).T
     nn.wo = pyb_ws[nn.wi.size:].reshape(NN.no, NN.nh).T
     correct = 0
     wrong = 0
     all_aos = []
     for i, x in enumerate(self.trn_d['input']):
         nn.activate(x)
         out = self.net.activate(x)
         all_aos.append(nn.ao)
         if not (out - self.trn_d['target'][i]).any():
             correct += 1
         else:
             wrong += 1
     for i in range(len(array(NN.pat)[:, 0])):
         npt.assert_array_equal(self.trn_d['input'][i],
                                array(NN.pat)[:, 0][i])
         npt.assert_array_equal(self.trn_d['input'][i],
                                array(nn.pat)[:, 0][i])
         npt.assert_array_equal(self.trn_d['target'][i],
                                array(NN.pat)[:, 1][i])
         npt.assert_array_equal(self.trn_d['target'][i],
                                array(nn.pat)[:, 1][i])
Ejemplo n.º 18
0
	def testDataAssignedCorrectly(self):
		NN.pat = zip(self.trn_d['input'], self.trn_d['target'])		
		pyb_ws = self.net.params.copy()
		nn = NN()
		nn.wi = pyb_ws[:nn.wi.size].reshape(NN.nh, NN.ni).T
		nn.wo = pyb_ws[nn.wi.size:].reshape(NN.no, NN.nh).T
		correct = 0
		wrong = 0
		all_aos = []
		for i, x in enumerate(self.trn_d['input']):
			nn.activate(x)
			out = self.net.activate(x)
			all_aos.append(nn.ao)
			if not (out - self.trn_d['target'][i]).any():
				correct += 1
			else:
				wrong += 1
		for i in range(len(array(NN.pat)[:,0])):
			npt.assert_array_equal(self.trn_d['input'][i], array(NN.pat)[:,0][i])
			npt.assert_array_equal(self.trn_d['input'][i], array(nn.pat)[:,0][i])
			npt.assert_array_equal(self.trn_d['target'][i], array(NN.pat)[:,1][i])
			npt.assert_array_equal(self.trn_d['target'][i], array(nn.pat)[:,1][i])
Ejemplo n.º 19
0
def nn_controller_details(filename, activation, reuse=False):
    """
    Return weights and bias
    """
    filename = 'nn/' + filename

    with open(filename) as inputfile:
        lines = inputfile.readlines()
    length = len(lines)
    res = np.zeros(length)
    for i, text in enumerate(lines):
        res[i] = eval(text)

    # Set the controller
    NN_controller = NN(res, activation, reuse=reuse)

    return NN_controller
	def setUp(self):
		weights_in = -2 + np.random.rand(NN.ni, NN.nh) * 4
		weights_out = -2 + np.random.rand(NN.nh, NN.no) * 4
		self.net1 = NN()
		self.net1.wi = weights_in.copy()
		self.net1.wo = weights_out.copy()
		self.net2 = NN()
		self.net2.wi = weights_in.copy()
		self.net2.wo = weights_out.copy()
		inputs = -2 + np.random.rand(4) * 8
		for i in range(NN.ni):
			self.net1.ai[i] = np.tanh(inputs[i])
		for j in range(NN.nh):
			self.net1.ah[j] = sigmoid(sum([self.net1.ai[i] * self.net1.wi[i][j] for i in range(NN.ni)]))
		for k in range(NN.no):
			self.net1.ao[k] = sum([self.net1.ah[j] * self.net1.wo[j][k] for j in range(NN.nh)])
		self.net1.ao = np.where(self.net1.ao > 0.5, 1.0, 0.0)
		self.net2.activate(inputs)
Ejemplo n.º 21
0
def testFittestInd(rankedPop):
    tester = NN()
    fittestWeights = rankedPop[0][0]
    tester.assignWeights(fittestWeights)
    count = 0
    for i, t in tester.test_pat:
        o = tester.activate(i)
        print i, o, t, '++' if list(o) == list(t) else '----'
        count += 1 if list(o) == list(t) else -1
    print float(count) / len(tester.test_pat)
    err, per = tester.sumErrors(test_data=True)
    print per
    return err, per
Ejemplo n.º 22
0
 def testWeightsAndActivationsEquivalent(self):
     pyb_ws = self.net.params
     nn = NN()
     nn.wi = pyb_ws[:nn.wi.size].reshape(NN.nh, NN.ni).T
     nn.wo = pyb_ws[nn.wi.size:].reshape(NN.no, NN.nh).T
     for i, x in enumerate(self.trn_d['input']):
         nn.activate(x)
         out = self.net.activate(x)
         npt.assert_array_equal(nn.ai, self.net['in'].outputbuffer[0])
         # self.assertItemsEqual(list(nn.ah), list(self.net['hidden0'].outputbuffer[0]))
         for j, pb_ah in enumerate(self.net['hidden0'].outputbuffer[0]):
             self.assertAlmostEqual(nn.ah[j], pb_ah)
         for k, pb_ao in enumerate(out):
             self.assertAlmostEqual(nn.ao[k], pb_ao)
Ejemplo n.º 23
0
def testFittestInd(rankedPop):
    tester = NN()
    fittestWeights = rankedPop[0][0]
    tester.assignWeights(fittestWeights)
    right = 0
    for i, t, c, l in tester.test_pat:
        o = tester.activate(i)
        print i, o, t, '++' if list(o) == list(t) else '----'
        right += 1 if list(o) == list(t) else 0
    print right, len(tester.test_pat)
    print 100 * (float(right) / len(tester.test_pat))
    err, per = tester.sumErrors(test_data=True)
    print per
    return err, per
Ejemplo n.º 24
0
def createPop():
    return [NN() for i in range(NN.pop_size)]
Ejemplo n.º 25
0
        plt.ylabel("skewness")

        #plt.show()

processed_data = list(zip(mean, kurt))
training_data = np.reshape(training_data, [16508, 3])
print(training_data)
print(np.shape(training_data))

data_cluster = Cluster(processed_data, training_data, ideal_data)
feature_sets, labels = data_cluster.cluster()

feature_sets = np.asarray(feature_sets)
print(np.shape(feature_sets))
plt.figure(figsize=(10, 10))
plt.scatter(feature_sets[:, 2],
            feature_sets[:, 0],
            c=feature_sets[:, 3],
            cmap='rainbow')
plt.show()

ideal_label = max(labels) + 1

print(np.shape(feature_sets))
print(feature_sets)

fault_predictor = NN(feature_sets, 1500, ideal_label, 256, 0.1, 'tanh', 10)
fault_predictor.processData()
fault_predictor.buildModel()
fault_predictor.trainModel()
Ejemplo n.º 26
0
def testFittestInd(rankedPop):
    tester = NN()
    fittestWeights = rankedPop[0][0]
    tester.assignWeights(fittestWeights)
    return tester.sumErrors(test_data=True)
Ejemplo n.º 27
0
 def setUp(self):
     self.nn = NN()
     self.in_to_hidden = self.nn.wi
     self.hidAstroL = AstrocyteLayer(self.nn.ah, self.in_to_hidden)
 def setUp(self):
     self.net = NN()
 def setUp(self):
     self.nn = NN()
     self.in_to_hidden = self.nn.wi
     self.hidAstroL = AstrocyteLayer(self.nn.ah, self.in_to_hidden)
Ejemplo n.º 30
0
	def setUp(self):
		self.nn = NN()
Ejemplo n.º 31
0
        self.train_labels = dataset[0][1]
        self.valid_labels = dataset[1][1]
        self.test_labels = dataset[2][1]

        self.n_features = self.train_data.shape[1]
        self.n_categories = np.unique(self.train_labels).shape[0]


if __name__ == '__main__':

    moon = moon()

    #1 et 2

    nn = NN(2, 4, 2)

    normal = nn.bprop_slow(moon.train_data[:1], moon.train_labels[:1])
    numerical = nn.FD_gradients(moon.train_data[:1], moon.train_labels[:1])

    print("\n" "-----Gradient par les formules-----" + "\n")
    print(sorted(normal.items()))
    print("\n" + "-----Gradient par difference finie-----" + "\n")
    print(sorted(numerical.items()))

    #3 et 4

    normal = nn.bprop_slow(moon.train_data[:10], moon.train_labels[:10])
    numerical = nn.FD_gradients(moon.train_data[:10], moon.train_labels[:10])

    print("\n" "-----Gradient par les formules-----" + "\n")
Ejemplo n.º 32
0
     -0.8418091583257694, 0.4202498209051319, 0.5564186113435203,
     0.555311886669758, -0.832890394661856, -0.98009859172038,
     -0.39445237156212176, -0.037779768457022134, -0.012204472378917697,
     0.03488907526535291, 0.7708496093822486, -0.8784655950195925,
     0.8746477155355499, -0.8575269090735795, -0.29599438495441377,
     -0.6668498658177002, -0.6385950413298903, 0.9697562635185897,
     0.8786459903244533, 0.6482911089121831, -0.11993920927832091,
     -0.772720183218961, 0.17790750188249205, -0.18653760475670889,
     0.3960711998381077, -0.0033684132198099626, -0.21067234485329456,
     -0.3753479446358803, 0.055153910155083516, -0.16949742782793037,
     -0.9052446924824077, 0.380809384299709, -0.8306618551590639,
     -0.2029395897397982, 0.6109089072465596, 0.9753912435095556,
     0.9997622353974134, 0.1909207578297727, -0.6091875451850151,
     -0.7803408332873301, -0.004608696712309346, -0.19606055624248586
 ])
 NN_controller = NN(x).controller
 #simple = Simple_controller().controller
 track = Tracker(1., 0, traj, NN_controller)
 plt.plot(pathListx, pathListy, '-')
 cost, carListx, carListy = track.run()
 plt.plot(carListx, carListy, '-')
 plt.show()
 print cost  #not perfect bc of float error y=x
 #
 # #case 2
 # c = Car(0., 0., 0., 1.)
 # pathListx = [0]
 # pathListy = [0]
 # for i in range(5):
 #     delta_theta = 0
 #     x, y, _ = c.update(delta_theta)
Ejemplo n.º 33
0
def testFittestInd(rankedPop):
    tester = NN ()
    fittestWeights = rankedPop[0][0]
    tester.assignWeights(fittestWeights)
    return tester.sumErrors(test_data=True)
Ejemplo n.º 34
0
	def setUp(self):
		self.nn = NN()
		self.inputs = [1, 2, 3, 4]
		self.nn.activate(self.inputs)
Ejemplo n.º 35
0
                        [x, v, i * 2 * pi / N_TRAIN_DIRECTIONS, traj])
            costs = pool.map(function_x, trackers)
            return max(costs)

        x = 2 * rand(1, 4 * N_NEURONS + 1)[0] - 1
        res = fmin2(objective,
                    x,
                    .5,
                    args=(VELOCITY, ),
                    options={
                        'popsize': 256,
                        'bounds': [-1, 1],
                        'maxiter': 256
                    })  # 5th is mean of final sample distribution
        res = res[1].result[0]
        controller = NN(res).controller
        trackers = []
        for i in range(N_TRAIN_DIRECTIONS):
            for t in paths:
                traj = Trajectory(t)
                orientation = i * 2 * pi / N_TRAIN_DIRECTIONS
                trackers.append(
                    Tracker(VELOCITY, orientation, traj, controller))
        traces = map(lambda x: x.run(), trackers)
        for i, trace in enumerate(traces):
            plt.plot(trace[1],
                     trace[2],
                     ':',
                     color=hsv_to_rgb(linspace(0, 1, len(traces))[i], 1, 1))
        for path in paths:
            plt.plot(*zip(*path))
Ejemplo n.º 36
0
def function_x(args):
    x, v, dir, traj = args
    controller = NN(x).controller
    traj = Trajectory(traj)
    tracker = Tracker(v, dir, traj, controller)
    return tracker.run()[0]