Python cupy.concatenate() Examples

The following are 23 code examples of cupy.concatenate(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cupy , or try the search function .
Example #1
Source File: test_cbpdnin.py    From sporco with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_05(self):
        D = cp.random.randn(4, 32)
        s = cp.random.randn(64)
        Wg = np.concatenate((cp.eye(16), cp.eye(16)), axis=-1)
        lmbda = 0.1
        mu = 0.01
        gamma = 0.01

        # ConvBPDNInhib class
        opt = cbpdnin.ConvBPDNInhib.Options(
            {'Verbose': False, 'MaxMainIter': 10})

        try:
            b = cbpdnin.ConvBPDNInhib(
                D, s, Wg=Wg, lmbda=lmbda, mu=mu, gamma=gamma, opt=opt, dimN=1)
            b.solve()
        except Exception as e:
            print(e)
            assert 0 
Example #2
Source File: test_cbpdnin.py    From sporco with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_02(self):
        D = cp.random.randn(4, 4, 32)
        s = cp.random.randn(8, 8)
        Wg = cp.concatenate((cp.eye(16), cp.eye(16)), axis=-1)
        lmbda = 0.1

        # ConvBPDNInhib class
        opt = cbpdnin.ConvBPDNInhib.Options(
            {'Verbose': False, 'MaxMainIter': 10})

        try:
            b = cbpdnin.ConvBPDNInhib(D, s, Wg=Wg, lmbda=lmbda, opt=opt)
            b.solve()
        except Exception as e:
            print(e)
            assert 0 
Example #3
Source File: test_join.py    From cupy with MIT License 5 votes vote down vote up
def test_concatenate_32bit_boundary(self):
        a = cupy.zeros((2 ** 30,), dtype=cupy.int8)
        b = cupy.zeros((2 ** 30,), dtype=cupy.int8)
        ret = cupy.concatenate([a, b])
        del a
        del b
        del ret
        # Free huge memory for slow test
        cupy.get_default_memory_pool().free_all_blocks() 
Example #4
Source File: pcanet.py    From PCANet with MIT License 5 votes vote down vote up
def histogram(self, binary_images):
        """
        Separate a given image into blocks and calculate a histogram
        in each block.

        Supporse data in a block is in range [0, 3] and the acutual
        values are

        ::

            [0 0 1]
            [2 2 2]
            [2 3 3]

        | If default bins ``[-0.5 0.5 1.5 2.5 3.5]`` applied,
          the histogram will be ``[2 1 4 2]``.
        | If ``n_bins`` is specified, the range of data divided equally.

        | For example, if the data is in range ``[0, 3]`` and ``n_bins = 2``,
        | bins will be ``[-0.5 1.5 3.5]`` and the histogram will be ``[3 6]``.
        """

        k = pow(2, self.n_l2_output)
        if self.n_bins is None:
            self.n_bins = k + 1
        bins = xp.linspace(-0.5, k - 0.5, self.n_bins)

        def bhist(image):
            # calculate Bhist(T) in the original paper
            ps = Patches(
                image,
                self.filter_shape_pooling,
                self.step_shape_pooling).patches

            H = [xp.histogram(p.flatten(), bins)[0] for p in ps]
            return xp.concatenate(H)
        return xp.vstack([bhist(image) for image in binary_images]) 
Example #5
Source File: model.py    From TSNetVocoder with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def loss(self, X, T, A):
        def duplication(a):
            return cupy.concatenate(cupy.broadcast_to(a, (self.fs, a.shape[0], a.shape[1])), axis=1).reshape(a.shape[0]*self.fs, -1)

        A = [duplication(a) for a in A]
        X, T = [Variable(x) for x in X], [Variable(t) for t in T]
        Y = self._forward(X, T=T)
        loss, lossA, lossP = self._loss(Y, T, A)
        reporter.report({'loss': loss, 'lossA': lossA, 'lossP': lossP}, self)
        return loss 
Example #6
Source File: test_join.py    From cupy with MIT License 5 votes vote down vote up
def test_concatenate_out_invalid_dtype(self):
        for xp in (numpy, cupy):
            a = testing.shaped_arange((3, 4), xp, xp.float64)
            b = testing.shaped_reverse_arange((3, 4), xp, xp.float64)
            c = testing.shaped_arange((3, 4), xp, xp.float64)
            out = xp.zeros((3, 12), dtype=xp.int64)
            with pytest.raises(TypeError):
                xp.concatenate((a, b, c), axis=1, out=out) 
Example #7
Source File: test_join.py    From cupy with MIT License 5 votes vote down vote up
def test_concatenate_out_invalid_shape_2(self):
        for xp in (numpy, cupy):
            a = testing.shaped_arange((3, 4), xp, xp.float64)
            b = testing.shaped_reverse_arange((3, 4), xp, xp.float64)
            c = testing.shaped_arange((3, 4), xp, xp.float64)
            out = xp.zeros((2, 2, 10), dtype=xp.float64)
            with pytest.raises(ValueError):
                xp.concatenate((a, b, c), axis=1, out=out) 
Example #8
Source File: test_join.py    From cupy with MIT License 5 votes vote down vote up
def test_concatenate_out_same_kind(self, xp):
        a = testing.shaped_arange((3, 4), xp, xp.float64)
        b = testing.shaped_reverse_arange((3, 4), xp, xp.float64)
        c = testing.shaped_arange((3, 4), xp, xp.float64)
        out = xp.zeros((3, 12), dtype=xp.float32)
        xp.concatenate((a, b, c), axis=1, out=out)
        return out 
Example #9
Source File: test_join.py    From cupy with MIT License 5 votes vote down vote up
def test_concatenate_out(self, xp, dtype):
        a = testing.shaped_arange((3, 4), xp, dtype)
        b = testing.shaped_reverse_arange((3, 4), xp, dtype)
        c = testing.shaped_arange((3, 4), xp, dtype)
        out = xp.zeros((3, 12), dtype=dtype)
        xp.concatenate((a, b, c), axis=1, out=out)
        return out 
Example #10
Source File: test_join.py    From cupy with MIT License 5 votes vote down vote up
def test_concatenate_wrong_shape(self):
        a = cupy.empty((2, 3, 4))
        b = cupy.empty((3, 3, 4))
        c = cupy.empty((4, 4, 4))
        with self.assertRaises(ValueError):
            cupy.concatenate((a, b, c)) 
Example #11
Source File: test_join.py    From cupy with MIT License 5 votes vote down vote up
def test_concatenate_wrong_ndim(self):
        a = cupy.empty((2, 3))
        b = cupy.empty((2,))
        with self.assertRaises(ValueError):
            cupy.concatenate((a, b)) 
Example #12
Source File: test_join.py    From cupy with MIT License 5 votes vote down vote up
def test_concatenate_large_f_contiguous(self, xp, dtype):
        a = testing.shaped_arange((2, 3, 4), xp, dtype)
        b = testing.shaped_arange((2, 3, 2), xp, dtype).T
        c = testing.shaped_arange((2, 3, 3), xp, dtype)
        d = testing.shaped_arange((2, 3, 2), xp, dtype).T
        e = testing.shaped_arange((2, 3, 2), xp, dtype)
        return xp.concatenate((a, b, c, d, e) * 2, axis=-1) 
Example #13
Source File: test_join.py    From cupy with MIT License 5 votes vote down vote up
def test_concatenate_f_contiguous(self, xp, dtype):
        a = testing.shaped_arange((2, 3, 4), xp, dtype)
        b = testing.shaped_arange((2, 3, 2), xp, dtype).T
        c = testing.shaped_arange((2, 3, 3), xp, dtype)
        return xp.concatenate((a, b, c), axis=-1) 
Example #14
Source File: test_join.py    From cupy with MIT License 5 votes vote down vote up
def test_concatenate_large_different_devices(self):
        arrs = []
        for i in range(10):
            with cuda.Device(i % 2):
                arrs.append(cupy.empty((2, 3, 4)))
        with pytest.raises(ValueError):
            cupy.concatenate(arrs) 
Example #15
Source File: test_join.py    From cupy with MIT License 5 votes vote down vote up
def test_concatenate_large_5(self, xp, dtype):
        a = testing.shaped_arange((2, 3, 4), xp, dtype)
        b = testing.shaped_reverse_arange((2, 3, 4), xp, 'i')
        return xp.concatenate((a, b) * 10, axis=-1) 
Example #16
Source File: test_join.py    From cupy with MIT License 5 votes vote down vote up
def test_concatenate_large_4(self, xp, dtype):
        a = testing.shaped_arange((2, 3, 4), xp, dtype)
        b = testing.shaped_reverse_arange((2, 3, 4), xp, dtype)
        return xp.concatenate((a, b) * 10, axis=-1) 
Example #17
Source File: test_join.py    From cupy with MIT License 5 votes vote down vote up
def test_concatenate_large_2(self, xp, dtype):
        a = testing.shaped_arange((2, 3, 4), xp, dtype)
        b = testing.shaped_reverse_arange((2, 3, 2), xp, dtype)
        c = testing.shaped_arange((2, 3, 3), xp, dtype)
        d = testing.shaped_arange((2, 3, 5), xp, dtype)
        e = testing.shaped_arange((2, 3, 2), xp, dtype)
        return xp.concatenate((a, b, c, d, e) * 2, axis=-1) 
Example #18
Source File: test_join.py    From cupy with MIT License 5 votes vote down vote up
def test_concatenate_axis_none(self, xp, dtype):
        a = testing.shaped_arange((2, 3), xp, dtype)
        b = testing.shaped_reverse_arange((3, 5, 2), xp, dtype)
        c = testing.shaped_arange((7, ), xp, dtype)
        return xp.concatenate((a, b, c), axis=None) 
Example #19
Source File: test_join.py    From cupy with MIT License 5 votes vote down vote up
def test_concatenate2(self, xp, dtype):
        a = testing.shaped_arange((2, 3, 4), xp, dtype)
        b = testing.shaped_reverse_arange((2, 3, 2), xp, dtype)
        c = testing.shaped_arange((2, 3, 3), xp, dtype)
        return xp.concatenate((a, b, c), axis=-1) 
Example #20
Source File: test_join.py    From cupy with MIT License 5 votes vote down vote up
def test_concatenate1(self, xp, dtype):
        a = testing.shaped_arange((2, 3, 4), xp, dtype)
        b = testing.shaped_reverse_arange((2, 3, 2), xp, dtype)
        c = testing.shaped_arange((2, 3, 3), xp, dtype)
        return xp.concatenate((a, b, c), axis=2) 
Example #21
Source File: construct.py    From cupy with MIT License 5 votes vote down vote up
def _compressed_sparse_stack(blocks, axis):
    """Fast path for stacking CSR/CSC matrices
    (i) vstack for CSR, (ii) hstack for CSC.
    """
    other_axis = 1 if axis == 0 else 0
    data = cupy.concatenate([b.data for b in blocks])
    constant_dim = blocks[0].shape[other_axis]
    idx_dtype = sputils.get_index_dtype(arrays=[b.indptr for b in blocks],
                                        maxval=max(data.size, constant_dim))
    indices = cupy.empty(data.size, dtype=idx_dtype)
    indptr = cupy.empty(sum(b.shape[axis]
                            for b in blocks) + 1, dtype=idx_dtype)
    last_indptr = idx_dtype(0)
    sum_dim = 0
    sum_indices = 0
    for b in blocks:
        if b.shape[other_axis] != constant_dim:
            raise ValueError(
                'incompatible dimensions for axis %d' % other_axis)
        indices[sum_indices:sum_indices+b.indices.size] = b.indices
        sum_indices += b.indices.size
        idxs = slice(sum_dim, sum_dim + b.shape[axis])
        indptr[idxs] = b.indptr[:-1]
        indptr[idxs] += last_indptr
        sum_dim += b.shape[axis]
        last_indptr += b.indptr[-1]
    indptr[-1] = last_indptr
    if axis == 0:
        return csr.csr_matrix((data, indices, indptr),
                              shape=(sum_dim, constant_dim))
    else:
        return csc.csc_matrix((data, indices, indptr),
                              shape=(constant_dim, sum_dim)) 
Example #22
Source File: inception_resnet_v2.py    From nips17-adversarial-attack with MIT License 4 votes vote down vote up
def __call__(self, x):
        with chainer.function.force_backprop_mode():
            with chainer.configuration.using_config('train', False):
                if isinstance(x, chainer.Variable):
                    x = x.data

                x = x[:, :, 10:309, 10:309]

                x = chainer.Variable(x)
                hs_enc = self.model(x)
                prob = hs_enc[-1]
                hs_enc = [x] + hs_enc[:-1]

                t = xp.argmax(prob.data, axis=1).astype(xp.int32)
                loss = F.softmax_cross_entropy(prob, t) * float(x.shape[0])
                loss.backward(retain_grad=True)

        del loss
        del prob
        for h in hs_enc:
            h.unchain_backward()

        data_scales = [1e-2, 1e0, 1e0, 1e0, 1e1, 1e0]
        grad_scales = [1e4, 1e3, 1e3, 1e2, 1e2, 1e4]
        for h, ds, gs in zip(hs_enc, data_scales, grad_scales):
            h.data *= ds
            h.grad *= gs

        #self.hoge.append([float(xp.std(h.data)) for h in hs_enc])
        #import numpy as np
        #print(1 / np.mean(self.hoge, axis=0))

        target_sizes = [320, 160, 80, 40, 20, 10]
        for i, h in enumerate(hs_enc):
            t = target_sizes[i]
            s = h.shape[2]

            h = xp.concatenate((h.data, h.grad), axis=1)
            p1 = (t - s) // 2
            p2 = t - s - p1
            h = xp.pad(h, ((0, 0), (0, 0), (p1, p2), (p1, p2)), 'constant', constant_values=0.0)
            hs_enc[i] = h

        return hs_enc 
Example #23
Source File: _mass_ts.py    From mass-ts with Apache License 2.0 4 votes vote down vote up
def mass2_gpu(ts, query):
    """
    Compute the distance profile for the given query over the given time 
    series. This require cupy to be installed.

    Parameters
    ----------
    ts : array_like
        The array to create a rolling window on.
    query : array_like
        The query.

    Returns
    -------
    An array of distances.

    Raises
    ------
    ValueError
        If ts is not a list or np.array.
        If query is not a list or np.array.
        If ts or query is not one dimensional.
    """
    def moving_mean_std_gpu(a, w):
        s = cp.concatenate([cp.array([0]), cp.cumsum(a)])
        sSq = cp.concatenate([cp.array([0]), cp.cumsum(a ** 2)])
        segSum = s[w:] - s[:-w]
        segSumSq = sSq[w:] -sSq[:-w]
    
        movmean = segSum / w
        movstd = cp.sqrt(segSumSq / w - (segSum / w) ** 2)
    
        return (movmean, movstd)

    x = cp.asarray(ts)
    y = cp.asarray(query)
    n = x.size
    m = y.size

    meany = cp.mean(y)
    sigmay = cp.std(y)
    
    meanx, sigmax = moving_mean_std_gpu(x, m)
    meanx = cp.concatenate([cp.ones(n - meanx.size), meanx])
    sigmax = cp.concatenate([cp.zeros(n - sigmax.size), sigmax])
    
    y = cp.concatenate((cp.flip(y, axis=0), cp.zeros(n - m)))
    
    X = cp.fft.fft(x)
    Y = cp.fft.fft(y)
    Z = X * Y
    z = cp.fft.ifft(Z)
    
    dist = 2 * (m - (z[m - 1:n] - m * meanx[m - 1:n] * meany) / 
                    (sigmax[m - 1:n] * sigmay))
    dist = cp.sqrt(dist)

    return cp.asnumpy(dist)