def test_backward(self):
        conv_params = {'stride': 2, 'pad': 1}

        nr_img = 2
        sz_img = 4
        nr_in_channel = 3
        sz_filter = 4
        nr_filter = 3

        a = np.random.randn(2, 1, 3, 2)
        p = np.array([1, 0, 1, 0, 1, 0, 1, 1, 1, 2, 3, 2, 1, 0, 1, 2, 1,
                      2]).reshape(1, 2, 3, 3)
        x = np.linspace(-.1, .5, 2 * 3 * 4 * 4).reshape(2, 3, 4, 4)
        w = np.linspace(-0.2, 0.3, 3 * 3 * 4 * 6).reshape(3, 3, 4, 6)

        x = np.linspace(-.1, .5, x_size).reshape(nr_img, nr_in_channel, sz_img,
                                                 sz_img)
        w = np.linspace(-0.2, 0.3, w_size).reshape(nr_filter, nr_in_channel,
                                                   sz_filter, sz_filter)
        dout = np.random.randn(nr_img, nr_in_channel - 1, sz_img,
                               4)  # standin for the derivitive from next layer

        dx_num = eval_numerical_gradient_array(
            lambda x: conv_forward(x, w, conv_params)[0], x, dout)
        dw_num = eval_numerical_gradient_array(
            lambda w: conv_forward(x, w, conv_params)[0], w, dout)

        y, cache = conv_forward(x, w, conv_param=conv_params)
        dx, dw = convolution_backward(dout, cache)

        # self.assertEqual(tpose1230(p).all(), p.transpose(1, 2, 3, 0).all())
        # self.assertEqual(tpose1230(w).all(), w.transpose(1, 2, 3, 0).all())
        # self.assertEqual(tpose1230(x).all(), x.transpose(1, 2, 3, 0).all())

        self.assertTrue(np.array_equal(tpose1230(a), a.transpose(1, 2, 3, 0)))
        self.assertTrue(np.array_equal(tpose1230(p), p.transpose(1, 2, 3, 0)))
        self.assertTrue(np.array_equal(tpose1230(w), w.transpose(1, 2, 3, 0)))
        self.assertTrue(np.array_equal(tpose1230(x), x.transpose(1, 2, 3, 0)))

        self.assertEqual(a.shape[0], a.transpose(1, 2, 3, 0).shape[3])
        self.assertEqual(a.shape[1], a.transpose(1, 2, 3, 0).shape[0])
        self.assertEqual(a.shape[2], a.transpose(1, 2, 3, 0).shape[1])
        self.assertEqual(a.shape[3], a.transpose(1, 2, 3, 0).shape[2])

        # print()
        # print(tpose1230(p).flatten())
        # print()
        # print(list(p.transpose(1, 2, 3, 0).flatten()))
        # print()
        # print(list(x.transpose(1, 2, 3, 0).flatten()))
        # print()
        print(list(w.transpose(1, 2, 3, 0).flatten()))
    def test_conv_backward_copy_231_assign(self):
        np.random.seed(231)
        x = np.random.randn(4, 3, 5, 5)
        w = np.random.randn(2, 3, 3, 3)

        dout = np.random.randn(4, 2, 5,
                               5)  # standin for the derivitive from next layer
        conv_param = {'stride': 1, 'pad': 1}

        dx_num = eval_numerical_gradient_array(
            lambda x: conv_forward(x, w, conv_param)[0], x, dout)
        dw_num = eval_numerical_gradient_array(
            lambda w: conv_forward(x, w, conv_param)[0], w, dout)

        out, cache = conv_forward(x, w, conv_param)
        dx, dw = convolution_backward(dout, cache)
        # print(dw_num)
        # print(dw)

        # Your errors should be around e-8 or less.
        print('Testing conv_backward_naive function')
        print('dx error: ', rel_error(dx, dx_num))
        print('dw error: ', rel_error(dw, dw_num))
    def test_backward_from_picture(self):

        conv_params = {'stride': 1, 'pad': 0}

        nr_img = 1
        sz_img = 3
        channels = 2  # input channels
        sz_filter = 2
        nr_filter = 2  # num output channels

        # x = np.random.randn(nr_img, nr_in_channel, sz_img, 4)
        # w = np.random.randn(nr_filter, nr_in_channel, sz_filter, sz_filter)
        x = np.array([1, 0, 1, 0, 1, 0, 1, 1, 1, 2, 3, 2, 1, 0, 1, 2, 1, 2],
                     dtype=float).reshape(nr_img, channels, sz_img, sz_img)
        w = np.array([1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 2, 2],
                     dtype=float).reshape(nr_filter, channels, sz_filter,
                                          sz_filter)
        dout = np.array([0.7, -1.08,
                         1.99, -1.36, .3, -.9, 1.22, -.75]).reshape(
                             1, 2, 2,
                             2)  # standin for the derivative from next layer

        dx_num = eval_numerical_gradient_array(
            lambda x: conv_forward(x, w, conv_params)[0], x, dout)
        dw_num = eval_numerical_gradient_array(
            lambda w: conv_forward(x, w, conv_params)[0], w, dout)

        y, cache = conv_forward(x, w, conv_param=conv_params)

        dx, dw = convolution_backward(dout, cache)
        print(list(dx.flatten()))
        print(list(dw.flatten()))

        # Your errors should be around e-8 or less.
        print('Testing conv_backward_naive function')
        print('dx error: ', rel_error(dx, dx_num))
        print('dw error: ', rel_error(dw, dw_num))
    def test_backward_2(self):

        conv_params = {'stride': 1, 'pad': 0}

        nr_img = 1
        sz_img = 3
        channels = 2  # input channels
        filter_rows = 3
        filter_cols = 2  # num output channels
        nr_filter = 2

        x = np.array([1, 0, 1, 0, 1, 0, 1, 1, 1, 2, 3, 2, 1, 0, 1, 2, 1, 2],
                     dtype=float).reshape(nr_img, channels, sz_img, sz_img)
        w = np.array([
            1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 2, 2, 1, 0, 2, 4, 3, 1,
            2, 4
        ],
                     dtype=float).reshape(nr_filter, channels, filter_rows,
                                          filter_cols)
        dout = np.array([0.7, -1.08, 1.99, -1.36]).reshape(
            1, 2, 1, 2)  # standin for the derivative from next layer

        dx_num = eval_numerical_gradient_array(
            lambda x: conv_forward(x, w, conv_params)[0], x, dout)
        dw_num = eval_numerical_gradient_array(
            lambda w: conv_forward(x, w, conv_params)[0], w, dout)

        y, cache = conv_forward(x, w, conv_param=conv_params)

        print(y.shape)
        dx, dw = convolution_backward(dout, cache)

        # # Your errors should be around e-8 or less.
        print('Testing conv_backward_naive function')
        print('dx error: ', rel_error(dx, dx_num))
        print('dw error: ', rel_error(dw, dw_num))
Beispiel #5
0
    def test_conv_forward_im2col(self):
        conv_params = {'stride': 2, 'pad': 1}

        nr_img = 2
        sz_img = 4
        nr_in_channel = 3
        sz_filter = 4
        nr_filter = 3

        x_size = nr_img * nr_in_channel * sz_img * sz_img
        w_size = nr_filter * nr_in_channel * sz_filter * sz_filter

        x = np.linspace(-.1, .5, x_size).reshape(nr_img, nr_in_channel, sz_img,
                                                 sz_img)
        w = np.linspace(-0.2, 0.3, w_size).reshape(nr_filter, nr_in_channel,
                                                   sz_filter, sz_filter)

        y, cache = conv_forward(x, w, conv_param=conv_params)
Beispiel #6
0
    def test_conv_forward_from_picture(self):
        """
            this is the example from the picture we looked at... source is here
            https://www.microsoft.com/en-us/research/uploads/prod/2018/05/spg-cnn-asplos17.pdf
        """

        conv_params = {'stride': 1, 'pad': 0}

        nr_img = 1
        sz_img = 3
        nr_in_channel = 2  # input channels
        sz_filter = 2
        nr_filter = 2  # num output channels

        x = np.array([1, 0, 1, 0, 1, 0, 1, 1, 1, 2, 3, 2, 1, 0, 1, 2, 1,
                      2]).reshape(nr_img, nr_in_channel, sz_img, sz_img)
        w = np.array([1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 2,
                      2]).reshape(nr_filter, nr_in_channel, sz_filter,
                                  sz_filter)

        y, cache = conv_forward(x, w, conv_param=conv_params)