Exemplo n.º 1
0
    def test_bn_mean_back_flat(self):
        eps = 1e-7
        dim = 128
        batch_size = 32

        random_inp = np.random.randn(batch_size, dim)
        beta = np.random.randn(1, dim)
        random_output_gradient = np.random.randn(batch_size, dim)
        learned_mean = np.random.randn(1, dim)

        b_layer = BN_mean(dim)
        b_layer.beta = beta
        b_layer.learned_mean = learned_mean

        dx_num = numerical_gradient_array(
            lambda x: b_layer.forward(random_inp),
            random_inp,
            random_output_gradient,
            h=eps)
        dbeta_num = numerical_gradient_array(
            lambda beta: b_layer.forward(random_inp),
            beta,
            random_output_gradient,
            h=eps)

        dx, grads = b_layer.backward(random_output_gradient)
        dbeta = grads[0][1]

        assert np.allclose(dx, dx_num, atol=eps)
        assert np.allclose(dbeta, dbeta_num, atol=eps)
Exemplo n.º 2
0
    def test_dilated_conv_layer_back_prop(self):
        eps = 1e-7
        batch_size = 32
        filter_size = 3
        h_x, w_x = 7, 7
        inChannels = 3
        n_filter = 5
        padding = 0
        stride = 1
        dilation = 2
        new_filter_size = dilation * (filter_size - 1) + 1
        h_out = (h_x - new_filter_size + 2 * padding) // stride + 1
        w_out = (w_x - new_filter_size + 2 * padding) // stride + 1

        x = np.random.randn(batch_size, inChannels, h_x, w_x)
        w = np.random.randn(n_filter, inChannels, filter_size, filter_size)
        b = np.random.randn(n_filter, 1)
        dout = np.random.randn(batch_size, n_filter, h_out, w_out)

        dc_layer = dilated_Conv2D(inChannels=inChannels,
                                  outChannels=n_filter,
                                  filter_size=filter_size,
                                  stride=stride,
                                  padding=padding,
                                  trainable=True)
        dc_layer.W = w
        dc_layer.b = b

        dx_num = numerical_gradient_array(dc_layer.forward, x, dout, h=eps)
        dw_num = numerical_gradient_array(lambda w: dc_layer.forward(x),
                                          w,
                                          dout,
                                          h=eps)
        db_num = numerical_gradient_array(lambda b: dc_layer.forward(x),
                                          b,
                                          dout,
                                          h=eps)

        dx, grads = dc_layer.backward(dout)
        dw, db = grads[0][1], grads[1][1]

        assert np.allclose(dx, dx_num, atol=eps)
        assert np.allclose(dw, dw_num, atol=eps)
        assert np.allclose(db, db_num, atol=eps)
Exemplo n.º 3
0
    def helper_grad_check(self, Layer):
        layer = Layer(trainable=True)
        dx_num = numerical_gradient_array(lambda x: layer.forward(self.x),
                                          self.x, self.dout, h=self.eps)
        dx, _ = layer.backward(self.dout)

        assert np.allclose(dx, dx_num, atol=self.eps)

        with self.assertRaises(RuntimeError):
            layer2 = Layer(trainable=False)
            layer2.backward(self.dout)