def test_base_creation(self): neurons = list() for i in range(0, 10): neurons.append(snn.Neuron("linear", _np.random.rand(43))) l = snn.Layer() l.add_neurons(neurons) # Need this to make layer create matrix. for neuron in l: pass self.assertEqual(l.out_len(), 10) self.assertEqual(l.in_len(), 43)
def test_layer_fails_on_bad_input_003(self): neurons = list() for i in range(0, 148): neurons.append(snn.Neuron("linear", _np.random.rand(44))) l = snn.Layer() l.add_neurons(neurons) for n in l: pass input = _np.array([]) self.assertRaises(ValueError, lambda: l.input(input))
def test_iterable(self): neuron_count = 10 neurons = list() for i in range(0, neuron_count): neurons.append(snn.Neuron("linear", _np.random.rand(43))) l = snn.Layer() l.add_neurons(neurons) count = 0 for neuron in l: count += 1 self.assertEqual(count, neuron_count)
def test_func_weights_give_right_output(self): l = snn.Layer() for i in range(0, 5): l.add_neurons(snn.Neuron("npower", func_weights_count=1)) input = _np.array([1, 1, 1]) good_output = _np.array([3.0, 9.0, 27.0, 81.0, 243.0]).reshape(5, 1) for neuron in l: neuron.set_input_weights(_np.array([1.0, 1.0, 1.0])) neuron.set_func_weights(_np.array(0.0)) func_weights = _np.array([1, 2, 3, 4, 5]) l.set_func_weights(func_weights) real_output = l.input(input.reshape(3, 1)) self.assertEquals(real_output.size, 5) _np_tests.assert_array_equal(real_output, good_output)
def test_layer_get_weights(self): weights = _np.array([5.0, 5.0, 5.0]) func_weights = _np.array([34.0]) neurons = list() for i in range(0, 150): neurons.append(snn.Neuron("linear", weights.copy(), func_weights.copy())) l = snn.Layer() l.add_neurons(neurons) for n in l: pass actual_weights = l.get_weights("input") actual_func_weights = l.get_weights("func") actual_all_weights = l.get_weights("all") _np_tests.assert_equal(actual_weights, 5.0) _np_tests.assert_equal(actual_func_weights, 34.0) self.assertEqual(actual_all_weights.size, actual_weights.size + actual_func_weights.size)
def load_from_json(json_str): net_dump = _json.loads(json_str) input_count = net_dump["input_count"] error_name = net_dump["error_name"] net = PerceptronNumpy(input_count, error_name) for layer_dump in net_dump["layers"]: layer = _snn.Layer() for neuron_dump in layer_dump: if "weights" in neuron_dump: weights = _np.array(neuron_dump["weights"]) else: weights = None if "func_weights" in neuron_dump: func_weights = _np.array(neuron_dump["func_weights"]) else: func_weights = None neuron = _snn.Neuron(neuron_dump["func_name"], weights, func_weights, neuron_dump["f_len"]) layer.add_neurons(neuron) net.add_layer(layer) return net
def test_layer_set_weights(self): weights = _np.array([5.0, 5.0, 5.0]) func_weights = _np.array([34.0]) neurons = list() for i in range(0, 150): neurons.append( snn.Neuron("linear", weights.copy(), func_weights.copy())) l = snn.Layer() l.add_neurons(neurons) for n in l: pass weights = l.get_weights("input") weights.fill(0) func_weights = l.get_weights("func") func_weights.fill(-.4) l.set_weights(weights.copy()) _np_tests.assert_equal(l.get_weights("input"), 0) l.set_func_weights(func_weights) _np_tests.assert_equal(l.get_weights("func"), -0.4)
def test_layer_returns_right_output(self): neuron_count = 45 input_len = 56 neurons = list() for i in range(0, neuron_count): weights = _np.zeros(input_len, dtype=_np.float64) weights.fill(1.0) neurons.append(snn.Neuron("linear", weights)) l = snn.Layer() l.add_neurons(neurons) for n in l: pass input = _np.zeros(input_len, dtype=_np.float64).reshape(input_len, 1) input.fill(2.0) expected_output = 2.0 * input_len expected_array = _np.zeros(neuron_count, dtype=_np.float64) expected_array.fill(expected_output) real_array = l.input(input).A1 _np_tests.assert_allclose(real_array, expected_array)
def test_add_neurons_raises(self): l = snn.Layer() self.assertRaises(ValueError, lambda: l.add_neurons("test"))