当前位置: 首页>>代码示例>>Python>>正文


Python chainer.Chain方法代码示例

本文整理汇总了Python中chainer.Chain方法的典型用法代码示例。如果您正苦于以下问题:Python chainer.Chain方法的具体用法?Python chainer.Chain怎么用?Python chainer.Chain使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在chainer的用法示例。


在下文中一共展示了chainer.Chain方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: setUp

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Chain [as 别名]
def setUp(self):

        class Model(chainer.Chain):

            def __init__(self, ops, args, input_argname):
                super(Model, self).__init__()
                self.ops = getattr(F, ops)
                self.args = args
                self.input_argname = input_argname

            def __call__(self, x):
                self.args[self.input_argname] = x
                return self.ops(**self.args)

        self.model = Model(self.ops, self.args, self.input_argname)
        self.x = input_generator.increasing(*self.input_shape) 
开发者ID:chainer,项目名称:chainer,代码行数:18,代码来源:test_arrays.py

示例2: make_model

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Chain [as 别名]
def make_model(self, env):
        n_dim_obs = env.observation_space.low.size
        n_dim_action = env.action_space.low.size
        n_hidden_channels = 50
        policy = Sequence(
            L.Linear(n_dim_obs, n_hidden_channels),
            F.relu,
            L.Linear(n_hidden_channels, n_hidden_channels),
            F.relu,
            L.LSTM(n_hidden_channels, n_hidden_channels),
            policies.FCGaussianPolicy(
                n_input_channels=n_hidden_channels,
                action_size=n_dim_action,
                min_action=env.action_space.low,
                max_action=env.action_space.high)
        )

        q_func = q_function.FCLSTMSAQFunction(
            n_dim_obs=n_dim_obs,
            n_dim_action=n_dim_action,
            n_hidden_layers=2,
            n_hidden_channels=n_hidden_channels)

        return chainer.Chain(policy=policy, q_function=q_func) 
开发者ID:chainer,项目名称:chainerrl,代码行数:26,代码来源:basetest_pgt.py

示例3: test_chain

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Chain [as 别名]
def test_chain(self):
        ch = chainer.Chain()
        with ch.init_scope():
            ch.l1 = chainer.links.Linear(3, 4)
            ch.l2 = chainer.links.Linear(5)
            ch.l3 = chainer.links.PReLU()
        self.assertEqual(
            names_of_links(ch),
            {'/l1', '/l2', '/l3'})

        to_factorized_noisy(ch)
        self.assertEqual(
            names_of_links(ch),
            {
                '/l1', '/l1/mu', '/l1/sigma',
                '/l2', '/l2/mu', '/l2/sigma', '/l3'}) 
开发者ID:chainer,项目名称:chainerrl,代码行数:18,代码来源:test_noisy_chain.py

示例4: _test

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Chain [as 别名]
def _test(self, gpu):

        model = chainer.Chain(
            a=L.Linear(1, 2, initialW=3, initial_bias=3),
            b=chainer.Chain(c=L.Linear(2, 3, initialW=4, initial_bias=4)),
        )
        if gpu >= 0:
            model.to_gpu(gpu)
            xp = model.xp
        else:
            xp = np
        optimizer = chainer.optimizers.SGD(self.lr)
        optimizer.setup(model)
        optimizer.add_hook(
            chainerrl.optimizers.NonbiasWeightDecay(
                rate=self.weight_decay_rate))
        optimizer.update(lambda: chainer.Variable(xp.asarray(0.0)))
        decay_factor = 1 - self.lr * self.weight_decay_rate
        xp.testing.assert_allclose(model.a.W.array, 3 * decay_factor)
        xp.testing.assert_allclose(model.a.b.array, 3)
        xp.testing.assert_allclose(model.b.c.W.array, 4 * decay_factor)
        xp.testing.assert_allclose(model.b.c.b.array, 4) 
开发者ID:chainer,项目名称:chainerrl,代码行数:24,代码来源:test_nonbias_weight_decay.py

示例5: test_soft_copy_param_scalar

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Chain [as 别名]
def test_soft_copy_param_scalar(self):
        a = chainer.Chain()
        with a.init_scope():
            a.p = chainer.Parameter(np.array(0.5))
        b = chainer.Chain()
        with b.init_scope():
            b.p = chainer.Parameter(np.array(1))

        # a = (1 - tau) * a + tau * b
        copy_param.soft_copy_param(target_link=a, source_link=b, tau=0.1)

        np.testing.assert_almost_equal(a.p.array, 0.55)
        np.testing.assert_almost_equal(b.p.array, 1.0)

        copy_param.soft_copy_param(target_link=a, source_link=b, tau=0.1)

        np.testing.assert_almost_equal(a.p.array, 0.595)
        np.testing.assert_almost_equal(b.p.array, 1.0) 
开发者ID:chainer,项目名称:chainerrl,代码行数:20,代码来源:test_copy_param.py

示例6: apply_to_seq

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Chain [as 别名]
def apply_to_seq(self, seq_list):
        mb_size = len(seq_list)
        mb_initial_cell, mb_initial_state = self.get_initial_states(mb_size)
        return self.nstep_lstm(mb_initial_cell, mb_initial_state, seq_list)


# class DoubleGRU(Chain):
#     def __init__(self, H, I):
#         log.info("using double GRU")
#         self.H1 = H/2
#         self.H2 = H - self.H1
#         super(DoubleGRU, self).__init__(
#             gru1 = faster_gru.GRU(self.H1, I),
#             gru2 = faster_gru.GRU(self.H2, self.H1)
#         )
#
#     def __call__(self, prev_state, inpt):
#         prev_state1, prev_state2 = F.split_axis(prev_state, (self.H1,), axis = 1)
#
#         prev_state1 = self.gru1(prev_state1, inpt)
#         prev_state2 = self.gru2(prev_state2, prev_state1)
#
#         return F.concat((prev_state1, prev_state2), axis = 1) 
开发者ID:fabiencro,项目名称:knmt,代码行数:25,代码来源:rnn_cells.py

示例7: test_lazy_init

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Chain [as 别名]
def test_lazy_init(self):
        class Test(chainer.Chain):
            def forward(self, x):
                if x is None:
                    x = 42
                return x

        id2type = generate_id2type_from_forward(Test(), (None,))

        self.assertEqual(str(id2type[1]), "class Test -> NoneType -> int")	# FunctionDef forward (line 1)
        self.assertEqual(str(id2type[7]), "NoneType")	# If
        self.assertEqual(str(id2type[8]), "bool")	# Compare  (line 2)
        self.assertEqual(str(id2type[9]), "NoneType")	# Name x (line 2)
        self.assertEqual(str(id2type[13]), "NoneType")	# Assign
        self.assertEqual(str(id2type[14]), "int")	# Name x (line 3)
        self.assertEqual(str(id2type[16]), "int")	# Num 42 (line 3)
        self.assertEqual(str(id2type[17]), "int")	# Return
        self.assertEqual(str(id2type[18]), "int")	# Name x (line 4) 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:20,代码来源:Primitive_test.py

示例8: test_lazy_init_branch_if

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Chain [as 别名]
def test_lazy_init_branch_if(self):
        type_inference_tools.reset_state()

        class Test(chainer.Chain):
            def forward(self, x):
                if x is None:
                    x = 42
                else:
                    x += 1
                return x

        id2type = generate_id2type_from_forward(Test(), (None,))

        self.assertEqual(str(id2type[1]), "class Test -> NoneType -> int")	# FunctionDef forward (line 1)
        self.assertEqual(str(id2type[7]), "NoneType")	# If
        self.assertEqual(str(id2type[8]), "bool")	# Compare  (line 2)
        self.assertEqual(str(id2type[9]), "NoneType")	# Name x (line 2)
        self.assertEqual(str(id2type[13]), "NoneType")	# Assign
        self.assertEqual(str(id2type[14]), "int")	# Name x (line 3)
        self.assertEqual(str(id2type[16]), "int")	# Num 42 (line 3)
        self.assertEqual(str(id2type[17]), "NoneType")	# AugAssign
        self.assertEqual(str(id2type[18]), "a1 (from line 5)")	# Name x (line 5)
        self.assertEqual(str(id2type[21]), "int")	# Num 1 (line 5)
        self.assertEqual(str(id2type[22]), "int")	# Return
        self.assertEqual(str(id2type[23]), "int")	# Name x (line 6) 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:27,代码来源:Primitive_test.py

示例9: test_lazy_init_branch_else

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Chain [as 别名]
def test_lazy_init_branch_else(self):
        class Test(chainer.Chain):
            def forward(self, x):
                if x is None:
                    x = 42
                else:
                    x += 1
                return x

        # XXX: the input is different from the previous one
        id2type = generate_id2type_from_forward(Test(), (2,))

        self.assertEqual(str(id2type[1]), "class Test -> int -> int")	# FunctionDef forward (line 1)
        self.assertEqual(str(id2type[7]), "NoneType")	# If
        self.assertEqual(str(id2type[8]), "bool")	# Compare  (line 2)
        self.assertEqual(str(id2type[9]), "int")	# Name x (line 2)
        self.assertEqual(str(id2type[13]), "NoneType")	# Assign
        self.assertEqual(str(id2type[14]), "int")	# Name x (line 3)
        self.assertEqual(str(id2type[16]), "int")	# Num 42 (line 3)
        self.assertEqual(str(id2type[17]), "NoneType")	# AugAssign
        self.assertEqual(str(id2type[18]), "int")	# Name x (line 5)
        self.assertEqual(str(id2type[21]), "int")	# Num 1 (line 5)
        self.assertEqual(str(id2type[22]), "int")	# Return
        self.assertEqual(str(id2type[23]), "int")	# Name x (line 6) 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:26,代码来源:Primitive_test.py

示例10: nasnet_dual_path_scheme_ordinal

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Chain [as 别名]
def nasnet_dual_path_scheme_ordinal(block,
                                    x,
                                    _):
    """
    NASNet specific scheme of dual path response for an ordinal block with dual inputs/outputs in a DualPathSequential
    block.

    Parameters:
    ----------
    block : Chain
        A block.
    x : Tensor
        Current processed tensor.

    Returns
    -------
    x_next : Tensor
        Next processed tensor.
    x : Tensor
        Current processed tensor.
    """
    return block(x), x 
开发者ID:osmr,项目名称:imgclsmob,代码行数:24,代码来源:nasnet.py

示例11: test_fake_as_funcnode_without_replace

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Chain [as 别名]
def test_fake_as_funcnode_without_replace():

    class Model(chainer.Chain):
        def _init__(self):
            super().__init__()

        def add(self, xs, value=0.01):
            return xs.array + value

        def __call__(self, xs):
            return F.sigmoid(self.add(xs))

    model = Model()
    x = input_generator.increasing(3, 4)

    onnx_model = export(model, x)
    sigmoid_nodes = [
        node for node in onnx_model.graph.node if node.op_type == 'Sigmoid']
    assert len(sigmoid_nodes) == 1
    # sigmoid node should be expected to connect with input
    # but the connection is cut because `add` method takes array.
    assert not sigmoid_nodes[0].input[0] == 'Input_0' 
开发者ID:chainer,项目名称:chainer,代码行数:24,代码来源:test_replace_func.py

示例12: get_model

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Chain [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() 
开发者ID:chainer,项目名称:chainer,代码行数:19,代码来源:test_replace_func.py

示例13: setUp

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Chain [as 别名]
def setUp(self):
        class Model(chainer.Chain):

            def __init__(self, ops):
                super(Model, self).__init__()
                self.ops = ops

            def __call__(self, a, b, c):
                if not isinstance(a, chainer.Variable):
                    a = chainer.Variable(a)
                if not isinstance(b, chainer.Variable):
                    b = chainer.Variable(b)
                if not isinstance(c, chainer.Variable):
                    c = chainer.Variable(c)
                return eval(self.ops)

        self.model = Model(self.ops)
        a = chainer.Variable(input_generator.increasing(2, 3))
        b = chainer.Variable(input_generator.increasing(2, 3) * 0.3)
        c = chainer.Variable(input_generator.increasing(2, 3) * 0.7)
        self.x = (a, b, c) 
开发者ID:chainer,项目名称:chainer,代码行数:23,代码来源:test_maths.py

示例14: test_output

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Chain [as 别名]
def test_output(self):
        class Model(chainer.Chain):

            def __init__(self, op, axis):
                self.axis = axis
                self.op = getattr(F, op)
                super(Model, self).__init__()

            def forward(self, x):
                return self.op(x, axis=self.axis)

        x = np.random.rand(2, 3).astype(np.float32)
        axis = getattr(self, 'axis', None)
        model = Model(self.op, axis)
        name = self.op
        if axis is not None:
            name += '_axis_{}'.format(self.axis)

        self.expect(model, x, name=name, expected_num_initializers=0) 
开发者ID:chainer,项目名称:chainer,代码行数:21,代码来源:test_maths.py

示例15: setUp

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Chain [as 别名]
def setUp(self):

        class Model(chainer.Chain):

            def __init__(self, link, args, kwargs):
                super(Model, self).__init__()
                with self.init_scope():
                    self.l1 = link(*args, **kwargs)

            def __call__(self, x):
                return self.l1(x)

        self.model = Model(self.link, self.args, self.kwargs)
        if self.link is L.EmbedID:
            self.x = np.random.randint(0, self.args[0], size=self.in_shape)
            self.x = self.x.astype(self.in_type)
        else:
            self.x = input_generator.increasing(
                *self.in_shape, dtype=self.in_type) 
开发者ID:chainer,项目名称:chainer,代码行数:21,代码来源:test_connections.py


注:本文中的chainer.Chain方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。