Пример #1
0
    def test_weights(self):
        # Test default weights
        hn = algorithms.HebbRule(
            n_inputs=5,
            n_outputs=1,
            n_unconditioned=2,
            verbose=False,
        )
        np.testing.assert_array_equal(
            hn.weight,
            np.array([[1, 1, 0, 0, 0]]).T
        )
        np.testing.assert_array_equal(
            hn.bias,
            np.array([-0.5])
        )

        # Test custom weights
        random_weight = np.random.random((5, 1))
        hn = algorithms.HebbRule(
            n_inputs=5,
            n_outputs=1,
            n_unconditioned=2,
            weight=random_weight,
            verbose=False,
        )
        np.testing.assert_array_equal(hn.weight, random_weight)
Пример #2
0
    def test_predict_different_inputs(self):
        inet = algorithms.HebbRule(self.conn,
                                   n_unconditioned=1,
                                   step=1,
                                   verbose=False)

        inet.train(input_data, epochs=10)
        self.assertInvalidVectorPred(inet, np.array([0, 0]), 0, row1d=True)
Пример #3
0
 def test_train_different_inputs(self):
     self.assertInvalidVectorTrain(
         algorithms.HebbRule(self.conn,
                             n_unconditioned=1,
                             step=1,
                             verbose=False),
         np.array([[0, 1]]),
         row1d=True,
     )
Пример #4
0
    def test_weights(self):
        # Test default weights
        hn = algorithms.HebbRule(
            StepLayer(5) > OutputLayer(1),
            n_unconditioned=2,
            verbose=False,
        )
        np.testing.assert_array_equal(hn.input_layer.weight,
                                      np.array([[1, 1, 0, 0, 0]]).T)

        # Test custom weights
        random_weight = np.random.random((5, 1))
        hn = algorithms.HebbRule(
            StepLayer(5, weight=random_weight) > OutputLayer(1),
            n_unconditioned=2,
            verbose=False,
        )
        np.testing.assert_array_equal(hn.input_layer.weight, random_weight)
Пример #5
0
 def test_train_different_inputs(self):
     self.assertInvalidVectorTrain(
         algorithms.HebbRule(n_inputs=2,
                             n_outputs=1,
                             n_unconditioned=1,
                             step=1,
                             verbose=False),
         np.array([[0, 1]]),
         is_feature1d=False,
     )
Пример #6
0
    def test_predict_different_inputs(self):
        inet = algorithms.HebbRule(
            n_inputs=2,
            n_outputs=1,
            n_unconditioned=1,
            step=1,
            verbose=False
        )

        inet.train(X, epochs=10)
        self.assertInvalidVectorPred(inet, np.array([0, 0]), 0,
                                     is_feature1d=False)
Пример #7
0
    def test_with_weight_decay(self):
        hn = algorithms.HebbRule(
            self.conn,
            n_unconditioned=1,
            step=1,
            verbose=False,
            decay_rate=0.1,
        )

        # Test learning limit
        hn.train(input_data, epochs=50)
        self.assertEqual(np.round(hn.input_layer.weight[1, 0], 2), 10)

        hn.train(input_data, epochs=50)
        self.assertEqual(np.round(hn.input_layer.weight[1, 0], 2), 10)
Пример #8
0
    def test_validations(self):
        with self.assertRaises(ValueError):
            # Wrong: too many layers
            algorithms.HebbRule(
                SigmoidLayer(2) > SigmoidLayer(2) > OutputLayer(2),
                n_unconditioned=1,
                verbose=False)

        with self.assertRaises(AttributeError):
            # Wrong: Algorithm is not converge
            hn = algorithms.HebbRule(self.conn, verbose=False)
            hn.train(input_data, epsilon=1e-5)

        with self.assertRaises(ValueError):
            # Wrong: Only step layers in connections
            algorithms.HebbRule(SigmoidLayer(2) > OutputLayer(2),
                                n_unconditioned=2,
                                verbose=False)

        with self.assertRaises(ValueError):
            # Wrong: #features must be bigger than #unconditioned features.
            algorithms.HebbRule(StepLayer(2) > OutputLayer(2),
                                n_unconditioned=2,
                                verbose=False)
Пример #9
0
    def test_learning_process(self):
        hn = algorithms.HebbRule(
            n_inputs=2,
            n_outputs=1,
            n_unconditioned=1,
            step=1,
            verbose=False,
        )

        hn.train(input_data, epochs=2)

        test_data = np.array([
            [0, 0],
            [0, 1],
            [1, 0],
            [1, 1],
        ])
        np.testing.assert_array_equal(hn.predict(test_data),
                                      np.array([[0, 1, 1, 1]]).T)
Пример #10
0
    def test_input_validations_for_hebb_rule(self):
        invalid_cases = (
            # Missed required parameters
            dict(),
            dict(n_inputs=2),
            dict(n_inputs=2, n_outputs=1),

            # n_inputs should be greater than n_unconditioned
            dict(n_inputs=2, n_outputs=2, n_unconditioned=2, verbose=False),

            # Invalid shapes for the arrays
            dict(n_inputs=2, n_outputs=1, n_unconditioned=1,
                 weight=np.array([1])),
            dict(n_inputs=2, n_outputs=1, n_unconditioned=1,
                 bias=np.array([1, 2, 3])),
        )

        for invalid_case_params in invalid_cases:
            with self.assertRaises(ValueError):
                algorithms.HebbRule(**invalid_case_params)
Пример #11
0
    def test_hebb_rule_trainig_exception(self):
        hebb = algorithms.HebbRule(n_inputs=2, n_outputs=3, n_unconditioned=1)
        err_message = "expected to have 2 features"

        with self.assertRaisesRegexp(ValueError, err_message):
            hebb.train(np.ones((6, 3)))