Python cupy.tensordot() Examples
The following are 5
code examples of cupy.tensordot().
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: cuda_tools.py From pyECO with MIT License | 6 votes |
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)
Example #2
Source File: cuda_tools.py From pyCFTrackers with MIT License | 6 votes |
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)
Example #3
Source File: solve.py From cupy with MIT License | 5 votes |
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)
Example #4
Source File: solve.py From cupy with MIT License | 5 votes |
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)
Example #5
Source File: pcanet.py From PCANet with MIT License | 5 votes |
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]))