當前位置: 首頁>>代碼示例>>Python>>正文


Python chainer.optimizers方法代碼示例

本文整理匯總了Python中chainer.optimizers方法的典型用法代碼示例。如果您正苦於以下問題:Python chainer.optimizers方法的具體用法?Python chainer.optimizers怎麽用?Python chainer.optimizers使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在chainer的用法示例。


在下文中一共展示了chainer.optimizers方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_hyperparams

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import optimizers [as 別名]
def test_hyperparams(self):
        # TODO(niboshi): The following optimizers do not pass this test
        # because their __init__ do not accept some hyperparameters.
        # The test should be fixed.
        if self.optimizer_impl in (
                chainer.optimizers.AdamW,
                chainer.optimizers.AMSGrad,
                chainer.optimizers.AdaBound,
                chainer.optimizers.AMSBound,
        ):
            raise unittest.SkipTest(
                'The optimizer is incompatible with this test')

        self.create()
        default = self.optimizer.hyperparam.get_dict()
        for name, default_value in six.iteritems(default):
            self.create()
            self.assertEqual(self.get_hyperparam(name), default_value)
            new_value = default_value + 0.1
            self.create(**{name: new_value})
            self.assertEqual(self.get_hyperparam(name), new_value) 
開發者ID:chainer,項目名稱:chainer,代碼行數:23,代碼來源:test_optimizers.py

示例2: test_adam_w

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import optimizers [as 別名]
def test_adam_w(self, backend_config):
        xp = backend_config.xp
        device = backend_config.device

        link = chainer.Link(x=(1,))
        link.to_device(device)

        opt = optimizers.Adam(eta=0.5, weight_decay_rate=0.1)
        opt.setup(link)

        link.x.data.fill(1)
        link.x.grad = device.send(xp.ones_like(link.x.data))

        opt.update()

        # compare against the value computed with v5 impl
        testing.assert_allclose(link.x.data, np.array([0.9495]),
                                atol=1e-7, rtol=1e-7) 
開發者ID:chainer,項目名稱:chainer,代碼行數:20,代碼來源:test_optimizers.py

示例3: test_all_optimizers_coverage

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import optimizers [as 別名]
def test_all_optimizers_coverage(self):
        module = chainer.optimizers
        module_optimizers = []
        for name in dir(module):
            obj = getattr(module, name)
            if (isinstance(obj, type) and issubclass(obj, chainer.Optimizer)):
                module_optimizers.append(name)

        assert sorted(_all_optimizers) == sorted(module_optimizers) 
開發者ID:chainer,項目名稱:chainer,代碼行數:11,代碼來源:test_optimizers.py

示例4: test_optimizer

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import optimizers [as 別名]
def test_optimizer(self, backend_config):
        device = backend_config.device
        target = SimpleChain(self.shape)
        target.to_device(device)
        optimizer_cls = getattr(chainer.optimizers, self.optimizer)
        optimizer = optimizer_cls(**self.kwargs)
        optimizer.setup(target)

        x_np = np.asarray(np.random.randn(*self.shape)).astype(np.float32)
        x = chainer.Variable(device.send(x_np))

        # Just ensures no error occurs. No numerical check is performed.
        optimizer.update(target, x) 
開發者ID:chainer,項目名稱:chainer,代碼行數:15,代碼來源:test_optimizers.py

示例5: test_amsgrad

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import optimizers [as 別名]
def test_amsgrad(self, backend_config):
        device = backend_config.device

        link = chainer.Link(x=(4,))
        x = link.x
        x.data.fill(0)
        link.to_device(device)

        opt = optimizers.Adam(alpha=0.01, beta2=0.7, amsgrad=True)
        opt.setup(link)

        x.grad = device.send(np.array([1, -1, 10, -10], np.float32))
        opt.update()
        testing.assert_allclose(
            x.update_rule.state['v'],
            [0.3, 0.3, 30, 30],
            atol=1e-7, rtol=1e-7)
        testing.assert_allclose(
            x.data,
            [-0.01, 0.01, -0.01, 0.01],
            atol=1e-7, rtol=1e-7)

        x.grad = device.send(np.array([-10, -10, -1, -1], np.float32))
        opt.update()
        testing.assert_allclose(
            x.update_rule.state['v'],
            [30.21, 30.21, 21.3, 21.3],
            atol=1e-7, rtol=1e-7)
        testing.assert_allclose(
            x.update_rule.state['vhat'],
            [30.21, 30.21, 30, 30],
            atol=1e-7, rtol=1e-7)
        testing.assert_allclose(
            x.data,
            # result with NumPy
            [-0.00377703, 0.01745388, -0.01548985, 0.01686232],
            atol=1e-7, rtol=1e-7) 
開發者ID:chainer,項目名稱:chainer,代碼行數:39,代碼來源:test_optimizers.py

示例6: create_optimizer

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import optimizers [as 別名]
def create_optimizer(config, model):
    Optimizer = getattr(chainer.optimizers, config['name'])
    opt = Optimizer(**config['args'])
    opt.setup(model)
    if 'hook' in config.keys():
        for key, value in config['hook'].items():
            hook = getattr(chainer.optimizer, key)
            opt.add_hook(hook(value))
    return opt 
開發者ID:yukitsuji,項目名稱:voxelnet_chainer,代碼行數:11,代碼來源:config_utils.py

示例7: setUp

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import optimizers [as 別名]
def setUp(self) -> None:
        if check_for_chainer_backend():
            import chainer
            import chainer.link
            import chainer.links
            import chainer.functions
            import chainer.optimizers
            from delira.models.backends.chainer.data_parallel import \
                DataParallelChainerOptimizer, \
                DataParallelChainerNetwork
            from delira.models.backends.chainer.abstract_network import \
                AbstractChainerNetwork

            # creating a really simple model to test dataparallel behavior
            class SimpleModel(AbstractChainerNetwork):
                def __init__(self):
                    super(SimpleModel, self).__init__()

                    with self.init_scope():
                        self.dense_1 = chainer.links.Linear(3, 32)
                        self.dense_2 = chainer.links.Linear(32, 2)

                def forward(self, x):
                    return self.dense_2(
                        chainer.functions.relu(
                            self.dense_1(x)))

            self.model = DataParallelChainerNetwork(SimpleModel(),
                                                    devices=["@numpy",
                                                             "@numpy"])

            self.optimizer = DataParallelChainerOptimizer.from_optimizer_class(
                chainer.optimizers.Adam
            )
            self.optimizer.setup(self.model) 
開發者ID:delira-dev,項目名稱:delira,代碼行數:37,代碼來源:test_chainer.py

示例8: _build_optimizer

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import optimizers [as 別名]
def _build_optimizer(self, config):
        kwargs = {k: float(v) for k, v in config.items() if k != 'name'}
        o = getattr(chainer.optimizers, config['name'])(**kwargs)
        return o 
開發者ID:kiyukuta,項目名稱:lencon,代碼行數:6,代碼來源:builder.py


注:本文中的chainer.optimizers方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。