本文整理汇总了Python中chainer.as_variable方法的典型用法代码示例。如果您正苦于以下问题:Python chainer.as_variable方法的具体用法?Python chainer.as_variable怎么用?Python chainer.as_variable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类chainer
的用法示例。
在下文中一共展示了chainer.as_variable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_backward_accumulate
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import as_variable [as 别名]
def check_backward_accumulate(self, xp):
inputs = self._get_inputs()
a, b, c = [inputs[i] for i in self.var_mapping]
y = muladd(a, b, c)
y.grad = self.gy
y.backward()
inputs2 = self._get_inputs()
a2, b2, c2 = [inputs2[i] for i in self.var_mapping]
y2 = chainer.as_variable(a2 * b2 + c2)
y2.grad = self.gy
y2.backward()
tol = {'atol': 1e-4, 'rtol': 1e-4}
for x, x2, (isvar, _) in zip(
inputs, inputs2, self.inputs_isvar_hasgrad):
if isvar:
xp.testing.assert_allclose(x.grad, x2.grad, **tol)
示例2: test_to_variable_from_array
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import as_variable [as 别名]
def test_to_variable_from_array(self, backend_config):
x = backend_config.get_array(np.random.randn(1).astype(np.float32))
y = chainer.as_variable(x)
assert isinstance(y, chainer.Variable)
assert y.requires_grad is False
if backend_config.xp is chainerx:
# chainerx
assert y.array.shape == x.shape
assert y.array.device == x.device
assert y.array.strides == x.strides
assert not y.array.is_backprop_required()
chainerx.testing.assert_array_equal(y.array, x)
else:
# non-chainerx
assert y.array is x
示例3: get_model
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import as_variable [as 别名]
def get_model(self):
class Model(chainer.Chain):
def __init__(self):
super().__init__()
with self.init_scope():
self.l = L.Linear(None, 2)
def half(self, xs, value=0.5):
return xs * value
def forward(self, xs):
h = self.l(xs)
h = self.half(h)
return F.sum(chainer.as_variable(h))
return Model()
示例4: forward
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import as_variable [as 别名]
def forward(self, x):
"""Applies the convolution layer.
Args:
x (~chainer.Variable): Input image.
Returns:
~chainer.Variable: Output of the convolution.
"""
x = chainer.as_variable(x)
assert x.layout == self.x_layout
# self.W can be a Variable instead of Parameter: #8462
# TODO(niboshi): Use Parameter.is_initialized.
if self.W.raw_array is None:
_, c, _, _ = memory_layouts.get_semantic_shape(
x, assumed_layout=self.x_layout)
self._initialize_params(c)
return convolution_2d.convolution_2d(
x, self.W, self.b, self.stride, self.pad, dilate=self.dilate,
groups=self.groups, cudnn_fast=self.cudnn_fast)
示例5: fix
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import as_variable [as 别名]
def fix(x):
"""Elementwise fix function.
.. math::
y_i = \\lfix x_i \\rfix
Args:
x (:class:`~chainer.Variable` or :ref:`ndarray`): Input variable.
Returns:
~chainer.Variable: Output variable.
"""
if isinstance(x, chainer.variable.Variable):
x = x.array
xp = backend.get_array_module(x)
return chainer.as_variable(utils.force_array(xp.fix(x), x.dtype))
示例6: ceil
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import as_variable [as 别名]
def ceil(x):
"""Elementwise ceil function.
.. math::
y_i = \\lceil x_i \\rceil
Args:
x (:class:`~chainer.Variable` or :ref:`ndarray`): Input variable.
Returns:
~chainer.Variable: Output variable.
"""
if isinstance(x, chainer.variable.Variable):
x = x.data
xp = backend.get_array_module(x)
return chainer.as_variable(utils.force_array(xp.ceil(x), x.dtype))
示例7: test_hook_for_funcnode
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import as_variable [as 别名]
def test_hook_for_funcnode(self, test_type):
class Model(chainer.Chain):
def forward(self, x):
if test_type in ['variable', 'array']:
x = [chainer.as_variable(x)]
elif test_type == 'dict':
x = list(x.values())
x.append(chainer.Variable(np.array(7, np.float32)))
return F.stack(x)
model = Model()
x = self.get_x(test_type)
with RetainInputHook() as h:
model(x)
expected_count = 1
if test_type == 'array':
# input is ndarray and not checked in forward_preprocess
expected_count += 1
assert len(h.retain_inputs) == expected_count
示例8: forward
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import as_variable [as 别名]
def forward(self, input, target, mask):
input = chainer.as_variable(input)
target = chainer.as_variable(target)
mask = chainer.as_variable(mask)
output = self.predictor(input)
output = output * mask
target = target * mask
d_fake = self.discriminator(input, output)
d_real = self.discriminator(input, target)
loss = {
'predictor': self._loss_predictor(self.predictor, output, target, d_fake),
'discriminator': self._loss_discriminator(self.discriminator, d_real, d_fake),
}
return loss
示例9: shifted_softplus
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import as_variable [as 别名]
def shifted_softplus(x, beta=1, shift=0.5, threshold=20):
"""shifted softplus function, which holds f(0)=0.
Args:
x (Variable): Input variable
beta (float): Parameter :math:`\\beta`.
shift (float): Shift Parameter
threshold (float): threshold to avoid overflow
Returns:
output (Variable): Output variable whose shape is same with `x`
"""
xp = chainer.cuda.get_array_module(x)
cond = chainer.as_variable(x).array > threshold
x = functions.where(cond, x,
functions.softplus(x, beta=beta))
x += xp.log(shift)
return x
示例10: forward
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import as_variable [as 别名]
def forward(self, input, target, mask):
input = chainer.as_variable(input)
target = chainer.as_variable(target)
mask = chainer.as_variable(mask)
output = self.predictor(input)
output = output * mask
target = target * mask
d_fake = self.discriminator(input, output)
d_real = self.discriminator(input, target)
loss = {
'predictor': _loss_predictor(self.predictor, output, target, d_fake, self.loss_config),
'discriminator': _loss_discriminator(self.discriminator, d_real, d_fake),
}
return loss
示例11: __init__
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import as_variable [as 别名]
def __init__(self):
self.link_inputs = set()
self.retain_inputs = []
self.replaced_inputs = []
self.org_apply = chainer.function_node.FunctionNode.apply
def hooked_apply(_self, inputs):
ret = self.org_apply(_self, inputs)
func_inodes = list(_self.inputs)
for i, inode in enumerate(func_inodes):
referenced_var = inode.get_variable_or_none()
if referenced_var is None:
# This variable is created within function node and weakref
# is lost. Make temporary variable and retain it.
temp_var = chainer.as_variable(inputs[i])
func_inodes[i] = temp_var.node
self.retain_inputs.append(temp_var)
else:
if id(referenced_var) not in self.link_inputs:
# This variable is created within link forward, outside
# of function node. To avoid to lose reference out
# of the forward, retain the variable.
self.retain_inputs.append(referenced_var)
self.replaced_inputs.append((_self, _self.inputs))
_self.inputs = tuple(func_inodes)
return ret
self.hooked_apply = hooked_apply
示例12: check_to_variable_from_variable
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import as_variable [as 别名]
def check_to_variable_from_variable(self, backend_config, requires_grad):
x_arr = backend_config.get_array(np.random.randn(1).astype(np.float32))
x = chainer.Variable(x_arr, requires_grad=requires_grad)
y = chainer.as_variable(x)
assert y is x
assert y.requires_grad is requires_grad
示例13: test_hook_for_childlink
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import as_variable [as 别名]
def test_hook_for_childlink(self, test_type):
# TODO(disktnk): test_type='variable' is failed
class ChildModel(chainer.Chain):
def forward(self, x, h):
if test_type in ['variable', 'array']:
h = [chainer.as_variable(h)]
elif test_type == 'dict':
h = list(h.values())
h.append(x)
return F.stack(h)
class ParentModel(chainer.Chain):
def __init__(self, get_x):
super().__init__()
self.get_x = get_x
with self.init_scope():
self.m = ChildModel()
def forward(self, x):
h = self.get_x(test_type)
return self.m(x, h)
model = ParentModel(self.get_x)
x = self.get_x('variable')
with RetainInputHook() as h:
model(x)
assert len(h.retain_inputs) == 1
示例14: k
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import as_variable [as 别名]
def k(self):
return chainer.as_variable(self.__k)
示例15: theta
# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import as_variable [as 别名]
def theta(self):
return chainer.as_variable(self.__theta)