本文整理汇总了Python中cupy.zeros方法的典型用法代码示例。如果您正苦于以下问题:Python cupy.zeros方法的具体用法?Python cupy.zeros怎么用?Python cupy.zeros使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cupy
的用法示例。
在下文中一共展示了cupy.zeros方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import zeros [as 别名]
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)
示例2: auto_inverse
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import zeros [as 别名]
def auto_inverse(self, whole_spectrum):
whole_spectrum = np.copy(whole_spectrum).astype(complex)
whole_spectrum[whole_spectrum < 1] = 1
overwrap = self.buffer_size * 2
height = whole_spectrum.shape[0]
parallel_dif = (height-overwrap) // self.parallel
if height < self.parallel*overwrap:
raise Exception('voice length is too small to use gpu, or parallel number is too big')
spec = [self.inverse(whole_spectrum[range(i, i+parallel_dif*self.parallel, parallel_dif), :]) for i in tqdm.tqdm(range(parallel_dif+overwrap))]
spec = spec[overwrap:]
spec = np.concatenate(spec, axis=1)
spec = spec.reshape(-1, self.wave_len)
#Below code don't consider wave_len and wave_dif, I'll fix.
wave = np.fft.ifft(spec, axis=1).real
pad = np.zeros((wave.shape[0], 2), dtype=float)
wave = np.concatenate([wave, pad], axis=1)
dst = np.zeros((wave.shape[0]+3)*self.wave_dif, dtype=float)
for i in range(4):
w = wave[range(i, wave.shape[0], 4),:]
w = w.reshape(-1)
dst[i*self.wave_dif:i*self.wave_dif+len(w)] += w
return dst*0.5
示例3: __init__
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import zeros [as 别名]
def __init__(self, size_limit=0, device_id=None):
super().__init__(size_limit=size_limit)
if device_id is not None:
os.environ['CUDA_VISIBLE_DEVICES'] = str(device_id)
# warm up cupy
try:
import cupy
cupy.zeros((10, 10)).sum()
except ImportError:
pass
# warm up cudf
try:
import cudf
import numpy as np
import pandas as pd
cudf.from_pandas(pd.DataFrame(np.zeros((10, 10))))
except ImportError:
pass
示例4: _non_maximum_suppression_gpu
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import zeros [as 别名]
def _non_maximum_suppression_gpu(bbox, thresh, score=None, limit=None):
if len(bbox) == 0:
return cp.zeros((0,), dtype=np.int32)
n_bbox = bbox.shape[0]
if score is not None:
order = score.argsort()[::-1].astype(np.int32)
else:
order = cp.arange(n_bbox, dtype=np.int32)
sorted_bbox = bbox[order, :]
selec, n_selec = _call_nms_kernel(
sorted_bbox, thresh)
selec = selec[:n_selec]
selec = order[selec]
if limit is not None:
selec = selec[:limit]
return selec
示例5: _call_nms_kernel
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import zeros [as 别名]
def _call_nms_kernel(bbox, thresh):
assert False, "Not supported."
n_bbox = bbox.shape[0]
threads_per_block = 64
col_blocks = np.ceil(n_bbox / threads_per_block).astype(np.int32)
blocks = (col_blocks, col_blocks, 1)
threads = (threads_per_block, 1, 1)
mask_dev = cp.zeros((n_bbox * col_blocks,), dtype=np.uint64)
bbox = cp.ascontiguousarray(bbox, dtype=np.float32)
kern = cp.RawKernel(_nms_gpu_code, 'nms_kernel')
kern(blocks, threads, args=(cp.int32(n_bbox), cp.float32(thresh),
bbox, mask_dev))
mask_host = mask_dev.get()
selection, n_selec = _nms_gpu_post(
mask_host, n_bbox, threads_per_block, col_blocks)
return selection, n_selec
示例6: _non_maximum_suppression_gpu
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import zeros [as 别名]
def _non_maximum_suppression_gpu(bbox, thresh, score=None, limit=None):
if len(bbox) == 0:
return cp.zeros((0,), dtype=np.int32)
n_bbox = bbox.shape[0]
if score is not None:
order = score.argsort()[::-1].astype(np.int32)
else:
order = cp.arange(n_bbox, dtype=np.int32)
sorted_bbox = bbox[order, :]
selec, n_selec = _call_nms_kernel(
sorted_bbox, thresh)
selec = selec[:n_selec]
selec = order[selec]
if limit is not None:
selec = selec[:limit]
return cp.asnumpy(selec)
示例7: _call_nms_kernel
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import zeros [as 别名]
def _call_nms_kernel(bbox, thresh):
# PyTorch does not support unsigned long Tensor.
# Doesn't matter,since it returns ndarray finally.
# So I'll keep it unmodified.
n_bbox = bbox.shape[0]
threads_per_block = 64
col_blocks = np.ceil(n_bbox / threads_per_block).astype(np.int32)
blocks = (col_blocks, col_blocks, 1)
threads = (threads_per_block, 1, 1)
mask_dev = cp.zeros((n_bbox * col_blocks,), dtype=np.uint64)
bbox = cp.ascontiguousarray(bbox, dtype=np.float32)
kern = _load_kernel('nms_kernel', _nms_gpu_code)
kern(blocks, threads, args=(cp.int32(n_bbox), cp.float32(thresh),
bbox, mask_dev))
mask_host = mask_dev.get()
selection, n_selec = _nms_gpu_post(
mask_host, n_bbox, threads_per_block, col_blocks)
return selection, n_selec
示例8: getnnz
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import zeros [as 别名]
def getnnz(self, axis=None):
"""Returns the number of stored values, including explicit zeros.
Args:
axis: Not supported yet.
Returns:
int: The number of stored values.
"""
if axis is None:
return self.data.size
else:
raise ValueError
# TODO(unno): Implement sorted_indices
示例9: getnnz
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import zeros [as 别名]
def getnnz(self, axis=None):
"""Returns the number of stored values, including explicit zeros.
Args:
axis: Not supported yet.
Returns:
int: The number of stored values.
"""
if axis is not None:
raise NotImplementedError(
'getnnz over an axis is not implemented for DIA format')
m, n = self.shape
nnz = core.ReductionKernel(
'int32 offsets, int32 m, int32 n', 'int32 nnz',
'offsets > 0 ? min(m, n - offsets) : min(m + offsets, n)',
'a + b', 'nnz = a', '0', 'dia_nnz')(self.offsets, m, n)
return int(nnz)
示例10: diagonal
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import zeros [as 别名]
def diagonal(self, k=0):
"""Returns the k-th diagonal of the matrix.
Args:
k (int, optional): Which diagonal to get, corresponding to elements
a[i, i+k]. Default: 0 (the main diagonal).
Returns:
cupy.ndarray : The k-th diagonal.
"""
rows, cols = self.shape
if k <= -rows or k >= cols:
return cupy.empty(0, dtype=self.data.dtype)
idx, = cupy.nonzero(self.offsets == k)
first_col, last_col = max(0, k), min(rows + k, cols)
if idx.size == 0:
return cupy.zeros(last_col - first_col, dtype=self.data.dtype)
return self.data[idx[0], first_col:last_col]
示例11: _label
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import zeros [as 别名]
def _label(x, structure, y):
elems = numpy.where(structure != 0)
vecs = [elems[dm] - 1 for dm in range(x.ndim)]
offset = vecs[0]
for dm in range(1, x.ndim):
offset = offset * 3 + vecs[dm]
indxs = numpy.where(offset < 0)[0]
dirs = [[vecs[dm][dr] for dm in range(x.ndim)] for dr in indxs]
dirs = cupy.array(dirs, dtype=numpy.int32)
ndirs = indxs.shape[0]
y_shape = cupy.array(y.shape, dtype=numpy.int32)
count = cupy.zeros(2, dtype=numpy.int32)
_kernel_init()(x, y)
_kernel_connect()(y_shape, dirs, ndirs, x.ndim, y, size=y.size)
_kernel_count()(y, count, size=y.size)
maxlabel = int(count[0])
labels = cupy.empty(maxlabel, dtype=numpy.int32)
_kernel_labels()(y, count, labels, size=y.size)
_kernel_finalize()(maxlabel, cupy.sort(labels), y, size=y.size)
return maxlabel
示例12: fit_custom
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import zeros [as 别名]
def fit_custom(X, n_clusters, max_iter):
assert X.ndim == 2
n_samples = len(X)
pred = cupy.zeros(n_samples)
initial_indexes = cupy.random.choice(n_samples, n_clusters, replace=False)
centers = X[initial_indexes]
for _ in range(max_iter):
distances = var_kernel(X[:, None, 0], X[:, None, 1],
centers[None, :, 1], centers[None, :, 0])
new_pred = cupy.argmin(distances, axis=1)
if cupy.all(new_pred == pred):
break
pred = new_pred
i = cupy.arange(n_clusters)
mask = pred == i[:, None]
sums = sum_kernel(X, mask[:, :, None], axis=1)
counts = count_kernel(mask, axis=1).reshape((n_clusters, 1))
centers = sums / counts
return centers, pred
示例13: test_cuda_array_interface_view
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import zeros [as 别名]
def test_cuda_array_interface_view(self):
arr = cupy.zeros(shape=(10, 20), dtype=cupy.float64)
view = arr[::2, ::5]
iface = view.__cuda_array_interface__
assert (set(iface.keys()) ==
set(['shape', 'typestr', 'data', 'version',
'strides', 'descr']))
assert iface['shape'] == (5, 4)
assert iface['typestr'] == '<f8'
assert isinstance(iface['data'], tuple)
assert len(iface['data']) == 2
assert iface['data'][0] == arr.data.ptr
assert not iface['data'][1]
assert iface['version'] == 2
assert iface['strides'] == (320, 40)
assert iface['descr'] == [('', '<f8')]
示例14: test_cuda_array_interface_zero_size
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import zeros [as 别名]
def test_cuda_array_interface_zero_size(self):
arr = cupy.zeros(shape=(10,), dtype=cupy.float64)
view = arr[0:3:-1]
iface = view.__cuda_array_interface__
assert (set(iface.keys()) ==
set(['shape', 'typestr', 'data', 'version',
'strides', 'descr']))
assert iface['shape'] == (0,)
assert iface['typestr'] == '<f8'
assert isinstance(iface['data'], tuple)
assert len(iface['data']) == 2
assert iface['data'][0] == 0
assert not iface['data'][1]
assert iface['version'] == 2
assert iface['strides'] is None
assert iface['descr'] == [('', '<f8')]
示例15: test_scatter_minmax_differnt_dtypes
# 需要导入模块: import cupy [as 别名]
# 或者: from cupy import zeros [as 别名]
def test_scatter_minmax_differnt_dtypes(self, src_dtype, dst_dtype):
shape = (2, 3)
a = cupy.zeros(shape, dtype=src_dtype)
value = cupy.array(1, dtype=dst_dtype)
slices = ([1, 1], slice(None))
a.scatter_max(slices, value)
numpy.testing.assert_almost_equal(
a.get(),
numpy.array([[0, 0, 0], [1, 1, 1]], dtype=src_dtype))
a = cupy.ones(shape, dtype=src_dtype)
value = cupy.array(0, dtype=dst_dtype)
a.scatter_min(slices, value)
numpy.testing.assert_almost_equal(
a.get(),
numpy.array([[1, 1, 1], [0, 0, 0]], dtype=src_dtype))