本文整理汇总了Python中chainer.backends.cuda.get_device_from_array方法的典型用法代码示例。如果您正苦于以下问题:Python cuda.get_device_from_array方法的具体用法?Python cuda.get_device_from_array怎么用?Python cuda.get_device_from_array使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer.backends.cuda
的用法示例。
在下文中一共展示了cuda.get_device_from_array方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: backward
# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import get_device_from_array [as 别名]
def backward(self, inputs, grads):
gpu = backend.get_array_module(*inputs) is cuda.cupy
# TODO(unno): We can remove redundant gpu-cpu copy using
# theano.sandbox.cuda.basic_ops.gpu_from_host
args = [cuda.to_cpu(x) for x in inputs + grads]
outputs = self.backward_func(*args)
assert len(outputs) == len(inputs)
if gpu:
# TODO(unno): We can remove redundant gpu-cpu copy using
# theano.sandbox.cuda.CudaNdarray.gpudata
device = cuda.get_device_from_array(inputs)
outputs = [cuda.to_gpu(x, device) for x in outputs]
results = []
for o, i in zip(outputs, inputs):
if i.dtype.kind != 'f':
o = None
elif o.dtype != i.dtype:
o = o.astype(i.dtype)
results.append(o)
return tuple(results)
示例2: test_get_device_from_array_for_numpy_int
# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import get_device_from_array [as 别名]
def test_get_device_from_array_for_numpy_int(self):
assert cuda.get_device_from_array(numpy.int64(0)) is cuda.DummyDevice
示例3: test_get_device_for_empty_array
# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import get_device_from_array [as 别名]
def test_get_device_for_empty_array(self):
x = cuda.get_device_from_array(cuda.cupy.array([]).reshape((0, 10)))
# TODO(okuta): Only check `assert x == cuda.Device(0)`
# when cupy/cupy#946 is merged
assert x == cuda.Device(0) or x == cuda.DummyDevice
示例4: test_get_device_warning
# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import get_device_from_array [as 别名]
def test_get_device_warning(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
cuda.get_device(cuda.cupy.array([1]))
assert len(w) == 1
assert w[0].category is DeprecationWarning
assert ('get_device is deprecated. Please use get_device_from_id'
' or get_device_from_array instead.' in str(w[0].message))
示例5: test_get_device_from_array
# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import get_device_from_array [as 别名]
def test_get_device_from_array(self):
arr = cuda.cupy.array([0])
assert cuda.get_device_from_array(arr) == cuda.Device(0)
示例6: __init__
# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import get_device_from_array [as 别名]
def __init__(self, array):
super(Parameter, self).__init__()
self.add_param('W', array.shape, dtype=array.dtype)
self.W.array = array
if isinstance(array, cuda.ndarray):
self.to_gpu(cuda.get_device_from_array(array))
示例7: forward
# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import get_device_from_array [as 别名]
def forward(self, inputs):
gpu = backend.get_array_module(*inputs) is cuda.cupy
inputs = [cuda.to_cpu(x) for x in inputs]
outputs = self.forward_func(*inputs)
if gpu:
# TODO(unno): We can remove redundant gpu-cpu copy using
# theano.sandbox.cuda.CudaNdarray.gpudata
device = cuda.get_device_from_array(inputs)
outputs = [cuda.to_gpu(x, device) for x in outputs]
return tuple(outputs)
示例8: concat_arrays
# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import get_device_from_array [as 别名]
def concat_arrays(arrays):
# Convert `arrays` to numpy.ndarray or cupy.ndarray
xp = cuda.get_array_module(arrays[0])
with cuda.get_device_from_array(arrays[0]):
return xp.concatenate(arrays)
示例9: __call__
# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import get_device_from_array [as 别名]
def __call__(self, rule, param):
g = param.grad
with cuda.get_device_from_array(g):
g *= self.rate
示例10: init_state
# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import get_device_from_array [as 别名]
def init_state(self, param):
xp = backend.get_array_module(param.data)
with cuda.get_device_from_array(param.data):
self.state['m'] = xp.zeros_like(param.data)
self.state['v'] = xp.zeros_like(param.data)
# For iDeep
if isinstance(param.data, intel64.mdarray):
self.state['m'] = intel64.ideep.array(
self.state['m'], itype=intel64.ideep.wgt_array)
self.state['v'] = intel64.ideep.array(
self.state['v'], itype=intel64.ideep.wgt_array)
示例11: _check_array
# 需要导入模块: from chainer.backends import cuda [as 别名]
# 或者: from chainer.backends.cuda import get_device_from_array [as 别名]
def _check_array(array, name):
xp = cuda.get_array_module(array)
with cuda.get_device_from_array(array):
if not array.dtype == xp.float32:
warnings.warn('non FP32 dtype detected in {}'.format(name))
array = array.astype(xp.float32)
if not (array.flags.c_contiguous or array.flags.f_contiguous):
warnings.warn('non contiguous array detected in {}'.format(name))
array = xp.ascontiguousarray(array)
return array