本文整理汇总了Python中cupy.asarray方法的典型用法代码示例。如果您正苦于以下问题:Python cupy.asarray方法的具体用法?Python cupy.asarray怎么用?Python cupy.asarray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cupy
的用法示例。
在下文中一共展示了cupy.asarray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_interp_fourier
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import asarray [as 别名]
def _get_interp_fourier(self, sz):
"""
compute the fourier series of the interpolation function.
"""
f1 = np.arange(-(sz[0]-1) / 2, (sz[0]-1)/2+1, dtype=np.float32)[:, np.newaxis] / sz[0]
interp1_fs = np.real(cubic_spline_fourier(f1, config.interp_bicubic_a) / sz[0])
f2 = np.arange(-(sz[1]-1) / 2, (sz[1]-1)/2+1, dtype=np.float32)[np.newaxis, :] / sz[1]
interp2_fs = np.real(cubic_spline_fourier(f2, config.interp_bicubic_a) / sz[1])
if config.interp_centering:
f1 = np.arange(-(sz[0]-1) / 2, (sz[0]-1)/2+1, dtype=np.float32)[:, np.newaxis]
interp1_fs = interp1_fs * np.exp(-1j*np.pi / sz[0] * f1)
f2 = np.arange(-(sz[1]-1) / 2, (sz[1]-1)/2+1, dtype=np.float32)[np.newaxis, :]
interp2_fs = interp2_fs * np.exp(-1j*np.pi / sz[1] * f2)
if config.interp_windowing:
win1 = np.hanning(sz[0]+2)[:, np.newaxis]
win2 = np.hanning(sz[1]+2)[np.newaxis, :]
interp1_fs = interp1_fs * win1[1:-1]
interp2_fs = interp2_fs * win2[1:-1]
if not config.use_gpu:
return (interp1_fs[:, :, np.newaxis, np.newaxis],
interp2_fs[:, :, np.newaxis, np.newaxis])
else:
return (cp.asarray(interp1_fs[:, :, np.newaxis, np.newaxis]),
cp.asarray(interp2_fs[:, :, np.newaxis, np.newaxis]))
示例2: __ua_convert__
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import asarray [as 别名]
def __ua_convert__(dispatchables, coerce):
if coerce:
try:
replaced = [
cupy.asarray(d.value) if d.coercible and d.type is np.ndarray
else d.value for d in dispatchables]
except TypeError:
return NotImplemented
else:
replaced = [d.value for d in dispatchables]
if not all(d.type is not np.ndarray or isinstance(r, cupy.ndarray)
for r, d in zip(replaced, dispatchables)):
return NotImplemented
return replaced
示例3: run
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import asarray [as 别名]
def run(gpuid, n_clusters, num, max_iter, use_custom_kernel, output):
samples = numpy.random.randn(num, 2)
X_train = numpy.r_[samples + 1, samples - 1]
with timer(' CPU '):
centers, pred = fit_xp(X_train, n_clusters, max_iter)
with cupy.cuda.Device(gpuid):
X_train = cupy.asarray(X_train)
with timer(' GPU '):
if use_custom_kernel:
centers, pred = fit_custom(X_train, n_clusters, max_iter)
else:
centers, pred = fit_xp(X_train, n_clusters, max_iter)
if output is not None:
index = numpy.random.choice(10000000, 300, replace=False)
draw(X_train[index].get(), n_clusters, centers.get(),
pred[index].get(), output)
示例4: test_array_function
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import asarray [as 别名]
def test_array_function(self):
a = numpy.random.randn(100, 100)
a_cpu = numpy.asarray(a)
a_gpu = cupy.asarray(a)
# The numpy call for both CPU and GPU arrays is intentional to test the
# __array_function__ protocol
qr_cpu = numpy.linalg.qr(a_cpu)
qr_gpu = numpy.linalg.qr(a_gpu)
if isinstance(qr_cpu, tuple):
for b_cpu, b_gpu in zip(qr_cpu, qr_gpu):
self.assertEqual(b_cpu.dtype, b_gpu.dtype)
cupy.testing.assert_allclose(b_cpu, b_gpu, atol=1e-4)
else:
self.assertEqual(qr_cpu.dtype, qr_gpu.dtype)
cupy.testing.assert_allclose(qr_cpu, qr_gpu, atol=1e-4)
示例5: test_reshape_contiguity
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import asarray [as 别名]
def test_reshape_contiguity(self):
shape_init, shape_final = self.shape_in_out
a_cupy = testing.shaped_arange(shape_init, xp=cupy)
a_cupy = cupy.asarray(a_cupy, order=self.order_init)
b_cupy = a_cupy.reshape(shape_final, order=self.order_reshape)
a_numpy = testing.shaped_arange(shape_init, xp=numpy)
a_numpy = numpy.asarray(a_numpy, order=self.order_init)
b_numpy = a_numpy.reshape(shape_final, order=self.order_reshape)
assert b_cupy.flags.f_contiguous == b_numpy.flags.f_contiguous
assert b_cupy.flags.c_contiguous == b_numpy.flags.c_contiguous
testing.assert_array_equal(b_cupy.strides, b_numpy.strides)
testing.assert_array_equal(b_cupy, b_numpy)
示例6: unwrap
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import asarray [as 别名]
def unwrap(p, discont=numpy.pi, axis=-1):
"""Unwrap by changing deltas between values to 2*pi complement.
Args:
p (cupy.ndarray): Input array.
discont (float): Maximum discontinuity between values, default is
``pi``.
axis (int): Axis along which unwrap will operate, default is the last
axis.
Returns:
cupy.ndarray: The result array.
.. seealso:: :func:`numpy.unwrap`
"""
p = cupy.asarray(p)
nd = p.ndim
dd = sumprod.diff(p, axis=axis)
slice1 = [slice(None, None)]*nd # full slices
slice1[axis] = slice(1, None)
slice1 = tuple(slice1)
ph_correct = _unwrap_correct(dd, discont)
up = cupy.array(p, copy=True, dtype='d')
up[slice1] = p[slice1] + cupy.cumsum(ph_correct, axis=axis)
return up
示例7: fftshift
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import asarray [as 别名]
def fftshift(x, axes=None):
"""Shift the zero-frequency component to the center of the spectrum.
Args:
x (cupy.ndarray): Input array.
axes (int or tuple of ints): Axes over which to shift. Default is
``None``, which shifts all axes.
Returns:
cupy.ndarray: The shifted array.
.. seealso:: :func:`numpy.fft.fftshift`
"""
x = cupy.asarray(x)
if axes is None:
axes = list(range(x.ndim))
elif isinstance(axes, np.compat.integer_types):
axes = (axes,)
for axis in axes:
x = cupy.roll(x, x.shape[axis] // 2, axis)
return x
示例8: ifftshift
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import asarray [as 别名]
def ifftshift(x, axes=None):
"""The inverse of :meth:`fftshift`.
Args:
x (cupy.ndarray): Input array.
axes (int or tuple of ints): Axes over which to shift. Default is
``None``, which shifts all axes.
Returns:
cupy.ndarray: The shifted array.
.. seealso:: :func:`numpy.fft.ifftshift`
"""
x = cupy.asarray(x)
if axes is None:
axes = list(range(x.ndim))
elif isinstance(axes, np.compat.integer_types):
axes = (axes,)
for axis in axes:
x = cupy.roll(x, -(x.shape[axis] // 2), axis)
return x
示例9: tril
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import asarray [as 别名]
def tril(m, k=0):
"""Returns a lower triangle of an array.
Args:
m (array-like): Array or array-like object.
k (int): The diagonal above which to zero elements. Zero is the main
diagonal, a positive value is above it, and a negative value is
below.
Returns:
cupy.ndarray: A lower triangle of an array.
.. seealso:: :func:`numpy.tril`
"""
m = cupy.asarray(m)
mask = tri(*m.shape[-2:], k=k, dtype=bool)
return cupy.where(mask, m, m.dtype.type(0))
示例10: dirichlet
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import asarray [as 别名]
def dirichlet(self, alpha, size=None, dtype=float):
"""Returns an array of samples drawn from the dirichlet distribution.
.. seealso::
:func:`cupy.random.dirichlet` for full documentation,
:meth:`numpy.random.RandomState.dirichlet
<numpy.random.mtrand.RandomState.dirichlet>`
"""
alpha = cupy.asarray(alpha)
if size is None:
size = alpha.shape
else:
size += alpha.shape
y = cupy.empty(shape=size, dtype=dtype)
_kernels.standard_gamma_kernel(alpha, self._rk_seed, y)
y /= y.sum(axis=-1, keepdims=True)
self._update_seed(y.size)
return y
示例11: exponential
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import asarray [as 别名]
def exponential(self, scale=1.0, size=None, dtype=float):
"""Returns an array of samples drawn from a exponential distribution.
.. warning::
This function may synchronize the device.
.. seealso::
:func:`cupy.random.exponential` for full documentation,
:meth:`numpy.random.RandomState.exponential
<numpy.random.mtrand.RandomState.exponential>`
"""
scale = cupy.asarray(scale, dtype)
if (scale < 0).any(): # synchronize!
raise ValueError('scale < 0')
if size is None:
size = scale.shape
x = self.standard_exponential(size, dtype)
x *= scale
return x
示例12: gamma
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import asarray [as 别名]
def gamma(self, shape, scale=1.0, size=None, dtype=float):
"""Returns an array of samples drawn from a gamma distribution.
.. seealso::
:func:`cupy.random.gamma` for full documentation,
:meth:`numpy.random.RandomState.gamma
<numpy.random.mtrand.RandomState.gamma>`
"""
shape, scale = cupy.asarray(shape), cupy.asarray(scale)
if size is None:
size = cupy.broadcast(shape, scale).shape
y = cupy.empty(shape=size, dtype=dtype)
_kernels.standard_gamma_kernel(shape, self._rk_seed, y)
y *= scale
self._update_seed(y.size)
return y
示例13: hypergeometric
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import asarray [as 别名]
def hypergeometric(self, ngood, nbad, nsample, size=None, dtype=int):
"""Returns an array of samples drawn from the hypergeometric distribution.
.. seealso::
:func:`cupy.random.hypergeometric` for full documentation,
:meth:`numpy.random.RandomState.hypergeometric
<numpy.random.mtrand.RandomState.hypergeometric>`
"""
ngood, nbad, nsample = \
cupy.asarray(ngood), cupy.asarray(nbad), cupy.asarray(nsample)
if size is None:
size = cupy.broadcast(ngood, nbad, nsample).shape
y = cupy.empty(shape=size, dtype=dtype)
_kernels.hypergeometric_kernel(ngood, nbad, nsample, self._rk_seed, y)
self._update_seed(y.size)
return y
示例14: logistic
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import asarray [as 别名]
def logistic(self, loc=0.0, scale=1.0, size=None, dtype=float):
"""Returns an array of samples drawn from the logistic distribution.
.. seealso::
:func:`cupy.random.logistic` for full documentation,
:meth:`numpy.random.RandomState.logistic
<numpy.random.mtrand.RandomState.logistic>`
"""
loc, scale = cupy.asarray(loc), cupy.asarray(scale)
if size is None:
size = cupy.broadcast(loc, scale).shape
x = cupy.empty(shape=size, dtype=dtype)
_kernels.open_uniform_kernel(self._rk_seed, x)
self._update_seed(x.size)
x = (1.0 - x) / x
cupy.log(x, out=x)
cupy.multiply(x, scale, out=x)
cupy.add(x, loc, out=x)
return x
示例15: negative_binomial
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import asarray [as 别名]
def negative_binomial(self, n, p, size=None, dtype=int):
"""Returns an array of samples drawn from the negative binomial distribution.
.. warning::
This function may synchronize the device.
.. seealso::
:func:`cupy.random.negative_binomial` for full documentation,
:meth:`numpy.random.RandomState.negative_binomial
<numpy.random.mtrand.RandomState.negative_binomial>`
"""
n = cupy.asarray(n)
p = cupy.asarray(p)
if cupy.any(n <= 0): # synchronize!
raise ValueError('n <= 0')
if cupy.any(p < 0): # synchronize!
raise ValueError('p < 0')
if cupy.any(p > 1): # synchronize!
raise ValueError('p > 1')
y = self.gamma(n, (1-p)/p, size)
return self.poisson(y, dtype=dtype)