Python cupy.abs() Examples

The following are 27 code examples of cupy.abs(). 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_cbpdn.py    From sporco with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_11(self):
        N = 63
        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(ifftn(fftn(D, (N, N), (0, 1)) *
                         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 rrs(X0, X1) < 5e-4
        Sr = b.reconstruct().squeeze()
        assert rrs(S, Sr) < 2e-4 
Example #2
Source File: test_cutensor.py    From cupy with MIT License 6 votes vote down vote up
def test_elementwise_binary(self):
        desc_a = cutensor.create_tensor_descriptor(self.a, ct.OP_SIGMOID)
        desc_c = cutensor.create_tensor_descriptor(self.c, ct.OP_ABS)

        d = cutensor.elementwise_binary(
            self.alpha, self.a, desc_a, self.mode_a,
            self.gamma, self.c, desc_c, self.mode_c,
            op_AC=ct.OP_MUL
        )

        testing.assert_allclose(
            self.alpha * (1 / (1 + cupy.exp(-self.a_transposed))) *
            self.gamma * cupy.abs(self.c),
            d,
            rtol=1e-6, atol=1e-6
        ) 
Example #3
Source File: test_cbpdn.py    From sporco with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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(ifftn(fftn(D, (N, N), (0, 1)) *
                            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 rrs(X0, X1) < 5e-4
        Sr = b.reconstruct().squeeze()
        assert rrs(S, Sr) < 3e-4 
Example #4
Source File: test_cbpdn.py    From sporco with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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(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 rrs(X0, X1) < 5e-5
        Sr = b.reconstruct().squeeze()
        assert rrs(S, Sr) < 1e-4 
Example #5
Source File: test_cbpdn.py    From sporco with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_11(self):
        N = 63
        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(ifftn(fftn(D, (N, N), (0, 1)) *
                         fftn(X0, None, (0, 1)), None, (0, 1)).real,
                   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 rrs(X0, X1) < 5e-5
        Sr = b.reconstruct().squeeze()
        assert rrs(S, Sr) < 1e-4 
Example #6
Source File: test_bpdn.py    From sporco with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_18(self):
        N = 64
        M = 2 * N
        L = 8
        cp.random.seed(12345)
        D = cp.random.randn(N, M)
        x0 = cp.zeros((M, 1))
        si = cp.random.permutation(M)
        x0[si[0:L]] = cp.random.randn(L, 1)
        s = D.dot(x0)
        lmbda = 5e-2
        opt = bpdn.BPDN.Options({'Verbose': False, 'MaxMainIter': 300,
                                 'RelStopTol': 1e-5, 'AutoRho':
                                 {'Enabled': False}})
        bp = bpdn.BPDN(D, s, lmbda=lmbda, opt=opt)
        Xp = bp.solve()
        epsilon = cp.linalg.norm(D.dot(Xp) - s)
        opt = bpdn.MinL1InL2Ball.Options(
            {'Verbose': False, 'MaxMainIter': 300, 'RelStopTol': 1e-5,
             'rho': 2e1, 'AutoRho': {'Enabled': False}})
        bc = bpdn.MinL1InL2Ball(D, s, epsilon=epsilon, opt=opt)
        Xc = bc.solve()
        assert cp.linalg.norm(Xp - Xc) / cp.linalg.norm(Xp) < 1e-3
        assert cp.abs(cp.linalg.norm(Xp, 1) - cp.linalg.norm(Xc, 1)) < 1e-3 
Example #7
Source File: gla_gpu.py    From Deep_VoiceChanger with MIT License 6 votes vote down vote up
def __init__(self, parallel, wave_len=254, wave_dif=64, buffer_size=5, loop_num=5, window=np.hanning(254)):
        self.wave_len = wave_len
        self.wave_dif = wave_dif
        self.buffer_size = buffer_size
        self.loop_num = loop_num
        self.parallel = parallel
        self.window = cp.array([window for _ in range(parallel)])

        self.wave_buf = cp.zeros((parallel, wave_len+wave_dif), dtype=float)
        self.overwrap_buf = cp.zeros((parallel, wave_dif*buffer_size+(wave_len-wave_dif)), dtype=float)
        self.spectrum_buffer = cp.ones((parallel, self.buffer_size, self.wave_len), dtype=complex)
        self.absolute_buffer = cp.ones((parallel, self.buffer_size, self.wave_len), dtype=complex)
        
        self.phase = cp.zeros((parallel, self.wave_len), dtype=complex)
        self.phase += cp.random.random((parallel, self.wave_len))-0.5 + cp.random.random((parallel, self.wave_len))*1j - 0.5j
        self.phase[self.phase == 0] = 1
        self.phase /= cp.abs(self.phase) 
Example #8
Source File: test_bpdn.py    From sporco with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_08(self):
        N = 64
        M = 2 * N
        L = 4
        np.random.seed(12345)
        D = cp.array(np.random.randn(N, M))
        x0 = cp.zeros((M, 1))
        si = cp.array(np.random.permutation(M))
        x0[si[0:L]] = cp.array(np.random.randn(L, 1))
        s0 = D.dot(x0)
        lmbda = 5e-3
        opt = bpdn.BPDN.Options({'Verbose': False, 'MaxMainIter': 500,
                                 'RelStopTol': 5e-4})
        b = bpdn.BPDN(D, s0, lmbda, opt)
        b.solve()
        x1 = b.Y
        assert cp.abs(b.itstat[-1].ObjFun - 0.012009) < 1e-5
        assert cp.abs(b.itstat[-1].DFid - 1.9636082e-06) < 1e-5
        assert cp.abs(b.itstat[-1].RegL1 - 2.401446) < 1e-5
        assert cp.linalg.norm(x1 - x0) < 1e-3 
Example #9
Source File: test_rpca.py    From sporco with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_01(self):
        N = 64
        K = 5
        L = 10
        u = cp.random.randn(N, K)
        U = cp.dot(u, u.T)
        V = cp.random.randn(N, N)
        t = cp.sort(cp.abs(V).ravel())[V.size-L]
        V[cp.abs(V) < t] = 0
        D = U + V
        opt = rpca.RobustPCA.Options({'Verbose': False, 'gEvalY': False,
                                      'MaxMainIter': 250,
                                      'AutoRho': {'Enabled': True}})
        b = rpca.RobustPCA(D, None, opt)
        X, Y = b.solve()
        assert sm.mse(U, X) < 5e-6
        assert sm.mse(V, Y) < 1e-8 
Example #10
Source File: test_tvl1.py    From sporco with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_02(self):
        lmbda = 3
        opt = tvl1.TVL1Deconv.Options({'Verbose': False, 'gEvalY': False,
                                       'MaxMainIter': 250, 'rho': 10.0})
        b = tvl1.TVL1Deconv(cp.ones((1, )), self.D, lmbda, opt, axes=(0, 1))
        X = b.solve()
        assert cp.abs(b.itstat[-1].ObjFun - 12364.029061174046) < 1e-5
        assert sm.mse(self.U, X) < 1e-4 
Example #11
Source File: __init__.py    From dybm with Apache License 2.0 5 votes vote down vote up
def allclose(a, b, rtol=1e-05, atol=1e-08):
    return cupy.all(cupy.abs(a - b) < (atol + rtol * cupy.abs(b)))


# Constants 
Example #12
Source File: test_tvl2.py    From sporco with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #13
Source File: test_tvl2.py    From sporco with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_01(self):
        lmbda = 1e-1
        opt = tvl2.TVL2Denoise.Options(
            {'Verbose': False, 'gEvalY': False, 'MaxMainIter': 250,
             'rho': 10 * lmbda})
        b = tvl2.TVL2Denoise(self.D, lmbda, opt, axes=(0, 1, 2))
        X = b.solve()
        assert cp.abs(b.itstat[-1].ObjFun - 366.04267554965134) < 1e-3
        assert sm.mse(self.U, X) < 1e-3 
Example #14
Source File: test_tvl2.py    From sporco with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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))
        X = b.solve()
        assert cp.abs(b.itstat[-1].ObjFun - 564.1586542) < 1e-3
        assert sm.mse(self.U, X) < 1e-3 
Example #15
Source File: test_tvl2.py    From sporco with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #16
Source File: test_tvl2.py    From sporco with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_01(self):
        lmbda = 1e-1
        opt = tvl2.TVL2Denoise.Options(
            {'Verbose': False, 'gEvalY': False, 'MaxMainIter': 300,
             'rho': 75 * lmbda})
        b = tvl2.TVL2Denoise(self.D, lmbda, opt)
        X = b.solve()
        assert cp.abs(b.itstat[-1].ObjFun - 32.875710674129564) < 1e-3
        assert sm.mse(self.U, X) < 1e-3 
Example #17
Source File: test_tvl1.py    From sporco with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_01(self):
        lmbda = 3
        opt = tvl1.TVL1Denoise.Options({'Verbose': False, 'gEvalY': False,
                                        'MaxMainIter': 250})
        b = tvl1.TVL1Denoise(self.D, lmbda, opt, axes=(0, 1, 2))
        X = b.solve()
        assert cp.abs(b.itstat[-1].ObjFun - 6219.6209699337605) < 1e-6
        assert sm.mse(self.U, X) < 1e-6 
Example #18
Source File: test_tvl1.py    From sporco with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setup_method(self, method):
        np.random.seed(12345)
        N = 32
        L = 20
        self.U = cp.ones((N, N, N))
        self.U[:, 0:(old_div(N, 2))] = -1
        self.V = cp.asarray(np.random.randn(N, N, N))
        t = cp.sort(cp.abs(self.V).ravel())[self.V.size - L]
        self.V[cp.abs(self.V) < t] = 0
        self.D = self.U + self.V 
Example #19
Source File: test_tvl1.py    From sporco with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_01(self):
        lmbda = 3
        opt = tvl1.TVL1Denoise.Options({'Verbose': False, 'gEvalY': False,
                                        'MaxMainIter': 250})
        b = tvl1.TVL1Denoise(self.D, lmbda, opt, axes=(0, 1))
        X = b.solve()
        assert cp.abs(b.itstat[-1].ObjFun - 6219.3241727233126) < 1e-6
        assert sm.mse(self.U, X) < 1e-6 
Example #20
Source File: test_tvl1.py    From sporco with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setup_method(self, method):
        np.random.seed(12345)
        N = 32
        L = 20
        self.U = cp.ones((N, N, N))
        self.U[:, 0:(old_div(N, 2))] = -1
        self.V = cp.asarray(np.random.randn(N, N, N))
        t = cp.sort(cp.abs(self.V).ravel())[self.V.size - L]
        self.V[cp.abs(self.V) < t] = 0
        self.D = self.U + self.V 
Example #21
Source File: test_tvl1.py    From sporco with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_01(self):
        lmbda = 3
        opt = tvl1.TVL1Denoise.Options({'Verbose': False, 'gEvalY': False,
                                        'MaxMainIter': 250})
        b = tvl1.TVL1Denoise(self.D, lmbda, opt)
        X = b.solve()
        assert cp.abs(b.itstat[-1].ObjFun - 447.78101756451662) < 1e-6
        assert sm.mse(self.U, X) < 1e-6 
Example #22
Source File: test_tvl1.py    From sporco with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setup_method(self, method):
        np.random.seed(12345)
        N = 64
        L = 20
        self.U = cp.ones((N, N))
        self.U[:, 0:(old_div(N, 2))] = -1
        self.V = cp.asarray(np.random.randn(N, N))
        t = cp.sort(cp.abs(self.V).ravel())[self.V.size - L]
        self.V[cp.abs(self.V) < t] = 0
        self.D = self.U + self.V 
Example #23
Source File: test_cbpdn.py    From sporco with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_22(self):
        N = 32
        M = 4
        Nd = 8
        D = cp.random.randn(Nd, Nd, M)
        D /= cp.sqrt(cp.sum(D**2, axis=(0, 1)))
        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(fftconv(D, X0), axis=2)
        lmbda = 1e-3
        opt = cbpdn.ConvBPDN.Options(
            {'Verbose': False, 'MaxMainIter': 500, 'RelStopTol': 1e-5,
             'rho': 5e-1, 'AutoRho': {'Enabled': False}})
        bp = cbpdn.ConvBPDN(D, S, lmbda, opt)
        Xp = bp.solve()
        epsilon = cp.linalg.norm(bp.reconstruct(Xp).squeeze() - S)
        opt = cbpdn.ConvMinL1InL2Ball.Options(
            {'Verbose': False, 'MaxMainIter': 500, 'RelStopTol': 1e-5,
             'rho': 2e2, 'RelaxParam': 1.0, 'AutoRho': {'Enabled': False}})
        bc = cbpdn.ConvMinL1InL2Ball(D, S, epsilon=epsilon, opt=opt)
        Xc = bc.solve()
        assert cp.linalg.norm(Xp - Xc) / cp.linalg.norm(Xp) < 1e-3
        assert cp.abs(cp.linalg.norm(Xp.ravel(), 1) -
                      cp.linalg.norm(Xc.ravel(), 1)) < 1e-3 
Example #24
Source File: spectralloss.py    From TSNetVocoder with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _spectrum(self, x, y):
        S = {'x' : cupy.fft.rfft(self._frame(x), self.fftl) / self.norm,
             'y' : cupy.fft.rfft(self._frame(y), self.fftl) / self.norm}
        A = {'x' : cupy.abs(S['x']),
             'y' : cupy.abs(S['y'])}
        P = {'x' : cupy.angle(S['x']),
             'y' : cupy.angle(S['y'])}
        return A, P 
Example #25
Source File: trigonometric.py    From cupy with MIT License 5 votes vote down vote up
def _unwrap_correct(dd, discont):
    ddmod = cupy.mod(dd + numpy.pi, 2*numpy.pi) - numpy.pi
    cupy.copyto(ddmod, numpy.pi, where=(ddmod == -numpy.pi) & (dd > 0))
    ph_correct = ddmod - dd
    cupy.copyto(ph_correct, 0., where=cupy.abs(dd) < discont)
    return ph_correct 
Example #26
Source File: gla_gpu.py    From Deep_VoiceChanger with MIT License 5 votes vote down vote up
def inverse(self, spectrum, in_phase=None):
        if in_phase is None:
            in_phase = self.phase
        else:
            in_phase = cp.array(in_phase)
        spectrum = cp.array(spectrum)
        self.spectrum_buffer[:, -1] = spectrum * in_phase
        self.absolute_buffer[:, -1] = spectrum

        for _ in range(self.loop_num):
            self.overwrap_buf *= 0
            waves = cp.fft.ifft(self.spectrum_buffer, axis=2).real
            last = self.spectrum_buffer

            for i in range(self.buffer_size):
                self.overwrap_buf[:,i*self.wave_dif:i*self.wave_dif+self.wave_len] += waves[:,i]
            waves = cp.stack([self.overwrap_buf[:, i*self.wave_dif:i*self.wave_dif+self.wave_len]*self.window for i in range(self.buffer_size)], axis=1)

            spectrum = cp.fft.fft(waves, axis=2)
            self.spectrum_buffer = self.absolute_buffer * spectrum / (cp.abs(spectrum)+1e-10)
            self.spectrum_buffer += 0.5 * (self.spectrum_buffer - last)

        dst = cp.asnumpy(self.spectrum_buffer[:, 0])
        self.absolute_buffer = cp.roll(self.absolute_buffer, -1, axis=1)
        self.spectrum_buffer = cp.roll(self.spectrum_buffer, -1, axis=1)

        return dst 
Example #27
Source File: test_raw.py    From cupy with MIT License 4 votes vote down vote up
def test_cuFloatComplex(self):
        N = 100
        block = 32
        grid = (N + block - 1) // block
        dtype = cupy.complex64

        mod = cupy.RawModule(
            code=_test_cuComplex,
            translate_cucomplex=True)
        a = cupy.random.random((N,)) + 1j*cupy.random.random((N,))
        a = a.astype(dtype)
        b = cupy.random.random((N,)) + 1j*cupy.random.random((N,))
        b = b.astype(dtype)
        c = cupy.random.random((N,)) + 1j*cupy.random.random((N,))
        c = c.astype(dtype)
        out = cupy.zeros((N,), dtype=dtype)
        out_float = cupy.zeros((N,), dtype=cupy.float32)
        out_up = cupy.zeros((N,), dtype=cupy.complex128)

        ker = mod.get_function('test_addf')
        ker((grid,), (block,), (a, b, out))
        assert (out == a + b).all()

        ker = mod.get_function('test_subf')
        ker((grid,), (block,), (a, b, out))
        assert (out == a - b).all()

        ker = mod.get_function('test_mulf')
        ker((grid,), (block,), (a, b, out))
        assert (out == a * b).all()

        ker = mod.get_function('test_divf')
        ker((grid,), (block,), (a, b, out))
        assert (out == a / b).all()

        ker = mod.get_function('test_conjf')
        ker((grid,), (block,), (a, out))
        assert (out == cupy.conj(a)).all()

        ker = mod.get_function('test_absf')
        ker((grid,), (block,), (a, out_float))
        assert (out_float == cupy.abs(a)).all()

        ker = mod.get_function('test_fmaf')
        ker((grid,), (block,), (a, b, c, out))
        assert (out == a * b + c).all()

        ker = mod.get_function('test_makef')
        ker((grid,), (block,), (out,))
        # because of precision issue, the (A==B).all() semantics would fail
        assert cupy.allclose(out, 1.8 - 1j * 8.7)

        ker = mod.get_function('test_upcast')
        ker((grid,), (block,), (a, out_up))
        assert (out_up == a.astype(cupy.complex128)).all()

        # NumPy scalars.
        b = cupy.complex64(2 + 3j)
        ker = mod.get_function('test_addf_scalar')
        ker((grid,), (block,), (a, b, out))
        assert (out == a + b).all()