Exemple #1
0
 def setup_method(self, method):
     np.random.seed(12345)
     N = 32
     self.U = cp.ones((N, N, N))
     self.U[:, 0:(old_div(N, 2)), :] = -1
     self.V = 1e-1 * cp.asarray(np.random.randn(N, N, N))
     self.D = self.U + self.V
Exemple #2
0
 def test_02(self):
     lmbda = 1e-1
     opt = tvl2.TVL2Deconv.Options(
         {'Verbose': False, 'gEvalY': False, 'MaxMainIter': 250})
     b = tvl2.TVL2Deconv(cp.ones((1)), self.D, lmbda, opt)
     X = b.solve()
     assert cp.abs(b.itstat[-1].ObjFun - 45.45958573088) < 1e-3
     assert sm.mse(self.U, X) < 1e-3
Exemple #3
0
 def test_02(self):
     lmbda = 3
     try:
         b = tvl2.TVL2Deconv(cp.ones((1, )), self.D, lmbda)
         b.solve()
     except Exception as e:
         print(e)
         assert 0
Exemple #4
0
 def test_02(self):
     lmbda = 1e-1
     opt = tvl2.TVL2Deconv.Options(
         {'Verbose': False, 'gEvalY': False, 'MaxMainIter': 250})
     b = tvl2.TVL2Deconv(cp.ones((1)), self.D, lmbda, opt, axes=(0, 1, 2))
     X = b.solve()
     assert cp.abs(b.itstat[-1].ObjFun - 567.72425227) < 1e-3
     assert sm.mse(self.U, X) < 1e-3
Exemple #5
0
def ones(shape, dtype=numpy.float32, stream=None):
    """Creates a zero-filled cupy.ndarray object.

    This function is equivalent to ``full(shape, 1, dtype, stream)``.

    """
    warnings.warn("chainer.cuda.ones is deprecated. Use cupy.ones instead.", DeprecationWarning)
    check_cuda_available()
    assert stream is None
    return cupy.ones(shape, dtype=dtype)
Exemple #6
0
 def test_07(self):
     lmbda = 3
     dt = cp.float64
     opt = tvl2.TVL2Deconv.Options(
         {'Verbose': False, 'MaxMainIter': 20, 'AutoRho':
          {'Enabled': True}, 'DataType': dt})
     b = tvl2.TVL2Deconv(cp.ones((1, )), self.D, lmbda, opt=opt)
     b.solve()
     assert b.X.dtype == dt
     assert b.Y.dtype == dt
     assert b.U.dtype == dt
Exemple #7
0
    def test_scan(self, dtype):
        element_num = 10000

        if dtype in {cupy.int8, cupy.uint8}:
            element_num = 100

        a = cupy.ones((element_num,), dtype=dtype)
        prefix_sum = cupy.core.core.scan(a)
        expect = cupy.arange(start=1, stop=element_num + 1).astype(dtype)

        testing.assert_array_equal(prefix_sum, expect)
Exemple #8
0
 def test_15(self):
     N = 16
     Nd = 5
     M = 4
     D = cp.random.randn(Nd, Nd, M)
     s = cp.random.randn(N, N)
     w = cp.ones(s.shape)
     lmbda = 1e-1
     try:
         b = cbpdn.ConvBPDNMask(D, s, lmbda, w)
         b.solve()
         b.reconstruct()
     except Exception as e:
         print(e)
         assert 0
Exemple #9
0
    def test_scan_out(self, dtype):
        element_num = 10000

        if dtype in {cupy.int8, cupy.uint8, cupy.float16}:
            element_num = 100

        a = cupy.ones((element_num,), dtype=dtype)
        b = cupy.zeros_like(a)
        cupy.core.core.scan(a, b)
        expect = cupy.arange(start=1, stop=element_num + 1).astype(dtype)

        testing.assert_array_equal(b, expect)

        cupy.core.core.scan(a, a)
        testing.assert_array_equal(a, expect)
Exemple #10
0
 def test_30(self):
     N = 16
     Nd = 5
     M = 4
     D = cp.random.randn(Nd, Nd, M)
     s = cp.random.randn(N, N)
     w = cp.ones(s.shape)
     dt = cp.float32
     opt = cbpdn.ConvBPDN.Options(
         {'Verbose': False, 'MaxMainIter': 20, 'AutoRho': {'Enabled': True},
          'DataType': dt})
     lmbda = 1e-1
     b = cbpdn.AddMaskSim(cbpdn.ConvBPDN, D, s, w, lmbda, opt=opt)
     b.solve()
     assert b.cbpdn.X.dtype == dt
     assert b.cbpdn.Y.dtype == dt
     assert b.cbpdn.U.dtype == dt
Exemple #11
0
def ones_like(array, stream=None):
    """Creates a one-filled cupy.ndarray object like the given array.

    Args:
        array (cupy.ndarray or numpy.ndarray): Base array.
        stream (cupy.cuda.Stream): CUDA stream.

    Returns:
        cupy.ndarray: One-filled array.

    """
    warnings.warn("chainer.cuda.ones_like is deprecated. Use cupy.ones_like instead.", DeprecationWarning)
    check_cuda_available()
    assert stream is None
    if isinstance(array, cupy.ndarray):
        return cupy.ones_like(array)
    return cupy.ones(array.shape, dtype=array.dtype)
    def test_backward_case2(self):
        """Backward if non-zero gradient is on a face."""

        vertices = [
            [0.8, 0.8, 1.],
            [-0.5, -0.8, 1.],
            [0.8, -0.8, 1.]]
        faces = [[0, 1, 2]]
        pyi = 40
        pxi = 50
        grad_ref = [
            [0.98646867, 1.04628897, 0.],
            [-1.03415668, - 0.10403691, 0.],
            [3.00094461, - 1.55173182, 0.],
        ]

        renderer = neural_renderer.Renderer()
        renderer.image_size = 64
        renderer.anti_aliasing = False
        renderer.perspective = False
        renderer.light_intensity_ambient = 1.0
        renderer.light_intensity_directional = 0.0

        vertices = cp.array(vertices, 'float32')
        faces = cp.array(faces, 'int32')
        textures = cp.ones((faces.shape[0], 4, 4, 4, 3), 'float32')
        grad_ref = cp.array(grad_ref, 'float32')
        vertices, faces, textures, grad_ref = utils.to_minibatch((vertices, faces, textures, grad_ref))
        vertices = chainer.Variable(vertices)

        images = renderer.render(vertices, faces, textures)
        images = cf.mean(images, axis=1)
        loss = cf.sum(cf.absolute(images[:, pyi, pxi]))
        loss.backward()

        grad_ref = cp.array(grad_ref, 'float32')
        chainer.testing.assert_allclose(vertices.grad, grad_ref, rtol=1e-2)
    def test_backward_case1(self):
        """Backward if non-zero gradient is out of a face."""

        vertices = [
            [0.8, 0.8, 1.],
            [0.0, -0.5, 1.],
            [0.2, -0.4, 1.]]
        faces = [[0, 1, 2]]
        pxi = 35
        pyi = 25
        grad_ref = [
            [1.6725862, -0.26021874, 0.],
            [1.41986704, -1.64284933, 0.],
            [0., 0., 0.],
        ]

        renderer = neural_renderer.Renderer()
        renderer.image_size = 64
        renderer.anti_aliasing = False
        renderer.perspective = False
        renderer.light_intensity_ambient = 1.0
        renderer.light_intensity_directional = 0.0

        vertices = cp.array(vertices, 'float32')
        faces = cp.array(faces, 'int32')
        textures = cp.ones((faces.shape[0], 4, 4, 4, 3), 'float32')
        grad_ref = cp.array(grad_ref, 'float32')
        vertices, faces, textures, grad_ref = utils.to_minibatch((vertices, faces, textures, grad_ref))
        vertices = chainer.Variable(vertices)

        images = renderer.render(vertices, faces, textures)
        images = cf.mean(images, axis=1)
        loss = cf.sum(cf.absolute(images[:, pyi, pxi] - 1))
        loss.backward()

        chainer.testing.assert_allclose(vertices.grad, grad_ref, rtol=1e-2)
Exemple #14
0
def fit2dh(A, deg, hgt, hgt_min, hgt_max, gpu=False):
    """
    Estimate best fit 2d ramp and topography-correlated component simultaneously.

    Inputs:
        A   : Input ndarray (can include nan)
        deg : degree of polynomial for fitting ramp
          - 1  -> a+bx+cy (ramp, default)
          - bl -> a+bx+cy+dxy (biliner)
          - 2  -> a+bx+cy+dxy+ex**2_fy**2 (2d polynomial)
          - []  -> a (must be used with hgt)
        hgt : Input hgt to estimate coefficient of topo-corr component
              If blank, don*t estimate topo-corr component.
        hgt_min : Minimum hgt to take into account in hgt-linear
        hgt_max : Maximum hgt to take into account in hgt-linear
        gpu     : GPU flag

    Returns:
        Afit : Best fit solution with the same demention as A
        m    : Set of parameters of best fit plain (a,b,c...)

    Note: GPU option seems slow and may return error when the size is large.
          Not recommended.

    """
    if gpu:
        import cupy as xp
        A = xp.asarray(A)
        hgt = xp.asarray(hgt)
        hgt_min = xp.asarray(hgt_min)
        hgt_max = xp.asarray(hgt_max)
    else:
        xp = np

    ### Make design matrix G
    length, width = A.shape

    if not deg:
        G = xp.ones((length * width))
    else:
        Xgrid, Ygrid = xp.meshgrid(xp.arange(width), xp.arange(length))
        Xgrid1 = Xgrid.ravel()
        Ygrid1 = Ygrid.ravel()

        if str(deg) == "1":
            G = xp.stack((Xgrid1, Ygrid1)).T
        elif str(deg) == "bl":
            G = xp.stack((Xgrid1, Ygrid1, Xgrid1 * Ygrid1)).T
        elif str(deg) == "2":
            G = xp.stack(
                (Xgrid1, Ygrid1, Xgrid1 * Ygrid1, Xgrid1**2, Ygrid1**2)).T
        else:
            print('\nERROR: Not proper deg ({}) is used\n'.format(deg),
                  file=sys.stderr)
            return False
        del Xgrid, Ygrid, Xgrid1, Ygrid1

        G = xp.hstack([xp.ones((length * width, 1)), G])

    if len(hgt) > 0:
        _hgt = hgt.copy()  ## Not to overwrite hgt in main
        _hgt[xp.isnan(_hgt)] = 0
        _hgt[_hgt < hgt_min] = 0
        _hgt[_hgt > hgt_max] = 0
        G2 = xp.vstack((G.T, hgt.ravel())).T  ## for Afit
        G = xp.vstack((G.T, _hgt.ravel())).T
        del _hgt
    else:
        G2 = G

    G = G.astype(xp.int32)

    ### Invert
    mask = xp.isnan(A.ravel())
    m = xp.linalg.lstsq(G[~mask, :], A.ravel()[~mask], rcond=None)[0]

    Afit = ((xp.matmul(G2, m)).reshape((length, width))).astype(xp.float32)

    if gpu:
        Afit = xp.asnumpy(Afit)
        m = xp.asnumpy(m)
        del A, hgt, hgt_min, hgt_max, length, width, G, G2, mask

    return Afit, m
Exemple #15
0
    def __init__(
        self,
        dim_x,
        dim_z,
        dim_u=0,
        points=1,
        dtype=cp.float32,
    ):

        self.points = points

        if dim_x < 1:
            raise ValueError("dim_x must be 1 or greater")
        if dim_z < 1:
            raise ValueError("dim_z must be 1 or greater")
        if dim_u < 0:
            raise ValueError("dim_u must be 0 or greater")

        self.dim_x = dim_x
        self.dim_z = dim_z
        self.dim_u = dim_u

        # Create data arrays
        self.x = cp.zeros((
            self.points,
            dim_x,
            1,
        ), dtype=dtype)  # state

        self.P = cp.repeat(
            cp.identity(dim_x, dtype=dtype)[cp.newaxis, :, :],
            self.points,
            axis=0,
        )  # uncertainty covariance

        self.Q = cp.repeat(
            cp.identity(dim_x, dtype=dtype)[cp.newaxis, :, :],
            self.points,
            axis=0,
        )  # process uncertainty

        self.B = None  # control transition matrix

        self.F = cp.repeat(
            cp.identity(dim_x, dtype=dtype)[cp.newaxis, :, :],
            self.points,
            axis=0,
        )  # state transition matrix

        self.H = cp.zeros((
            self.points,
            dim_z,
            dim_z,
        ), dtype=dtype)  # Measurement function

        self.R = cp.repeat(
            cp.identity(dim_z, dtype=dtype)[cp.newaxis, :, :],
            self.points,
            axis=0,
        )  # process uncertainty

        self._alpha_sq = cp.ones((
            self.points,
            1,
            1,
        ), dtype=dtype)  # fading memory control

        self.z = cp.empty((
            self.points,
            dim_z,
            1,
        ), dtype=dtype)

        # Allocate GPU resources
        numSM = _get_numSM()
        threads_z_axis = 16
        threadsperblock = (self.dim_x, self.dim_x, threads_z_axis)
        blockspergrid = (1, 1, numSM * 20)

        max_threads_per_block = self.dim_x * self.dim_x * threads_z_axis

        # Only need to populate cache once
        # At class initialization
        _filters_cuda._populate_kernel_cache(
            self.x.dtype,
            threads_z_axis,
            self.dim_x,
            self.dim_z,
            self.dim_u,
            max_threads_per_block,
        )

        # Retrieve kernel from cache
        self.predict_kernel = _filters_cuda._get_backend_kernel(
            self.x.dtype,
            blockspergrid,
            threadsperblock,
            "predict",
        )

        self.update_kernel = _filters_cuda._get_backend_kernel(
            self.x.dtype,
            blockspergrid,
            threadsperblock,
            "update",
        )

        _print_atts(self.predict_kernel)
        _print_atts(self.update_kernel)
Exemple #16
0
def test_each_channel_with_asymmetric_kernel():
    mask = cp.triu(cp.ones(COLOR_IMAGE.shape[:2], dtype=np.bool_))
    mask_each(COLOR_IMAGE, mask)
Exemple #17
0
            fileobj.write("IS:{}pm{}\n".format(inception_mean, inception_std))
            fileobj.write("FID:{}\n\n".format(fid))


if __name__ == '__main__':
    args = parse_args()
    if not os.path.exists("scores"):
        os.mkdir("scores")
    evmodel = Inception()
    serializers.load_hdf5('metric/inception_score.model', evmodel)
    G, D, data = load_GD(args.G, args.D)
    if args.gpu >= 0:
        cuda.get_device(args.gpu).use()
        evmodel.to_gpu()
        G.to_gpu()
        D.to_gpu()
    G, D = DOT.thermalize_spectral_norm(G, D)
    if args.k == None:
        k = DOT.eff_k(G, D)
    else:
        k = args.k * xp.ones([1])
    main(args,
         G,
         D,
         data,
         evmodel,
         k,
         transport=args.transport,
         N_update=args.N_update,
         showing_period=args.showing_period)
Exemple #18
0
def warpAffine_gpu(im, T, out_size, gpuID=0):

    H = im.shape[0]
    W = im.shape[1]
    N = np.prod(out_size)

    x_t, y_t = np.meshgrid(np.linspace(-1, 1, out_size[1]),
                           np.linspace(-1, 1, out_size[0]))
    with cupy.cuda.Device(gpuID):

        im_gpu = cupy.array(im)

        x_t = cupy.array(x_t, cupy.float32)
        y_t = cupy.array(y_t, cupy.float32)

        source_grid = cupy.vstack(
            [x_t.flatten(),
             y_t.flatten(),
             cupy.ones(np.prod(x_t.shape))])
        T_gpu = cupy.array(T, cupy.float32)

        target_grid = cupy.dot(T_gpu, source_grid)
        x_s = cupy.reshape(target_grid[0, :], (-1))
        y_s = cupy.reshape(target_grid[1, :], (-1))

        del target_grid, source_grid

        x_s = cupy.array((x_s + 1.0) * (W - 1) / 2.0, cupy.float32)
        y_s = cupy.array((y_s + 1.0) * (H - 1) / 2.0, cupy.float32)

        x0 = cupy.array(cupy.floor(x_s), cupy.int32)
        x1 = x0 + 1
        y0 = cupy.array(cupy.floor(y_s), cupy.int32)
        y1 = y0 + 1

        x0 = cupy.clip(x0, a_min=0, a_max=W - 1)
        x1 = cupy.clip(x1, a_min=0, a_max=W - 1)
        y0 = cupy.clip(y0, a_min=0, a_max=H - 1)
        y1 = cupy.clip(y1, a_min=0, a_max=H - 1)

        ind_x0y0 = y0 * W + x0
        ind_x0y1 = y1 * W + x0
        ind_x1y0 = y0 * W + x1
        ind_x1y1 = y1 * W + x1

        x0_f = cupy.array(x0, cupy.float32)
        x1_f = cupy.array(x1, cupy.float32)
        y0_f = cupy.array(y0, cupy.float32)
        y1_f = cupy.array(y1, cupy.float32)

        im_out_gpu = cupy.zeros((N, 1), cupy.float32)

        val_orig = cupy.reshape(im_gpu[:, :], (-1))
        im_out_gpu = val_orig[ind_x0y0] * (x1_f - x_s) * (y1_f - y_s)\
                    + val_orig[ind_x0y1] * (x1_f - x_s) * (y_s - y0_f)\
                    + val_orig[ind_x1y0] * (x_s - x0_f) * (y1_f - y_s)\
                    + val_orig[ind_x1y1] * (x_s - x0_f) * (y_s - y0_f)

        del ind_x0y0, ind_x0y1
        im_out_gpu = im_out_gpu.reshape(out_size[0], out_size[1])
        im_out = im_out_gpu.get()

    return im_out
iter_per_epoch = 100
print("Linear classifier probe: input")
weight_init_std = 0.03
W_lin = weight_init_std * cp.random.randn(3072, 10)
b_lin = cp.zeros(10)
alpha = 0.01
for i in range(100000):
    batch_mask = cp.random.choice(train_size, batch_size)
    x_batch = x_train[batch_mask]
    t_batch = t_train[batch_mask]
    # h = cp.dot(x_batch,FA_W1) + FA_b1
    # h = cp.tanh(h)
    output = softmax(cp.dot(x_batch, W_lin) + b_lin)
    delta = (output - t_batch) / batch_size
    delta_W_lin = cp.dot(x_batch.T, delta)
    delta_b_lin = cp.dot(cp.ones(batch_size), delta)
    W_lin -= alpha * delta_W_lin
    b_lin -= alpha * delta_b_lin
    if i % 100 == 0:
        # h = cp.dot(x_test, FA_W1) + FA_b1
        # h = cp.tanh(h)
        output = softmax(cp.dot(x_test, W_lin) + b_lin)
        y = cp.argmax(output, axis=1)
        t = cp.argmax(t_test, axis=1)
        accuracy = cp.sum(y == t) / 10000
        # print(accuracy)

        output = softmax(cp.dot(x_train, W_lin) + b_lin)
        y = cp.argmax(output, axis=1)
        t = cp.argmax(t_train, axis=1)
        train_accuracy = cp.sum(y == t) / 50000
def test_image_less_than_mask():
    """Test reconstruction where the image is uniform and less than mask"""
    image = cp.ones((5, 5))
    mask = cp.ones((5, 5)) * 2
    assert_array_almost_equal(reconstruction(image, mask), 1)
Exemple #21
0
 def testOrderC(self):
     sbuf = cupy.ones([3, 2])
     rbuf = cupy.zeros([3, 2])
     Sendrecv(sbuf, rbuf)
     self.assertTrue((sbuf == rbuf).all())
Exemple #22
0
def confusion_matrix(y_true,
                     y_pred,
                     labels=None,
                     sample_weight=None,
                     normalize=None,
                     convert_dtype=False) -> CumlArray:
    """Compute confusion matrix to evaluate the accuracy of a classification.

    Parameters
    ----------
    y_true : array-like (device or host) shape = (n_samples,)
        or (n_samples, n_outputs)
        Ground truth (correct) target values.
    y_pred : array-like (device or host) shape = (n_samples,)
        or (n_samples, n_outputs)
        Estimated target values.
    labels : array-like (device or host) shape = (n_classes,), optional
        List of labels to index the matrix. This may be used to reorder or
        select a subset of labels. If None is given, those that appear at least
        once in y_true or y_pred are used in sorted order.
    sample_weight : array-like (device or host) shape = (n_samples,), optional
        Sample weights.
    normalize : string in [‘true’, ‘pred’, ‘all’]
        Normalizes confusion matrix over the true (rows), predicted (columns)
        conditions or all the population. If None, confusion matrix will not be
        normalized.
    convert_dtype : bool, optional (default = False)
        When set to True, the confusion matrix method will automatically
        convert the predictions, ground truth, and labels arrays to np.int32.

    Returns
    -------
    C : array-like (device or host) shape = (n_classes, n_classes)
        Confusion matrix.
    """
    y_true, n_rows, n_cols, dtype = \
        input_to_cuml_array(y_true, check_dtype=[cp.int32, cp.int64],
                            convert_to_dtype=(cp.int32 if convert_dtype
                                              else None))

    y_pred, _, _, _ = \
        input_to_cuml_array(y_pred, check_dtype=[cp.int32, cp.int64],
                            check_rows=n_rows, check_cols=n_cols,
                            convert_to_dtype=(cp.int32 if convert_dtype
                                              else None))

    if labels is None:
        labels = sorted_unique_labels(y_true, y_pred)
        n_labels = len(labels)
    else:
        labels, n_labels, _, _ = \
            input_to_cupy_array(labels, check_dtype=[cp.int32, cp.int64],
                                convert_to_dtype=(cp.int32 if convert_dtype
                                                  else None), check_cols=1)
    if sample_weight is None:
        sample_weight = cp.ones(n_rows, dtype=dtype)
    else:
        sample_weight, _, _, _ = \
            input_to_cupy_array(sample_weight,
                                check_dtype=[cp.float32, cp.float64,
                                             cp.int32, cp.int64],
                                check_rows=n_rows, check_cols=n_cols)

    if normalize not in ['true', 'pred', 'all', None]:
        msg = "normalize must be one of " \
              f"{{'true', 'pred', 'all', None}}, got {normalize}."
        raise ValueError(msg)

    with using_output_type("cupy"):
        y_true, _ = make_monotonic(y_true, labels, copy=True)
        y_pred, _ = make_monotonic(y_pred, labels, copy=True)

    # intersect y_pred, y_true with labels, eliminate items not in labels
    ind = cp.logical_and(y_pred < n_labels, y_true < n_labels)
    y_pred = y_pred[ind]
    y_true = y_true[ind]
    sample_weight = sample_weight[ind]

    cm = cupyx.scipy.sparse.coo_matrix((sample_weight, (y_true, y_pred)),
                                       shape=(n_labels, n_labels),
                                       dtype=np.float64).toarray()

    # Choose the accumulator dtype to always have high precision
    if sample_weight.dtype.kind in {'i', 'u', 'b'}:
        cm = cm.astype(np.int64)

    with np.errstate(all='ignore'):
        if normalize == 'true':
            cm = cp.divide(cm, cm.sum(axis=1, keepdims=True))
        elif normalize == 'pred':
            cm = cp.divide(cm, cm.sum(axis=0, keepdims=True))
        elif normalize == 'all':
            cm = cp.divide(cm, cm.sum())
        cm = cp.nan_to_num(cm)

    return cm
Exemple #23
0
    def transform(self, X):
        """
        Transform X using one-hot encoding.

        Parameters
        ----------
        X : cudf.DataFrame or cupy.ndarray
            The data to encode.

        Returns
        -------
        X_out : sparse matrix if sparse=True else a 2-d array
            Transformed input.
        """
        self._check_is_fitted()
        X = self._check_input(X)

        cols, rows = list(), list()
        col_idx = None
        j = 0

        try:
            for feature in X.columns:
                encoder = self._encoders[feature]
                col_idx = encoder.transform(X[feature])
                idx_to_keep = cp.asarray(col_idx.notnull().to_gpu_array())
                col_idx = cp.asarray(col_idx.dropna().to_gpu_array())

                # Simple test to auto upscale col_idx type as needed
                # First, determine the maximum value we will add assuming
                # monotonically increasing up to len(encoder.classes_)
                # Ensure we dont go negative by clamping to 0
                max_value = int(max(len(encoder.classes_) - 1, 0) + j)

                # If we exceed the max value, upconvert
                if (max_value > np.iinfo(col_idx.dtype).max):
                    col_idx = col_idx.astype(np.min_scalar_type(max_value))
                    logger.debug("Upconverting column: '{}', to dtype: '{}', \
                            to support up to {} classes".format(
                        feature, np.min_scalar_type(max_value), max_value))

                # increase indices to take previous features into account
                col_idx += j

                # Filter out rows with null values
                row_idx = cp.arange(len(X))[idx_to_keep]

                if self.drop_idx_ is not None:
                    drop_idx = self.drop_idx_[feature] + j
                    mask = cp.ones(col_idx.shape, dtype=cp.bool)
                    mask[col_idx == drop_idx] = False
                    col_idx = col_idx[mask]
                    row_idx = row_idx[mask]
                    # account for dropped category in indices
                    col_idx[col_idx > drop_idx] -= 1
                    # account for dropped category in current cats number
                    j -= 1

                j += len(encoder.classes_)
                cols.append(col_idx)
                rows.append(row_idx)

            cols = cp.concatenate(cols)
            rows = cp.concatenate(rows)
            val = cp.ones(rows.shape[0], dtype=self.dtype)
            ohe = cupyx.scipy.sparse.coo_matrix((val, (rows, cols)),
                                                shape=(len(X), j),
                                                dtype=self.dtype)

            if not self.sparse:
                ohe = ohe.toarray()

            return ohe

        except TypeError as e:
            # Append to cols to include the column that threw the error
            cols.append(col_idx)

            # Build a string showing what the types are
            input_types_str = ", ".join([str(x.dtype) for x in cols])

            raise TypeError(
                "A TypeError occurred while calculating column "
                "category indices, most likely due to integer overflow. This "
                "can occur when columns have a large difference in the number "
                "of categories, resulting in different category code dtypes "
                "for different columns."
                "Calculated column code dtypes: {}.\n"
                "Internal Error: {}".format(input_types_str, repr(e)))
Exemple #24
0
 def testOrderFortran(self):
     sbuf = cupy.ones([3, 2]).T
     rbuf = cupy.zeros([3, 2]).T
     Sendrecv(sbuf, rbuf)
     self.assertTrue((sbuf == rbuf).all())
Exemple #25
0
 def testNotContiguous(self):
     sbuf = cupy.ones([3, 2])[:, 0]
     rbuf = cupy.zeros([3])
     self.assertRaises((BufferError, ValueError), Sendrecv, sbuf, rbuf)
Exemple #26
0
    def test_ones_like_reshape_cupy_only(self, dtype):
        a = testing.shaped_arange((2, 3, 4), cupy, dtype)
        b = cupy.ones_like(a, shape=self.shape)
        c = cupy.ones(self.shape, dtype=dtype)

        testing.assert_array_equal(b, c)
  def testInhibition(self):
    """
    Test if the firing number of coincidences after inhibition
    equals spatial pooler numActiveColumnsPerInhArea.
    """
    # Miscellaneous variables:
    # n, w:                 n, w of encoders
    # inputLen:             Length of binary input
    # synPermConnected:     Spatial pooler synPermConnected
    # synPermActiveInc:     Spatial pooler synPermActiveInc
    # connectPct:           Initial connect percentage of permanences
    # columnDimensions:     Number of spatial pooler coincidences
    # numActiveColumnsPerInhArea:  Spatial pooler numActiveColumnsPerInhArea
    # stimulusThreshold:    Spatial pooler stimulusThreshold
    # spSeed:               Spatial pooler for initial permanences
    # stimulusThresholdInh: Parameter for inhibition, default value 0.00001
    # kDutyCycleFactor:     kDutyCycleFactor for dutyCycleTieBreaker in
    #                       Inhibition
    # spVerbosity:          Verbosity to print other sp initial parameters
    # testIter:             Testing iterations
    n = 100
    w = 15
    inputLen = 300
    columnDimensions = 2048
    numActiveColumnsPerInhArea = 40
    stimulusThreshold = 0
    spSeed = 1956
    stimulusThresholdInh = 0.00001
    kDutyCycleFactor = 0.01
    spVerbosity = 0
    testIter = 100

    spTest = SpatialPooler(
                           columnDimensions=(columnDimensions, 1),
                           inputDimensions=(1, inputLen),
                           potentialRadius=inputLen / 2,
                           numActiveColumnsPerInhArea=numActiveColumnsPerInhArea,
                           spVerbosity=spVerbosity,
                           stimulusThreshold=stimulusThreshold,
                           seed=spSeed
                           )
    initialPermanence = spTest._initialPermanence()
    spTest._masterPotentialM, spTest._masterPermanenceM = (
        spTest._makeMasterCoincidences(spTest.numCloneMasters,
                                       spTest._coincRFShape,
                                       spTest.potentialPct,
                                       initialPermanence,
                                       spTest.random))

    spTest._updateInhibitionObj()
    boostFactors = cupy.ones(columnDimensions)

    for i in range(testIter):
      spTest._iterNum = i
      # random binary input
      input_ = cupy.zeros((1, inputLen))
      nonzero = numpy.random.random(inputLen)
      input_[0][cupy.where (nonzero < float(w)/float(n))] = 1

      # overlap step
      spTest._computeOverlapsFP(input_,
                                stimulusThreshold=spTest.stimulusThreshold)
      spTest._overlaps *= boostFactors
      onCellIndices = cupy.where(spTest._overlaps > 0)
      spTest._onCells.fill(0)
      spTest._onCells[onCellIndices] = 1
      denseOn = spTest._onCells

      # update _dutyCycleBeforeInh
      spTest.dutyCyclePeriod = min(i + 1, 1000)
      spTest._dutyCycleBeforeInh = (
          (spTest.dutyCyclePeriod - 1) *
          spTest._dutyCycleBeforeInh +denseOn) / spTest.dutyCyclePeriod
      dutyCycleTieBreaker = spTest._dutyCycleAfterInh.copy()
      dutyCycleTieBreaker *= kDutyCycleFactor

      # inhibition step
      numOn = spTest._inhibitionObj.compute(
          spTest._overlaps + dutyCycleTieBreaker, spTest._onCellIndices,
          stimulusThresholdInh,  # stimulusThresholdInh
          max(spTest._overlaps)/1000,  # addToWinners
      )
      # update _dutyCycleAfterInh
      spTest._onCells.fill(0)
      onCellIndices = spTest._onCellIndices[0:numOn]
      spTest._onCells[onCellIndices] = 1
      denseOn = spTest._onCells
      spTest._dutyCycleAfterInh = (((spTest.dutyCyclePeriod-1) *
                                    spTest._dutyCycleAfterInh + denseOn) /
                                   spTest.dutyCyclePeriod)

      # learning step
      spTest._adaptSynapses(onCellIndices, [], input_)

      # update boostFactor
      spTest._updateBoostFactors()
      boostFactors = spTest._firingBoostFactors

      # update dutyCycle and boost
      if ((spTest._iterNum+1) % 50) == 0:
        spTest._updateInhibitionObj()
        spTest._updateMinDutyCycles(
            spTest._dutyCycleBeforeInh,
            spTest.minPctDutyCycleBeforeInh,
            spTest._minDutyCycleBeforeInh)
        spTest._updateMinDutyCycles(
            spTest._dutyCycleAfterInh,
            spTest.minPctDutyCycleAfterInh,
            spTest._minDutyCycleAfterInh)

      # test numOn and spTest.numActiveColumnsPerInhArea
      self.assertEqual(numOn, spTest.numActiveColumnsPerInhArea,
                       "Error at input %s, actual numOn are: %i, "
                       "numActivePerInhAre is: %s" % (
                           i, numOn, numActiveColumnsPerInhArea))
def test_one_image_peak():
    """Test reconstruction with one peak pixel"""
    image = cp.ones((5, 5))
    image[2, 2] = 2
    mask = cp.ones((5, 5)) * 3
    assert_array_almost_equal(reconstruction(image, mask), 2)
def test_image_equals_mask():
    """Test reconstruction where the image and mask are the same"""
    assert_array_almost_equal(
        reconstruction(cp.ones((7, 5)), cp.ones((7, 5))), 1
    )
 def check_distribution(self, dist_func, loc_dtype, scale_dtype, dtype):
     loc = cupy.ones(self.loc_shape, dtype=loc_dtype)
     scale = cupy.ones(self.scale_shape, dtype=scale_dtype)
     out = dist_func(loc, scale, self.shape, dtype)
     self.assertEqual(self.shape, out.shape)
     self.assertEqual(out.dtype, dtype)
def test_zero_image_one_mask():
    """Test reconstruction with an image of all zeros and a mask that's not"""
    result = reconstruction(cp.zeros((10, 10)), cp.ones((10, 10)))
    assert_array_almost_equal(result, 0)
Exemple #32
0
def slogdet(a):
    """Returns sign and logarithm of the determinant of an array.

    It calculates the natural logarithm of the determinant of a given value.

    Args:
        a (cupy.ndarray): The input matrix with dimension ``(..., N, N)``.

    Returns:
        tuple of :class:`~cupy.ndarray`:
            It returns a tuple ``(sign, logdet)``. ``sign`` represents each
            sign of the determinant as a real number ``0``, ``1`` or ``-1``.
            'logdet' represents the natural logarithm of the absolute of the
            determinant.
            If the determinant is zero, ``sign`` will be ``0`` and ``logdet``
            will be ``-inf``.
            The shapes of both ``sign`` and ``logdet`` are equal to
            ``a.shape[:-2]``.

    .. warning::
        This function calls one or more cuSOLVER routine(s) which may yield
        invalid results if input conditions are not met.
        To detect these invalid results, you can set the `linalg`
        configuration to a value that is not `ignore` in
        :func:`cupyx.errstate` or :func:`cupyx.seterr`.

    .. warning::
        To produce the same results as :func:`numpy.linalg.slogdet` for
        singular inputs, set the `linalg` configuration to `raise`.

    .. seealso:: :func:`numpy.linalg.slogdet`
    """
    _util._assert_stacked_2d(a)
    _util._assert_stacked_square(a)

    dtype, sign_dtype = _util.linalg_common_type(a)
    logdet_dtype = numpy.dtype(sign_dtype.char.lower())

    a_shape = a.shape
    shape = a_shape[:-2]
    n = a_shape[-2]

    if a.size == 0:
        # empty batch (result is empty, too) or empty matrices det([[]]) == 1
        sign = cupy.ones(shape, sign_dtype)
        logdet = cupy.zeros(shape, logdet_dtype)
        return sign, logdet

    lu, ipiv, dev_info = _decomposition._lu_factor(a, dtype)

    # dev_info < 0 means illegal value (in dimensions, strides, and etc.) that
    # should never happen even if the matrix contains nan or inf.
    # TODO(kataoka): assert dev_info >= 0 if synchronization is allowed for
    # debugging purposes.

    diag = cupy.diagonal(lu, axis1=-2, axis2=-1)

    logdet = cupy.log(cupy.abs(diag)).sum(axis=-1)

    # ipiv is 1-origin
    non_zero = cupy.count_nonzero(ipiv != cupy.arange(1, n + 1), axis=-1)
    if dtype.kind == "f":
        non_zero += cupy.count_nonzero(diag < 0, axis=-1)

    # Note: sign == -1 ** (non_zero % 2)
    sign = (non_zero % 2) * -2 + 1
    if dtype.kind == "c":
        sign = sign * cupy.prod(diag / cupy.abs(diag), axis=-1)

    sign = sign.astype(dtype)
    logdet = logdet.astype(logdet_dtype, copy=False)
    singular = dev_info > 0
    return (
        cupy.where(singular, sign_dtype.type(0), sign).reshape(shape),
        cupy.where(singular, logdet_dtype.type('-inf'), logdet).reshape(shape),
    )
import cupy as xp
from chainer import Variable, gradient_check, testing
from sum_of_squared_error import *

batch_size = 32
n_input = 10

x_data = xp.random.randn(batch_size, n_input).astype(xp.float32)
t_data = xp.zeros(x_data.shape, dtype=xp.float32)
for i in range(batch_size):
    t_data[i][xp.random.randint(n_input)] = 1

x = Variable(x_data)
t = Variable(t_data)
y = sum_of_squared_error(x, t)

# backward to compute dy/dx
y.grad = xp.ones(y.data.shape, dtype=xp.float32)
y.backward()

# compute numerical grad
f = lambda: (sum_of_squared_error(x_data, t_data).data, )
gx, = gradient_check.numerical_grad(f, (x.data, ), (y.grad, ))

testing.assert_allclose(gx, x.grad)
Exemple #34
0
def cupy_ones(*args, **kwargs):
    return cupy.ones(*args, **kwargs)
import numpy as np
import cupy
from chainer import cuda

def _mul_i():
    return cuda.elementwise(
            "raw T x", "raw T y",
            """
                y[i] = x[i]
            """,
            "muli")

o = cupy.ones((3,2,2))
y = cupy.zeros_like(o)
print _mul_i()(o,y, size=6)
Exemple #36
0
def shift(input,
          shift,
          output=None,
          order=3,
          mode='constant',
          cval=0.0,
          prefilter=True):
    """Shift an array.

    The array is shifted using spline interpolation of the requested order.
    Points outside the boundaries of the input are filled according to the
    given mode.

    Args:
        input (cupy.ndarray): The input array.
        shift (float or sequence): The shift along the axes. If a float,
            ``shift`` is the same for each axis. If a sequence, ``shift``
            should contain one value for each axis.
        output (cupy.ndarray or ~cupy.dtype): The array in which to place the
            output, or the dtype of the returned array.
        order (int): The order of the spline interpolation, default is 3. Must
            be in the range 0-5.
        mode (str): Points outside the boundaries of the input are filled
            according to the given mode (``'constant'``, ``'nearest'``,
            ``'mirror'``, ``'reflect'``, ``'wrap'``, ``'grid-mirror'``,
            ``'grid-wrap'``, ``'grid-constant'`` or ``'opencv'``).
        cval (scalar): Value used for points outside the boundaries of
            the input if ``mode='constant'`` or ``mode='opencv'``. Default is
            0.0
        prefilter (bool): It is not used yet. It just exists for compatibility
            with :mod:`scipy.ndimage`.

    Returns:
        cupy.ndarray or None:
            The shifted input.

    .. seealso:: :func:`scipy.ndimage.shift`
    """

    _check_parameter('shift', order, mode)

    shift = _util._fix_sequence_arg(shift, input.ndim, 'shift', float)

    if mode == 'opencv':
        mode = '_opencv_edge'

        output = affine_transform(
            input,
            cupy.ones(input.ndim, input.dtype),
            cupy.negative(cupy.asarray(shift)),
            None,
            output,
            order,
            mode,
            cval,
            prefilter,
        )
    else:
        output = _util._get_output(output, input)
        if input.dtype.kind in 'iu':
            input = input.astype(cupy.float32)
        filtered, nprepad = _filter_input(input, prefilter, mode, cval, order)
        integer_output = output.dtype.kind in 'iu'
        _util._check_cval(mode, cval, integer_output)
        large_int = _prod(input.shape) > 1 << 31
        kern = _interp_kernels._get_shift_kernel(input.ndim,
                                                 large_int,
                                                 input.shape,
                                                 mode,
                                                 cval=cval,
                                                 order=order,
                                                 integer_output=integer_output,
                                                 nprepad=nprepad)
        shift = cupy.asarray(shift, dtype=cupy.float64, order='C')
        if shift.ndim != 1:
            raise ValueError('shift must be 1d')
        if shift.size != filtered.ndim:
            raise ValueError('len(shift) must equal input.ndim')
        kern(filtered, shift, output)
    return output
Exemple #37
-1
def conv(x, w, b, count=1, x_nhwc=False, w_nhwc=False):
    y_shape = (bsize, ochan, ow, oh)
    d_layout = cudnn.CUDNN_TENSOR_NCHW
    w_layout = cudnn.CUDNN_TENSOR_NCHW

    if x_nhwc:
        d_layout = cudnn.CUDNN_TENSOR_NHWC
        x = np.transpose(x, (0, 2, 3, 1))
        y_shape = (bsize, ow, oh, ochan)

    if w_nhwc:
        w_layout = cudnn.CUDNN_TENSOR_NHWC
        w = np.transpose(w, (0, 2, 3, 1))

    x = cupy.array(x)
    w = cupy.array(w)
    b = cupy.array(b)
    y = cupy.ones(y_shape, dtype=x.dtype)
    times = [time.time()]
    for _ in range(count):
        cupy.cudnn.convolution_forward(x, w, b, y, (0, 0), (1, 1), (1, 1), 1,
                                       auto_tune=True, tensor_core='auto',
                                       d_layout=d_layout, w_layout=w_layout)
        cupy.cuda.device.Device().synchronize()
        times.append(time.time())

    if count > 1:
        print('Elapsed:', (times[-1] - times[1]) / (count - 1))

    y = chainer.cuda.to_cpu(y)
    if x_nhwc:
        y = np.transpose(y, (0, 3, 1, 2))
    return y