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)
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)
 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])
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)
 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)
示例#6
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)
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)
	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)
 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)
示例#10
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
示例#11
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
	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])
示例#13
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
示例#14
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
示例#15
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