示例#1
0
    def test_pooling(self):
        x_shape = [2, 1, 3, 3]
        x = singa_wrap.Tensor(x_shape)
        x.CopyFloatDataFromHostPtr(
            [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9])

        y_shape = [2, 1, 2, 2]
        dy = singa_wrap.Tensor(y_shape)
        dy.CopyFloatDataFromHostPtr([0.1, 0.2, 0.3, 0.4, 0.1, 0.2, 0.3, 0.4])

        k_dim = [2, 2]
        s_dim = [1, 1]
        p_dim = [0, 0]

        # max pooling
        handle = singa_wrap.PoolingHandle(x, k_dim, s_dim, p_dim, True)
        y = singa_wrap.CpuPoolingForward(handle, x)
        self.assertListEqual([2, 1, 2, 2], list(y.shape()))
        dx = singa_wrap.CpuPoolingBackward(handle, dy, x, y)
        self.assertListEqual([2, 1, 3, 3], list(dx.shape()))

        # avg pooling
        handle = singa_wrap.PoolingHandle(x, k_dim, s_dim, p_dim, False)
        y = singa_wrap.CpuPoolingForward(handle, x)
        self.assertListEqual([2, 1, 2, 2], list(y.shape()))
        dx = singa_wrap.CpuPoolingBackward(handle, dy, x, y)
        self.assertListEqual([2, 1, 3, 3], list(dx.shape()))
示例#2
0
文件: test_api.py 项目: zlheui/singa
    def test_dnnl_pooling_avg(self):
        dev = cpu_dev
        N = 1
        C = 3
        H = 2
        W = 2

        data_shape = [N, C, H, W]
        param_shape = [1, C, 1, 1]
        data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

        x0 = np.array(data, dtype=np.float32).reshape(data_shape)
        x0_ct = tensor.Tensor(device=dev, data=x0).data

        dy0 = np.array([1, 2, 3], dtype=np.float32).reshape([1, 3, 1, 1])
        dy0_ct = tensor.Tensor(device=dev, data=dy0).data

        hndl = singa_api.PoolingHandle(x0_ct, [2, 2], [1, 1], [0, 0], False)

        y0_ct = singa_api.CpuPoolingForward(hndl, x0_ct)

        y1 = np.array([[[[2.5000]], [[6.5000]], [[10.5000]]]])
        np.testing.assert_array_almost_equal(
            tensor.to_numpy(_cTensor_to_pyTensor(y0_ct)), y1)
        dx0_ct = singa_api.CpuPoolingBackward(hndl, dy0_ct, x0_ct, y0_ct)
        dx1 = np.array([[[[0.2500, 0.2500], [0.2500, 0.2500]],
                         [[0.5000, 0.5000], [0.5000, 0.5000]],
                         [[0.7500, 0.7500], [0.7500, 0.7500]]]])
        np.testing.assert_array_almost_equal(
            tensor.to_numpy(_cTensor_to_pyTensor(dx0_ct)), dx1)