Python cupy.array() Examples
The following are 30
code examples of cupy.array().
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: filters.py From cupy with MIT License | 6 votes |
def minimum_filter1d(input, size, axis=-1, output=None, mode="reflect", cval=0.0, origin=0): """Compute the minimum filter along a single axis. Args: input (cupy.ndarray): The input array. size (int): Length of the minimum filter. axis (int): The axis of input along which to calculate. Default is -1. output (cupy.ndarray, dtype or None): The array in which to place the output. Default is is same dtype as the input. mode (str): The array borders are handled according to the given mode (``'reflect'``, ``'constant'``, ``'nearest'``, ``'mirror'``, ``'wrap'``). Default is ``'reflect'``. cval (scalar): Value to fill past edges of input if mode is ``'constant'``. Default is ``0.0``. origin (int): The origin parameter controls the placement of the filter, relative to the center of the current element of the input. Default is ``0``. Returns: cupy.ndarray: The result of the filtering. .. seealso:: :func:`scipy.ndimage.minimum_filter1d` """ return _min_or_max_1d(input, size, axis, output, mode, cval, origin, 'min')
Example #2
Source File: cuda.py From chainer with MIT License | 6 votes |
def to_cpu(array, stream=None): """Copies the given GPU array to host CPU. Args: array (*array*, None, list or tuple): Array or arrays to be sent to CPU. stream (cupy.cuda.Stream): CUDA stream. Returns: numpy.ndarray, list or tuple: Array on CPU. If some of the arrays are already on CPU, then this function just returns those arrays without performing any copy. If input arrays include `None`, it is returned as `None` as is. """ return _backend._convert_arrays( array, lambda arr: _array_to_cpu(arr, stream))
Example #3
Source File: measurements.py From cupy with MIT License | 6 votes |
def standard_deviation(input, labels=None, index=None): """Calculates the standard deviation of the values of an n-D image array, optionally at specified sub-regions. Args: input (cupy.ndarray): Nd-image data to process. labels (cupy.ndarray or None): Labels defining sub-regions in `input`. If not None, must be same shape as `input`. index (cupy.ndarray or None): `labels` to include in output. If None (default), all values where `labels` is non-zero are used. Returns: standard_deviation (cupy.ndarray): standard deviation of values, for each sub-region if `labels` and `index` are specified. .. seealso:: :func:`scipy.ndimage.standard_deviation` """ return cupy.sqrt(variance(input, labels, index))
Example #4
Source File: gmm.py From cupy with MIT License | 6 votes |
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
Example #5
Source File: cuda.py From chainer with MIT License | 6 votes |
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)
Example #6
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 #7
Source File: fallback.py From cupy with MIT License | 6 votes |
def __init__(self, numpy_object, cupy_object, array=None): """ _RecursiveAttr initializer. Args: numpy_object (method): NumPy method. cupy_method (method): Corresponding CuPy method. array (ndarray): Acts as flag to know if _RecursiveAttr object is called from ``ndarray`` class. Also, acts as container for modifying args in case it is called from ``ndarray``. None otherwise. """ self._numpy_object = numpy_object self._cupy_object = cupy_object self._fallback_array = array
Example #8
Source File: filters.py From cupy with MIT License | 6 votes |
def _check_nd_args(input, weights, mode, origins, wghts_name='filter weights'): if input.dtype.kind == 'c': raise TypeError('Complex type not supported') _check_mode(mode) # The integer type to use for indices in the input array # The indices actually use byte positions and we can't just use # input.nbytes since that won't tell us the number of bytes between the # first and last elements when the array is non-contiguous nbytes = sum((x-1)*abs(stride) for x, stride in zip(input.shape, input.strides)) + input.dtype.itemsize int_type = 'int' if nbytes < (1 << 31) else 'ptrdiff_t' # However, weights must always be 2 GiB or less if weights.nbytes > (1 << 31): raise RuntimeError('weights must be 2 GiB or less, use FFTs instead') weight_dims = [x for x in weights.shape if x != 0] if len(weight_dims) != input.ndim: raise RuntimeError('{} array has incorrect shape'.format(wghts_name)) origins = _fix_sequence_arg(origins, len(weight_dims), 'origin', int) for origin, width in zip(origins, weight_dims): _check_origin(origin, width) return tuple(origins), int_type
Example #9
Source File: char_encdec.py From knmt with GNU General Public License v3.0 | 6 votes |
def generate_voc_encodings(encoder, charlist, voc_list, mb_size=1024): dataset = encode_voc_list(voc_list, charlist) print "voc_size:", len(dataset) xp = encoder.xp encodings_list = [] cursor = 0 while cursor < len(dataset): batch = dataset[cursor:cursor+mb_size] if xp != np: batch = [xp.array(bb) for bb in batch] cursor += mb_size encodings = encoder.compute_h(batch, train = False, use_workaround = True) encodings_list.append(encodings.data.reshape(encodings.data.shape[1:])) print "processed", cursor result = np.vstack(encodings_list) return result
Example #10
Source File: char_encdec.py From knmt with GNU General Public License v3.0 | 6 votes |
def do_eval(args): ced, charlist, chardict = load_encdec_from_config(args.config, args.model) if args.gpu is not None: chainer.cuda.Device(args.gpu).use() import cupy ced = ced.to_gpu(args.gpu) xp = cupy else: xp = np def enc(word): w_array=xp.array([chardict[c] for c in word], dtype=xp.int32) hx=ced.enc.compute_h((w_array,), train=False) return hx def dec(hx): decoded = ced.dec.decode(hx, length = 40, train = False) return "".join([charlist[int(idx)] for idx in decoded[0]]) IPython.embed()
Example #11
Source File: fallback.py From cupy with MIT License | 6 votes |
def _update_cupy_array(self): """ Updates _cupy_array from _numpy_array. To be executed before calling cupy function. """ base = self.base if base is None: if self._remember_numpy: if self._cupy_array is None: self._cupy_array = cp.array(self._numpy_array) else: self._cupy_array[:] = self._numpy_array else: if base._remember_numpy: base._update_cupy_array()
Example #12
Source File: dia.py From cupy with MIT License | 6 votes |
def get(self, stream=None): """Returns a copy of the array on host memory. Args: stream (cupy.cuda.Stream): CUDA stream object. If it is given, the copy runs asynchronously. Otherwise, the copy is synchronous. Returns: scipy.sparse.dia_matrix: Copy of the array on host memory. """ if not _scipy_available: raise RuntimeError('scipy is not available') data = self.data.get(stream) offsets = self.offsets.get(stream) return scipy.sparse.dia_matrix((data, offsets), shape=self._shape)
Example #13
Source File: coo.py From cupy with MIT License | 6 votes |
def get(self, stream=None): """Returns a copy of the array on host memory. Args: stream (cupy.cuda.Stream): CUDA stream object. If it is given, the copy runs asynchronously. Otherwise, the copy is synchronous. Returns: scipy.sparse.coo_matrix: Copy of the array on host memory. """ if not _scipy_available: raise RuntimeError('scipy is not available') data = self.data.get(stream) row = self.row.get(stream) col = self.col.get(stream) return scipy.sparse.coo_matrix( (data, (row, col)), shape=self.shape)
Example #14
Source File: gla_gpu.py From Deep_VoiceChanger with MIT License | 6 votes |
def __init__(self, parallel, wave_len=254, wave_dif=64, buffer_size=5, loop_num=5, window=np.hanning(254)): self.wave_len = wave_len self.wave_dif = wave_dif self.buffer_size = buffer_size self.loop_num = loop_num self.parallel = parallel self.window = cp.array([window for _ in range(parallel)]) self.wave_buf = cp.zeros((parallel, wave_len+wave_dif), dtype=float) self.overwrap_buf = cp.zeros((parallel, wave_dif*buffer_size+(wave_len-wave_dif)), dtype=float) self.spectrum_buffer = cp.ones((parallel, self.buffer_size, self.wave_len), dtype=complex) self.absolute_buffer = cp.ones((parallel, self.buffer_size, self.wave_len), dtype=complex) self.phase = cp.zeros((parallel, self.wave_len), dtype=complex) self.phase += cp.random.random((parallel, self.wave_len))-0.5 + cp.random.random((parallel, self.wave_len))*1j - 0.5j self.phase[self.phase == 0] = 1 self.phase /= cp.abs(self.phase)
Example #15
Source File: test_coo.py From cupy with MIT License | 5 votes |
def col(self, xp): return xp.array([0, 1, 3, 2], 'i')
Example #16
Source File: test_coo.py From cupy with MIT License | 5 votes |
def row(self, xp): return xp.array([0, 0, 1, 2], 'i')
Example #17
Source File: test_coo.py From cupy with MIT License | 5 votes |
def data(self, xp): return xp.array([0, 1, 2, 3], self.dtype)
Example #18
Source File: test_coo.py From cupy with MIT License | 5 votes |
def test_intlike_shape(self, xp, sp): s = sp.coo_matrix((self.data(xp), (self.row(xp), self.col(xp))), shape=(xp.array(self.shape[0]), xp.int32(self.shape[1]))) assert isinstance(s.shape[0], int) assert isinstance(s.shape[1], int) return s
Example #19
Source File: test_coo.py From cupy with MIT License | 5 votes |
def test_fail_to_infer_shape(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): data = xp.array([], dtype=self.dtype) row = xp.array([], dtype='i') col = xp.array([], dtype='i') with pytest.raises(ValueError): sp.coo_matrix((data, (row, col)), shape=None)
Example #20
Source File: test_coo.py From cupy with MIT License | 5 votes |
def test_col_too_large(self): for xp, sp in ((numpy, scipy.sparse), (cupy, sparse)): col = xp.array([0, 1, 4, 2], 'i') with pytest.raises(ValueError): sp.coo_matrix( (self.data(xp), (self.row(xp), col)), shape=self.shape)
Example #21
Source File: test_scatter.py From cupy with MIT License | 5 votes |
def test_scatter_min(self): a = cupy.zeros((4,), dtype=numpy.float32) i = cupy.array([0, 1, 0, 1, 2, 2], numpy.int32) v = cupy.array([1, -1, -1, -3, -2, -4], dtype=numpy.float32) cupyx.scatter_min(a, i, v) testing.assert_array_equal(a, cupy.array([-1, -3, -4, 0]))
Example #22
Source File: test_coo.py From cupy with MIT License | 5 votes |
def test_dot_zero_dim(self, xp, sp): m = _make(xp, sp, self.dtype) x = xp.array(2, dtype=self.dtype) return m.dot(x)
Example #23
Source File: measurements.py From cupy with MIT License | 5 votes |
def _generate_binary_structure(rank, connectivity): if connectivity < 1: connectivity = 1 if rank < 1: return numpy.array(True, dtype=bool) output = numpy.fabs(numpy.indices([3] * rank) - 1) output = numpy.add.reduce(output, 0) return output <= connectivity
Example #24
Source File: morphology.py From cupy with MIT License | 5 votes |
def grey_opening(input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0): """Calculates a multi-dimensional greyscale opening. Args: input (cupy.ndarray): The input array. size (tuple of ints): Shape of a flat and full structuring element used for the greyscale opening. Optional if ```footprint``` or ```structure``` is provided. footprint (array of ints): Positions of non-infinite elements of a flat structuring element used for greyscale opening. Non-zero values give the set of neighbors of the center over which opening is chosen. structure (array of ints): Structuring element used for the greyscale opening. ```structure``` may be a non-flat structuring element. output (cupy.ndarray, dtype or None): The array in which to place the output. mode (str): The array borders are handled according to the given mode (``'reflect'``, ``'constant'``, ``'nearest'``, ``'mirror'``, ``'wrap'``). Default is ``'reflect'``. cval (scalar): Value to fill past edges of input if mode is ``constant``. Default is ``0.0``. origin (scalar or tuple of scalar): The origin parameter controls the placement of the filter, relative to the center of the current element of the input. Default of 0 is equivalent to ``(0,)*input.ndim``. Returns: cupy.ndarray: The result of greyscale opening. .. seealso:: :func:`scipy.ndimage.grey_opening` """ if (size is not None) and (footprint is not None): warnings.warn("ignoring size because footprint is set", UserWarning, stacklevel=2) tmp = grey_erosion(input, size, footprint, structure, None, mode, cval, origin) return grey_dilation(tmp, size, footprint, structure, output, mode, cval, origin)
Example #25
Source File: morphology.py From cupy with MIT License | 5 votes |
def grey_closing(input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0): """Calculates a multi-dimensional greyscale closing. Args: input (cupy.ndarray): The input array. size (tuple of ints): Shape of a flat and full structuring element used for the greyscale closing. Optional if ```footprint``` or ```structure``` is provided. footprint (array of ints): Positions of non-infinite elements of a flat structuring element used for greyscale closing. Non-zero values give the set of neighbors of the center over which closing is chosen. structure (array of ints): Structuring element used for the greyscale closing. ```structure``` may be a non-flat structuring element. output (cupy.ndarray, dtype or None): The array in which to place the output. mode (str): The array borders are handled according to the given mode (``'reflect'``, ``'constant'``, ``'nearest'``, ``'mirror'``, ``'wrap'``). Default is ``'reflect'``. cval (scalar): Value to fill past edges of input if mode is ``constant``. Default is ``0.0``. origin (scalar or tuple of scalar): The origin parameter controls the placement of the filter, relative to the center of the current element of the input. Default of 0 is equivalent to ``(0,)*input.ndim``. Returns: cupy.ndarray: The result of greyscale closing. .. seealso:: :func:`scipy.ndimage.grey_closing` """ if (size is not None) and (footprint is not None): warnings.warn("ignoring size because footprint is set", UserWarning, stacklevel=2) tmp = grey_dilation(input, size, footprint, structure, None, mode, cval, origin) return grey_erosion(tmp, size, footprint, structure, output, mode, cval, origin)
Example #26
Source File: morphology.py From cupy with MIT License | 5 votes |
def grey_erosion(input, size=None, footprint=None, structure=None, output=None, mode='reflect', cval=0.0, origin=0): """Calculates a greyscale erosion. Args: input (cupy.ndarray): The input array. size (tuple of ints): Shape of a flat and full structuring element used for the greyscale erosion. Optional if ```footprint``` or ```structure``` is provided. footprint (array of ints): Positions of non-infinite elements of a flat structuring element used for greyscale erosion. Non-zero values give the set of neighbors of the center over which minimum is chosen. structure (array of ints): Structuring element used for the greyscale erosion. ```structure``` may be a non-flat structuring element. output (cupy.ndarray, dtype or None): The array in which to place the output. mode (str): The array borders are handled according to the given mode (``'reflect'``, ``'constant'``, ``'nearest'``, ``'mirror'``, ``'wrap'``). Default is ``'reflect'``. cval (scalar): Value to fill past edges of input if mode is ``constant``. Default is ``0.0``. origin (scalar or tuple of scalar): The origin parameter controls the placement of the filter, relative to the center of the current element of the input. Default of 0 is equivalent to ``(0,)*input.ndim``. Returns: cupy.ndarray: The result of greyscale erosion. .. seealso:: :func:`scipy.ndimage.grey_erosion` """ if size is None and footprint is None and structure is None: raise ValueError('size, footprint or structure must be specified') return filters._min_or_max_filter(input, size, footprint, structure, output, mode, cval, origin, 'min')
Example #27
Source File: filters.py From cupy with MIT License | 5 votes |
def _check_size_footprint_structure(ndim, size, footprint, structure, stacklevel=3, force_footprint=False): if structure is None and footprint is None: if size is None: raise RuntimeError("no footprint or filter size provided") sizes = _fix_sequence_arg(size, ndim, 'size', int) if force_footprint: return None, cupy.ones(sizes, bool), None return sizes, None, None if size is not None: warnings.warn("ignoring size because {} is set".format( 'structure' if footprint is None else 'footprint'), UserWarning, stacklevel=stacklevel+1) if footprint is not None: footprint = cupy.array(footprint, bool, True, 'C') if not footprint.any(): raise ValueError("all-zero footprint is not supported") if structure is None: if not force_footprint and footprint.all(): return footprint.shape, None, None return None, footprint, None structure = cupy.ascontiguousarray(structure) if footprint is None: footprint = cupy.ones(structure.shape, bool) return None, footprint, structure
Example #28
Source File: filters.py From cupy with MIT License | 5 votes |
def percentile_filter(input, percentile, size=None, footprint=None, output=None, mode="reflect", cval=0.0, origin=0): """Multi-dimensional percentile filter. Args: input (cupy.ndarray): The input array. percentile (scalar): The percentile of the element to get (from ``0`` to ``100``). Can be negative, thus ``-20`` equals ``80``. size (int or sequence of int): One of ``size`` or ``footprint`` must be provided. If ``footprint`` is given, ``size`` is ignored. Otherwise ``footprint = cupy.ones(size)`` with ``size`` automatically made to match the number of dimensions in ``input``. footprint (cupy.ndarray): a boolean array which specifies which of the elements within this shape will get passed to the filter function. output (cupy.ndarray, dtype or None): The array in which to place the output. Default is is same dtype as the input. mode (str): The array borders are handled according to the given mode (``'reflect'``, ``'constant'``, ``'nearest'``, ``'mirror'``, ``'wrap'``). Default is ``'reflect'``. cval (scalar): Value to fill past edges of input if mode is ``'constant'``. Default is ``0.0``. origin (int or sequence of int): The origin parameter controls the placement of the filter, relative to the center of the current element of the input. Default of 0 is equivalent to ``(0,)*input.ndim``. Returns: cupy.ndarray: The result of the filtering. .. seealso:: :func:`scipy.ndimage.percentile_filter` """ percentile = float(percentile) if percentile < 0.0: percentile += 100.0 if percentile < 0.0 or percentile > 100.0: raise RuntimeError('invalid percentile') if percentile == 100.0: def get_rank(fs): return fs - 1 else: def get_rank(fs): return int(float(fs) * percentile / 100.0) return _rank_filter(input, get_rank, size, footprint, output, mode, cval, origin)
Example #29
Source File: filters.py From cupy with MIT License | 5 votes |
def median_filter(input, size=None, footprint=None, output=None, mode="reflect", cval=0.0, origin=0): """Multi-dimensional median filter. Args: input (cupy.ndarray): The input array. size (int or sequence of int): One of ``size`` or ``footprint`` must be provided. If ``footprint`` is given, ``size`` is ignored. Otherwise ``footprint = cupy.ones(size)`` with ``size`` automatically made to match the number of dimensions in ``input``. footprint (cupy.ndarray): a boolean array which specifies which of the elements within this shape will get passed to the filter function. output (cupy.ndarray, dtype or None): The array in which to place the output. Default is is same dtype as the input. mode (str): The array borders are handled according to the given mode (``'reflect'``, ``'constant'``, ``'nearest'``, ``'mirror'``, ``'wrap'``). Default is ``'reflect'``. cval (scalar): Value to fill past edges of input if mode is ``'constant'``. Default is ``0.0``. origin (int or sequence of int): The origin parameter controls the placement of the filter, relative to the center of the current element of the input. Default of 0 is equivalent to ``(0,)*input.ndim``. Returns: cupy.ndarray: The result of the filtering. .. seealso:: :func:`scipy.ndimage.median_filter` """ return _rank_filter(input, lambda fs: fs//2, size, footprint, output, mode, cval, origin)
Example #30
Source File: filters.py From cupy with MIT License | 5 votes |
def rank_filter(input, rank, size=None, footprint=None, output=None, mode="reflect", cval=0.0, origin=0): """Multi-dimensional rank filter. Args: input (cupy.ndarray): The input array. rank (int): The rank of the element to get. Can be negative to count from the largest value, e.g. ``-1`` indicates the largest value. size (int or sequence of int): One of ``size`` or ``footprint`` must be provided. If ``footprint`` is given, ``size`` is ignored. Otherwise ``footprint = cupy.ones(size)`` with ``size`` automatically made to match the number of dimensions in ``input``. footprint (cupy.ndarray): a boolean array which specifies which of the elements within this shape will get passed to the filter function. output (cupy.ndarray, dtype or None): The array in which to place the output. Default is is same dtype as the input. mode (str): The array borders are handled according to the given mode (``'reflect'``, ``'constant'``, ``'nearest'``, ``'mirror'``, ``'wrap'``). Default is ``'reflect'``. cval (scalar): Value to fill past edges of input if mode is ``'constant'``. Default is ``0.0``. origin (int or sequence of int): The origin parameter controls the placement of the filter, relative to the center of the current element of the input. Default of 0 is equivalent to ``(0,)*input.ndim``. Returns: cupy.ndarray: The result of the filtering. .. seealso:: :func:`scipy.ndimage.rank_filter` """ rank = int(rank) return _rank_filter(input, lambda fs: rank+fs if rank < 0 else rank, size, footprint, output, mode, cval, origin)