本文整理汇总了Python中chainer.utils方法的典型用法代码示例。如果您正苦于以下问题:Python chainer.utils方法的具体用法?Python chainer.utils怎么用?Python chainer.utils使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer
的用法示例。
在下文中一共展示了chainer.utils方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: forward
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import utils [as 别名]
def forward(self, inputs):
logit, x = inputs
self.retain_inputs((0, 1))
xp = backend.get_array_module(x)
y = logit * (x - 1) - xp.log(xp.exp(-logit) + 1)
y = utils.force_array(y)
# extreme logit
logit_isinf = xp.isinf(logit)
self.logit_ispinf = xp.bitwise_and(logit_isinf, logit > 0)
self.logit_isminf = xp.bitwise_and(logit_isinf, logit <= 0)
with numpy.errstate(divide='ignore', invalid='raise'):
y = xp.where(self.logit_ispinf, xp.log(x), y)
y = xp.where(self.logit_isminf, xp.log(1 - x), y)
if self.binary_check:
self.invalid = utils.force_array(xp.bitwise_and(x != 0, x != 1))
y[self.invalid] = -xp.inf
return utils.force_array(y, logit.dtype),
示例2: forward
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import utils [as 别名]
def forward(self, xs):
self.len = len(xs)
if len(xs) == 1:
return xs
if (intel64.should_use_ideep('>=auto')
and intel64.inputs_all_ready(xs)
and all(x.shape == xs[0].shape for x in xs[1:])):
y = intel64.ideep.multi_add(xs)
else:
# The output should be a new array. Add the first 2 arrays
# and get the result y. Then add the rest arrays to y.
y = xs[0] + xs[1]
for x in xs[2:]:
if x.shape == y.shape:
y += x
else:
y = x + y
return utils.force_array(y),
示例3: forward
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import utils [as 别名]
def forward(self, x):
self.retain_outputs((0,))
xp = cuda.get_array_module(*x)
return utils.force_array(xp.sqrt(x[0], dtype=x[0].dtype)),
示例4: __init__
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import utils [as 别名]
def __init__(self, size, comm, decay=0.9, eps=2e-5, dtype=None,
use_gamma=True, use_beta=True,
initial_gamma=None, initial_beta=None,
communication_backend='auto'):
chainer.utils.experimental(
'chainermn.links.MultiNodeBatchNormalization')
super(MultiNodeBatchNormalization, self).__init__()
self._highprec_dtype = chainer.get_dtype(
dtype, map_mixed16=numpy.float32)
self.comm = comm
self.avg_mean = numpy.zeros(size, dtype=self._highprec_dtype)
self.register_persistent('avg_mean')
self.avg_var = numpy.zeros(size, dtype=self._highprec_dtype)
self.register_persistent('avg_var')
self.N = 0
self.register_persistent('N')
self.decay = decay
self.eps = eps
self._communication_backend = \
chainermn_batch_normalization.get_communication_backend(
comm, communication_backend)
with self.init_scope():
if use_gamma:
if initial_gamma is None:
initial_gamma = 1
initial_gamma = initializers._get_initializer(initial_gamma)
initial_gamma.dtype = self._highprec_dtype
self.gamma = variable.Parameter(initial_gamma, size)
if use_beta:
if initial_beta is None:
initial_beta = 0
initial_beta = initializers._get_initializer(initial_beta)
initial_beta.dtype = self._highprec_dtype
self.beta = variable.Parameter(initial_beta, size)
示例5: allgather
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import utils [as 别名]
def allgather(self, x):
chainer.utils.experimental(
'chainermn.communicators.MpiCommunicatorBase.allgather')
msgtype = _MessageType(x)
_check_dtype('allgather', msgtype)
msgtypes = self.mpi_comm.allgather(msgtype)
_check_dtypes_are_same(msgtypes)
# Type check.
for msgtype in msgtypes:
if msgtype.is_tuple:
raise TypeError('allgather cannot handle tuple data')
assert len(msgtype.shapes) == 1
# Collective communication.
xp = chainer.backend.get_array_module(x)
shapes = [msgtype.shapes[0] for msgtype in msgtypes]
sbuf = _memory_utility.array_to_buffer_object(
x, _get_mpi_type(msgtype))
rlens = [chainer.utils.size_of_shape(s) for s in shapes]
rbuf = xp.empty([sum(rlens)], dtype=msgtype.dtype)
if xp is not numpy:
chainer.cuda.Stream.null.synchronize()
self.mpi_comm.Allgatherv(
sbuf,
[_memory_utility.get_device_memory_pointer(rbuf),
(rlens, _cnt_to_dsp(rlens)), _get_mpi_type(msgtype)])
ys = [rbuf[i:i + l].reshape(s)
for i, l, s in zip(_cnt_to_dsp(rlens), rlens, shapes)]
return tuple(ys)
示例6: __init__
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import utils [as 别名]
def __init__(self, comm, peer_rank, peer_tag):
chainer.utils.experimental('chainermn.functions.Send')
self.comm = comm
self.peer_rank = peer_rank
self.peer_tag = peer_tag
示例7: setup_chainerx
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import utils [as 别名]
def setup_chainerx(self, device_name='native:0'):
self._setup(chainer.get_device(device_name))
self.f.forward = mock.MagicMock(side_effect=lambda inputs: (
utils.force_array(inputs[0] * inputs[1]),
utils.force_array(inputs[0] + inputs[1])))
示例8: test_apply_single_return_value_chainerx_gpu
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import utils [as 别名]
def test_apply_single_return_value_chainerx_gpu(self):
self.setup_chainerx('cuda:0')
self.f.forward.side_effect = lambda inputs: (
utils.force_array(inputs[0] * inputs[1]),)
self.check_apply_single_return_value_chainerx()
示例9: check_SPDN_forward
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import utils [as 别名]
def check_SPDN_forward(self, a_data, b_data, atol=1e-4, rtol=1e-5):
sp_a = utils.to_coo(a_data, requires_grad=True)
b = chainer.Variable(b_data)
c = F.sparse_matmul(sp_a, b, transa=self.transa, transb=self.transb)
testing.assert_allclose(self.forward_answer, c.data, atol, rtol)
示例10: check_SPDN_backward
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import utils [as 别名]
def check_SPDN_backward(self, a_data, b_data, c_grad, atol, rtol):
sp_a = utils.to_coo(a_data)
func = F.math.sparse_matmul.CooMatMul(
sp_a.row, sp_a.col, sp_a.shape, sp_a.order,
transa=self.transa, transb=self.transb, transc=False)
def op(a, b):
return func.apply((a, b))[0]
gradient_check.check_backward(
op, (sp_a.data.data, b_data), c_grad, atol=atol, rtol=rtol,
dtype=numpy.float32)
示例11: check_DNSP_forward
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import utils [as 别名]
def check_DNSP_forward(self, a_data, b_data, atol=1e-4, rtol=1e-5):
a = chainer.Variable(a_data)
sp_b = utils.to_coo(b_data, requires_grad=True)
c = F.sparse_matmul(a, sp_b, transa=self.transa, transb=self.transb)
testing.assert_allclose(self.forward_answer, c.data, atol, rtol)
示例12: check_DNSP_backward
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import utils [as 别名]
def check_DNSP_backward(self, a_data, b_data, c_grad, atol, rtol):
sp_b = utils.to_coo(b_data)
func = F.math.sparse_matmul.CooMatMul(
sp_b.row, sp_b.col, sp_b.shape, sp_b.order,
transa=not self.transb, transb=not self.transa, transc=True)
def op(b, a):
return func.apply((b, a))[0]
gradient_check.check_backward(
op, (sp_b.data.data, a_data), c_grad, atol=atol, rtol=rtol,
dtype=numpy.float32)
示例13: check_DNSP_double_backward
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import utils [as 别名]
def check_DNSP_double_backward(
self, a_data, b_data, c_grad, a_grad_grad, b_grad_grad,
atol, rtol):
sp_b = utils.to_coo(b_data)
sp_ggb = utils.to_coo(b_grad_grad)
func = F.math.sparse_matmul.CooMatMul(
sp_b.row, sp_b.col, sp_b.shape, sp_b.order,
transa=not self.transb, transb=not self.transa, transc=True)
def op(b, a):
return func.apply((b, a))[0]
gradient_check.check_double_backward(
op, (sp_b.data.data, a_data),
c_grad, (sp_ggb.data.data, a_grad_grad),
atol=atol, rtol=rtol, dtype=numpy.float32)
示例14: test_invalid_ndim
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import utils [as 别名]
def test_invalid_ndim(self):
a = _setup_tensor(.5, 1, (2, 3, 3), numpy.float32, .75)
b = _setup_tensor(.5, 1, (3, 3), numpy.float32, .75)
sp_a = utils.to_coo(a)
sp_b = utils.to_coo(b)
with self.assertRaises(type_check.InvalidType):
F.sparse_matmul(sp_a, b, self.transa, self.transb)
with self.assertRaises(type_check.InvalidType):
F.sparse_matmul(a, sp_b, self.transa, self.transb)
示例15: test_invalid_nbatch
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import utils [as 别名]
def test_invalid_nbatch(self):
a = _setup_tensor(.5, 1, (2, 3, 3), numpy.float32, .75)
b = _setup_tensor(.5, 1, (3, 3, 3), numpy.float32, .75)
sp_a = utils.to_coo(a)
sp_b = utils.to_coo(b)
with self.assertRaises(type_check.InvalidType):
F.sparse_matmul(sp_a, b, self.transa, self.transb)
with self.assertRaises(type_check.InvalidType):
F.sparse_matmul(a, sp_b, self.transa, self.transb)