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


Python chainer.Sequential方法代码示例

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


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

示例1: test_manual

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Sequential [as 别名]
def test_manual(self):
        link1 = L.Linear(2, 3)
        link2 = L.Linear(2, 5)
        link3 = chainer.Sequential(
            L.Linear(2, 7),
            F.tanh,
        )
        plink = Branched(link1, link2, link3)
        x = np.zeros((self.batch_size, 2), dtype=np.float32)
        pout = plink(x)
        self.assertIsInstance(pout, tuple)
        self.assertEqual(len(pout), 3)
        out1 = link1(x)
        out2 = link2(x)
        out3 = link3(x)
        np.testing.assert_allclose(pout[0].array, out1.array)
        np.testing.assert_allclose(pout[1].array, out2.array)
        np.testing.assert_allclose(pout[2].array, out3.array) 
开发者ID:chainer,项目名称:chainerrl,代码行数:20,代码来源:test_branched.py

示例2: n_step_forward

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Sequential [as 别名]
def n_step_forward(self, x, recurrent_state):
        """Multi-step batch forward computation.

        This method sequentially applies layers as chainer.Sequential does.

        Args:
            x (list): Input sequences. Each sequence should be a variable whose
                first axis corresponds to time or a tuple of such variables.
            recurrent_state (object): Batched recurrent state. If set to None,
                it is initialized.
            output_mode (str): If set to 'concat', the output value is
                concatenated into a single large batch, which can be suitable
                for loss computation. If set to 'split', the output value is
                a list of output sequences.

        Returns:
            object: Output sequences. See the description of the `output_mode`
                argument.
            object: New batched recurrent state.
        """
        raise NotImplementedError 
开发者ID:chainer,项目名称:chainerrl,代码行数:23,代码来源:stateless_recurrent.py

示例3: test_copyparams

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Sequential [as 别名]
def test_copyparams(self):
        l1 = links.Linear(None, 3)
        l2 = links.Linear(3, 2)
        l3 = links.Linear(2, 3)
        s1 = chainer.Sequential(l1, l2)
        s2 = chainer.Sequential(s1, l3)
        l1.b.data.fill(0)
        l2.W.data.fill(1)
        l2.b.data.fill(2)
        l3.W.data.fill(3)
        l3.b.data.fill(4)

        self.s2.copyparams(s2)

        numpy.testing.assert_array_equal(self.l1.b.data, l1.b.data)
        numpy.testing.assert_array_equal(self.l2.W.data, l2.W.data)
        numpy.testing.assert_array_equal(self.l2.b.data, l2.b.data)
        numpy.testing.assert_array_equal(self.l3.W.data, l3.W.data)
        numpy.testing.assert_array_equal(self.l3.b.data, l3.b.data) 
开发者ID:chainer,项目名称:chainer,代码行数:21,代码来源:test_sequential.py

示例4: test_serialize

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Sequential [as 别名]
def test_serialize(self):
        l1 = links.Linear(None, 1)
        l2 = links.Linear(None, 3)
        with l2.init_scope():
            l2.x = variable.Parameter(0, 2)
        s1 = chainer.Sequential(l1, l2)
        mocks = {'0': mock.MagicMock(), '1': mock.MagicMock()}
        serializer = mock.MagicMock()
        serializer.__getitem__.side_effect = lambda k: mocks[k]
        serializer.return_value = None
        mocks['0'].return_value = None
        mocks['1'].return_value = None
        s1.serialize(serializer)

        self.assertEqual(serializer.call_count, 0)
        self.assertEqual(serializer.__getitem__.call_count, 2)
        serializer.__getitem__.assert_any_call('0')
        serializer.__getitem__.assert_any_call('1')

        mocks['0'].assert_any_call('W', None)
        mocks['0'].assert_any_call('b', l1.b.data)
        mocks['1'].assert_any_call('W', None)
        mocks['1'].assert_any_call('b', l2.b.data)
        mocks['1'].assert_any_call('x', l2.x.data) 
开发者ID:chainer,项目名称:chainer,代码行数:26,代码来源:test_sequential.py

示例5: test_str

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Sequential [as 别名]
def test_str(self):
        self.assertEqual(str(chainer.Sequential()), 'Sequential()')

        expected = '''\
  (0): Sequential(
    (0): Linear(in_size=None, out_size=3, nobias=False),
    (1): Linear(in_size=3, out_size=2, nobias=False),
  ),
  (1): Linear(in_size=2, out_size=3, nobias=False),
  (2): lambda x: functions.leaky_relu(x, slope=0.2),
'''
        layers = [
            self.s1,
            self.l3,
            lambda x: functions.leaky_relu(x, slope=0.2),
        ]
        if six.PY3:
            # In Python2, it fails because of different id of the function.
            layer = functools.partial(functions.leaky_relu, slope=0.2)
            layers.append(layer)
            expected += '  (3): %s,\n' % layer
        expected = 'Sequential(\n%s)' % expected
        s = chainer.Sequential(*layers)
        self.assertEqual(str(s), expected) 
开发者ID:chainer,项目名称:chainer,代码行数:26,代码来源:test_sequential.py

示例6: test_export_external_converters_overwrite

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Sequential [as 别名]
def test_export_external_converters_overwrite(tmpdir, check_model_expect):
    path = str(tmpdir)

    model = chainer.Sequential(chainer.functions.sigmoid)
    x = input_generator.positive_increasing(2, 5)

    def custom_converter(params):
        return onnx_helper.make_node(
            'Tanh', params.input_names, params.output_names),

    addon_converters = {'Sigmoid': custom_converter}
    export_testcase(model, x, path, external_converters=addon_converters)

    tanh_outputs = chainer.functions.tanh(x).array
    output_path = os.path.join(path, 'test_data_set_0', 'output_0.pb')
    onnx_helper.write_tensor_pb(output_path, '', tanh_outputs)  # overwrite

    check_model_expect(path) 
开发者ID:chainer,项目名称:chainer,代码行数:20,代码来源:test_external_converter.py

示例7: check_create_mnbn_model_sequential

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Sequential [as 别名]
def check_create_mnbn_model_sequential(self, use_gpu, use_chx):
        size = 3
        model = chainer.Sequential(
            chainer.links.Convolution2D(
                None, size, 1, 1, 1, nobias=True),
            chainer.links.BatchNormalization(size),
            chainer.functions.relu
        )
        mnbn_model = chainermn.links.create_mnbn_model(model,
                                                       self.communicator)

        device = get_device(self.communicator.intra_rank if use_gpu else None,
                            use_chx)
        mnbn_model.to_device(device)

        with chainer.using_device(mnbn_model.device):
            x = mnbn_model.xp.zeros((1, 1, 1, 1))
            mnbn_model(x) 
开发者ID:chainer,项目名称:chainer,代码行数:20,代码来源:test_create_mnbn_model.py

示例8: __reduce__

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Sequential [as 别名]
def __reduce__(self):
        n_lambda = 0
        for layer in self._layers:
            if callable(layer) and hasattr(layer, '__name__') \
                    and layer.__name__ == '<lambda>':
                n_lambda += 1

        if n_lambda > 0:
            raise ValueError(
                'This Sequential object has at least one lambda function as '
                'its component. Lambda function can not be pickled, so please '
                'consider to use functools.partial instead of the lambda '
                'function or use "dill", which is an external package that '
                'enables pickling an object including lambda functions intead '
                'of built-in pickle.')
        return super(Sequential, self).__reduce__() 
开发者ID:chainer,项目名称:chainer,代码行数:18,代码来源:sequential.py

示例9: build_decoder

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Sequential [as 别名]
def build_decoder(vocab_size, N=6, model_size=512, ff_size=2048, num_heads=8, dropout_ratio=0.1):
    attention = MultiHeadedAttention(num_heads, model_size, dropout_ratio=dropout_ratio)
    feed_forward = PositionwiseFeedForward(model_size, ff_size, dropout_ratio=dropout_ratio)
    positional_encoding = PositionalEncoding(model_size, dropout_ratio=dropout_ratio)

    decoder_layer = DecoderLayer(
        model_size,
        copy.deepcopy(attention),
        copy.deepcopy(attention),
        feed_forward,
        dropout_ratio=dropout_ratio
    )

    decoder = Decoder(decoder_layer, N)

    embeddings = Embedding(model_size, vocab_size)

    return chainer.Sequential(embeddings, positional_encoding), decoder 
开发者ID:Bartzi,项目名称:kiss,代码行数:20,代码来源:__init__.py

示例10: create_model

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Sequential [as 别名]
def create_model(trial):
    # We optimize the numbers of layers and their units.
    n_layers = trial.suggest_int("n_layers", 1, 3)

    layers = []
    for i in range(n_layers):
        n_units = int(trial.suggest_loguniform("n_units_l{}".format(i), 32, 256))
        layers.append(L.Linear(None, n_units))
        layers.append(F.relu)
    layers.append(L.Linear(None, 10))

    return chainer.Sequential(*layers)


# FYI: Objective functions can take additional arguments
# (https://optuna.readthedocs.io/en/stable/faq.html#objective-func-additional-args). 
开发者ID:optuna,项目名称:optuna,代码行数:18,代码来源:chainer_integration.py

示例11: create_model

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Sequential [as 别名]
def create_model(trial):
    # We optimize the numbers of layers and their units.
    n_layers = trial.suggest_int("n_layers", 1, 3)

    layers = []
    for i in range(n_layers):
        n_units = trial.suggest_int("n_units_l{}".format(i), 4, 128, log=True)
        layers.append(L.Linear(None, n_units))
        layers.append(F.relu)
    layers.append(L.Linear(None, 10))

    return chainer.Sequential(*layers)


# FYI: Objective functions can take additional arguments
# (https://optuna.readthedocs.io/en/stable/faq.html#objective-func-additional-args). 
开发者ID:optuna,项目名称:optuna,代码行数:18,代码来源:chainermn_simple.py

示例12: test_chainer_pruning_extension

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Sequential [as 别名]
def test_chainer_pruning_extension() -> None:
    def objective(trial: optuna.trial.Trial) -> float:

        model = L.Classifier(chainer.Sequential(L.Linear(None, 2)))
        optimizer = chainer.optimizers.Adam()
        optimizer.setup(model)

        train_iter = chainer.iterators.SerialIterator(FixedValueDataset(), 16)
        updater = chainer.training.StandardUpdater(train_iter, optimizer)
        trainer = chainer.training.Trainer(updater, (1, "epoch"))
        trainer.extend(
            optuna.integration.chainer.ChainerPruningExtension(trial, "main/loss", (1, "epoch"))
        )

        trainer.run(show_loop_exception_msg=False)
        return 1.0

    study = optuna.create_study(pruner=DeterministicPruner(True))
    study.optimize(objective, n_trials=1)
    assert study.trials[0].state == optuna.trial.TrialState.PRUNED

    study = optuna.create_study(pruner=DeterministicPruner(False))
    study.optimize(objective, n_trials=1)
    assert study.trials[0].state == optuna.trial.TrialState.COMPLETE
    assert study.trials[0].value == 1.0 
开发者ID:optuna,项目名称:optuna,代码行数:27,代码来源:test_chainer.py

示例13: test_sequential

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Sequential [as 别名]
def test_sequential(self):
        model = chainer.Sequential(
            chainer.links.Linear(3),
            chainer.functions.relu,
            chainer.links.Linear(4),
        )
        self.assertEqual(
            names_of_links(model),
            {'/0', '/1'}
        )
        self.assertIs(model._layers[1], chainer.functions.relu)
        to_factorized_noisy(model)
        self.assertEqual(
            names_of_links(model),
            {
                '/0', '/0/mu', '/0/sigma',
                '/1', '/1/mu', '/1/sigma',
            })
        self.assertIs(model._layers[1], chainer.functions.relu)
        model(numpy.ones((2, 3), numpy.float32))

        # assert new parameters are used
        y = model(numpy.ones((2, 3), numpy.float32))
        chainer.functions.sum(y).backward()
        for p in model.params():
            self.assertIsNotNone(p.grad) 
开发者ID:chainer,项目名称:chainerrl,代码行数:28,代码来源:test_noisy_chain.py

示例14: _map_links

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Sequential [as 别名]
def _map_links(func, link):
    if isinstance(link, chainer.Chain):
        children_names = link._children.copy()
        for name in children_names:
            child = getattr(link, name)
            new_child = func(child)
            if new_child is child:
                _map_links(func, child)
            else:
                delattr(link, name)
                with link.init_scope():
                    setattr(link, name, new_child)
    elif isinstance(link, chainer.ChainList):
        children = link._children
        for i in range(len(children)):
            child = children[i]
            new_child = func(child)
            if new_child is child:
                _map_links(func, child)
            else:
                # mimic ChainList.add_link
                children[i] = new_child
                children[i].name = str(i)

                if isinstance(link, Sequence):
                    _replace_unique_item(link.layers, child, new_child)
                # Check chainer.Sequential if it exists.
                sequential_class = getattr(chainer, 'Sequential', ())
                if isinstance(link, sequential_class):
                    _replace_unique_item(link._layers, child, new_child) 
开发者ID:chainer,项目名称:chainerrl,代码行数:32,代码来源:noisy_chain.py

示例15: recurrent_children

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import Sequential [as 别名]
def recurrent_children(self):
        """Return recurrent child links.

        This overrides `StatelessRecurrentChainList.recurrent_children`
        because `Sequential`'s evaluation order can be different from the
        order of links in `Sequential.children()`.

        See https://github.com/chainer/chainer/issues/6053

        Returns:
            tuple: Tuple of `chainer.Link`s that are recurrent.
        """
        return tuple(child for child in self._layers
                     if is_recurrent_link(child)) 
开发者ID:chainer,项目名称:chainerrl,代码行数:16,代码来源:stateless_recurrent_sequential.py


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