Python cupy.asnumpy() Examples
The following are 30
code examples of cupy.asnumpy().
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: npz.py From cupy with MIT License | 6 votes |
def savez(file, *args, **kwds): """Saves one or more arrays into a file in uncompressed ``.npz`` format. Arguments without keys are treated as arguments with automatic keys named ``arr_0``, ``arr_1``, etc. corresponding to the positions in the argument list. The keys of arguments are used as keys in the ``.npz`` file, which are used for accessing NpzFile object when the file is read by :func:`cupy.load` function. Args: file (file or str): File or filename to save. *args: Arrays with implicit keys. **kwds: Arrays with explicit keys. .. seealso:: :func:`numpy.savez` """ args = map(cupy.asnumpy, args) for key in kwds: kwds[key] = cupy.asnumpy(kwds[key]) numpy.savez(file, *args, **kwds)
Example #2
Source File: array.py From cupy with MIT License | 6 votes |
def assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True): """Raises an AssertionError if objects are not equal up to desired precision. Args: x(numpy.ndarray or cupy.ndarray): The actual object to check. y(numpy.ndarray or cupy.ndarray): The desired, expected object. decimal(int): Desired precision. err_msg(str): The error message to be printed in case of failure. verbose(bool): If ``True``, the conflicting values are appended to the error message. .. seealso:: :func:`numpy.testing.assert_array_almost_equal` """ # NOQA numpy.testing.assert_array_almost_equal( cupy.asnumpy(x), cupy.asnumpy(y), decimal=decimal, err_msg=err_msg, verbose=verbose)
Example #3
Source File: monitor.py From LaSO with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_percentiles(data, sigma): """Compute percentiles for data and return an array with the same length as the number of elements in ``sigma``. Args: data (array): 1-dimensional NumPy or CuPy arryay. sigma (tuple): Sigmas for which percentiles are computed. Returns: array: Array of percentiles. """ def _get_percentiles(_data, _sigma): try: return np.percentile(_data, _sigma) except IndexError: # Handle uninitialized model parameters return np.array((float('NaN'),) * 7) if isinstance(data, cupy.ndarray): # TODO(hvy): Make percentile computation faster for GPUs data = cupy.asnumpy(data) return cupy.asarray(_get_percentiles(data, sigma)) return _get_percentiles(data, sigma)
Example #4
Source File: array.py From cupy with MIT License | 6 votes |
def assert_allclose(actual, desired, rtol=1e-7, atol=0, err_msg='', verbose=True): """Raises an AssertionError if objects are not equal up to desired tolerance. Args: actual(numpy.ndarray or cupy.ndarray): The actual object to check. desired(numpy.ndarray or cupy.ndarray): The desired, expected object. rtol(float): Relative tolerance. atol(float): Absolute tolerance. err_msg(str): The error message to be printed in case of failure. verbose(bool): If ``True``, the conflicting values are appended to the error message. .. seealso:: :func:`numpy.testing.assert_allclose` """ # NOQA numpy.testing.assert_allclose( cupy.asnumpy(actual), cupy.asnumpy(desired), rtol=rtol, atol=atol, err_msg=err_msg, verbose=verbose)
Example #5
Source File: parameter_statistics.py From wavenet with Apache License 2.0 | 6 votes |
def _percentiles(x, sigmas): """Compute percentiles for the given array. Args: x (array): Target array for which percentiles are computed. sigmas (iterable): Percentile sigma values. Returns: array: List of percentiles. The list has the same length as the given ``sigma``. """ def _percentiles_cpu(_x): try: return numpy.percentile(_x, sigmas) except IndexError: return numpy.array((float('NaN'),) * 7) # TODO(hvy): Make percentile computation faster for GPUs if isinstance(x, cupy.ndarray): x = cupy.asnumpy(x) return cupy.asarray(_percentiles_cpu(x)) return _percentiles_cpu(x)
Example #6
Source File: cuda.py From deep-learning-from-scratch-3 with MIT License | 6 votes |
def as_numpy(x): """Convert to `numpy.ndarray`. Args: x (`numpy.ndarray` or `cupy.ndarray`): Arbitrary object that can be converted to `numpy.ndarray`. Returns: `numpy.ndarray`: Converted array. """ if isinstance(x, Variable): x = x.data if np.isscalar(x): return np.array(x) elif isinstance(x, np.ndarray): return x return cp.asnumpy(x)
Example #7
Source File: non_maximum_suppression.py From FATE with Apache License 2.0 | 6 votes |
def _non_maximum_suppression_gpu(bbox, thresh, score=None, limit=None): if len(bbox) == 0: return cp.zeros((0,), dtype=np.int32) n_bbox = bbox.shape[0] if score is not None: order = score.argsort()[::-1].astype(np.int32) else: order = cp.arange(n_bbox, dtype=np.int32) sorted_bbox = bbox[order, :] selec, n_selec = _call_nms_kernel( sorted_bbox, thresh) selec = selec[:n_selec] selec = order[selec] if limit is not None: selec = selec[:limit] return cp.asnumpy(selec)
Example #8
Source File: faster_rcnn.py From FATE with Apache License 2.0 | 6 votes |
def _suppress(self, raw_cls_bbox, raw_prob): bbox = list() label = list() score = list() # skip cls_id = 0 because it is the background class for l in range(1, self.n_class): cls_bbox_l = raw_cls_bbox.reshape((-1, self.n_class, 4))[:, l, :] prob_l = raw_prob[:, l] mask = prob_l > self.score_thresh cls_bbox_l = cls_bbox_l[mask] prob_l = prob_l[mask] keep = non_maximum_suppression( cp.array(cls_bbox_l), self.nms_thresh, prob_l) keep = cp.asnumpy(keep) bbox.append(cls_bbox_l[keep]) # The labels are in [0, self.n_class - 2]. label.append((l - 1) * np.ones((len(keep),))) score.append(prob_l[keep]) bbox = np.concatenate(bbox, axis=0).astype(np.float32) label = np.concatenate(label, axis=0).astype(np.int32) score = np.concatenate(score, axis=0).astype(np.float32) return bbox, label, score
Example #9
Source File: formatting.py From cupy with MIT License | 6 votes |
def array_repr(arr, max_line_width=None, precision=None, suppress_small=None): """Returns the string representation of an array. Args: arr (array_like): Input array. It should be able to feed to :func:`cupy.asnumpy`. max_line_width (int): The maximum number of line lengths. precision (int): Floating point precision. It uses the current printing precision of NumPy. suppress_small (bool): If ``True``, very small numbers are printed as zeros Returns: str: The string representation of ``arr``. .. seealso:: :func:`numpy.array_repr` """ return numpy.array_repr(cupy.asnumpy(arr), max_line_width, precision, suppress_small)
Example #10
Source File: test_decomp_lu.py From cupy with MIT License | 6 votes |
def test_lu_factor_reconstruction(self, dtype): m, n = self.shape A = cupy.random.randn(m, n, dtype=dtype) lu, piv = cupyx.scipy.linalg.lu_factor(A) # extract ``L`` and ``U`` from ``lu`` L = cupy.tril(lu, k=-1) cupy.fill_diagonal(L, 1.) L = L[:, :m] U = cupy.triu(lu) U = U[:n, :] # check output shapes assert lu.shape == (m, n) assert L.shape == (m, min(m, n)) assert U.shape == (min(m, n), n) assert piv.shape == (min(m, n),) # apply pivot (on CPU since slaswp is not available in cupy) piv = cupy.asnumpy(piv) rows = numpy.arange(m) for i, row in enumerate(piv): if i != row: rows[i], rows[row] = rows[row], rows[i] PA = A[rows] # check that reconstruction is close to original LU = L.dot(U) cupy.testing.assert_allclose(LU, PA, atol=1e-5)
Example #11
Source File: array.py From cupy with MIT License | 5 votes |
def assert_array_list_equal(xlist, ylist, err_msg='', verbose=True): """Compares lists of arrays pairwise with ``assert_array_equal``. Args: x(array_like): Array of the actual objects. y(array_like): Array of the desired, expected objects. err_msg(str): The error message to be printed in case of failure. verbose(bool): If ``True``, the conflicting values are appended to the error message. Each element of ``x`` and ``y`` must be either :class:`numpy.ndarray` or :class:`cupy.ndarray`. ``x`` and ``y`` must have same length. Otherwise, this function raises ``AssertionError``. It compares elements of ``x`` and ``y`` pairwise with :func:`assert_array_equal` and raises error if at least one pair is not equal. .. seealso:: :func:`numpy.testing.assert_array_equal` """ x_type = type(xlist) y_type = type(ylist) if x_type is not y_type: raise AssertionError( 'Matching types of list or tuple are expected, ' 'but were different types ' '(xlist:{} ylist:{})'.format(x_type, y_type)) if x_type not in (list, tuple): raise AssertionError( 'List or tuple is expected, but was {}'.format(x_type)) if len(xlist) != len(ylist): raise AssertionError('List size is different') for x, y in zip(xlist, ylist): numpy.testing.assert_array_equal( cupy.asnumpy(x), cupy.asnumpy(y), err_msg=err_msg, verbose=verbose)
Example #12
Source File: array.py From cupy with MIT License | 5 votes |
def assert_array_almost_equal_nulp(x, y, nulp=1): """Compare two arrays relatively to their spacing. Args: x(numpy.ndarray or cupy.ndarray): The actual object to check. y(numpy.ndarray or cupy.ndarray): The desired, expected object. nulp(int): The maximum number of unit in the last place for tolerance. .. seealso:: :func:`numpy.testing.assert_array_almost_equal_nulp` """ numpy.testing.assert_array_almost_equal_nulp( cupy.asnumpy(x), cupy.asnumpy(y), nulp=nulp)
Example #13
Source File: array.py From cupy with MIT License | 5 votes |
def assert_array_max_ulp(a, b, maxulp=1, dtype=None): """Check that all items of arrays differ in at most N Units in the Last Place. Args: a(numpy.ndarray or cupy.ndarray): The actual object to check. b(numpy.ndarray or cupy.ndarray): The desired, expected object. maxulp(int): The maximum number of units in the last place that elements of ``a`` and ``b`` can differ. dtype(numpy.dtype): Data-type to convert ``a`` and ``b`` to if given. .. seealso:: :func:`numpy.testing.assert_array_max_ulp` """ # NOQA numpy.testing.assert_array_max_ulp( cupy.asnumpy(a), cupy.asnumpy(b), maxulp=maxulp, dtype=dtype)
Example #14
Source File: test_backends.py From opt_einsum with MIT License | 5 votes |
def test_cupy(string): # pragma: no cover views = helpers.build_views(string) ein = contract(string, *views, optimize=False, use_blas=False) shps = [v.shape for v in views] expr = contract_expression(string, *shps, optimize=True) opt = expr(*views, backend='cupy') assert np.allclose(ein, opt) # test non-conversion mode cupy_views = [backends.to_cupy(view) for view in views] cupy_opt = expr(*cupy_views) assert isinstance(cupy_opt, cupy.ndarray) assert np.allclose(ein, cupy.asnumpy(cupy_opt))
Example #15
Source File: toeplitz.py From geoist with MIT License | 5 votes |
def cg(A, b, x=None, tol=1.0e-5, max_iter=None): # Note that this function works even tensors 'A' and 'b' are NumPy or CuPy # arrays. if use_gpu > 0: import cupy xp = cupy.get_array_module(b) else: xp = np if max_iter is None: max_iter = 10*len(b) if x is None: x = xp.zeros_like(b, dtype=np.float32) r0 = b - A.matvec(x) p = r0 now = time.time() for i in range(max_iter): a = xp.inner(r0, r0) / xp.inner(p, A.matvec(p)) x += a * p r1 = r0 - a * A.matvec(p) res = xp.linalg.norm(r1) if res < tol: return x b = xp.inner(r1, r1) / xp.inner(r0, r0) p = r1 + b * p r0 = r1 if time.time() - now > 10: now = time.time() print('iter: {:8d} residual: {:16.7e}'.format(i,xp.asnumpy(res))) #print('iter: {:8d} residual: {:16.7e}'.format(i,cupy.asnumpy(res))) print('Failed to converge. Increase max-iter or tol.') return x
Example #16
Source File: cupy.py From chainladder-python with Mozilla Public License 2.0 | 5 votes |
def nanpercentile(a, *args, **kwargs): """ For cupy v0.6.0 compatibility """ return cp.array(np.nanpercentile(cp.asnumpy(a), *args, **kwargs))
Example #17
Source File: cupy.py From chainladder-python with Mozilla Public License 2.0 | 5 votes |
def unique(ar, axis=None, *args, **kwargs): """ For cupy v0.6.0 compatibility """ return cp.array(np.unique(cp.asnumpy(ar), axis=axis, *args, **kwargs))
Example #18
Source File: toeplitz.py From geoist with MIT License | 5 votes |
def block_toep2_sym(a): '''generate full representation of 2-level symmetric toeplitz matrix Args: a (ndarray): 1-st column of the symmetrec toeplitz matrix in proper shape. Returns: Full filled toeplitz matrix. ''' if use_gpu > 0: import cupy xp = cupy.get_array_module(a) if xp is cupy: a = xp.asnumpy(a) else: xp = np a = np.asnumpy(a) A1 = [] n0,n1 = a.shape for i in range(n1): A1.append(splin.toeplitz(a[:,i])) A = np.empty((n1,n0,n1,n0)) for i in range(n1): for j in range(n1): A[i,:,j,:] = A1[np.int(np.abs(i-j))] A.shape = (n0*n1,n0*n1) A = xp.asarray(A) return(A)
Example #19
Source File: CPUCupyPinned.py From SpeedTorch with MIT License | 5 votes |
def getNumpyVersion(self): return cupy.asnumpy(self.CUPYmemmap)
Example #20
Source File: trainer.py From Deep_VoiceChanger with MIT License | 5 votes |
def preview_convert(iterator_a, iterator_b, g_a, g_b, device, gla, dst): @chainer.training.make_extension() def make_preview(trainer): with chainer.using_config('train', False): with chainer.no_backprop_mode(): x_a = iterator_a.next() x_a = convert.concat_examples(x_a, device) x_a = chainer.Variable(x_a) x_b = iterator_b.next() x_b = convert.concat_examples(x_b, device) x_b = chainer.Variable(x_b) x_ab = g_a(x_a) x_ba = g_b(x_b) x_bab = g_a(x_ba) x_aba = g_b(x_ab) preview_dir = '{}/preview'.format(dst) if not os.path.exists(preview_dir): os.makedirs(preview_dir) image_dir = '{}/image'.format(dst) if not os.path.exists(image_dir): os.makedirs(image_dir) names = ['a', 'ab', 'aba', 'b', 'ba', 'bab'] images = [x_a, x_ab, x_aba, x_b, x_ba, x_bab] for n, i in zip(names, images): i = cp.asnumpy(i.data)[:,:,padding:-padding,:].reshape(1, -1, 128) image.save(image_dir+'/{}{}.jpg'.format(trainer.updater.epoch,n), i) w = np.concatenate([gla.inverse(_i) for _i in dataset.reverse(i)]) dataset.save(preview_dir+'/{}{}.wav'.format(trainer.updater.epoch,n), 16000, w) return make_preview
Example #21
Source File: test_generator.py From cupy with MIT License | 5 votes |
def _check_ks( self, significance_level, cupy_len, numpy_len, *args, **kwargs): assert 'size' in kwargs # cupy func = self._get_generator_func(*args, **kwargs) vals_cupy = func() assert vals_cupy.size > 0 count = 1 + (cupy_len - 1) // vals_cupy.size vals_cupy = [vals_cupy] for _ in range(1, count): vals_cupy.append(func()) vals_cupy = cupy.stack(vals_cupy).ravel() # numpy kwargs['size'] = numpy_len dtype = kwargs.pop('dtype', None) numpy_rs = numpy.random.RandomState(self.__seed) vals_numpy = getattr(numpy_rs, self.target_method)(*args, **kwargs) if dtype is not None: vals_numpy = vals_numpy.astype(dtype, copy=False) # test d_plus, d_minus, p_value = \ two_sample_Kolmogorov_Smirnov_test( cupy.asnumpy(vals_cupy), vals_numpy) if p_value < significance_level: message = '''Rejected null hypothesis: p: %f D+ (cupy < numpy): %f D- (cupy > numpy): %f''' % (p_value, d_plus, d_minus) raise AssertionError(message)
Example #22
Source File: test_solve.py From cupy with MIT License | 5 votes |
def check_x(self, a_shape, rcond, dtype): a_gpu = testing.shaped_random(a_shape, dtype=dtype) a_cpu = cupy.asnumpy(a_gpu) a_gpu_copy = a_gpu.copy() result_cpu = numpy.linalg.pinv(a_cpu, rcond=rcond) result_gpu = cupy.linalg.pinv(a_gpu, rcond=rcond) self.assertEqual(result_cpu.dtype, result_gpu.dtype) cupy.testing.assert_allclose(result_cpu, result_gpu, atol=1e-3) cupy.testing.assert_array_equal(a_gpu_copy, a_gpu)
Example #23
Source File: test_erf.py From cupy with MIT License | 5 votes |
def test_erfcinv_behavior(self, dtype): a = cupy.empty((1,), dtype=dtype) a[:] = 2.0 + 1E-6 a = cupyx.scipy.special.erfcinv(a) assert cupy.isnan(a) a[:] = 0.0 - 1E-6 a = cupyx.scipy.special.erfcinv(a) assert cupy.isnan(a) a[:] = 0.0 a = cupyx.scipy.special.erfcinv(a) assert numpy.isposinf(cupy.asnumpy(a)) a[:] = 2.0 a = cupyx.scipy.special.erfcinv(a) assert numpy.isneginf(cupy.asnumpy(a))
Example #24
Source File: test_erf.py From cupy with MIT License | 5 votes |
def test_erfinv_behavior(self, dtype): a = cupy.empty((1,), dtype=dtype) a[:] = 1.0 + 1E-6 a = cupyx.scipy.special.erfinv(a) assert cupy.isnan(a) a[:] = -1.0 - 1E-6 a = cupyx.scipy.special.erfinv(a) assert cupy.isnan(a) a[:] = 1.0 a = cupyx.scipy.special.erfinv(a) assert numpy.isposinf(cupy.asnumpy(a)) a[:] = -1.0 a = cupyx.scipy.special.erfinv(a) assert numpy.isneginf(cupy.asnumpy(a))
Example #25
Source File: test_csc.py From cupy with MIT License | 5 votes |
def test_argmax_dense_axis_1(self): dm_data = numpy.random.random((10, 20)) dm_data = scipy.sparse.csc_matrix(dm_data) cp_matrix = sparse.csc_matrix((cupy.array(dm_data.data), cupy.array(dm_data.indices), cupy.array(dm_data.indptr)), shape=(10, 20)) da_cupy_values = cupy.asnumpy(cp_matrix.argmax(axis=1)) da_scipy_values = numpy.array(dm_data.argmax(axis=1))[:, 0] assert numpy.array_equal(da_cupy_values, da_scipy_values)
Example #26
Source File: test_csc.py From cupy with MIT License | 5 votes |
def test_argmax_dense_axis_0(self): dm_data = numpy.random.random((10, 20)) dm_data = scipy.sparse.csc_matrix(dm_data) cp_matrix = sparse.csc_matrix((cupy.array(dm_data.data), cupy.array(dm_data.indices), cupy.array(dm_data.indptr)), shape=(10, 20)) da_cupy_values = cupy.asnumpy(cp_matrix.argmax(axis=0)) da_scipy_values = numpy.array(dm_data.argmax(axis=0))[0, :] assert numpy.array_equal(da_cupy_values, da_scipy_values)
Example #27
Source File: test_csc.py From cupy with MIT License | 5 votes |
def test_argmax_sparse_axis_0(self): dm_data = numpy.random.random((10, 20)) dm_data[dm_data < 0.95] = 0 dm_data = scipy.sparse.csc_matrix(dm_data) cp_matrix = sparse.csc_matrix((cupy.array(dm_data.data), cupy.array(dm_data.indices), cupy.array(dm_data.indptr)), shape=(10, 20)) da_cupy_values = cupy.asnumpy(cp_matrix.argmax(axis=0)) da_scipy_values = numpy.array(dm_data.argmax(axis=0))[0, :] assert numpy.array_equal(da_cupy_values, da_scipy_values)
Example #28
Source File: test_csc.py From cupy with MIT License | 5 votes |
def test_argmin_dense_axis_1(self): dm_data = numpy.random.random((10, 20)) dm_data = scipy.sparse.csc_matrix(dm_data) cp_matrix = sparse.csc_matrix((cupy.array(dm_data.data), cupy.array(dm_data.indices), cupy.array(dm_data.indptr)), shape=(10, 20)) da_cupy_values = cupy.asnumpy(cp_matrix.argmin(axis=1)) da_scipy_values = numpy.array(dm_data.argmin(axis=1))[:, 0] assert numpy.array_equal(da_cupy_values, da_scipy_values)
Example #29
Source File: test_csc.py From cupy with MIT License | 5 votes |
def test_argmin_sparse_axis_1(self): dm_data = numpy.random.random((10, 20)) dm_data[dm_data < 0.95] = 0 dm_data = scipy.sparse.csc_matrix(dm_data) cp_matrix = sparse.csc_matrix((cupy.array(dm_data.data), cupy.array(dm_data.indices), cupy.array(dm_data.indptr)), shape=(10, 20)) da_cupy_values = cupy.asnumpy(cp_matrix.argmin(axis=1)) da_scipy_values = numpy.array(dm_data.argmin(axis=1))[:, 0] assert numpy.array_equal(da_cupy_values, da_scipy_values)
Example #30
Source File: test_csc.py From cupy with MIT License | 5 votes |
def test_argmin_dense_axis_0(self): dm_data = numpy.random.random((10, 20)) dm_data = scipy.sparse.csc_matrix(dm_data) cp_matrix = sparse.csc_matrix((cupy.array(dm_data.data), cupy.array(dm_data.indices), cupy.array(dm_data.indptr)), shape=(10, 20)) da_cupy_values = cupy.asnumpy(cp_matrix.argmin(axis=0)) da_scipy_values = numpy.array(dm_data.argmin(axis=0))[0, :] assert numpy.array_equal(da_cupy_values, da_scipy_values)