python类Link()的实例源码

nn.py 文件源码 项目:chainer-speech-recognition 作者: musyoku 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def layer(self, *layers):
        with self.init_scope():
            for i, layer in enumerate(layers):
                index = i + len(self.layers)

                if isinstance(layer, chainer.Link):
                    setattr(self, "layer_%d" % index, layer)

                if isinstance(layer, GLU):
                    setattr(self, "layer_%d" % index, layer.W)

                if isinstance(layer, Residual):
                    for _index, _layer in enumerate(layer.layers):
                        if isinstance(_layer, chainer.Link):
                            setattr(self, "layer_{}_{}".format(index, _index), _layer)

        self.layers += layers
nn.py 文件源码 项目:chainer-speech-recognition 作者: musyoku 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def _set_module(self, namespace, module):
        assert isinstance(module, Module)

        for index, layer in enumerate(module.layers):
            if isinstance(layer, chainer.Link):
                super(Module, self).__setattr__("_module_{}_sequential_{}".format(namespace, index), layer)

            if isinstance(layer, Residual):
                for _index, _layer in enumerate(layer.layers):
                    if isinstance(_layer, chainer.Link):
                        super(Module, self).__setattr__("_module_{}_sequential_{}_{}".format(namespace, index, _index), _layer)

        for index, (link_name, link) in enumerate(module.links):
            assert isinstance(link, chainer.Link)
            super(Module, self).__setattr__("_module_{}_link_{}".format(namespace, link_name), link)

        for index, (module_name, module) in enumerate(module.modules):
            assert isinstance(module, Module)
            self._set_module("{}_{}".format(namespace, module_name), module)

        module._locked = True
link.py 文件源码 项目:chainer-deconv 作者: germanRos 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def copy(self):
        """Copies the link hierarchy to new one.

        The whole hierarchy rooted by this link is copied. The copy is
        basically shallow, except that the parameter variables are also
        shallowly copied. It means that the parameter variables of copied one
        are different from ones of original link, while they share the data and
        gradient arrays.

        The name of the link is reset on the copy, since the copied instance
        does not belong to the original parent chain (even if exists).

        Returns:
            Link: Copied link object.

        """
        ret = copy.copy(self)
        ret._params = list(self._params)
        ret._persistent = list(self._persistent)
        ret.name = None
        d = ret.__dict__
        for name in ret._params:
            d[name] = copy.copy(d[name])
        return ret
test_link.py 文件源码 项目:chainer-deconv 作者: germanRos 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_copyparams(self):
        self.link.x.grad.fill(0)
        self.link.y.grad.fill(1)
        gx = self.link.x.grad.copy()
        gy = self.link.y.grad.copy()

        l = chainer.Link(x=(2, 3), y=2)
        l.x.data.fill(2)
        l.x.grad.fill(3)
        l.y.data.fill(4)
        l.y.grad.fill(5)
        self.link.copyparams(l)
        numpy.testing.assert_array_equal(self.link.x.data, l.x.data)
        numpy.testing.assert_array_equal(self.link.x.grad, gx)
        numpy.testing.assert_array_equal(self.link.y.data, l.y.data)
        numpy.testing.assert_array_equal(self.link.y.grad, gy)
test_link.py 文件源码 项目:chainer-deconv 作者: germanRos 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_addgrads(self):
        l1 = chainer.Link(x=(2, 3))
        l2 = chainer.Link(x=2)
        l3 = chainer.Link(x=3)
        c1 = chainer.Chain(l1=l1, l2=l2)
        c2 = chainer.Chain(c1=c1, l3=l3)
        l1.x.grad.fill(1)
        l2.x.grad.fill(2)
        l3.x.grad.fill(3)

        self.l1.x.grad.fill(-1)
        self.l2.x.grad.fill(-2)
        self.l3.x.grad.fill(-3)

        self.c2.addgrads(c2)
        numpy.testing.assert_array_equal(self.l1.x.grad, numpy.zeros((2, 3)))
        numpy.testing.assert_array_equal(self.l2.x.grad, numpy.zeros(2))
        numpy.testing.assert_array_equal(self.l3.x.grad, numpy.zeros(3))
test_link.py 文件源码 项目:chainer-deconv 作者: germanRos 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def test_addgrads(self):
        l1 = chainer.Link(x=(2, 3))
        l2 = chainer.Link(x=2)
        l3 = chainer.Link(x=3)
        c1 = chainer.ChainList(l1, l2)
        c2 = chainer.ChainList(c1, l3)
        l1.x.grad.fill(1)
        l2.x.grad.fill(2)
        l3.x.grad.fill(3)

        self.l1.x.grad.fill(-1)
        self.l2.x.grad.fill(-2)
        self.l3.x.grad.fill(-3)

        self.c2.addgrads(c2)
        numpy.testing.assert_array_equal(self.l1.x.grad, numpy.zeros((2, 3)))
        numpy.testing.assert_array_equal(self.l2.x.grad, numpy.zeros(2))
        numpy.testing.assert_array_equal(self.l3.x.grad, numpy.zeros(3))
nn.py 文件源码 项目:adversarial-autoencoder 作者: musyoku 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def __setattr__(self, name, value):
        assert isinstance(value, Residual) is False

        if isinstance(value, Module):
            self.__module_name__ = name

            self.__modules__.append((name, value))
            value.set_parent_module(self)

            self.update_params()
            return super(chainer.Link, self).__setattr__(name, value)   # prevent module from being added to self._children

        if isinstance(value, chainer.Link):
            if name.startswith("_nn_layer_"):
                return self.super__setattr__(name, value)

            self.__links__.append((name, value))

            self.update_params()

            with self.init_scope():
                return self.super__setattr__(name, value)

        super(Module, self).__setattr__(name, value)
caffe2npz.py 文件源码 项目:chainercv 作者: chainer 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def __setattr__(self, name, value):
        if self.within_init_scope and isinstance(value, Link):
            new_name = rename(name)

            if new_name == 'extractor/conv1_1':
                # BGR -> RGB
                value.W.array[:, ::-1] = value.W.array
                print('{:s} -> {:s} (BGR -> RGB)'.format(name, new_name))
            elif new_name.startswith('multibox/loc/'):
                # xy -> yx
                for data in (value.W.array, value.b.array):
                    data = data.reshape((-1, 4) + data.shape[1:])
                    data[:, [1, 0, 3, 2]] = data.copy()
                print('{:s} -> {:s} (xy -> yx)'.format(name, new_name))
            else:
                print('{:s} -> {:s}'.format(name, new_name))
        else:
            new_name = name

        super(SSDCaffeFunction, self).__setattr__(new_name, value)
test_constant_stub_link.py 文件源码 项目:chainercv 作者: chainer 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def _check(self, xp):
        self.assertIsInstance(self.link, chainer.Link)
        self.assertEqual(self.link.xp, xp)

        outputs = self.link('ignored', -1, 'inputs', 1.0)

        if isinstance(self.outputs, tuple):
            originals = self.outputs
            outputs = outputs
        else:
            originals = self.outputs,
            outputs = outputs,

        self.assertEqual(len(originals), len(outputs))

        for orig, out in zip(originals, outputs):
            self.assertIsInstance(out, chainer.Variable)
            self.assertEqual(out.shape, orig.shape)
            self.assertEqual(out.dtype, orig.dtype)

            self.assertEqual(
                chainer.cuda.get_array_module(out.array), xp)
            out.to_cpu()
            np.testing.assert_equal(out.array, orig)
dpn.py 文件源码 项目:chainer-DPNs 作者: oyam 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def __init__(self, *args):
        super(Sequential, self).__init__()
        assert len(args) > 0
        assert not hasattr(self, "layers")
        if len(args) == 1 and isinstance(args[0], OrderedDict):
            self.layers = args[0].values()
            with self.init_scope():
                for key, layer in args[0].items():
                    if isinstance(layer, (chainer.Link, chainer.Chain, chainer.ChainList)):
                        setattr(self, key, layer)
        else:
            self.layers = args
            with self.init_scope():
                for idx, layer in enumerate(args):
                    if isinstance(layer, (chainer.Link, chainer.Chain, chainer.ChainList)):
                        setattr(self, str(idx), layer)
monitor.py 文件源码 项目:wavenet 作者: rampage644 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def layer_params(layer, param_name, attr_name):

    """Return parameters in a flattened array from the given layer or an empty
    array if the parameters are not found.

    Args:
        layer (~chainer.Link): The layer from which parameters are collected.
        param_name (str): Name of the parameter, ``'W'`` or ``'b'``.
        attr_name (str): Name of the attribute, ``'data'`` or ``'grad'``.

    Returns:
        array: Flattened array of parameters.
    """

    if isinstance(layer, chainer.Chain):
        # Nested chainer.Chain, aggregate all underlying statistics
        return layers_params(layer, param_name, attr_name)
    elif not hasattr(layer, param_name):
        return layer.xp.array([])

    params = getattr(layer, param_name)
    params = getattr(params, attr_name)
    return params.flatten()
sequence.py 文件源码 项目:chainerrl 作者: chainer 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def __init__(self, *layers):
        self.layers = layers
        links = [layer for layer in layers if isinstance(layer, chainer.Link)]
        # Cache the signatures because it might be slow
        self.argnames = [set(signature(layer).parameters)
                         for layer in layers]
        self.accept_var_args = [accept_variable_arguments(layer)
                                for layer in layers]
        super().__init__(*links)
async.py 文件源码 项目:chainerrl 作者: chainer 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def set_shared_params(a, b):
    """Set shared params to a link.

    Args:
      a (chainer.Link): link whose params are to be replaced
      b (dict): dict that consists of (param_name, multiprocessing.Array)
    """
    assert isinstance(a, chainer.Link)
    for param_name, param in a.namedparams():
        if param_name in b:
            shared_param = b[param_name]
            param.data = np.frombuffer(
                shared_param, dtype=param.data.dtype).reshape(param.data.shape)
async.py 文件源码 项目:chainerrl 作者: chainer 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def make_params_not_shared(a):
    """Make a link's params not shared.

    Args:
      a (chainer.Link): link whose params are to be made not shared
    """
    assert isinstance(a, chainer.Link)
    for param in a.params():
        param.data = param.data.copy()
async.py 文件源码 项目:chainerrl 作者: chainer 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def assert_params_not_shared(a, b):
    assert isinstance(a, chainer.Link)
    assert isinstance(b, chainer.Link)
    a_params = dict(a.namedparams())
    b_params = dict(b.namedparams())
    for name, a_param in a_params.items():
        b_param = b_params[name]
        assert a_param.data.ctypes.data != b_param.data.ctypes.data
async.py 文件源码 项目:chainerrl 作者: chainer 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def extract_params_as_shared_arrays(link):
    assert isinstance(link, chainer.Link)
    shared_arrays = {}
    for param_name, param in link.namedparams():
        shared_arrays[param_name] = mp.RawArray('f', param.data.ravel())
    return shared_arrays
async.py 文件源码 项目:chainerrl 作者: chainer 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def synchronize_to_shared_objects(obj, shared_memory):
    if isinstance(obj, tuple):
        return tuple(synchronize_to_shared_objects(o, s)
                     for o, s in zip(obj, shared_memory))
    elif isinstance(obj, chainer.Link):
        set_shared_params(obj, shared_memory)
        return obj
    elif isinstance(obj, chainer.Optimizer):
        set_shared_states(obj, shared_memory)
        return obj
    elif isinstance(obj, mp.sharedctypes.Synchronized):
        return shared_memory
    else:
        raise ValueError('')
test_agent.py 文件源码 项目:chainerrl 作者: chainer 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def create_simple_link():
    link = chainer.Link()
    with link.init_scope():
        link.param = chainer.Parameter(np.zeros(1))
    return link
async.py 文件源码 项目:async-rl 作者: muupan 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def set_shared_params(a, b):
    """
    Args:
      a (chainer.Link): link whose params are to be replaced
      b (dict): dict that consists of (param_name, multiprocessing.Array)
    """
    assert isinstance(a, chainer.Link)
    for param_name, param in a.namedparams():
        if param_name in b:
            shared_param = b[param_name]
            param.data = np.frombuffer(
                shared_param, dtype=param.data.dtype).reshape(param.data.shape)
async.py 文件源码 项目:async-rl 作者: muupan 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def extract_params_as_shared_arrays(link):
    assert isinstance(link, chainer.Link)
    shared_arrays = {}
    for param_name, param in link.namedparams():
        shared_arrays[param_name] = mp.RawArray('f', param.data.ravel())
    return shared_arrays
nn.py 文件源码 项目:chainer-speech-recognition 作者: musyoku 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def add(self, *layers):
        with self.init_scope():
            for i, layer in enumerate(layers):
                index = i + len(self.layers)

                if isinstance(layer, chainer.Link):
                    setattr(self, "_sequential_%d" % index, layer)

                if isinstance(layer, Residual):
                    for _index, _layer in enumerate(layer.layers):
                        if isinstance(_layer, chainer.Link):
                            setattr(self, "_sequential_{}_{}".format(index, _index), _layer)
        self.layers += layers
        self.blocks.append(layers)
nn.py 文件源码 项目:chainer-speech-recognition 作者: musyoku 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def __setattr__(self, name, value):
        if isinstance(value, Module):
            self.modules.append((name, value))
            self._set_module(name, value)
            return super(chainer.Link, self).__setattr__(name, value)   # prevent module from being added to self._children

        if isinstance(value, chainer.Link):
            assert self._locked is False, "Since this module is owned by another module, it is not possible to add Link."
            with self.init_scope():
                if name.startswith("_sequential_"):
                    return super(Module, self).__setattr__(name, value)
                self.links.append((name, value))
                return super(Module, self).__setattr__(name, value)

        super(Module, self).__setattr__(name, value)
link.py 文件源码 项目:chainer-deconv 作者: germanRos 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def copyparams(self, link):
        """Copies all parameters from given link.

        This method copies data arrays of all parameters in the hierarchy. The
        copy is even done across the host and devices. Note that this method
        does not copy the gradient arrays.

        Args:
            link (Link): Source link object.

        """
        src = link.__dict__
        dst = self.__dict__
        for name in self._params:
            dst[name].copydata(src[name])
link.py 文件源码 项目:chainer-deconv 作者: germanRos 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def addgrads(self, link):
        """Accumulates gradient values from given link.

        This method adds each gradient array of the given link to corresponding
        gradient array of this link. The accumulation is even done across
        host and different devices.

        Args:
            link (Link): Source link object.

        """
        src = link.__dict__
        dst = self.__dict__
        for name in self._params:
            dst[name].addgrad(src[name])
link.py 文件源码 项目:chainer-deconv 作者: germanRos 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def add_link(self, name, link):
        """Registers a child link to this chain.

        The registered link is saved and loaded on serialization and
        deserialization, and involved in the optimization. The registered link
        is called a child. The child link is set to an attribute of the chain
        with the given name.

        This method also sets the :attr:`~Link.name` attribute of the
        registered link. If the given link already has the name attribute set,
        then it raises an error.

        Args:
            name (str): Name of the child link. This name is also used as the
                attribute name.
            link (Link): The link object to be registered.

        """
        if link.name is not None:
            raise ValueError(
                'given link is already registered to another chain by name %s'
                % link.name)
        d = self.__dict__
        if name in d:
            raise AttributeError(
                'cannot register a new link %s: attribute exists' % name)
        self._children.append(name)
        link.name = name
        d[name] = link
link.py 文件源码 项目:chainer-deconv 作者: germanRos 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def __getitem__(self, index):
        """Returns the child at given index.

        Args:
            index (int): Index of the child in the list.

        Returns:
            Link: The ``index``-th child link.

        """
        return self._children[index]
test_optimizers_by_linear_model.py 文件源码 项目:chainer-deconv 作者: germanRos 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def test_initialize(self):
        model = self.model.model
        assert isinstance(model, chainer.Link)
        optimizer = self.create()
        optimizer.setup(model)

        msg = 'optimization target must be a link'
        with six.assertRaisesRegex(self, TypeError, msg):
            optimizer.setup('xxx')
test_link.py 文件源码 项目:chainer-deconv 作者: germanRos 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def setUp(self):
        self.link = chainer.Link(x=(2, 3), y=2)
        self.p = numpy.array([1, 2, 3], dtype='f')
        self.link.add_persistent('p', self.p)
        self.link.name = 'a'
test_link.py 文件源码 项目:chainer-deconv 作者: germanRos 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_addgrads(self):
        l = chainer.Link(x=(2, 3), y=2)
        l.x.grad.fill(1)
        l.y.grad.fill(2)

        self.link.x.grad.fill(-1)
        self.link.y.grad.fill(-2)

        self.link.addgrads(l)

        gx_expect = numpy.zeros_like(l.x.grad)
        gy_expect = numpy.zeros_like(l.y.grad)
        numpy.testing.assert_array_equal(self.link.x.grad, gx_expect)
        numpy.testing.assert_array_equal(self.link.y.grad, gy_expect)
test_link.py 文件源码 项目:chainer-deconv 作者: germanRos 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def test_serialize(self):
        serializer = mock.MagicMock(return_value=3)
        l = chainer.Link(x=(2, 3), y=2)
        l.add_persistent('z', 1)
        l.serialize(serializer)
        self.assertEqual(serializer.call_count, 3)
        serializer.assert_any_call('x', l.x.data)
        serializer.assert_any_call('y', l.y.data)
        serializer.assert_any_call('z', 1)

        self.assertEqual(l.z, 3)


问题


面经


文章

微信
公众号

扫码关注公众号