Python chainer.Chain() Examples

The following are 30 code examples of chainer.Chain(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module chainer , or try the search function .
Example #1
Source File: test_replace_func.py    From chainer with MIT License 6 votes vote down vote up
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' 
Example #2
Source File: test_inout.py    From chainer with MIT License 6 votes vote down vote up
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 
Example #3
Source File: test_nonbias_weight_decay.py    From chainerrl with MIT License 6 votes vote down vote up
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) 
Example #4
Source File: basetest_pgt.py    From chainerrl with MIT License 6 votes vote down vote up
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) 
Example #5
Source File: test_copy_param.py    From chainerrl with MIT License 6 votes vote down vote up
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) 
Example #6
Source File: test_noisy_chain.py    From chainerrl with MIT License 6 votes vote down vote up
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'}) 
Example #7
Source File: rnn_cells.py    From knmt with GNU General Public License v3.0 6 votes vote down vote up
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) 
Example #8
Source File: basetest_pgt.py    From chainerrl with MIT License 6 votes vote down vote up
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 = policies.FCGaussianPolicy(
            n_input_channels=n_dim_obs,
            n_hidden_layers=2,
            n_hidden_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.FCSAQFunction(
            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) 
Example #9
Source File: test_arrays.py    From chainer with MIT License 6 votes vote down vote up
def test_output(self):

        class Model(chainer.Chain):
            def __init__(self, kwargs):
                super(Model, self).__init__()
                self.kwargs = kwargs

            def forward(self, x, indices):
                return F.permutate(x, indices, **self.kwargs)

        model = Model(kwargs=self.kwargs)

        x = np.arange(6).reshape((3, 2)).astype(np.float32)
        if self.kwargs.get('axis') == 1:
            indices = np.array([1, 0], np.int32)
        else:
            indices = np.array([2, 0, 1], np.int32)
        self.expect(model, (x, indices), name=self.name,
                    skip_opset_version=[7, 8]) 
Example #10
Source File: Primitive_test.py    From chainer-compiler with MIT License 6 votes vote down vote up
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) 
Example #11
Source File: Primitive_test.py    From chainer-compiler with MIT License 6 votes vote down vote up
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) 
Example #12
Source File: Primitive_test.py    From chainer-compiler with MIT License 6 votes vote down vote up
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) 
Example #13
Source File: test_arrays.py    From chainer with MIT License 6 votes vote down vote up
def test_output(self):
        from onnx_chainer.replace_func import as_funcnode

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

            @as_funcnode('Shape')
            def shape(self, x):
                # ONNX Shape operator constrains to return int64 type
                return np.array(x.shape, dtype=np.int64)

            def forward(self, x):
                # use shape method instead of x.shape to connect graph.
                return self.shape(x)

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

        self.expect(model, (x,)) 
Example #14
Source File: test_arrays.py    From chainer with MIT License 6 votes vote down vote up
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) 
Example #15
Source File: nasnet.py    From imgclsmob with MIT License 6 votes vote down vote up
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 
Example #16
Source File: test_arrays.py    From chainer with MIT License 6 votes vote down vote up
def test_output(self):
        from onnx_chainer.replace_func import as_funcnode

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

            @as_funcnode('Reshape')
            def dynamic_reshape(self, x, shape):
                # shape is expected as variable type
                return F.reshape(x, tuple(shape.array))

            def forward(self, x, shape):
                return self.dynamic_reshape(x, shape)

        model = Model()
        x = input_generator.increasing(3, 4, 5)
        shape = np.array([12, 5], dtype=np.int64)

        def check_no_param(onnx_model, path):
            assert not any(['param' in v.name for v in onnx_model.graph.input])

        self.expect(model, (x, shape), custom_model_test_func=check_no_param) 
Example #17
Source File: test_normalizations.py    From chainer with MIT License 6 votes vote down vote up
def setUp(self):

        class Model(chainer.Chain):

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

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

        ops = getattr(F, self.name)
        self.model = Model(ops, self.args, self.input_argname)
        self.x = input_generator.increasing(2, 5, 3, 3) 
Example #18
Source File: test_activations.py    From chainer with MIT License 6 votes vote down vote up
def setUp(self):

        class Model(chainer.Chain):

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

            def __call__(self, x):
                return self.ops(x, **self.args)

        ops = getattr(F, self.name)
        args = {}
        if hasattr(self, 'args'):
            args = self.args
        self.model = Model(ops, args)
        self.x = input_generator.increasing(2, 5, 3) 
Example #19
Source File: test_connections.py    From chainer with MIT License 6 votes vote down vote up
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) 
Example #20
Source File: test_arrays.py    From chainer with MIT License 6 votes vote down vote up
def setUp(self):

        class Model(chainer.Chain):

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

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

        # (batch, channel, height, width) = (1, 1, 2, 2)
        self.x = np.array([[[[64, 32], [64, 32]]]], np.float32)

        # 2x upsampling
        args = {'output_shape': (4, 4)}
        self.model = Model(F.resize_images, args, 'x') 
Example #21
Source File: test_maths.py    From chainer with MIT License 6 votes vote down vote up
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) 
Example #22
Source File: test_maths.py    From chainer with MIT License 6 votes vote down vote up
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) 
Example #23
Source File: test_replace_func.py    From chainer with MIT License 6 votes vote down vote up
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() 
Example #24
Source File: test_replace_func.py    From chainer with MIT License 5 votes vote down vote up
def test_fake_as_funcnode_keep_structure(tmpdir):
    path = str(tmpdir)

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

        def f(self, x):
            return {'a': (x, x+1), 'b': [x+2, x+3, x+4]}

        def __call__(self, x):
            ret = self.f(x)
            return ret['a'][0] + ret['b'][1]

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

    with warnings.catch_warnings(record=True):
        model.f = fake_as_funcnode(model.f, 'xF')

    def f_converter(params):
        return onnx_helper.make_node(
            'xF', params.input_names, params.output_names),

    addon_converters = {'xF': f_converter}

    with testing.assert_warns(UserWarning):
        export_testcase(model, x, path, external_converters=addon_converters)

    model_filepath = os.path.join(path, 'model.onnx')
    assert os.path.isfile(model_filepath)

    onnx_model = onnx.load(model_filepath)
    node_names = [n.name for n in onnx_model.graph.node]
    assert len(node_names) == 2
    assert node_names[0] == 'xF_0'
    assert len(onnx_model.graph.node[0].output) == 5
    assert len(onnx_model.graph.output) == 1 
Example #25
Source File: test_replace_func.py    From chainer with MIT License 5 votes vote down vote up
def test_output(self):
        class Model(chainer.Chain):
            def __init__(self, value):
                super().__init__()
                self.value = value

            @as_funcnode('NumpyFull')
            def full(self, xs, value=0):
                # not support `def full(self, xs_shape, value=0)`
                # wrapped function node cannot handle shape directly yet.
                return np.full(xs.array.shape, value, dtype=np.float32)

            def __call__(self, xs):
                return F.sigmoid(self.full(xs, value=self.value))

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

        def numpy_full_converter(params):
            gb = onnx_helper.GraphBuilder()
            output = gb.op('Shape', params.input_names)
            value = onnx.helper.make_tensor(
                'value', onnx.TensorProto.FLOAT, [1], [params.func.value])
            gb.op_output_named(
                'ConstantOfShape', [output], params.output_names, value=value)
            return gb.nodes()

        addon_converters = {'NumpyFull': numpy_full_converter}

        self.expect(
            model, x, skip_opset_version=[7, 8],
            external_converters=addon_converters) 
Example #26
Source File: test_loss.py    From chainer with MIT License 5 votes vote down vote up
def setUp(self):

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

            def __call__(self, x, t):
                return chainer.functions.softmax_cross_entropy(x, t)

        self.model = Model()
        self.x = np.random.uniform(size=self.in_shape).astype('f')
        self.t = np.random.randint(size=self.in_shape[0], low=0,
                                   high=self.in_shape[1]).astype(np.int32) 
Example #27
Source File: test_inout.py    From chainer with MIT License 5 votes vote down vote up
def get_model(self):
        class Model(chainer.Chain):

            def __init__(self):
                super().__init__()
                with self.init_scope():
                    self.l1 = L.Linear(4)
                    self.l2 = L.Linear(5, initial_bias=0.1)

            def __call__(self, x):
                y = self.l1(x)
                z = self.l2(y)
                return y, z
        return Model() 
Example #28
Source File: test_activations.py    From chainer with MIT License 5 votes vote down vote up
def setUp(self):

        class Model(chainer.Chain):

            def __init__(self):
                super(Model, self).__init__()
                with self.init_scope():
                    self.prelu = L.PReLU()

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

        self.model = Model()
        self.x = input_generator.increasing(2, 5) 
Example #29
Source File: test_inout.py    From chainer with MIT License 5 votes vote down vote up
def get_model(self, use_bn=False, out_type=None):
        class Model(chainer.Chain):

            def __init__(self, use_bn=False, out_type=None):
                super(Model, self).__init__()

                self._use_bn = use_bn
                self._out_type = out_type
                with self.init_scope():
                    self.conv = L.Convolution2D(None, 32, ksize=3, stride=1)
                    if self._use_bn:
                        self.bn = L.BatchNormalization(32)

            def __call__(self, x):
                h = self.conv(x)
                if self._use_bn:
                    h = self.bn(h)
                o1 = F.tanh(h)
                o2 = F.sigmoid(h)
                if self._out_type == 'dict':
                    return {
                        'tanh': o1,
                        'sigmoid': o2
                    }
                elif self._out_type == 'tuple':
                    return o1, o2
                elif self._out_type == 'list':
                    return [o1, o2]

        return Model(use_bn=use_bn, out_type=out_type) 
Example #30
Source File: test_rnn.py    From chainer with MIT License 5 votes vote down vote up
def test_output(self):
        n_layers = self.n_layers
        dropout_ratio = 0.0
        batch_size = 3
        input_size = 4
        hidden_size = 5
        seq_length = 6

        class Model(chainer.Chain):
            def __init__(self):
                super().__init__()
                with self.init_scope():
                    self.gru = L.NStepGRU(
                        n_layers, input_size, hidden_size, dropout_ratio)

            def __call__(self, *xs):
                hy, ys = self.gru(None, xs)
                return [hy] + ys

        model = Model()
        xs = [input_generator.increasing(seq_length, input_size)
              for i in range(batch_size)]

        # NOTE(msakai): Replace Permutate converter for avoiding error like:
        # ValidationError: Nodes in a graph must be topologically sorted, \
        # however input 'v330' of node:
        # input: "Permutate_0_const_empty" input: "v330" \
        # input: "Permutate_0_const_range" output: "Permutate_0_tmp_0" \
        # name: "Permutate_0_tmp_0" op_type: "Scatter"
        # is not output of any previous nodes.
        addon_converters = {
            'Permutate': convert_Permutate,
        }
        self.expect(model, xs, skip_opset_version=[7, 8],
                    external_converters=addon_converters)