Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
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):
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)