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


Python links.Classifier方法代码示例

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


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

示例1: check_crossing_model

# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Classifier [as 别名]
def check_crossing_model(gpu, param):
    communicator, rank_next, rank_prev = create_communicator(gpu)

    n, d = 100, 10
    X = np.random.randn(n, d).astype(param.dtype)
    Y = (np.random.rand(n) * 2).astype(np.int32)

    with chainer.using_config('dtype', param.dtype):
        if communicator.rank == 0:
            model = L.Classifier(Cross0(
                d, communicator, rank_next, rank_prev))
        else:
            model = L.Classifier(Cross1(
                d, communicator, rank_next, rank_prev))

        if gpu:
            model.to_device(cupy.cuda.Device())
            X = chainer.backends.cuda.to_gpu(X)
            Y = chainer.backends.cuda.to_gpu(Y)

        for i in range(n):
            err = model(X[i:i + 1], Y[i:i + 1])
            err.backward() 
开发者ID:chainer,项目名称:chainer,代码行数:25,代码来源:test_multi_node_chain_list.py

示例2: test_chainer_pruning_extension

# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Classifier [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

示例3: test_bn

# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Classifier [as 别名]
def test_bn(device_name, translator, computation_order):
    if skip_check(device_name, translator, computation_order):
        pytest.skip()

    np.random.seed(40)
    if has_cupy:
        cupy.random.seed(40)

    batch_size = 3
    in_size = 5
    n_out = 10

    device = chainer.get_device(device_name)
    device.use()

    bn = BN(in_size, n_out)
    bn.to_device(device)

    input = np.random.rand(batch_size, in_size, 1, 1).astype(np.float32)
    input = device.xp.array(input)
    target = device.xp.array(np.random.randint(n_out, size=batch_size))

    bn_compiled = chainer_compiler.compile(
        bn, [input], translator=translator,
        computation_order=computation_order)
    model = L.Classifier(bn_compiled)
    model.to_device(device)

    old_avg_mean = CpuDevice().send(model.predictor.mc.bn.avg_mean.copy())
    old_avg_var = CpuDevice().send(model.predictor.mc.bn.avg_var.copy())

    loss, grads = _run_fwd_bwd(model, [input, target])

    new_avg_mean = CpuDevice().send(model.predictor.mc.bn.avg_mean.copy())
    new_avg_var = CpuDevice().send(model.predictor.mc.bn.avg_var.copy())

    # running_mean and running_var should be updated
    assert not np.allclose(old_avg_mean, new_avg_mean)
    assert not np.allclose(old_avg_var, new_avg_var) 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:41,代码来源:chainer_compiler_test.py

示例4: model_fn

# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Classifier [as 别名]
def model_fn(model_dir):
    model = L.Classifier(MLP(1000, 10))
    serializers.load_npz(os.path.join(model_dir, "model.npz"), model)
    return model.predictor 
开发者ID:aws,项目名称:sagemaker-python-sdk,代码行数:6,代码来源:distributed_mnist.py

示例5: _run_test

# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Classifier [as 别名]
def _run_test(self, tempdir, initial_flag):
        n_data = 4
        n_epochs = 3
        outdir = os.path.join(tempdir, 'testresult')

        # Prepare
        model = Model()
        classifier = links.Classifier(model)
        optimizer = chainer.optimizers.Adam()
        optimizer.setup(classifier)

        dataset = Dataset([i for i in range(n_data)])
        iterator = chainer.iterators.SerialIterator(dataset, 1, shuffle=False)
        updater = training.updaters.StandardUpdater(iterator, optimizer)
        trainer = training.Trainer(updater, (n_epochs, 'epoch'), out=outdir)

        extension = c.DumpGraph('main/loss', filename='test.dot')
        trainer.extend(extension)

        # Run
        with chainer.using_config('keep_graph_on_report', initial_flag):
            trainer.run()

        # Check flag history
        self.assertEqual(model.flag_history,
                         [True] + [initial_flag] * (n_data * n_epochs - 1))

        # Check the dumped graph
        graph_path = os.path.join(outdir, 'test.dot')
        with open(graph_path) as f:
            graph_dot = f.read()

        # Check that only the first iteration is dumped
        self.assertIn('Function1', graph_dot)
        self.assertNotIn('Function2', graph_dot)

        if c.is_graphviz_available():
            self.assertTrue(os.path.exists(os.path.join(outdir, 'test.png'))) 
开发者ID:chainer,项目名称:chainer,代码行数:40,代码来源:test_computational_graph.py

示例6: setUp

# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Classifier [as 别名]
def setUp(self):
        self.n_data = 4
        self.n_epochs = 3

        self.model = Model()
        self.classifier = links.Classifier(self.model)
        self.optimizer = chainer.optimizers.Adam()
        self.optimizer.setup(self.classifier)

        self.dataset = Dataset([i for i in range(self.n_data)])
        self.iterator = chainer.iterators.SerialIterator(
            self.dataset, 1, shuffle=False)
        self.temp_dir = tempfile.mkdtemp() 
开发者ID:chainer,项目名称:chainer,代码行数:15,代码来源:test_fail_on_nonnumber.py

示例7: check_call

# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Classifier [as 别名]
def check_call(
            self, gpu, label_key, args, kwargs, model_args, model_kwargs):
        init_kwargs = {'label_key': label_key}
        if self.accfun is not None:
            init_kwargs['accfun'] = self.accfun
        link = links.Classifier(chainer.Link(), **init_kwargs)

        if gpu:
            xp = cuda.cupy
            with testing.assert_warns(DeprecationWarning):
                link.to_gpu()
        else:
            xp = numpy

        link.compute_accuracy = self.compute_accuracy

        y = chainer.Variable(self.y)
        link.predictor = mock.MagicMock(return_value=y)

        loss = link(*args, **kwargs)
        link.predictor.assert_called_with(*model_args, **model_kwargs)

        self.assertTrue(hasattr(link, 'y'))
        self.assertIsNotNone(link.y)

        self.assertTrue(hasattr(link, 'loss'))
        xp.testing.assert_allclose(link.loss.data, loss.data)

        self.assertTrue(hasattr(link, 'accuracy'))
        if self.compute_accuracy:
            self.assertIsNotNone(link.accuracy)
        else:
            self.assertIsNone(link.accuracy) 
开发者ID:chainer,项目名称:chainer,代码行数:35,代码来源:test_classifier.py

示例8: setUp

# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Classifier [as 别名]
def setUp(self):
        self.link = links.Classifier(links.Linear(10, 3))
        self.x = numpy.random.uniform(-1, 1, (5, 10)).astype(numpy.float32) 
开发者ID:chainer,项目名称:chainer,代码行数:5,代码来源:test_classifier.py

示例9: test_invalid_label_key_type

# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Classifier [as 别名]
def test_invalid_label_key_type(self):
        with six.assertRaisesRegex(
                self, TypeError, 'label_key must be int or str'):
            links.Classifier(links.Linear(10, 3), label_key=None) 
开发者ID:chainer,项目名称:chainer,代码行数:6,代码来源:test_classifier.py

示例10: _prepare_multinode_snapshot

# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Classifier [as 别名]
def _prepare_multinode_snapshot(n, result):
    n_units = 100
    batchsize = 10
    comm = create_communicator('naive')
    model = L.Classifier(MLP(n_units, 10))
    optimizer = chainermn.create_multi_node_optimizer(
        chainer.optimizers.Adam(), comm)
    optimizer.setup(model)

    if comm.rank == 0:
        train, _ = chainer.datasets.get_mnist()
    else:
        train, _ = None, None

    train = chainermn.scatter_dataset(train, comm, shuffle=True)
    train_iter = chainer.iterators.SerialIterator(train, batchsize)

    updater = StandardUpdater(train_iter, optimizer)
    trainer = Trainer(updater, out=result)

    snapshot = extensions.snapshot(target=updater, autoload=True)
    replica_sets = []
    mn_snapshot = multi_node_snapshot(comm, snapshot, replica_sets)
    mn_snapshot.initialize(trainer)
    for _ in range(n):
        updater.update()

    return updater, mn_snapshot, trainer 
开发者ID:chainer,项目名称:chainer,代码行数:30,代码来源:test_multi_node_snapshot.py

示例11: setup_mnist_trainer

# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Classifier [as 别名]
def setup_mnist_trainer(self, display_log=False, use_chx=False):
        batchsize = 100
        n_units = 100

        comm = self.communicator
        model = L.Classifier(MLP(n_units, 10))

        model.to_device(get_device(None, use_chx))

        optimizer = chainermn.create_multi_node_optimizer(
            chainer.optimizers.Adam(), comm)
        optimizer.setup(model)

        if comm.rank == 0:
            train, test = chainer.datasets.get_mnist()
        else:
            train, test = None, None

        train = chainermn.scatter_dataset(train, comm, shuffle=True)
        test = chainermn.scatter_dataset(test, comm, shuffle=True)

        train_iter = chainer.iterators.SerialIterator(train, batchsize)
        test_iter = chainer.iterators.SerialIterator(test, batchsize,
                                                     repeat=False,
                                                     shuffle=False)

        updater = training.StandardUpdater(
            train_iter,
            optimizer
        )

        return updater, optimizer, train_iter, test_iter, model 
开发者ID:chainer,项目名称:chainer,代码行数:34,代码来源:test_checkpoint.py

示例12: check_cycle_model

# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Classifier [as 别名]
def check_cycle_model(gpu, param):
    communicator, rank_next, rank_prev = create_communicator(gpu)

    n, d = 100, 10

    with chainer.using_config('dtype', param.dtype):
        if communicator.rank == 0:
            X = np.random.randn(n, d).astype(param.dtype)
            Y = (np.random.rand(n) * 2).astype(np.int32)
            model = L.Classifier(
                Cycle0(d, communicator, rank_next, rank_prev))

            if gpu:
                model.to_device(cupy.cuda.Device())
                X = chainer.backends.cuda.to_gpu(X)
                Y = chainer.backends.cuda.to_gpu(Y)

            for i in range(n):
                err = model(X[i:i + 1], Y[i:i + 1])
                err.backward()
        else:
            model = Cycle1(
                d, communicator, rank_next, rank_prev)
            if gpu:
                model.to_device(cupy.cuda.Device())

            for i in range(n):
                err = model()
                err.backward() 
开发者ID:chainer,项目名称:chainer,代码行数:31,代码来源:test_multi_node_chain_list.py

示例13: check_branching_model

# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Classifier [as 别名]
def check_branching_model(gpu, communicator, rank_next, rank_prev,
                          parent_model, param):
    n, d = 100, 10
    X = np.random.randn(n, d).astype(param.dtype)
    Y = (np.random.rand(n) * 2).astype(np.int32)

    with chainer.using_config('dtype', param.dtype):
        if communicator.rank == 0:
            rank_children = [rank for rank in range(1, communicator.size)]
            model = L.Classifier(parent_model(
                d, communicator, rank_children))
            if gpu:
                model.to_device(cupy.cuda.Device())
                X = chainer.backends.cuda.to_gpu(X)
                Y = chainer.backends.cuda.to_gpu(Y)

            for i in range(n):
                err = model(X[i:i + 1], Y[i:i + 1])
                err.backward()
        else:
            model = BranchChild(d, communicator, 0)
            if gpu:
                model.to_device(cupy.cuda.Device())

            for i in range(n):
                err = model()
                err.backward() 
开发者ID:chainer,项目名称:chainer,代码行数:29,代码来源:test_multi_node_chain_list.py

示例14: check_tuple_data_model

# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Classifier [as 别名]
def check_tuple_data_model(gpu, param):
    # This test only uses pairs (0, 1), (2, 3), ... (2m, 2m+1)
    communicator, rank_next, rank_prev = create_communicator(gpu)

    n, d = 100, 10
    X = np.random.randn(n, d).astype(param.dtype)
    Y = (np.random.rand(n) * 2).astype(np.int32)

    with chainer.using_config('dtype', param.dtype):
        if communicator.rank % 2 == 0:
            if communicator.rank == communicator.size - 1:
                # in case 2m is the right end with odd number of nodes
                return
            model = L.Classifier(
                TupleDataParent(communicator, d, rank_next))
        elif communicator.rank % 2 == 1:
            model = TupleDataChild(communicator, d, rank_prev)

        assert model is not None
        if gpu:
            model.to_device(cupy.cuda.Device())
            X = chainer.backends.cuda.to_gpu(X)
            Y = chainer.backends.cuda.to_gpu(Y)

        for i in range(n):
            if communicator.rank % 2 == 0:
                err = model(X[i:i + 1], Y[i:i + 1])
            elif communicator.rank % 2 == 1:
                err = model()
            assert err is not None
            err.backward() 
开发者ID:chainer,项目名称:chainer,代码行数:33,代码来源:test_multi_node_chain_list.py

示例15: model_fn

# 需要导入模块: from chainer import links [as 别名]
# 或者: from chainer.links import Classifier [as 别名]
def model_fn(model_dir):
    model = L.Classifier(MLP(1000, 10))
    serializers.load_npz(os.path.join(model_dir, 'model.npz'), model)
    return model.predictor 
开发者ID:aws,项目名称:sagemaker-chainer-container,代码行数:6,代码来源:distributed_customer_script.py


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