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


Python chainer.get_device方法代码示例

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


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

示例1: test_multi_in_outs

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import get_device [as 别名]
def test_multi_in_outs(device_name, translator):
    device = chainer.get_device(device_name)
    device.use()

    model = MultiInOuts()
    model.to_device(device)

    inputs = [np.array(3, dtype=np.float32), np.array(39, dtype=np.float32)]

    expected = model(*inputs)

    model = chainer_compiler.compile(model, inputs, translator=translator)
    model.to_device(device)

    actual = model(*inputs)

    assert len(expected) == len(actual)
    for e, a in zip(expected, actual):
        e = _array(e)
        a = _array(a)
        assert _get_device(e) == _get_device(a)
        _assert_allclose(e, a) 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:24,代码来源:chainer_compiler_test.py

示例2: test_const_mul

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import get_device [as 别名]
def test_const_mul(device_name, translator):
    device = chainer.get_device(device_name)
    device.use()

    # This checks if the default ChainerX device is set properly by
    # Constant op, whose result will be placed on the default device.
    model = ConstMul()
    model.to_device(device)

    inputs = [np.array(3, dtype=np.float32)]

    expected = model(*inputs)

    model = chainer_compiler.compile(model, inputs, translator=translator)
    model.to_device(device)

    actual = model(*inputs)

    e = _array(expected)
    a = _array(actual)
    assert _get_device(e) == _get_device(a)
    _assert_allclose(e, a) 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:24,代码来源:chainer_compiler_test.py

示例3: test_sequence

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import get_device [as 别名]
def test_sequence(device_name, translator):
    device = chainer.get_device(device_name)
    device.use()

    model = Sequence()
    model.to_device(device)

    xs = [device.xp.array(i + 1, dtype=np.float32) for i in range(3)]
    expected = model(xs)

    model = chainer_compiler.compile(model, [xs], translator=translator)
    model.to_device(device)

    xs = [device.xp.array(i + 1, dtype=np.float32) for i in range(3)]
    actual = model(xs)

    assert len(expected) == len(actual)
    for e, a in zip(expected, actual):
        e = _array(e)
        a = _array(a)
        assert _get_device(e) == _get_device(a)
        _assert_allclose(e, a) 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:24,代码来源:chainer_compiler_test.py

示例4: test_backward_default_device

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import get_device [as 别名]
def test_backward_default_device(self):
        # Default device in backward should be determined by arrays,
        # otherwise, creation routines in backward do not create new arrays
        # on the proper device.

        device = chainerx.get_device('cuda:0')
        shape = (2, 3)
        dtype = numpy.float32
        x1 = chainerx.full(shape, 3, dtype, device=device)
        x2 = chainerx.full(shape, 5, dtype, device=device).require_grad()

        backward_call_new_array = []

        def backward_call_callback(call_arg):
            backward_call_new_array.append(chainerx.empty(shape, dtype))

        with chainerx.using_device('native:0'):
            # forward
            func = self.SimpleFunctionNode(backward_call_callback)
            y1, y2 = func.apply((x1, x2))

            # backward
            y2.backward()

        assert backward_call_new_array[0].device is device 
开发者ID:chainer,项目名称:chainer,代码行数:27,代码来源:test_function_node.py

示例5: test_sample_unit_vector

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import get_device [as 别名]
def test_sample_unit_vector(self, backend_config):
        size = self.size
        device = backend_config.device

        # _sample_unit_vector uses the current device
        with chainer.using_device(device):
            y = gradient_check._CheckBackward._sample_unit_vector(
                size, device.xp)
        assert device.is_array_supported(y)
        assert y.shape == (size,)

        y_cpu = chainer.get_device('@numpy').send(y)
        if size >= 1:
            numpy.testing.assert_allclose(numpy.square(y_cpu).sum(), 1.0)
            assert numpy.min(abs(y_cpu)) >= 0.1 / numpy.sqrt(size)
        if size >= 64:
            assert numpy.min(y_cpu) < 0 < numpy.max(y_cpu) 
开发者ID:chainer,项目名称:chainer,代码行数:19,代码来源:test_gradient_check.py

示例6: to_device

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import get_device [as 别名]
def to_device(
            self,
            device: types.DeviceSpec
    ) -> 'DeviceResident':
        """Copies parameter variables and persistent values to the specified \
device.

        This method does not handle non-registered attributes. If some of such
        attributes must be copied to the device, the link implementation must
        override this method to do so.

        Args:
            device: Target device specifier. See
                :func:`~chainer.get_device` for available values.

        Returns: self

        """
        device = chainer.get_device(device)
        self.__to_device(_ToDeviceVisitor(device))
        return self 
开发者ID:chainer,项目名称:chainer,代码行数:23,代码来源:device_resident.py

示例7: using_device

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import get_device [as 别名]
def using_device(device_spec):
    """Context manager to apply the thread-local device state.

    Args:
        device_spec (object): Device specifier. See :func:`chainer.get_device`
            for details.

    .. admonition:: Example

        .. testcode::
           :skipif: doctest_helper.skipif_not_enough_cuda_devices(2)

           with chainer.using_device('@cupy:1'):
               a = cupy.empty((3, 2))

           assert a.device.id == 1

    """

    # TODO(niboshi): Set default device (once this concept is introduced in
    # Chainer).
    device = get_device(device_spec)
    return device.create_context() 
开发者ID:chainer,项目名称:chainer,代码行数:25,代码来源:backend.py

示例8: to_chx

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import get_device [as 别名]
def to_chx(self):
        if not chainerx.is_available():
            raise RuntimeError('ChainerX is not available.')

        # Derive the target ChainerX device from the array if it is
        # initialized. Otherwise, from the current initial device.
        if self.array is not None:
            device = backend.get_device_from_array(self.array)
        else:
            device = self._initial_device

        if device.xp is numpy:
            self._initial_device = backend.ChainerxDevice(
                chainerx.get_device('native:0'))
        elif device.xp is cuda.cupy:
            self._initial_device = backend.ChainerxDevice(
                chainerx.get_device('cuda:{}'.format(device.device.id)))

        super(Parameter, self)._to_chx(allow_unchaining=True) 
开发者ID:chainer,项目名称:chainer,代码行数:21,代码来源:variable.py

示例9: test_keyword_arguments_different_batchsize

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import get_device [as 别名]
def test_keyword_arguments_different_batchsize(self):
        import numpy as np
        import chainer

        # test batchsize smaller than, equal to and greater than number devices
        for batchsize in [1, 2, 3]:
            with self.subTest(batchsize=batchsize):
                input_kwargs = {
                    "x": np.random.rand(batchsize, 3).astype(np.float32)
                }

                pred = self.model(**input_kwargs)
                self.assertTupleEqual(pred.shape,
                                      (batchsize, 2))
                self.assertEqual(chainer.get_device(pred.device),
                                 chainer.get_device("@numpy"))

    # test with positional arguments 
开发者ID:delira-dev,项目名称:delira,代码行数:20,代码来源:test_chainer.py

示例10: test_positional_arguments

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import get_device [as 别名]
def test_positional_arguments(self):
        import numpy as np
        import chainer

        # test batchsize smaller than, equal to and greater than number devices
        for batchsize in [1, 2, 3]:
            with self.subTest(batchsize=batchsize):
                input_args = [
                    np.random.rand(batchsize, 3).astype(np.float32)
                ]

                pred = self.model(*input_args)
                self.assertTupleEqual(pred.shape,
                                      (batchsize, 2))

                self.assertEqual(chainer.get_device(pred.device),
                                 chainer.get_device("@numpy")) 
开发者ID:delira-dev,项目名称:delira,代码行数:19,代码来源:test_chainer.py

示例11: converter

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import get_device [as 别名]
def converter(self, batch, device=None):
        """Converter

        Args:
            batch (list[BaseGraphData]): list of graph data
            device (int, optional): specifier of device. Defaults to None.

        Returns:
            self sent to `device`
        """
        if not isinstance(device, Device):
            device = chainer.get_device(device)
        batch = [method(name, batch, device=device) for name, method in
                 zip(self._feature_entries, self._feature_batch_method)]
        data = BaseGraphData(
            **{key: value for key, value in zip(self._feature_entries, batch)})
        return data 
开发者ID:chainer,项目名称:chainer-chemistry,代码行数:19,代码来源:base_graph_dataset.py

示例12: _test_save_load_pickle

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import get_device [as 别名]
def _test_save_load_pickle(device, tmpdir):
    model = DummyForwardModel(device=device, dummy_str='hoge')

    filepath = os.path.join(str(tmpdir), 'model.pkl')
    model.save_pickle(filepath)
    model_load = DummyForwardModel.load_pickle(filepath, device=device)

    # --- check model class ---
    assert isinstance(model_load, DummyForwardModel)
    # --- check model attribute is same ---
    assert model_load.dummy_str == model.dummy_str
    assert model_load.dummy_str == 'hoge'
    assert model_load.device == chainer.get_device(device)

    # --- check model parameter is same ---
    params = model.namedparams()
    params_load = dict(model_load.namedparams())
    for k, v in params:
        v_load = params_load[k]
        assert cuda.get_device_from_array(v_load.data).id == device
        assert numpy.allclose(cuda.to_cpu(v.data), cuda.to_cpu(v_load.data)) 
开发者ID:chainer,项目名称:chainer-chemistry,代码行数:23,代码来源:test_base.py

示例13: test_bn

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

示例14: translate

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import get_device [as 别名]
def translate(self, xs, max_length=100):
        batch = len(xs)
        with chainer.no_backprop_mode(), chainer.using_config('train', False):
            xs = [x[::-1] for x in xs]
            exs = sequence_embed(self.embed_x, xs)
            h, c, _ = self.encoder(None, None, exs)
            ys = self.xp.full(batch, EOS, numpy.int32)
            result = []
            for i in range(max_length):
                eys = self.embed_y(ys)
                eys = F.split_axis(eys, batch, 0)
                h, c, ys = self.decoder(h, c, eys)
                cys = F.concat(ys, axis=0)
                wy = self.W(cys)
                ys = self.xp.argmax(wy.array, axis=1).astype(numpy.int32)
                result.append(ys)

        # Using `xp.concatenate(...)` instead of `xp.stack(result)` here to
        # support NumPy 1.9.
        result = chainer.get_device('@numpy').send(
            self.xp.concatenate([x[None, :] for x in result]).T)

        # Remove EOS taggs
        outs = []
        for y in result:
            inds = numpy.argwhere(y == EOS)
            if len(inds) > 0:
                y = y[:inds[0, 0]]
            outs.append(y)
        return outs 
开发者ID:chainer,项目名称:chainer,代码行数:32,代码来源:seq2seq.py

示例15: save3x3

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import get_device [as 别名]
def save3x3(x, filename):
    numpy_device = chainer.get_device('@numpy')
    fig, ax = plt.subplots(3, 3, figsize=(9, 9), dpi=100)
    for ai, xi in zip(ax.flatten(), x):
        im = xi.reshape(28, 28)
        im = numpy_device.send(im)
        ai.imshow(im)
    fig.savefig(filename)


# Saves reconstruction images using:
# - training image samples
# - test image samples
# - randomly sampled values of z 
开发者ID:chainer,项目名称:chainer,代码行数:16,代码来源:train_vae.py


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