Beispiel #1
0
 def test_update_weights(self):
     layer = Layer(np.array([1, 1, 2]))
     w0 = layer.w
     acc_error, lr = np.array([0.5, 0.5, 0.5]), 5
     layer.solve_bwd(acc_error, lr)
     layer.update_weights()
     self.assertTrue((layer.w == (w0 + layer.delta_w)).all())
Beispiel #2
0
 def test_input_should_match_neurons_in_regular_layers(self):
     i = np.array([1, 2, 3])
     regex = 'In hidden layers input dimension should match neurons.'
     with self.assertRaisesRegex(ValueError, regex):
         Layer(i, neurons=2)
Beispiel #3
0
 def test_solve_bwd_acc_error_matches_dimension(self):
     layer = Layer(np.array([1, 1, 2]))
     regex = 'The accumulated error dimension doesn\'t match!'
     with self.assertRaisesRegex(ValueError, regex):
         layer.solve_bwd(np.zeros(5))
Beispiel #4
0
 def test_str_returns_name(self):
     layer = Layer(np.zeros(3))
     layer.name = 'test'
     self.assertEqual(layer.__str__(), 'test')
Beispiel #5
0
 def test_init_value_for_delta_w_is_an_np_zeros(self):
     layer = Layer(np.zeros(3))
     self.assertIsInstance(layer.delta_w, np.ndarray)
     self.assertTrue((layer.delta_w == 0).all())
Beispiel #6
0
 def test_output_for_regular_layer_matches_the_no_of_neurons(self):
     layer = Layer(np.array([1, 2, 3]))
     self.assertEqual(len(layer.s), layer.dim)
Beispiel #7
0
 def test_z_is_the_product_between_input_and_weight(self):
     i = np.array([1, 2, 3])
     layer = Layer(i)
     self.assertTrue((layer.z == (i * layer.w)).all())
Beispiel #8
0
 def test_solve_fwd_requires_one_argument(self):
     layer = Layer(np.array([1, 2, 3]))
     with self.assertRaises(TypeError):
         layer.solve_fwd()
Beispiel #9
0
 def test_layer_input_should_be_a_numpy_array(self):
     i = [1, 2, 3]
     regex = 'The input should be a numpy array'
     with self.assertRaisesRegex(TypeError, regex):
         Layer(i)
Beispiel #10
0
 def test_delta_w_value(self):
     layer = Layer(np.array([1, 1, 2]))
     acc_error, lr = np.array([0.5, 0.5, 0.5]), 5
     layer.solve_bwd(acc_error, lr)
     expected = -lr * acc_error * layer.partial_s * layer.x
     self.assertTrue((layer.delta_w == expected).all())
Beispiel #11
0
 def test_error_passed_back_matches_neuron_dimension(self):
     layer = Layer(np.array([1, 1, 2]))
     layer.solve_bwd(np.zeros(3))
     self.assertEqual(len(layer.e), layer.dim)
Beispiel #12
0
 def test_error_passed_back_meets_chain_rule(self):
     layer = Layer(np.array([1, 1, 2]))
     acc_error = np.array([0.5, 0.5, 0.5])
     layer.solve_bwd(acc_error, 5)
     chain_rule = acc_error * layer.partial_s * layer.w
     self.assertTrue((layer.e == chain_rule).all())
Beispiel #13
0
 def test_partial_s_dimension_matches_neuron_qty(self):
     layer = Layer(np.array([1, 1, 2]))
     layer.solve_bwd(np.zeros(3), 5)
     self.assertEqual(len(layer.partial_s), layer.dim)
Beispiel #14
0
 def test_partial_s_is_the_derivative_of_sigmoid_function(self):
     layer = Layer(np.array([1, 1, 2]))
     part_s = layer.s * (1 - layer.s)
     layer.solve_bwd(np.zeros(3), 5)
     self.assertTrue((part_s == layer.partial_s).all())
Beispiel #15
0
 def test_auto_weigths_match_shape_on_regular_layer(self):
     layer = Layer(np.array([1, 2, 3]))
     self.assertEqual(layer.w.shape, (3, ))
Beispiel #16
0
 def test_auto_weights_are_smaller_than_one_in_regular_layers(self):
     layer = Layer(np.zeros(300), neurons=300)  # big enough
     self.assertTrue((layer.w < 1).all())
Beispiel #17
0
 def test_delta_w_matches_neuron_dimension(self):
     layer = Layer(np.array([1, 1, 2]))
     layer.solve_bwd(np.zeros(3))
     self.assertEqual(len(layer.delta_w), layer.dim)
Beispiel #18
0
 def test_input_attribute_in_regular_layer(self):
     layer = Layer(np.array([1, 2, 3]))
     self.assertTrue((layer.x == np.array([1, 2, 3])).all())
Beispiel #19
0
 def test_neurons_should_be_an_int(self):
     i = np.array([1, 2, 3])
     regex = 'Neurons should be a int.'
     with self.assertRaisesRegex(TypeError, regex):
         Layer(i, neurons='void')
Beispiel #20
0
 def test_output_for_regular_layer(self):
     layer = Layer(np.array([1, 2, 3]))
     expected = 1 / (1 + np.exp(-layer.x * layer.w))
     self.assertTrue((layer.s == expected).all())
Beispiel #21
0
 def test_only_certain_types_of_layer_are_allowed(self):
     i = np.array([1, 2, 3])
     regex = 'Only regular, gateway & output layer types are allowed.'
     with self.assertRaisesRegex(ValueError, regex):
         Layer(i, layer_type='void')
Beispiel #22
0
 def test_init_value_for_accumulated_error_is_an_np_zeros(self):
     layer = Layer(np.zeros(3))
     self.assertIsInstance(layer.e, np.ndarray)
     self.assertTrue((layer.e == 0).all())
Beispiel #23
0
 def test_layer_name_is_set_to_none(self):
     self.assertTrue(Layer(np.array([1, 2, 3])).name is None)
Beispiel #24
0
 def test_layer_requires_an_input(self):
     regex = 'missing 1 required positional argument: \'x\''
     with self.assertRaisesRegex(TypeError, regex):
         Layer()
Beispiel #25
0
 def test_dim_is_the_number_of_neurons(self):
     i = np.array([1, 2, 3])
     layer = Layer(i)
     self.assertEqual(layer.dim, 3)
Beispiel #26
0
 def test_solve_bwd_requires_one_arg(self):
     layer = Layer(np.array([1, 1, 2]))
     regex = 'missing 1 required positional argument: \'acc_error\''
     with self.assertRaisesRegex(TypeError, regex):
         layer.solve_bwd()
Beispiel #27
0
 def test_solve_bwd_acc_error_should_be_np_array(self):
     layer = Layer(np.array([1, 1, 2]))
     regex = 'The accumulated error should be a numpy array'
     with self.assertRaisesRegex(TypeError, regex):
         layer.solve_bwd('void')