本文整理汇总了Python中cupy.tensordot方法的典型用法代码示例。如果您正苦于以下问题:Python cupy.tensordot方法的具体用法?Python cupy.tensordot怎么用?Python cupy.tensordot使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cupy
的用法示例。
在下文中一共展示了cupy.tensordot方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convolve2d
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import tensordot [as 别名]
def convolve2d(in1, in2, mode='full'):
"""
note only support H * W * N * 1 convolve 2d
"""
in1 = in1.transpose(2, 3, 0, 1) # to N * C * H * W
in2 = in2.transpose(2, 3, 0, 1)
out_c, _, kh, kw = in2.shape
n, _, h, w = in1.shape
if mode == 'full':
ph, pw = kh-1, kw-1
out_h, out_w = h-kh+1+ph*2, w-kw+1+pw*2# TODO
elif mode == 'valid':
ph, pw = 0, 0
out_h, out_w = h-kh+1, w-kw+1 # TODO
else:
raise NotImplementedError
y = cp.empty((n, out_c, out_h, out_w), dtype=in1.dtype)
col = im2col_gpu(in1, kh, kw, 1, 1, ph, pw)
y = cp.tensordot(
col, in2, ((1, 2, 3), (1, 2, 3))).astype(in1.dtype, copy=False)
y = cp.rollaxis(y, 3, 1)
return y.transpose(2, 3, 0, 1)
示例2: tensorsolve
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import tensordot [as 别名]
def tensorsolve(a, b, axes=None):
"""Solves tensor equations denoted by ``ax = b``.
Suppose that ``b`` is equivalent to ``cupy.tensordot(a, x)``.
This function computes tensor ``x`` from ``a`` and ``b``.
Args:
a (cupy.ndarray): The tensor with ``len(shape) >= 1``
b (cupy.ndarray): The tensor with ``len(shape) >= 1``
axes (tuple of ints): Axes in ``a`` to reorder to the right
before inversion.
Returns:
cupy.ndarray:
The tensor with shape ``Q`` such that ``b.shape + Q == a.shape``.
.. warning::
This function calls one or more cuSOLVER routine(s) which may yield
invalid results if input conditions are not met.
To detect these invalid results, you can set the `linalg`
configuration to a value that is not `ignore` in
:func:`cupyx.errstate` or :func:`cupyx.seterr`.
.. seealso:: :func:`numpy.linalg.tensorsolve`
"""
if axes is not None:
allaxes = list(range(a.ndim))
for k in axes:
allaxes.remove(k)
allaxes.insert(a.ndim, k)
a = a.transpose(allaxes)
oldshape = a.shape[-(a.ndim - b.ndim):]
prod = cupy.core.internal.prod(oldshape)
a = a.reshape(-1, prod)
b = b.ravel()
result = solve(a, b)
return result.reshape(oldshape)
示例3: tensorinv
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import tensordot [as 别名]
def tensorinv(a, ind=2):
"""Computes the inverse of a tensor.
This function computes tensor ``a_inv`` from tensor ``a`` such that
``tensordot(a_inv, a, ind) == I``, where ``I`` denotes the identity tensor.
Args:
a (cupy.ndarray):
The tensor such that
``prod(a.shape[:ind]) == prod(a.shape[ind:])``.
ind (int):
The positive number used in ``axes`` option of ``tensordot``.
Returns:
cupy.ndarray:
The inverse of a tensor whose shape is equivalent to
``a.shape[ind:] + a.shape[:ind]``.
.. warning::
This function calls one or more cuSOLVER routine(s) which may yield
invalid results if input conditions are not met.
To detect these invalid results, you can set the `linalg`
configuration to a value that is not `ignore` in
:func:`cupyx.errstate` or :func:`cupyx.seterr`.
.. seealso:: :func:`numpy.linalg.tensorinv`
"""
util._assert_cupy_array(a)
if ind <= 0:
raise ValueError('Invalid ind argument')
oldshape = a.shape
invshape = oldshape[ind:] + oldshape[:ind]
prod = cupy.core.internal.prod(oldshape[ind:])
a = a.reshape(prod, -1)
a_inv = inv(a)
return a_inv.reshape(*invshape)
示例4: binary_to_decimal
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import tensordot [as 别名]
def binary_to_decimal(X):
"""
| This function takes :code:`X` of shape (n_images, L2, y, x) as an argument.
| Supporse that :code:`X[k]` (0 <= k < n_images) can be represented as
.. code-block:: none
X[k] = [map_k[0], map_k[1], ..., map_k[L2-1]]
where the shape of each map_k is (y, x).
Then we calculate
.. code-block:: none
a[0] * map_k[0] + a[1] * map_k[1] + ... + a[L2-1] * map_k[L2-1]
for each :code:`X[k]`, where :math:`a = [2^{L2-1}, 2^{L2-2}, ..., 2^{0}]`
Therefore, the output shape must be (n_images, y, x)
Parameters
----------
X: xp.ndarray
Feature maps
"""
a = xp.arange(X.shape[1])[::-1]
a = xp.power(2, a)
return xp.tensordot(X, a, axes=([1], [0]))