Python mxnet.gluon.Block() Examples

The following are 30 code examples of mxnet.gluon.Block(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module mxnet.gluon , or try the search function .
Example #1
Source File: block.py    From mxnet-lambda with Apache License 2.0 6 votes vote down vote up
def load_params(self, filename, ctx, allow_missing=False,
                    ignore_extra=False):
        """Load parameters from file.

        filename : str
            Path to parameter file.
        ctx : Context or list of Context
            Context(s) initialize loaded parameters on.
        allow_missing : bool, default False
            Whether to silently skip loading parameters not represents in the file.
        ignore_extra : bool, default False
            Whether to silently ignore parameters from the file that are not
            present in this Block.
        """
        self.collect_params().load(filename, ctx, allow_missing, ignore_extra,
                                   self.prefix) 
Example #2
Source File: block.py    From SNIPER-mxnet with Apache License 2.0 6 votes vote down vote up
def __setattr__(self, name, value):
        """Registers parameters."""

        if hasattr(self, name):
            existing = getattr(self, name)
            if isinstance(existing, (Parameter, Block)) and not isinstance(value, type(existing)):
                raise TypeError('Changing attribute type for {name} from {type1} to {type2}' \
                                'is not allowed.'.format(
                                    name=name, type1=type(existing), type2=type(value)))

        if isinstance(value, Block):
            self.register_child(value, name)
        elif isinstance(value, Parameter):
            assert name not in self._reg_params, \
                "Overriding Parameter attribute %s is not allowed. " \
                "If you want to share parameters between blocks, please set " \
                "'params' at Block construction instead."
            self._reg_params[name] = value

        super(Block, self).__setattr__(name, value) 
Example #3
Source File: block.py    From SNIPER-mxnet with Apache License 2.0 6 votes vote down vote up
def create(prefix, params, hint):
        """Creates prefix and params for new `Block`."""
        current = _BlockScope._current
        if current is None:
            if prefix is None:
                prefix = _name.NameManager.current.get(None, hint) + '_'
            if params is None:
                params = ParameterDict(prefix)
            else:
                params = ParameterDict(params.prefix, params)
            return prefix, params

        if prefix is None:
            count = current._counter.get(hint, 0)
            prefix = '%s%d_'%(hint, count)
            current._counter[hint] = count + 1
        if params is None:
            parent = current._block.params
            params = ParameterDict(parent.prefix+prefix, parent._shared)
        else:
            params = ParameterDict(params.prefix, params)
        return current._block.prefix+prefix, params 
Example #4
Source File: block.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def initialize(self, init=initializer.Uniform(), ctx=None, verbose=False,
                   force_reinit=False):
        """Initializes :py:class:`Parameter` s of this :py:class:`Block` and its children.
        Equivalent to ``block.collect_params().initialize(...)``

        Parameters
        ----------
        init : Initializer
            Global default Initializer to be used when :py:meth:`Parameter.init` is ``None``.
            Otherwise, :py:meth:`Parameter.init` takes precedence.
        ctx : Context or list of Context
            Keeps a copy of Parameters on one or many context(s).
        verbose : bool, default False
            Whether to verbosely print out details on initialization.
        force_reinit : bool, default False
            Whether to force re-initialization if parameter is already initialized.
        """
        self.collect_params().initialize(init, ctx, verbose, force_reinit) 
Example #5
Source File: block.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def load_params(self, filename, ctx=None, allow_missing=False,
                    ignore_extra=False):
        """[Deprecated] Please use load_parameters.

        Load parameters from file.

        filename : str
            Path to parameter file.
        ctx : Context or list of Context, default cpu()
            Context(s) to initialize loaded parameters on.
        allow_missing : bool, default False
            Whether to silently skip loading parameters not represents in the file.
        ignore_extra : bool, default False
            Whether to silently ignore parameters from the file that are not
            present in this Block.
        """
        warnings.warn("load_params is deprecated. Please use load_parameters.")
        self.load_parameters(filename, ctx, allow_missing, ignore_extra) 
Example #6
Source File: block.py    From mxnet-lambda with Apache License 2.0 6 votes vote down vote up
def create(prefix, params, hint):
        """Creates prefix and params for new `Block`."""
        current = _BlockScope._current
        if current is None:
            if prefix is None:
                prefix = _name.NameManager.current.get(None, hint) + '_'
            if params is None:
                params = ParameterDict(prefix)
            else:
                params = ParameterDict(params.prefix, params)
            return prefix, params

        if prefix is None:
            count = current._counter.get(hint, 0)
            prefix = '%s%d_'%(hint, count)
            current._counter[hint] = count + 1
        if params is None:
            parent = current._block.params
            params = ParameterDict(parent.prefix+prefix, parent._shared)
        else:
            params = ParameterDict(params.prefix, params)
        return current._block.prefix+prefix, params 
Example #7
Source File: block.py    From mxnet-lambda with Apache License 2.0 6 votes vote down vote up
def __setattr__(self, name, value):
        """Registers parameters."""

        if hasattr(self, name):
            existing = getattr(self, name)
            if isinstance(existing, (Parameter, Block)) and not isinstance(value, type(existing)):
                raise TypeError('Changing attribute type for {name} from {type1} to {type2}' \
                                'is not allowed.'.format(name=name,
                                                         type1=type(existing),
                                                         type2=type(value)))
            if isinstance(existing, Block):
                for i, c in enumerate(self._children):
                    if c is existing:
                        self._children[i] = value
            elif isinstance(value, Block):
                self.register_child(value)
        elif isinstance(value, Block):
            self.register_child(value)

        super(Block, self).__setattr__(name, value) 
Example #8
Source File: block.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def create(prefix, params, hint):
        """Creates prefix and params for new `Block`."""
        current = getattr(_BlockScope._current, "value", None)
        if current is None:
            if prefix is None:
                if not hasattr(_name.NameManager._current, "value"):
                    _name.NameManager._current.value = _name.NameManager()
                prefix = _name.NameManager._current.value.get(None, hint) + '_'
            if params is None:
                params = ParameterDict(prefix)
            else:
                params = ParameterDict(params.prefix, params)
            return prefix, params

        if prefix is None:
            count = current._counter.get(hint, 0)
            prefix = '%s%d_'%(hint, count)
            current._counter[hint] = count + 1
        if params is None:
            parent = current._block.params
            params = ParameterDict(parent.prefix+prefix, parent._shared)
        else:
            params = ParameterDict(params.prefix, params)
        return current._block.prefix+prefix, params 
Example #9
Source File: block.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def __setattr__(self, name, value):
        """Registers parameters."""

        if hasattr(self, name):
            existing = getattr(self, name)
            if isinstance(existing, (Parameter, Block)) and not isinstance(value, type(existing)):
                raise TypeError('Changing attribute type for {name} from {type1} to {type2}' \
                                'is not allowed.'.format(
                                    name=name, type1=type(existing), type2=type(value)))

        if isinstance(value, Block):
            self.register_child(value, name)
        elif isinstance(value, Parameter):
            assert name not in self._reg_params, \
                "Overriding Parameter attribute %s is not allowed. " \
                "If you want to share parameters between blocks, please set " \
                "'params' at Block construction instead."
            self._reg_params[name] = value

        super(Block, self).__setattr__(name, value) 
Example #10
Source File: block.py    From mxnet-lambda with Apache License 2.0 5 votes vote down vote up
def hybrid_forward(self, F, x, *args, **kwargs):
        """Overrides to construct symbolic graph for this `Block`.

        Parameters
        ----------
        x : Symbol or NDArray
            The first input tensor.
        *args : list of Symbol or list of NDArray
            Additional input tensors.
        """
        # pylint: disable= invalid-name
        raise NotImplementedError 
Example #11
Source File: block.py    From mxnet-lambda with Apache License 2.0 5 votes vote down vote up
def initialize(self, init=initializer.Uniform(), ctx=None, verbose=False):
        """Initializes :py:class:`Parameter` s of this :py:class:`Block` and its children.

        Equivalent to ``block.collect_params().initialize(...)``
        """
        self.collect_params().initialize(init, ctx, verbose) 
Example #12
Source File: block.py    From mxnet-lambda with Apache License 2.0 5 votes vote down vote up
def register_child(self, block):
        """Registers block as a child of self. :py:class:`Block` s assigned to self as
        attributes will be registered automatically."""
        self._children.append(block) 
Example #13
Source File: mxfusion_gluon_function.py    From MXFusion with Apache License 2.0 5 votes vote down vote up
def __init__(self, block, num_outputs, dtype=None, broadcastable=False):

        if not isinstance(block, (Block, HybridBlock)):
            raise ModelSpecificationError('The block argument must be of type Block or HybridBlock from MXNet Gluon.')

        super(MXFusionGluonFunction, self).__init__(
            func_name=block.name, dtype=dtype, broadcastable=broadcastable)
        self._gluon_block = block
        self.num_outputs = num_outputs
        self._gluon_parameters = self._create_variables_from_gluon_block(block)
        self._input_names = None
        self._input_variable_names = None
        self._output_names = [self.name + "_output_" + str(i) for i in
                              range(self.num_outputs)]
        self._gluon_parameter_names = sorted(self._gluon_parameters.keys()) 
Example #14
Source File: mxfusion_gluon_function.py    From MXFusion with Apache License 2.0 5 votes vote down vote up
def _create_variables_from_gluon_block(self, block):
        """
        Create a list of Parameter type variables from a MXNet Gluon block's parameters.
        One variable per parameter.

        :param block: The Block to create variables from.
        :rtype: {'block_variable.name' : block_variable}
        """
        params = block.collect_params()
        vs = {}
        for param in params.values():
            v = Variable(isInherited=True, shape=param.shape, initial_value=param.data())
            v.inherited_name = param.name
            vs[v.inherited_name] = v
        return vs 
Example #15
Source File: matcher.py    From cascade_rcnn_gluon with Apache License 2.0 5 votes vote down vote up
def __init__(self, matchers):
        super(CompositeMatcher, self).__init__()
        assert len(matchers) > 0, "At least one matcher required."
        for matcher in matchers:
            assert isinstance(matcher, (gluon.Block, gluon.HybridBlock))
        self._matchers = nn.HybridSequential()
        for m in matchers:
            self._matchers.add(m) 
Example #16
Source File: block.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def __repr__(self):
        s = '{name}(\n{modstr}\n)'
        modstr = '\n'.join(['  ({key}): {block}'.format(key=key,
                                                        block=_indent(block.__repr__(), 2))
                            for key, block in self.__dict__.items() if isinstance(block, Block)])
        return s.format(name=self.__class__.__name__, modstr=modstr) 
Example #17
Source File: block.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def name_scope(self):
        """Returns a name space object managing a child :py:class:`Block` and parameter
        names. Should be used within a ``with`` statement::

            with self.name_scope():
                self.dense = nn.Dense(20)

        Please refer to
        `naming tutorial <http://mxnet.incubator.apache.org/tutorials/basic/naming.html>`_
        for more info on prefix and naming.
        """
        return self._scope 
Example #18
Source File: block.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def _check_container_with_block(self):
        def _find_block_in_container(data):
            # Find whether a nested container structure contains Blocks
            if isinstance(data, (list, tuple)):
                for ele in data:
                    if _find_block_in_container(ele):
                        return True
                return False
            elif isinstance(data, dict):
                for _, v in data.items():
                    if _find_block_in_container(v):
                        return True
                return False
            elif isinstance(data, Block):
                return True
            else:
                return False
        for k, v in self.__dict__.items():
            if isinstance(v, (list, tuple, dict)) and not (k.startswith('__') or k == '_children'):
                if _find_block_in_container(v):
                    warnings.warn('"{name}" is a container with Blocks. '
                                  'Note that Blocks inside the list, tuple or dict will not be '
                                  'registered automatically. Make sure to register them using '
                                  'register_child() or switching to '
                                  'nn.Sequential/nn.HybridSequential instead. '
                                  .format(name=self.__class__.__name__ + "." + k), stacklevel=3) 
Example #19
Source File: block.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def prefix(self):
        """Prefix of this :py:class:`Block`."""
        return self._prefix 
Example #20
Source File: block.py    From mxnet-lambda with Apache License 2.0 5 votes vote down vote up
def name(self):
        """Name of this :py:class:`Block`, without '_' in the end."""
        return self._name 
Example #21
Source File: block.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def params(self):
        """Returns this :py:class:`Block`'s parameter dictionary (does not include its
        children's parameters)."""
        return self._params 
Example #22
Source File: block.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def collect_params(self, select=None):
        """Returns a :py:class:`ParameterDict` containing this :py:class:`Block` and all of its
        children's Parameters(default), also can returns the select :py:class:`ParameterDict`
        which match some given regular expressions.

        For example, collect the specified parameter in ['conv1_weight', 'conv1_bias', 'fc_weight',
        'fc_bias']::

            model.collect_params('conv1_weight|conv1_bias|fc_weight|fc_bias')

        or collect all paramters which their name ends with 'weight' or 'bias', this can be done
        using regular expressions::

            model.collect_params('.*weight|.*bias')

        Parameters
        ----------
        select : str
            regular expressions

        Returns
        -------
        The selected :py:class:`ParameterDict`
        """
        # We need to check here because blocks inside containers are not supported.
        self._check_container_with_block()
        ret = ParameterDict(self._params.prefix)
        if not select:
            ret.update(self.params)
        else:
            pattern = re.compile(select)
            ret.update({name:value for name, value in self.params.items() if pattern.match(name)})
        for cld in self._children.values():
            ret.update(cld.collect_params(select=select))
        return ret 
Example #23
Source File: block.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def load_params(self, filename, ctx=None, allow_missing=False,
                    ignore_extra=False):
        """Load parameters from file.

        filename : str
            Path to parameter file.
        ctx : Context or list of Context, default cpu()
            Context(s) initialize loaded parameters on.
        allow_missing : bool, default False
            Whether to silently skip loading parameters not represents in the file.
        ignore_extra : bool, default False
            Whether to silently ignore parameters from the file that are not
            present in this Block.
        """
        loaded = ndarray.load(filename)
        params = self._collect_params_with_prefix()
        if not loaded and not params:
            return

        if not any('.' in i for i in loaded.keys()):
            # legacy loading
            del loaded
            self.collect_params().load(
                filename, ctx, allow_missing, ignore_extra, self.prefix)
            return

        if not allow_missing:
            for name in params.keys():
                assert name in loaded, \
                    "Parameter '%s' is missing in file '%s', which contains parameters: %s. " \
                    "Set allow_missing=True to ignore missing parameters."%(
                        name, filename, _brief_print_list(loaded.keys()))
        for name in loaded:
            if not ignore_extra and name not in params:
                raise ValueError(
                    "Parameter '%s' loaded from file '%s' is not present in ParameterDict, " \
                    "which contains parameters %s. Set ignore_extra=True to ignore. "%(
                        name, filename, _brief_print_list(self._params.keys())))
            params[name]._load_init(loaded[name], ctx) 
Example #24
Source File: block.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def register_child(self, block, name=None):
        """Registers block as a child of self. :py:class:`Block` s assigned to self as
        attributes will be registered automatically."""
        if name is None:
            name = str(len(self._children))
        self._children[name] = block 
Example #25
Source File: block.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def cast(self, dtype):
        """Cast this Block to use another data type.

        Parameters
        ----------
        dtype : str or numpy.dtype
            The new data type.
        """
        for child in self._children.values():
            child.cast(dtype)
        for _, param in self.params.items():
            param.cast(dtype) 
Example #26
Source File: block.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def hybrid_forward(self, F, x, *args, **kwargs):
        """Overrides to construct symbolic graph for this `Block`.

        Parameters
        ----------
        x : Symbol or NDArray
            The first input tensor.
        *args : list of Symbol or list of NDArray
            Additional input tensors.
        """
        # pylint: disable= invalid-name
        raise NotImplementedError 
Example #27
Source File: test_gluon.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def test_parameter_sharing():
    class Net(gluon.Block):
        def __init__(self, in_units=0, **kwargs):
            super(Net, self).__init__(**kwargs)
            with self.name_scope():
                self.dense0 = nn.Dense(5, in_units=in_units)
                self.dense1 = nn.Dense(5, in_units=in_units)

        def forward(self, x):
            return self.dense1(self.dense0(x))

    net1 = Net(prefix='net1_', in_units=5)
    net2 = Net(prefix='net2_', params=net1.collect_params())
    net1.collect_params().initialize()
    net2(mx.nd.zeros((3, 5)))

    net1.save_params('net1.params')

    net3 = Net(prefix='net3_')
    net3.load_params('net1.params', mx.cpu())

    net4 = Net(prefix='net4_')
    net5 = Net(prefix='net5_', in_units=5, params=net4.collect_params())
    net4.collect_params().initialize()
    net5(mx.nd.zeros((3, 5)))

    net4.save_params('net4.params')

    net6 = Net(prefix='net6_')
    net6.load_params('net4.params', mx.cpu()) 
Example #28
Source File: test_gluon.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def test_parameter_str():
    class Net(gluon.Block):
        def __init__(self, **kwargs):
            super(Net, self).__init__(**kwargs)
            with self.name_scope():
                self.dense0 = nn.Dense(10, in_units=5, use_bias=False)

    net = Net(prefix='net1_')
    lines = str(net.collect_params()).splitlines()

    assert lines[0] == 'net1_ ('
    assert 'net1_dense0_weight' in lines[1]
    assert '(10, 5)' in lines[1]
    assert 'float32' in lines[1]
    assert lines[2] == ')' 
Example #29
Source File: test_gluon.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def test_block_attr_hidden():
    b = gluon.Block()

    # regular attributes can change types
    b.a = None
    b.a = 1 
Example #30
Source File: test_gluon.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def test_block_attr_param():
    b = gluon.Block()

    # regular variables can't change types
    b.b = gluon.Parameter()
    b.b = (2,)