def test_forward1(self): layer = ConvLayer(1, 1, 3) x = fake_data((1, 1, 3, 3)) layer.W = fake_data((1, 1, 3, 3)) layer.b = fake_data(layer.b.shape) y = layer.forward(x) should_be = np.array([[[[58., 100., 70.], [132., 204., 132.], [70., 100., 58.]]]]) self.assertTrue(np.allclose(y, should_be))
def test_backward3_5(self): layer = ConvLayer(5, 3, 3) x = fake_data((2, 5, 3, 3)) layer.W = fake_data(layer.W.shape) layer.b = fake_data(layer.b.shape) y = layer.forward(x) x_grad = layer.backward(np.ones_like(y)) # do numerical gradients nm_x_grad = numerical_gradient(layer, x, x) nm_w_grad = numerical_gradient(layer, x, layer.W) nm_b_grad = numerical_gradient(layer, x, layer.b) self.assertTrue(np.allclose(nm_x_grad, x_grad)) self.assertTrue(np.allclose(nm_w_grad, layer.W_grad)) self.assertTrue(np.allclose(nm_b_grad, layer.b_grad))
def test_backward2(self): layer = ConvLayer(2, 1, 3) x = fake_data((1, 2, 4, 4)) layer.W = fake_data((1, 2, 3, 3)) layer.b = fake_data(layer.b.shape) y = layer.forward(x) x_grad = layer.backward(np.ones_like(y)) # do numerical gradients nm_x_grad = numerical_gradient(layer, x, x) nm_w_grad = numerical_gradient(layer, x, layer.W) nm_b_grad = numerical_gradient(layer, x, layer.b) self.assertTrue(np.allclose(nm_x_grad, x_grad)) #print("expected", nm_x_grad) #print(x_grad) self.assertTrue(np.allclose(nm_w_grad, layer.W_grad)) #print("expected2", nm_w_grad) #print(layer.W_grad) self.assertTrue(np.allclose(nm_b_grad, layer.b_grad))