How to use the cupy.zeros function in cupy

To help you get started, we’ve selected a few cupy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rossant / pykilosort / pykilosort / cluster.py View on Github external
maxFR = constants.maxFR

    NT, Nchan, NchanNear, nt0, nt0min, spkTh, NrankPC = Params
    NT = int(NT)
    Nchan = int(Nchan)

    # Input GPU arrays.
    d_Params = cp.asarray(Params, dtype=np.float64, order='F')
    d_data = cp.asarray(dataRAW, dtype=np.float32, order='F')
    d_W = cp.asarray(wPCA, dtype=np.float32, order='F')
    d_iC = cp.asarray(iC, dtype=np.int32, order='F')

    # New GPU arrays.
    d_dout = cp.zeros((Nchan, NT), dtype=np.float32, order='F')
    d_dmax = cp.zeros((Nchan, NT), dtype=np.float32, order='F')
    d_st = cp.zeros(maxFR, dtype=np.int32, order='F')
    d_id = cp.zeros(maxFR, dtype=np.int32, order='F')
    d_counter = cp.zeros(1, dtype=np.int32, order='F')

    # filter the data with the temporal templates
    Conv1D = cp.RawKernel(code, 'Conv1D')
    Conv1D((Nchan,), (Nthreads,), (d_Params, d_data, d_W, d_dout))

    # get the max of the data
    max1D = cp.RawKernel(code, 'max1D')
    max1D((Nchan,), (Nthreads,), (d_Params, d_dout, d_dmax))

    # take max across nearby channels
    maxChannels = cp.RawKernel(code, 'maxChannels')
    maxChannels(
        (int(NT // Nthreads),), (Nthreads,),
        (d_Params, d_dout, d_dmax, d_iC, d_st, d_id, d_counter))
github bwohlberg / sporco / tests / cupy / admm / test_cbpdn.py View on Github external
def test_10(self):
        N = 64
        M = 4
        Nd = 8
        D = cp.random.randn(Nd, Nd, M)
        X0 = cp.zeros((N, N, M))
        xr = cp.random.randn(N, N, M)
        xp = cp.abs(xr) > 3
        X0[xp] = cp.random.randn(X0[xp].size)
        S = cp.sum(sl.fftconv(D, X0), axis=2)
        lmbda = 1e-4
        rho = 1e-1
        opt = cbpdn.ConvBPDN.Options({'Verbose': False, 'MaxMainIter': 500,
                                      'RelStopTol': 1e-3, 'rho': rho,
                                      'AutoRho': {'Enabled': False}})
        b = cbpdn.ConvBPDN(D, S, lmbda, opt)
        b.solve()
        X1 = b.Y.squeeze()
        assert sl.rrs(X0, X1) < 5e-5
        Sr = b.reconstruct().squeeze()
        assert sl.rrs(S, Sr) < 1e-4
github bwohlberg / sporco / tests / cupy / fista / test_cbpdn.py View on Github external
def test_10(self):
        N = 64
        M = 4
        Nd = 8
        D = cp.random.randn(Nd, Nd, M)
        X0 = cp.zeros((N, N, M))
        xr = cp.random.randn(N, N, M)
        xp = cp.abs(xr) > 3
        X0[xp] = cp.random.randn(X0[xp].size)
        S = cp.sum(sl.ifftn(sl.fftn(D, (N, N), (0, 1)) *
                            sl.fftn(X0, None, (0, 1)), None, (0, 1)).real,
                   axis=2)
        lmbda = 1e-2
        L = 1e3
        opt = cbpdn.ConvBPDN.Options({'Verbose': False, 'MaxMainIter': 2000,
                                      'RelStopTol': 1e-9, 'L': L,
                                      'BackTrack': {'Enabled': False}})
        b = cbpdn.ConvBPDN(D, S, lmbda, opt)
        b.solve()
        X1 = b.X.squeeze()
        assert sl.rrs(X0, X1) < 5e-4
        Sr = b.reconstruct().squeeze()
github chainer / chainer / cupy / random / generator.py View on Github external
``size`` is returned.
            Currently, each element of the array is ``numpy.int32``.
        """
        dtype = numpy.int32
        if size is None:
            return self.interval(mx, 1).reshape(())
        elif isinstance(size, int):
            size = (size, )

        if mx == 0:
            return cupy.zeros(size, dtype=dtype)

        mask = (1 << mx.bit_length()) - 1
        mask = cupy.array(mask, dtype=dtype)

        ret = cupy.zeros(size, dtype=dtype)
        sample = cupy.zeros(size, dtype=dtype)
        done = cupy.zeros(size, dtype=numpy.bool_)
        while True:
            curand.generate(
                self._generator, sample.data.ptr, sample.size)
            sample &= mask
            success = sample <= mx
            ret = cupy.where(success, sample, ret)
            done |= success
            if done.all():
                return ret
github rossant / pykilosort / pykilosort / learn.py View on Github external
d_iList = cp.asarray(iList, dtype=np.int32, order='F')
    d_wPCA = cp.asarray(wPCA, dtype=np.float32, order='F')

    d_nsp = cp.zeros(Nfilt, dtype=np.int32, order='F')
    d_dWU = cp.zeros((nt0, Nchan, Nfilt), dtype=np.float64, order='F')

    d_dout = cp.zeros((2 * NT, Nfilt), dtype=np.float32, order='F')
    d_data = cp.zeros((NT, Nfilt, Nrank), dtype=np.float32, order='F')
    d_err = cp.zeros(NT, dtype=np.float32, order='F')
    d_ftype = cp.zeros(NT, dtype=np.int32, order='F')
    d_eloss = cp.zeros(NT, dtype=np.float32, order='F')
    d_st = cp.zeros(maxFR, dtype=np.int32, order='F')
    d_id = cp.zeros(maxFR, dtype=np.int32, order='F')
    d_x = cp.zeros(maxFR, dtype=np.float32, order='F')
    d_y = cp.zeros(maxFR, dtype=np.float32, order='F')
    d_z = cp.zeros(maxFR, dtype=np.float32, order='F')

    d_counter = cp.zeros(2, dtype=np.int32, order='F')
    d_count = cp.zeros(nmaxiter, dtype=np.int32, order='F')
    d_feat = cp.zeros((Nnearest, maxFR), dtype=np.float32, order='F')
    d_featPC = cp.zeros((NchanU, Nrank, maxFR), dtype=np.float32, order='F')

    counter = np.zeros(2, dtype=np.int32, order='F')

    # tpB = (8, 2 * nt0 - 1)
    tpF = (16, Nnearest)
    tpS = (nt0, 16)
    # tpW = (Nnearest, Nrank)
    tpPC = (NchanU, Nrank)

    # filter the data with the spatial templates
    spaceFilter = cp.RawKernel(code, 'spaceFilter')
github rossant / pykilosort / pykilosort / postprocess.py View on Github external
x = cp.dot(clp, w)
        s1 = var(x[x > mean(x)])  # initialize estimates of variance for the first
        s2 = var(x[x < mean(x)])  # and second gaussian in the mixture of 1D gaussians

        mu1 = mean(x[x > mean(x)])  # initialize the means as well
        mu2 = mean(x[x < mean(x)])
        # and the probability that a spike is assigned to the first Gaussian
        p = mean(x > mean(x))

        # initialize matrix of log probabilities that each spike is assigned to the first
        # or second cluster
        logp = cp.zeros((nSpikes, 2), order='F')

        # do 50 pursuit iteration

        logP = cp.zeros(50)  # used to monitor the cost function

        for k in range(50):
            # for each spike, estimate its probability to come from either Gaussian cluster
            logp[:, 0] = -1. / 2 * log(s1) - ((x - mu1) ** 2) / (2 * s1) + log(p)
            logp[:, 1] = -1. / 2 * log(s2) - ((x - mu2) ** 2) / (2 * s2) + log(1 - p)

            lMax = logp.max(axis=1)
            logp = logp - lMax[:, cp.newaxis]  # subtract the max for floating point accuracy
            rs = cp.exp(logp)  # exponentiate the probabilities

            pval = cp.log(cp.sum(rs, axis=1)) + lMax  # get the normalizer and add back the max
            logP[k] = mean(pval)  # this is the cost function: we can monitor its increase

            rs = rs / cp.sum(rs, axis=1)[:, cp.newaxis]  # normalize so that probabilities sum to 1

            p = mean(rs[:, 0])  # mean probability to be assigned to Gaussian 1
github rossant / pykilosort / pykilosort / preprocess.py View on Github external
NTbuff = params.NTbuff
    whiteningRange = params.whiteningRange
    scaleproc = params.scaleproc
    NT = params.NT
    fs = params.fs
    fshigh = params.fshigh
    nSkipCov = params.nSkipCov

    xc = probe.xc
    yc = probe.yc
    chanMap = probe.chanMap
    Nchan = probe.Nchan
    chanMap = probe.chanMap

    # Nchan is obtained after the bad channels have been removed
    CC = cp.zeros((Nchan, Nchan))

    for ibatch in tqdm(range(0, Nbatch, nSkipCov), desc="Computing the whitening matrix"):
        i = max(0, (NT - ntbuff) * ibatch - 2 * ntbuff)
        # WARNING: we no longer use Fortran order, so raw_data is nsamples x NchanTOT
        buff = raw_data[i:i + NT - ntbuff]
        assert buff.shape[0] > buff.shape[1]
        assert buff.flags.c_contiguous

        nsampcurr = buff.shape[0]
        if nsampcurr < NTbuff:
            buff = np.concatenate(
                (buff, np.tile(buff[nsampcurr - 1], (NTbuff, 1))), axis=0)

        buff_g = cp.asarray(buff, dtype=np.float32)

        # apply filters and median subtraction
github rossant / pykilosort / pykilosort / learn.py View on Github external
Nrank = int(Params[14])

    maxFR = constants.maxFR
    Nthreads = constants.Nthreads

    # tpB = (8, 2 * nt0 - 1)
    # tpF = (16, Nnearest)
    tpS = (nt0, 16)

    d_Params = cp.asarray(Params, dtype=np.float64, order='F')
    d_data = cp.asarray(drez, dtype=np.float32, order='F')
    d_W = cp.asarray(wTEMP, dtype=np.float32, order='F')
    d_iC = cp.asarray(iC, dtype=np.int32, order='F')

    d_counter = cp.zeros(2, dtype=np.int32, order='F')
    d_dout = cp.zeros((NT, Nchan), dtype=np.float32, order='F')
    d_dfilt = cp.zeros((Nrank, NT, Nchan), dtype=np.float32, order='F')
    d_err = cp.zeros(NT, dtype=np.float32, order='F')
    d_kkmax = cp.zeros((NT, Nchan), dtype=np.int32, order='F')
    d_kk = cp.zeros(NT, dtype=np.int32, order='F')
    d_ftype = cp.zeros(NT, dtype=np.int32, order='F')
    d_st = cp.zeros(maxFR, dtype=np.int32, order='F')
    d_id = cp.zeros(maxFR, dtype=np.int32, order='F')
    d_x = cp.zeros(maxFR, dtype=np.float32, order='F')
    d_st1 = cp.zeros(maxFR, dtype=np.int32, order='F')
    d_id1 = cp.zeros(maxFR, dtype=np.int32, order='F')

    counter = np.zeros(2, dtype=np.int32, order='F')

    # filter the data with the temporal templates
    Conv1D = cp.RawKernel(code, 'Conv1D')
    Conv1D((Nchan,), (Nthreads,), (d_Params, d_data, d_W, d_dfilt))
github rossant / pykilosort / pykilosort / learn.py View on Github external
NchanU = int(Params[10])
    Nchan = int(Params[9])

    d_Params = cp.asarray(Params, dtype=np.float64, order='F')

    d_draw = cp.asarray(dataRAW, dtype=np.float32, order='F')
    d_U = cp.asarray(U, dtype=np.float32, order='F')
    d_W = cp.asarray(W, dtype=np.float32, order='F')
    d_mu = cp.asarray(mu, dtype=np.float32, order='F')
    d_iC = cp.asarray(iC, dtype=np.int32, order='F')
    d_iW = cp.asarray(iW, dtype=np.int32, order='F')
    d_UtU = cp.asarray(UtU, dtype=np.bool, order='F')
    d_iList = cp.asarray(iList, dtype=np.int32, order='F')
    d_wPCA = cp.asarray(wPCA, dtype=np.float32, order='F')

    d_nsp = cp.zeros(Nfilt, dtype=np.int32, order='F')
    d_dWU = cp.zeros((nt0, Nchan, Nfilt), dtype=np.float64, order='F')

    d_dout = cp.zeros((2 * NT, Nfilt), dtype=np.float32, order='F')
    d_data = cp.zeros((NT, Nfilt, Nrank), dtype=np.float32, order='F')
    d_err = cp.zeros(NT, dtype=np.float32, order='F')
    d_ftype = cp.zeros(NT, dtype=np.int32, order='F')
    d_eloss = cp.zeros(NT, dtype=np.float32, order='F')
    d_st = cp.zeros(maxFR, dtype=np.int32, order='F')
    d_id = cp.zeros(maxFR, dtype=np.int32, order='F')
    d_x = cp.zeros(maxFR, dtype=np.float32, order='F')
    d_y = cp.zeros(maxFR, dtype=np.float32, order='F')
    d_z = cp.zeros(maxFR, dtype=np.float32, order='F')

    d_counter = cp.zeros(2, dtype=np.int32, order='F')
    d_count = cp.zeros(nmaxiter, dtype=np.int32, order='F')
    d_feat = cp.zeros((Nnearest, maxFR), dtype=np.float32, order='F')
github rossant / pykilosort / pykilosort / learn.py View on Github external
d_U = cp.asarray(U, dtype=np.float32, order='F')
    d_W = cp.asarray(W, dtype=np.float32, order='F')
    d_mu = cp.asarray(mu, dtype=np.float32, order='F')
    d_iC = cp.asarray(iC, dtype=np.int32, order='F')
    d_iW = cp.asarray(iW, dtype=np.int32, order='F')
    d_UtU = cp.asarray(UtU, dtype=np.bool, order='F')
    d_iList = cp.asarray(iList, dtype=np.int32, order='F')
    d_wPCA = cp.asarray(wPCA, dtype=np.float32, order='F')

    d_nsp = cp.zeros(Nfilt, dtype=np.int32, order='F')
    d_dWU = cp.zeros((nt0, Nchan, Nfilt), dtype=np.float64, order='F')

    d_dout = cp.zeros((2 * NT, Nfilt), dtype=np.float32, order='F')
    d_data = cp.zeros((NT, Nfilt, Nrank), dtype=np.float32, order='F')
    d_err = cp.zeros(NT, dtype=np.float32, order='F')
    d_ftype = cp.zeros(NT, dtype=np.int32, order='F')
    d_eloss = cp.zeros(NT, dtype=np.float32, order='F')
    d_st = cp.zeros(maxFR, dtype=np.int32, order='F')
    d_id = cp.zeros(maxFR, dtype=np.int32, order='F')
    d_x = cp.zeros(maxFR, dtype=np.float32, order='F')
    d_y = cp.zeros(maxFR, dtype=np.float32, order='F')
    d_z = cp.zeros(maxFR, dtype=np.float32, order='F')

    d_counter = cp.zeros(2, dtype=np.int32, order='F')
    d_count = cp.zeros(nmaxiter, dtype=np.int32, order='F')
    d_feat = cp.zeros((Nnearest, maxFR), dtype=np.float32, order='F')
    d_featPC = cp.zeros((NchanU, Nrank, maxFR), dtype=np.float32, order='F')

    counter = np.zeros(2, dtype=np.int32, order='F')

    # tpB = (8, 2 * nt0 - 1)
    tpF = (16, Nnearest)