Пример #1
0
    def test_neuron_returns_right_output(self):
        weights = _np.array([1.0, 1.0, 1.0])
        inputs = _np.array([3.0, 3.0, 3.0])
        neuron = snn.Neuron("cubic", weights)
        output = neuron.activate(_np.sum(weights * inputs))

        _np_tests.assert_allclose(output, 729.0)
Пример #2
0
    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)
Пример #3
0
    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))
Пример #4
0
    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)
Пример #5
0
    def test_neuron_set_weights(self):
        weights = _np.array([1.0, 1.0, 1.0])
        func_weights = _np.array([5.0, 5.0, 5.0, 4.0])

        neuron = snn.Neuron("linear", weights.copy(), func_weights.copy())

        weights.fill(0.0)
        func_weights.fill(25.0)

        neuron.set_input_weights(weights.copy())
        neuron.set_func_weights(func_weights.copy())

        _np_tests.assert_equal(neuron.get_input_weights(), weights)
        _np_tests.assert_equal(neuron.get_func_weights(), func_weights)
Пример #6
0
    def test_neuron_copy(self):
        weights = _np.array([1.0, 1.0, 1.0])
        func_weights = _np.array([5.0, 5.0, 5.0, 4.0])

        neuron = snn.Neuron("linear", weights.copy(), func_weights.copy())
        copy = neuron.copy()

        weights.fill(.0)
        func_weights.fill(3.0)
        neuron.set_input_weights(weights)
        neuron.set_func_weights(func_weights)

        self.assertEqual(_np.equal(neuron.get_input_weights(), copy.get_input_weights()).all(),
                          False)
        self.assertEqual(_np.equal(neuron.get_func_weights(),
                                    copy.get_func_weights()).all(),
                          False)
Пример #7
0
    def test_base_creation(self):
        w_len = 10
        f_len = 1
        weights = _np.linspace(1, 10, w_len)
        func_weights = _np.linspace(1, 10, f_len)
        neuron = snn.Neuron("linear", weights, func_weights)

        total_len = neuron.total_len()
        self.assertEqual(total_len, w_len + f_len)

        self.assertEqual(neuron.w_len(), w_len)
        self.assertEqual(neuron.f_len(), f_len)

        _np_tests.assert_array_equal(neuron.get_input_weights(), weights)
        _np_tests.assert_array_equal(neuron.get_func_weights(), func_weights)

        self.assertEqual(neuron.func_name(), "linear")
Пример #8
0
    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)
Пример #9
0
    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)
Пример #10
0
 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
Пример #11
0
    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)
Пример #12
0
    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)
Пример #13
0
    def test_neuron_raises_if_uninitialized_002(self):
        neuron = snn.Neuron("linear", func_weights_count=5)

        self.assertRaises(NameError, lambda: neuron.activate(5))
Пример #14
0
    def test_neuron_raises_if_uninitialized_001(self):

        neuron = snn.Neuron("linear")

        self.assertRaises(NameError, neuron.get_input_weights)