Пример #1
0
    def test_operator_matrix_multiply(self):
        matrix_size = (10,10)
        m = yp.rand(matrix_size, global_dtype, global_backend)
        xm = yp.rand(matrix_size[1], global_dtype, global_backend)
        M = ops.MatrixMultiply(m)

        # Check Forward operator
        assert yp.sumb(yp.abs(yp.vec(yp.changeBackend(M * xm, 'numpy')) - yp.vec(yp.changeBackend(m, 'numpy').dot(yp.changeBackend(xm, 'numpy'))))) < eps, "%f" % yp.sumb(yp.abs(yp.changeBackend(M * xm, 'numpy') - yp.changeBackend(m, 'numpy').dot(yp.changeBackend(xm, 'numpy'))[:, np.newaxis]))

        # Check Adjoint
        assert yp.sumb(yp.abs(yp.vec(yp.changeBackend(M.H * xm, 'numpy')) - yp.vec(np.conj(yp.changeBackend(m, 'numpy').T).dot(yp.changeBackend(xm, 'numpy'))))) < eps, "%f" % yp.sumb(yp.abs(yp.changeBackend(M.H * xm, 'numpy') - np.conj(yp.changeBackend(m, 'numpy').T).dot(yp.changeBackend(xm, 'numpy'))[:, np.newaxis]))

        # Check gradient
        M.gradient_check()
Пример #2
0
    def _check_communitivity(self):
        """This function simply checks whether a list of objective functions is communative"""
        # Generate test arrays
        test_arrays = []
        for objective in self.objective_list:
            test_arrays.append(yp.rand(objective.N))

        # Loop over each objective, setting arguments and operating on missing one
        objective_value_list = []
        for index, objective in enumerate(self.objective_list):

            # Get sublist with all arguments EXCEPT the index. These will be used to set arguments.
            arguments = [None] * len(objective.arguments)
            for (_index, replacement) in zip(
                    self.argument_mask,
                    test_arrays[:index] + test_arrays[index + 1:]):
                arguments[_index] = replacement
            objective.arguments = arguments

            # Determine value of objective function and append to list
            objective_value_list.append(
                yp.scalar(objective * test_arrays[index]))

        # Ensure all the objective values are the same
        assert all([
            value == objective_value_list[0] for value in objective_value_list
        ]), "Objective functions are not commutative"
Пример #3
0
    def test_matmul(self):
        matrix_size = (10, 20)
        m = bops.rand(matrix_size, dtype, 'arrayfire')
        xm = bops.rand(matrix_size[1], dtype, 'arrayfire')
        assert np.sum(
            np.abs(
                bops.changeBackend(bops.matmul(m, xm), 'numpy') -
                bops.changeBackend(m, 'numpy').dot(
                    bops.changeBackend(xm, 'numpy')))) < eps

        # Check matrix multiply (numpy to arrayfire)
        m_np = bops.rand(matrix_size, dtype, 'numpy')
        xm_np = bops.rand(matrix_size[1], dtype, 'numpy')
        m_ocl = bops.changeBackend(m_np, 'arrayfire')
        xm_ocl = bops.changeBackend(xm_np, 'arrayfire')
        assert np.sum(
            np.abs(
                bops.changeBackend(bops.matmul(m_ocl, xm_ocl), 'numpy') -
                m_np.dot(xm_np))) < eps
Пример #4
0
    def setup_method(self, test_method):
        # Load object and crop to size
        x_0 = yp.rand(image_size)

        # Convert object to desired backend
        self.x = yp.changeBackend(x_0, global_backend)

        # Generate convolution kernel h
        h_size = np.array([4, 4])
        self.h = yp.zeros(image_size, global_dtype, global_backend)
        self.h[image_size[0] // 2 - h_size[0] // 2:image_size[0] // 2 + h_size[0] // 2,
          image_size[1] // 2 - h_size[1] // 2:image_size[1] // 2 + h_size[1] // 2] = yp.randn((h_size[0], h_size[1]), global_dtype, global_backend)

        # A = ops.Convolution(image_size, h, dtype=global_dtype, fft_backend='numpy', backend=global_backend)
        self.A = ops.FourierTransform(image_size, dtype=global_dtype, backend=global_backend, center=True)
        self.y = self.A(yp.vectorize(self.x))
Пример #5
0
    def test_indexing(self):
        q = bops.randn((10, 10), backend='numpy', dtype='complex32')
        q_ocl = bops.changeBackend(q, 'arrayfire')
        q_ocl_np = bops.changeBackend(q_ocl, 'numpy')
        assert np.sum(np.abs(q - q_ocl_np)) < eps

        m = bops.rand((10, 20), dtype, "numpy")
        m_ocl = bops.changeBackend(m, 'arrayfire')

        assert abs(m[0, 1] - bops.scalar(m_ocl[0, 1])) < eps
        assert abs(m[1, 1] - bops.scalar(m_ocl[1, 1])) < eps
        assert abs(bops.scalar(m_ocl[4, 1]) - m[4, 1]) < eps

        assert bops.scalar(self.x_np_rect[5, 15]) == bops.scalar(
            bops.changeBackend(self.x_np_rect, 'arrayfire')[5, 15])
        assert bops.scalar(self.x_ocl_rect[5, 15]) == bops.scalar(
            bops.changeBackend(self.x_ocl_rect, 'numpy')[5, 15])
Пример #6
0
    def test_crop(self):
        crop_size = [self.x_np_randn.shape[i] - 2 for i in range(len(shape))]
        assert np.sum(
            np.abs(
                bops.crop(self.x_np_randn, crop_size, crop_start=(0, 0)) -
                np.asarray(
                    bops.crop(self.x_ocl_randn, crop_size, crop_start=(
                        0, 0))))) < 1e-4

        x_full_np = bops.rand((20, 20), dtype=dtype, backend='numpy')
        x_full_ocl = bops.changeBackend(x_full_np, 'arrayfire')
        crop_size = tuple(np.asarray(bops.shape(x_full_np)) // 2)
        crop_start = (2, 2)

        x_crop_np = bops.crop(x_full_np, crop_size, crop_start=crop_start)
        x_crop_ocl = bops.crop(x_full_ocl, crop_size, crop_start=crop_start)

        assert np.sum(
            np.abs(bops.changeBackend(x_crop_ocl, 'numpy') - x_crop_np)) < 1e-4