本文整理汇总了Python中cupy.get_array_module方法的典型用法代码示例。如果您正苦于以下问题:Python cupy.get_array_module方法的具体用法?Python cupy.get_array_module怎么用?Python cupy.get_array_module使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cupy
的用法示例。
在下文中一共展示了cupy.get_array_module方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cfft2
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import get_array_module [as 别名]
def cfft2(x):
in_shape = x.shape
# if both dimensions are odd
if config.use_gpu:
xp = cp.get_array_module(x)
else:
xp = np
if in_shape[0] % 2 == 1 and in_shape[1] % 2 == 1:
xf = xp.fft.fftshift(xp.fft.fftshift(fft2(x), 0), 1).astype(xp.complex64)
else:
out_shape = list(in_shape)
out_shape[0] = in_shape[0] + (in_shape[0] + 1) % 2
out_shape[1] = in_shape[1] + (in_shape[1] + 1) % 2
out_shape = tuple(out_shape)
xf = xp.zeros(out_shape, dtype=xp.complex64)
xf[:in_shape[0], :in_shape[1]] = xp.fft.fftshift(xp.fft.fftshift(fft2(x), 0), 1).astype(xp.complex64)
if out_shape[0] != in_shape[0]:
xf[-1,:] = xp.conj(xf[0,::-1])
if out_shape[1] != in_shape[1]:
xf[:,-1] = xp.conj(xf[::-1,0])
return xf
示例2: _init_proj_matrix
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import get_array_module [as 别名]
def _init_proj_matrix(self, init_sample, compressed_dim, proj_method):
"""
init the projection matrix
"""
if config.use_gpu:
xp = cp.get_array_module(init_sample[0])
else:
xp = np
x = [xp.reshape(x, (-1, x.shape[2])) for x in init_sample]
x = [z - z.mean(0) for z in x]
proj_matrix_ = []
if config.proj_init_method == 'pca':
for x_, compressed_dim_ in zip(x, compressed_dim):
proj_matrix, _, _ = xp.linalg.svd(x_.T.dot(x_))
proj_matrix = proj_matrix[:, :compressed_dim_]
proj_matrix_.append(proj_matrix)
elif config.proj_init_method == 'rand_uni':
for x_, compressed_dim_ in zip(x, compressed_dim):
proj_matrix = xp.random.uniform(size=(x_.shape[1], compressed_dim_))
proj_matrix /= xp.sqrt(xp.sum(proj_matrix**2, axis=0, keepdims=True))
proj_matrix_.append(proj_matrix)
return proj_matrix_
示例3: get_array_module
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import get_array_module [as 别名]
def get_array_module(*args):
"""Gets an appropriate one from :mod:`numpy` or :mod:`cupy`.
This is almost equivalent to :func:`cupy.get_array_module`. The differences
are that this function can be used even if CUDA is not available and that
it will return their data arrays' array module for
:class:`~chainer.Variable` arguments.
.. deprecated:: v5.0.0
This API is deprecated. Please use
:func:`~chainer.backend.get_array_module` instead.
Args:
args: Values to determine whether NumPy or CuPy should be used.
Returns:
module: :mod:`cupy` or :mod:`numpy` is returned based on the types of
the arguments.
"""
return chainer.backend.get_array_module(*args)
示例4: train_gmm
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import get_array_module [as 别名]
def train_gmm(X, max_iter, tol, means, covariances):
xp = cupy.get_array_module(X)
lower_bound = -np.infty
converged = False
weights = xp.array([0.5, 0.5], dtype=np.float32)
inv_cov = 1 / xp.sqrt(covariances)
for n_iter in range(max_iter):
prev_lower_bound = lower_bound
log_prob_norm, log_resp = e_step(X, inv_cov, means, weights)
weights, means, covariances = m_step(X, xp.exp(log_resp))
inv_cov = 1 / xp.sqrt(covariances)
lower_bound = log_prob_norm
change = lower_bound - prev_lower_bound
if abs(change) < tol:
converged = True
break
if not converged:
print('Failed to converge. Increase max-iter or tol.')
return inv_cov, means, weights, covariances
示例5: draw
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import get_array_module [as 别名]
def draw(X, pred, means, covariances, output):
xp = cupy.get_array_module(X)
for i in range(2):
labels = X[pred == i]
if xp is cupy:
labels = labels.get()
plt.scatter(labels[:, 0], labels[:, 1], c=np.random.rand(1, 3))
if xp is cupy:
means = means.get()
covariances = covariances.get()
plt.scatter(means[:, 0], means[:, 1], s=120, marker='s', facecolors='y',
edgecolors='k')
x = np.linspace(-5, 5, 1000)
y = np.linspace(-5, 5, 1000)
X, Y = np.meshgrid(x, y)
for i in range(2):
dist = stats.multivariate_normal(means[i], covariances[i])
Z = dist.pdf(np.stack([X, Y], axis=-1))
plt.contour(X, Y, Z)
plt.savefig(output)
示例6: fit
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import get_array_module [as 别名]
def fit(A, b, tol, max_iter):
# Note that this function works even tensors 'A' and 'b' are NumPy or CuPy
# arrays.
xp = cupy.get_array_module(A)
x = xp.zeros_like(b, dtype=np.float64)
r0 = b - xp.dot(A, x)
p = r0
for i in range(max_iter):
a = xp.inner(r0, r0) / xp.inner(p, xp.dot(A, p))
x += a * p
r1 = r0 - a * xp.dot(A, p)
if xp.linalg.norm(r1) < tol:
return x
b = xp.inner(r1, r1) / xp.inner(r0, r0)
p = r1 + b * p
r0 = r1
print('Failed to converge. Increase max-iter or tol.')
return x
示例7: cfft2
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import get_array_module [as 别名]
def cfft2(x):
in_shape = x.shape
# if both dimensions are odd
if gpu_config.use_gpu:
xp = cp.get_array_module(x)
else:
xp = np
if in_shape[0] % 2 == 1 and in_shape[1] % 2 == 1:
xf = xp.fft.fftshift(xp.fft.fftshift(fft2(x), 0), 1).astype(xp.complex64)
else:
out_shape = list(in_shape)
out_shape[0] = in_shape[0] + (in_shape[0] + 1) % 2
out_shape[1] = in_shape[1] + (in_shape[1] + 1) % 2
out_shape = tuple(out_shape)
xf = xp.zeros(out_shape, dtype=xp.complex64)
xf[:in_shape[0], :in_shape[1]] = xp.fft.fftshift(xp.fft.fftshift(fft2(x), 0), 1).astype(xp.complex64)
if out_shape[0] != in_shape[0]:
xf[-1,:] = xp.conj(xf[0,::-1])
if out_shape[1] != in_shape[1]:
xf[:,-1] = xp.conj(xf[::-1,0])
return xf
示例8: _init_proj_matrix
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import get_array_module [as 别名]
def _init_proj_matrix(self, init_sample, compressed_dim, proj_method):
"""
init the projection matrix
"""
if gpu_config.use_gpu:
xp = cp.get_array_module(init_sample[0])
else:
xp = np
x = [xp.reshape(x, (-1, x.shape[2])) for x in init_sample]
x = [z - z.mean(0) for z in x]
proj_matrix_ = []
if self.config.proj_init_method == 'pca':
for x_, compressed_dim_ in zip(x, compressed_dim):
proj_matrix, _, _ = xp.linalg.svd(x_.T.dot(x_))
proj_matrix = proj_matrix[:, :compressed_dim_]
proj_matrix_.append(proj_matrix)
elif self.config.proj_init_method == 'rand_uni':
for x_, compressed_dim_ in zip(x, compressed_dim):
proj_matrix = xp.random.uniform(size=(x_.shape[1], compressed_dim_))
proj_matrix /= xp.sqrt(xp.sum(proj_matrix**2, axis=0, keepdims=True))
proj_matrix_.append(proj_matrix)
return proj_matrix_
示例9: get_array_module
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import get_array_module [as 别名]
def get_array_module(x):
"""Returns the array module for `x`.
Args:
x (dezero.Variable or numpy.ndarray or cupy.ndarray): Values to
determine whether NumPy or CuPy should be used.
Returns:
module: `cupy` or `numpy` is returned based on the argument.
"""
if isinstance(x, Variable):
x = x.data
if not gpu_enable:
return np
xp = cp.get_array_module(x)
return xp
示例10: circ_mul_v
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import get_array_module [as 别名]
def circ_mul_v(circ,v,eigs=None):
''' multiply a circulant matrix A by multi vector v.
Args:
circ (ndarray): representation of the multilevel circulant matrix A, i.e.
the first column of A in proper shape.
v (ndarray): vector to be multiplied. Should be reshaped to the same shape
as circ. Should be the same reshape order (column first/row first) as circ.
Returns:
result of multiplication.
'''
if use_gpu > 0:
import cupy
xp = cupy.get_array_module(circ)
else:
xp = np
if eigs is None:
eigs = circ_eigs(circ)
tmp = xp.real(xp.fft.ifft2(xp.fft.fft2(v,norm='ortho')*eigs,norm='ortho'))
if xp is cupy:
return tmp.astype(xp.float32)
else:
return tmp
示例11: gradient_loss
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import get_array_module [as 别名]
def gradient_loss(generated, truth):
"""
:param generated: generated image by the generator at any scale
:param truth: The ground truth image at that scale
:return: GDL loss
"""
xp = cp.get_array_module(generated.data)
n, c, h, w = generated.shape
wx = xp.array([[[1, -1]]]*c, ndmin=4).astype(xp.float32)
wy = xp.array([[[1], [-1]]]*c, ndmin=4).astype(xp.float32)
d_gx = F.convolution_2d(generated, wx)
d_gy = F.convolution_2d(generated, wy)
d_tx = F.convolution_2d(truth, wx)
d_ty = F.convolution_2d(truth, wy)
return (F.sum(F.absolute(d_gx - d_tx)) + F.sum(F.absolute(d_gy - d_ty)))
示例12: get_array_module
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import get_array_module [as 别名]
def get_array_module(array):
"""Gets an appropriate module from :mod:`numpy` or :mod:`cupy`.
This is almost equivalent to :func:`cupy.get_array_module`. The differences
are that this function can be used even if cupy is not available.
Args:
array: Input array.
Returns:
module: :mod:`cupy` or :mod:`numpy` is returned based on input.
"""
if config.cupy_enabled:
return cp.get_array_module(array)
else:
return np
示例13: get_array_module
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import get_array_module [as 别名]
def get_array_module(x):
if cupy is not None:
return cupy.get_array_module(x)
else:
return numpy
示例14: fft2
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import get_array_module [as 别名]
def fft2(x):
if config.use_gpu:
xp = cp.get_array_module(x)
else:
xp = np
return xp.fft.fft(xp.fft.fft(x, axis=1), axis=0).astype(xp.complex64)
# return fft(fft(x, axis=1), axis=0)
示例15: ifft2
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import get_array_module [as 别名]
def ifft2(x):
if config.use_gpu:
xp = cp.get_array_module(x)
else:
xp = np
return xp.fft.ifft(xp.fft.ifft(x, axis=1), axis=0).astype(xp.complex64)
# return ifft(ifft(x, axis=1), axis=0)