repo
stringlengths
7
55
path
stringlengths
4
223
func_name
stringlengths
1
134
original_string
stringlengths
75
104k
language
stringclasses
1 value
code
stringlengths
75
104k
code_tokens
listlengths
19
28.4k
docstring
stringlengths
1
46.9k
docstring_tokens
listlengths
1
1.97k
sha
stringlengths
40
40
url
stringlengths
87
315
partition
stringclasses
1 value
apache/incubator-mxnet
python/mxnet/gluon/block.py
Block.load_params
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)
python
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)
[ "def", "load_params", "(", "self", ",", "filename", ",", "ctx", "=", "None", ",", "allow_missing", "=", "False", ",", "ignore_extra", "=", "False", ")", ":", "warnings", ".", "warn", "(", "\"load_params is deprecated. Please use load_parameters.\"", ")", "self", ".", "load_parameters", "(", "filename", ",", "ctx", ",", "allow_missing", ",", "ignore_extra", ")" ]
[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.
[ "[", "Deprecated", "]", "Please", "use", "load_parameters", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/block.py#L404-L421
train
apache/incubator-mxnet
python/mxnet/gluon/block.py
Block.register_child
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
python
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
[ "def", "register_child", "(", "self", ",", "block", ",", "name", "=", "None", ")", ":", "if", "name", "is", "None", ":", "name", "=", "str", "(", "len", "(", "self", ".", "_children", ")", ")", "self", ".", "_children", "[", "name", "]", "=", "block" ]
Registers block as a child of self. :py:class:`Block` s assigned to self as attributes will be registered automatically.
[ "Registers", "block", "as", "a", "child", "of", "self", ".", ":", "py", ":", "class", ":", "Block", "s", "assigned", "to", "self", "as", "attributes", "will", "be", "registered", "automatically", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/block.py#L423-L428
train
apache/incubator-mxnet
python/mxnet/gluon/block.py
Block.register_forward_pre_hook
def register_forward_pre_hook(self, hook): r"""Registers a forward pre-hook on the block. The hook function is called immediately before :func:`forward`. It should not modify the input or output. Parameters ---------- hook : callable The forward hook function of form `hook(block, input) -> None`. Returns ------- :class:`mxnet.gluon.utils.HookHandle` """ handle = HookHandle() handle.attach(self._forward_pre_hooks, hook) return handle
python
def register_forward_pre_hook(self, hook): r"""Registers a forward pre-hook on the block. The hook function is called immediately before :func:`forward`. It should not modify the input or output. Parameters ---------- hook : callable The forward hook function of form `hook(block, input) -> None`. Returns ------- :class:`mxnet.gluon.utils.HookHandle` """ handle = HookHandle() handle.attach(self._forward_pre_hooks, hook) return handle
[ "def", "register_forward_pre_hook", "(", "self", ",", "hook", ")", ":", "handle", "=", "HookHandle", "(", ")", "handle", ".", "attach", "(", "self", ".", "_forward_pre_hooks", ",", "hook", ")", "return", "handle" ]
r"""Registers a forward pre-hook on the block. The hook function is called immediately before :func:`forward`. It should not modify the input or output. Parameters ---------- hook : callable The forward hook function of form `hook(block, input) -> None`. Returns ------- :class:`mxnet.gluon.utils.HookHandle`
[ "r", "Registers", "a", "forward", "pre", "-", "hook", "on", "the", "block", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/block.py#L430-L447
train
apache/incubator-mxnet
python/mxnet/gluon/block.py
Block.register_forward_hook
def register_forward_hook(self, hook): r"""Registers a forward hook on the block. The hook function is called immediately after :func:`forward`. It should not modify the input or output. Parameters ---------- hook : callable The forward hook function of form `hook(block, input, output) -> None`. Returns ------- :class:`mxnet.gluon.utils.HookHandle` """ handle = HookHandle() handle.attach(self._forward_hooks, hook) return handle
python
def register_forward_hook(self, hook): r"""Registers a forward hook on the block. The hook function is called immediately after :func:`forward`. It should not modify the input or output. Parameters ---------- hook : callable The forward hook function of form `hook(block, input, output) -> None`. Returns ------- :class:`mxnet.gluon.utils.HookHandle` """ handle = HookHandle() handle.attach(self._forward_hooks, hook) return handle
[ "def", "register_forward_hook", "(", "self", ",", "hook", ")", ":", "handle", "=", "HookHandle", "(", ")", "handle", ".", "attach", "(", "self", ".", "_forward_hooks", ",", "hook", ")", "return", "handle" ]
r"""Registers a forward hook on the block. The hook function is called immediately after :func:`forward`. It should not modify the input or output. Parameters ---------- hook : callable The forward hook function of form `hook(block, input, output) -> None`. Returns ------- :class:`mxnet.gluon.utils.HookHandle`
[ "r", "Registers", "a", "forward", "hook", "on", "the", "block", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/block.py#L449-L466
train
apache/incubator-mxnet
python/mxnet/gluon/block.py
Block.apply
def apply(self, fn): r"""Applies ``fn`` recursively to every child block as well as self. Parameters ---------- fn : callable Function to be applied to each submodule, of form `fn(block)`. Returns ------- this block """ for cld in self._children.values(): cld.apply(fn) fn(self) return self
python
def apply(self, fn): r"""Applies ``fn`` recursively to every child block as well as self. Parameters ---------- fn : callable Function to be applied to each submodule, of form `fn(block)`. Returns ------- this block """ for cld in self._children.values(): cld.apply(fn) fn(self) return self
[ "def", "apply", "(", "self", ",", "fn", ")", ":", "for", "cld", "in", "self", ".", "_children", ".", "values", "(", ")", ":", "cld", ".", "apply", "(", "fn", ")", "fn", "(", "self", ")", "return", "self" ]
r"""Applies ``fn`` recursively to every child block as well as self. Parameters ---------- fn : callable Function to be applied to each submodule, of form `fn(block)`. Returns ------- this block
[ "r", "Applies", "fn", "recursively", "to", "every", "child", "block", "as", "well", "as", "self", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/block.py#L468-L483
train
apache/incubator-mxnet
python/mxnet/gluon/block.py
Block.initialize
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)
python
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)
[ "def", "initialize", "(", "self", ",", "init", "=", "initializer", ".", "Uniform", "(", ")", ",", "ctx", "=", "None", ",", "verbose", "=", "False", ",", "force_reinit", "=", "False", ")", ":", "self", ".", "collect_params", "(", ")", ".", "initialize", "(", "init", ",", "ctx", ",", "verbose", ",", "force_reinit", ")" ]
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.
[ "Initializes", ":", "py", ":", "class", ":", "Parameter", "s", "of", "this", ":", "py", ":", "class", ":", "Block", "and", "its", "children", ".", "Equivalent", "to", "block", ".", "collect_params", "()", ".", "initialize", "(", "...", ")" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/block.py#L485-L502
train
apache/incubator-mxnet
python/mxnet/gluon/block.py
Block.hybridize
def hybridize(self, active=True, **kwargs): """Activates or deactivates :py:class:`HybridBlock` s recursively. Has no effect on non-hybrid children. Parameters ---------- active : bool, default True Whether to turn hybrid on or off. static_alloc : bool, default False Statically allocate memory to improve speed. Memory usage may increase. static_shape : bool, default False Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower. """ for cld in self._children.values(): cld.hybridize(active, **kwargs)
python
def hybridize(self, active=True, **kwargs): """Activates or deactivates :py:class:`HybridBlock` s recursively. Has no effect on non-hybrid children. Parameters ---------- active : bool, default True Whether to turn hybrid on or off. static_alloc : bool, default False Statically allocate memory to improve speed. Memory usage may increase. static_shape : bool, default False Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower. """ for cld in self._children.values(): cld.hybridize(active, **kwargs)
[ "def", "hybridize", "(", "self", ",", "active", "=", "True", ",", "*", "*", "kwargs", ")", ":", "for", "cld", "in", "self", ".", "_children", ".", "values", "(", ")", ":", "cld", ".", "hybridize", "(", "active", ",", "*", "*", "kwargs", ")" ]
Activates or deactivates :py:class:`HybridBlock` s recursively. Has no effect on non-hybrid children. Parameters ---------- active : bool, default True Whether to turn hybrid on or off. static_alloc : bool, default False Statically allocate memory to improve speed. Memory usage may increase. static_shape : bool, default False Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
[ "Activates", "or", "deactivates", ":", "py", ":", "class", ":", "HybridBlock", "s", "recursively", ".", "Has", "no", "effect", "on", "non", "-", "hybrid", "children", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/block.py#L504-L520
train
apache/incubator-mxnet
python/mxnet/gluon/block.py
Block.cast
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)
python
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)
[ "def", "cast", "(", "self", ",", "dtype", ")", ":", "for", "child", "in", "self", ".", "_children", ".", "values", "(", ")", ":", "child", ".", "cast", "(", "dtype", ")", "for", "_", ",", "param", "in", "self", ".", "params", ".", "items", "(", ")", ":", "param", ".", "cast", "(", "dtype", ")" ]
Cast this Block to use another data type. Parameters ---------- dtype : str or numpy.dtype The new data type.
[ "Cast", "this", "Block", "to", "use", "another", "data", "type", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/block.py#L522-L533
train
apache/incubator-mxnet
python/mxnet/gluon/block.py
Block.summary
def summary(self, *inputs): """Print the summary of the model's output and parameters. The network must have been initialized, and must not have been hybridized. Parameters ---------- inputs : object Any input that the model supports. For any tensor in the input, only :class:`mxnet.ndarray.NDArray` is supported. """ summary = OrderedDict() seen = set() hooks = [] def _get_shape_str(args): def flatten(args): if not isinstance(args, (list, tuple)): return [args], int(0) flat = [] fmts = [] for i in args: arg, fmt = flatten(i) flat.extend(arg) fmts.append(fmt) return flat, fmts def regroup(args, fmt): if isinstance(fmt, int): if fmt == 0: return args[0], args[1:] return args[:fmt], args[fmt:] ret = [] for i in fmt: res, args = regroup(args, i) ret.append(res) return ret, args flat_args, fmts = flatten(args) flat_arg_shapes = [x.shape if isinstance(x, ndarray.NDArray) else x for x in flat_args] shapes = regroup(flat_arg_shapes, fmts)[0] if isinstance(shapes, list): shape_str = str(shapes)[1:-1] else: shape_str = str(shapes) return shape_str.replace('L', '') def _register_summary_hook(block): assert not isinstance(block, HybridBlock) or not block._active, \ '"{}" must not be hybridized to print summary.'.format(block.name) def _summary_hook(block, _, outputs): class_name = block.__class__.__name__ block_idx = len(summary) - 1 m_key = '%s-%i' % (class_name, block_idx+1) summary[m_key] = OrderedDict() summary[m_key]['output_shape'] = _get_shape_str(outputs) params = 0 summary[m_key]['trainable'] = 0 summary[m_key]['shared'] = 0 for p in block.params.values(): params += p.data().size summary[m_key]['trainable'] += 0 if p.grad_req == 'null' else p.data().size if p in seen: summary[m_key]['shared'] += p.data().size else: seen.add(p) summary[m_key]['n_params'] = params from .nn.basic_layers import Sequential, HybridSequential if not isinstance(block, (Sequential, HybridSequential)): hooks.append(block.register_forward_hook(_summary_hook)) summary['Input'] = OrderedDict() summary['Input']['output_shape'] = _get_shape_str(inputs) summary['Input']['n_params'] = 0 summary['Input']['trainable'] = 0 summary['Input']['shared'] = 0 try: self.apply(_register_summary_hook) self(*inputs) line_format = '{:>20} {:>42} {:>15}' print('-'*80) print(line_format.format('Layer (type)', 'Output Shape', 'Param #')) print('='*80) total_params = 0 trainable_params = 0 shared_params = 0 for layer in summary: print(line_format.format(layer, str(summary[layer]['output_shape']), summary[layer]['n_params'])) total_params += summary[layer]['n_params'] trainable_params += summary[layer]['trainable'] shared_params += summary[layer]['shared'] print('='*80) print('Parameters in forward computation graph, duplicate included') print(' Total params: ' + str(total_params)) print(' Trainable params: ' + str(trainable_params)) print(' Non-trainable params: ' + str(total_params - trainable_params)) print('Shared params in forward computation graph: ' + str(shared_params)) print('Unique parameters in model: ' + str(total_params - shared_params)) print('-'*80) finally: for h in hooks: h.detach()
python
def summary(self, *inputs): """Print the summary of the model's output and parameters. The network must have been initialized, and must not have been hybridized. Parameters ---------- inputs : object Any input that the model supports. For any tensor in the input, only :class:`mxnet.ndarray.NDArray` is supported. """ summary = OrderedDict() seen = set() hooks = [] def _get_shape_str(args): def flatten(args): if not isinstance(args, (list, tuple)): return [args], int(0) flat = [] fmts = [] for i in args: arg, fmt = flatten(i) flat.extend(arg) fmts.append(fmt) return flat, fmts def regroup(args, fmt): if isinstance(fmt, int): if fmt == 0: return args[0], args[1:] return args[:fmt], args[fmt:] ret = [] for i in fmt: res, args = regroup(args, i) ret.append(res) return ret, args flat_args, fmts = flatten(args) flat_arg_shapes = [x.shape if isinstance(x, ndarray.NDArray) else x for x in flat_args] shapes = regroup(flat_arg_shapes, fmts)[0] if isinstance(shapes, list): shape_str = str(shapes)[1:-1] else: shape_str = str(shapes) return shape_str.replace('L', '') def _register_summary_hook(block): assert not isinstance(block, HybridBlock) or not block._active, \ '"{}" must not be hybridized to print summary.'.format(block.name) def _summary_hook(block, _, outputs): class_name = block.__class__.__name__ block_idx = len(summary) - 1 m_key = '%s-%i' % (class_name, block_idx+1) summary[m_key] = OrderedDict() summary[m_key]['output_shape'] = _get_shape_str(outputs) params = 0 summary[m_key]['trainable'] = 0 summary[m_key]['shared'] = 0 for p in block.params.values(): params += p.data().size summary[m_key]['trainable'] += 0 if p.grad_req == 'null' else p.data().size if p in seen: summary[m_key]['shared'] += p.data().size else: seen.add(p) summary[m_key]['n_params'] = params from .nn.basic_layers import Sequential, HybridSequential if not isinstance(block, (Sequential, HybridSequential)): hooks.append(block.register_forward_hook(_summary_hook)) summary['Input'] = OrderedDict() summary['Input']['output_shape'] = _get_shape_str(inputs) summary['Input']['n_params'] = 0 summary['Input']['trainable'] = 0 summary['Input']['shared'] = 0 try: self.apply(_register_summary_hook) self(*inputs) line_format = '{:>20} {:>42} {:>15}' print('-'*80) print(line_format.format('Layer (type)', 'Output Shape', 'Param #')) print('='*80) total_params = 0 trainable_params = 0 shared_params = 0 for layer in summary: print(line_format.format(layer, str(summary[layer]['output_shape']), summary[layer]['n_params'])) total_params += summary[layer]['n_params'] trainable_params += summary[layer]['trainable'] shared_params += summary[layer]['shared'] print('='*80) print('Parameters in forward computation graph, duplicate included') print(' Total params: ' + str(total_params)) print(' Trainable params: ' + str(trainable_params)) print(' Non-trainable params: ' + str(total_params - trainable_params)) print('Shared params in forward computation graph: ' + str(shared_params)) print('Unique parameters in model: ' + str(total_params - shared_params)) print('-'*80) finally: for h in hooks: h.detach()
[ "def", "summary", "(", "self", ",", "*", "inputs", ")", ":", "summary", "=", "OrderedDict", "(", ")", "seen", "=", "set", "(", ")", "hooks", "=", "[", "]", "def", "_get_shape_str", "(", "args", ")", ":", "def", "flatten", "(", "args", ")", ":", "if", "not", "isinstance", "(", "args", ",", "(", "list", ",", "tuple", ")", ")", ":", "return", "[", "args", "]", ",", "int", "(", "0", ")", "flat", "=", "[", "]", "fmts", "=", "[", "]", "for", "i", "in", "args", ":", "arg", ",", "fmt", "=", "flatten", "(", "i", ")", "flat", ".", "extend", "(", "arg", ")", "fmts", ".", "append", "(", "fmt", ")", "return", "flat", ",", "fmts", "def", "regroup", "(", "args", ",", "fmt", ")", ":", "if", "isinstance", "(", "fmt", ",", "int", ")", ":", "if", "fmt", "==", "0", ":", "return", "args", "[", "0", "]", ",", "args", "[", "1", ":", "]", "return", "args", "[", ":", "fmt", "]", ",", "args", "[", "fmt", ":", "]", "ret", "=", "[", "]", "for", "i", "in", "fmt", ":", "res", ",", "args", "=", "regroup", "(", "args", ",", "i", ")", "ret", ".", "append", "(", "res", ")", "return", "ret", ",", "args", "flat_args", ",", "fmts", "=", "flatten", "(", "args", ")", "flat_arg_shapes", "=", "[", "x", ".", "shape", "if", "isinstance", "(", "x", ",", "ndarray", ".", "NDArray", ")", "else", "x", "for", "x", "in", "flat_args", "]", "shapes", "=", "regroup", "(", "flat_arg_shapes", ",", "fmts", ")", "[", "0", "]", "if", "isinstance", "(", "shapes", ",", "list", ")", ":", "shape_str", "=", "str", "(", "shapes", ")", "[", "1", ":", "-", "1", "]", "else", ":", "shape_str", "=", "str", "(", "shapes", ")", "return", "shape_str", ".", "replace", "(", "'L'", ",", "''", ")", "def", "_register_summary_hook", "(", "block", ")", ":", "assert", "not", "isinstance", "(", "block", ",", "HybridBlock", ")", "or", "not", "block", ".", "_active", ",", "'\"{}\" must not be hybridized to print summary.'", ".", "format", "(", "block", ".", "name", ")", "def", "_summary_hook", "(", "block", ",", "_", ",", "outputs", ")", ":", "class_name", "=", "block", ".", "__class__", ".", "__name__", "block_idx", "=", "len", "(", "summary", ")", "-", "1", "m_key", "=", "'%s-%i'", "%", "(", "class_name", ",", "block_idx", "+", "1", ")", "summary", "[", "m_key", "]", "=", "OrderedDict", "(", ")", "summary", "[", "m_key", "]", "[", "'output_shape'", "]", "=", "_get_shape_str", "(", "outputs", ")", "params", "=", "0", "summary", "[", "m_key", "]", "[", "'trainable'", "]", "=", "0", "summary", "[", "m_key", "]", "[", "'shared'", "]", "=", "0", "for", "p", "in", "block", ".", "params", ".", "values", "(", ")", ":", "params", "+=", "p", ".", "data", "(", ")", ".", "size", "summary", "[", "m_key", "]", "[", "'trainable'", "]", "+=", "0", "if", "p", ".", "grad_req", "==", "'null'", "else", "p", ".", "data", "(", ")", ".", "size", "if", "p", "in", "seen", ":", "summary", "[", "m_key", "]", "[", "'shared'", "]", "+=", "p", ".", "data", "(", ")", ".", "size", "else", ":", "seen", ".", "add", "(", "p", ")", "summary", "[", "m_key", "]", "[", "'n_params'", "]", "=", "params", "from", ".", "nn", ".", "basic_layers", "import", "Sequential", ",", "HybridSequential", "if", "not", "isinstance", "(", "block", ",", "(", "Sequential", ",", "HybridSequential", ")", ")", ":", "hooks", ".", "append", "(", "block", ".", "register_forward_hook", "(", "_summary_hook", ")", ")", "summary", "[", "'Input'", "]", "=", "OrderedDict", "(", ")", "summary", "[", "'Input'", "]", "[", "'output_shape'", "]", "=", "_get_shape_str", "(", "inputs", ")", "summary", "[", "'Input'", "]", "[", "'n_params'", "]", "=", "0", "summary", "[", "'Input'", "]", "[", "'trainable'", "]", "=", "0", "summary", "[", "'Input'", "]", "[", "'shared'", "]", "=", "0", "try", ":", "self", ".", "apply", "(", "_register_summary_hook", ")", "self", "(", "*", "inputs", ")", "line_format", "=", "'{:>20} {:>42} {:>15}'", "print", "(", "'-'", "*", "80", ")", "print", "(", "line_format", ".", "format", "(", "'Layer (type)'", ",", "'Output Shape'", ",", "'Param #'", ")", ")", "print", "(", "'='", "*", "80", ")", "total_params", "=", "0", "trainable_params", "=", "0", "shared_params", "=", "0", "for", "layer", "in", "summary", ":", "print", "(", "line_format", ".", "format", "(", "layer", ",", "str", "(", "summary", "[", "layer", "]", "[", "'output_shape'", "]", ")", ",", "summary", "[", "layer", "]", "[", "'n_params'", "]", ")", ")", "total_params", "+=", "summary", "[", "layer", "]", "[", "'n_params'", "]", "trainable_params", "+=", "summary", "[", "layer", "]", "[", "'trainable'", "]", "shared_params", "+=", "summary", "[", "layer", "]", "[", "'shared'", "]", "print", "(", "'='", "*", "80", ")", "print", "(", "'Parameters in forward computation graph, duplicate included'", ")", "print", "(", "' Total params: '", "+", "str", "(", "total_params", ")", ")", "print", "(", "' Trainable params: '", "+", "str", "(", "trainable_params", ")", ")", "print", "(", "' Non-trainable params: '", "+", "str", "(", "total_params", "-", "trainable_params", ")", ")", "print", "(", "'Shared params in forward computation graph: '", "+", "str", "(", "shared_params", ")", ")", "print", "(", "'Unique parameters in model: '", "+", "str", "(", "total_params", "-", "shared_params", ")", ")", "print", "(", "'-'", "*", "80", ")", "finally", ":", "for", "h", "in", "hooks", ":", "h", ".", "detach", "(", ")" ]
Print the summary of the model's output and parameters. The network must have been initialized, and must not have been hybridized. Parameters ---------- inputs : object Any input that the model supports. For any tensor in the input, only :class:`mxnet.ndarray.NDArray` is supported.
[ "Print", "the", "summary", "of", "the", "model", "s", "output", "and", "parameters", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/block.py#L559-L668
train
apache/incubator-mxnet
python/mxnet/gluon/block.py
HybridBlock._infer_attrs
def _infer_attrs(self, infer_fn, attr, *args): """Generic infer attributes.""" inputs, out = self._get_graph(*args) args, _ = _flatten(args, "input") with warnings.catch_warnings(record=True) as w: arg_attrs, _, aux_attrs = getattr(out, infer_fn)( **{i.name: getattr(j, attr) for i, j in zip(inputs, args)}) if arg_attrs is None: raise ValueError(w[0].message) sdict = {i: j for i, j in zip(out.list_arguments(), arg_attrs)} sdict.update({name : attr for name, attr in \ zip(out.list_auxiliary_states(), aux_attrs)}) for i in self.collect_params().values(): setattr(i, attr, sdict[i.name])
python
def _infer_attrs(self, infer_fn, attr, *args): """Generic infer attributes.""" inputs, out = self._get_graph(*args) args, _ = _flatten(args, "input") with warnings.catch_warnings(record=True) as w: arg_attrs, _, aux_attrs = getattr(out, infer_fn)( **{i.name: getattr(j, attr) for i, j in zip(inputs, args)}) if arg_attrs is None: raise ValueError(w[0].message) sdict = {i: j for i, j in zip(out.list_arguments(), arg_attrs)} sdict.update({name : attr for name, attr in \ zip(out.list_auxiliary_states(), aux_attrs)}) for i in self.collect_params().values(): setattr(i, attr, sdict[i.name])
[ "def", "_infer_attrs", "(", "self", ",", "infer_fn", ",", "attr", ",", "*", "args", ")", ":", "inputs", ",", "out", "=", "self", ".", "_get_graph", "(", "*", "args", ")", "args", ",", "_", "=", "_flatten", "(", "args", ",", "\"input\"", ")", "with", "warnings", ".", "catch_warnings", "(", "record", "=", "True", ")", "as", "w", ":", "arg_attrs", ",", "_", ",", "aux_attrs", "=", "getattr", "(", "out", ",", "infer_fn", ")", "(", "*", "*", "{", "i", ".", "name", ":", "getattr", "(", "j", ",", "attr", ")", "for", "i", ",", "j", "in", "zip", "(", "inputs", ",", "args", ")", "}", ")", "if", "arg_attrs", "is", "None", ":", "raise", "ValueError", "(", "w", "[", "0", "]", ".", "message", ")", "sdict", "=", "{", "i", ":", "j", "for", "i", ",", "j", "in", "zip", "(", "out", ".", "list_arguments", "(", ")", ",", "arg_attrs", ")", "}", "sdict", ".", "update", "(", "{", "name", ":", "attr", "for", "name", ",", "attr", "in", "zip", "(", "out", ".", "list_auxiliary_states", "(", ")", ",", "aux_attrs", ")", "}", ")", "for", "i", "in", "self", ".", "collect_params", "(", ")", ".", "values", "(", ")", ":", "setattr", "(", "i", ",", "attr", ",", "sdict", "[", "i", ".", "name", "]", ")" ]
Generic infer attributes.
[ "Generic", "infer", "attributes", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/block.py#L845-L858
train
apache/incubator-mxnet
python/mxnet/gluon/block.py
HybridBlock.export
def export(self, path, epoch=0): """Export HybridBlock to json format that can be loaded by `SymbolBlock.imports`, `mxnet.mod.Module` or the C++ interface. .. note:: When there are only one input, it will have name `data`. When there Are more than one inputs, they will be named as `data0`, `data1`, etc. Parameters ---------- path : str Path to save model. Two files `path-symbol.json` and `path-xxxx.params` will be created, where xxxx is the 4 digits epoch number. epoch : int Epoch number of saved model. """ if not self._cached_graph: raise RuntimeError( "Please first call block.hybridize() and then run forward with " "this block at least once before calling export.") sym = self._cached_graph[1] sym.save('%s-symbol.json'%path) arg_names = set(sym.list_arguments()) aux_names = set(sym.list_auxiliary_states()) arg_dict = {} for name, param in self.collect_params().items(): if name in arg_names: arg_dict['arg:%s'%name] = param._reduce() else: assert name in aux_names arg_dict['aux:%s'%name] = param._reduce() ndarray.save('%s-%04d.params'%(path, epoch), arg_dict)
python
def export(self, path, epoch=0): """Export HybridBlock to json format that can be loaded by `SymbolBlock.imports`, `mxnet.mod.Module` or the C++ interface. .. note:: When there are only one input, it will have name `data`. When there Are more than one inputs, they will be named as `data0`, `data1`, etc. Parameters ---------- path : str Path to save model. Two files `path-symbol.json` and `path-xxxx.params` will be created, where xxxx is the 4 digits epoch number. epoch : int Epoch number of saved model. """ if not self._cached_graph: raise RuntimeError( "Please first call block.hybridize() and then run forward with " "this block at least once before calling export.") sym = self._cached_graph[1] sym.save('%s-symbol.json'%path) arg_names = set(sym.list_arguments()) aux_names = set(sym.list_auxiliary_states()) arg_dict = {} for name, param in self.collect_params().items(): if name in arg_names: arg_dict['arg:%s'%name] = param._reduce() else: assert name in aux_names arg_dict['aux:%s'%name] = param._reduce() ndarray.save('%s-%04d.params'%(path, epoch), arg_dict)
[ "def", "export", "(", "self", ",", "path", ",", "epoch", "=", "0", ")", ":", "if", "not", "self", ".", "_cached_graph", ":", "raise", "RuntimeError", "(", "\"Please first call block.hybridize() and then run forward with \"", "\"this block at least once before calling export.\"", ")", "sym", "=", "self", ".", "_cached_graph", "[", "1", "]", "sym", ".", "save", "(", "'%s-symbol.json'", "%", "path", ")", "arg_names", "=", "set", "(", "sym", ".", "list_arguments", "(", ")", ")", "aux_names", "=", "set", "(", "sym", ".", "list_auxiliary_states", "(", ")", ")", "arg_dict", "=", "{", "}", "for", "name", ",", "param", "in", "self", ".", "collect_params", "(", ")", ".", "items", "(", ")", ":", "if", "name", "in", "arg_names", ":", "arg_dict", "[", "'arg:%s'", "%", "name", "]", "=", "param", ".", "_reduce", "(", ")", "else", ":", "assert", "name", "in", "aux_names", "arg_dict", "[", "'aux:%s'", "%", "name", "]", "=", "param", ".", "_reduce", "(", ")", "ndarray", ".", "save", "(", "'%s-%04d.params'", "%", "(", "path", ",", "epoch", ")", ",", "arg_dict", ")" ]
Export HybridBlock to json format that can be loaded by `SymbolBlock.imports`, `mxnet.mod.Module` or the C++ interface. .. note:: When there are only one input, it will have name `data`. When there Are more than one inputs, they will be named as `data0`, `data1`, etc. Parameters ---------- path : str Path to save model. Two files `path-symbol.json` and `path-xxxx.params` will be created, where xxxx is the 4 digits epoch number. epoch : int Epoch number of saved model.
[ "Export", "HybridBlock", "to", "json", "format", "that", "can", "be", "loaded", "by", "SymbolBlock", ".", "imports", "mxnet", ".", "mod", ".", "Module", "or", "the", "C", "++", "interface", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/block.py#L868-L899
train
apache/incubator-mxnet
python/mxnet/gluon/block.py
HybridBlock.forward
def forward(self, x, *args): """Defines the forward computation. Arguments can be either :py:class:`NDArray` or :py:class:`Symbol`.""" if isinstance(x, NDArray): with x.context as ctx: if self._active: return self._call_cached_op(x, *args) try: params = {i: j.data(ctx) for i, j in self._reg_params.items()} except DeferredInitializationError: self._deferred_infer_shape(x, *args) for _, i in self.params.items(): i._finish_deferred_init() params = {i: j.data(ctx) for i, j in self._reg_params.items()} return self.hybrid_forward(ndarray, x, *args, **params) assert isinstance(x, Symbol), \ "HybridBlock requires the first argument to forward be either " \ "Symbol or NDArray, but got %s"%type(x) params = {i: j.var() for i, j in self._reg_params.items()} with self.name_scope(): return self.hybrid_forward(symbol, x, *args, **params)
python
def forward(self, x, *args): """Defines the forward computation. Arguments can be either :py:class:`NDArray` or :py:class:`Symbol`.""" if isinstance(x, NDArray): with x.context as ctx: if self._active: return self._call_cached_op(x, *args) try: params = {i: j.data(ctx) for i, j in self._reg_params.items()} except DeferredInitializationError: self._deferred_infer_shape(x, *args) for _, i in self.params.items(): i._finish_deferred_init() params = {i: j.data(ctx) for i, j in self._reg_params.items()} return self.hybrid_forward(ndarray, x, *args, **params) assert isinstance(x, Symbol), \ "HybridBlock requires the first argument to forward be either " \ "Symbol or NDArray, but got %s"%type(x) params = {i: j.var() for i, j in self._reg_params.items()} with self.name_scope(): return self.hybrid_forward(symbol, x, *args, **params)
[ "def", "forward", "(", "self", ",", "x", ",", "*", "args", ")", ":", "if", "isinstance", "(", "x", ",", "NDArray", ")", ":", "with", "x", ".", "context", "as", "ctx", ":", "if", "self", ".", "_active", ":", "return", "self", ".", "_call_cached_op", "(", "x", ",", "*", "args", ")", "try", ":", "params", "=", "{", "i", ":", "j", ".", "data", "(", "ctx", ")", "for", "i", ",", "j", "in", "self", ".", "_reg_params", ".", "items", "(", ")", "}", "except", "DeferredInitializationError", ":", "self", ".", "_deferred_infer_shape", "(", "x", ",", "*", "args", ")", "for", "_", ",", "i", "in", "self", ".", "params", ".", "items", "(", ")", ":", "i", ".", "_finish_deferred_init", "(", ")", "params", "=", "{", "i", ":", "j", ".", "data", "(", "ctx", ")", "for", "i", ",", "j", "in", "self", ".", "_reg_params", ".", "items", "(", ")", "}", "return", "self", ".", "hybrid_forward", "(", "ndarray", ",", "x", ",", "*", "args", ",", "*", "*", "params", ")", "assert", "isinstance", "(", "x", ",", "Symbol", ")", ",", "\"HybridBlock requires the first argument to forward be either \"", "\"Symbol or NDArray, but got %s\"", "%", "type", "(", "x", ")", "params", "=", "{", "i", ":", "j", ".", "var", "(", ")", "for", "i", ",", "j", "in", "self", ".", "_reg_params", ".", "items", "(", ")", "}", "with", "self", ".", "name_scope", "(", ")", ":", "return", "self", ".", "hybrid_forward", "(", "symbol", ",", "x", ",", "*", "args", ",", "*", "*", "params", ")" ]
Defines the forward computation. Arguments can be either :py:class:`NDArray` or :py:class:`Symbol`.
[ "Defines", "the", "forward", "computation", ".", "Arguments", "can", "be", "either", ":", "py", ":", "class", ":", "NDArray", "or", ":", "py", ":", "class", ":", "Symbol", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/block.py#L901-L924
train
apache/incubator-mxnet
python/mxnet/gluon/block.py
SymbolBlock.imports
def imports(symbol_file, input_names, param_file=None, ctx=None): """Import model previously saved by `HybridBlock.export` or `Module.save_checkpoint` as a SymbolBlock for use in Gluon. Parameters ---------- symbol_file : str Path to symbol file. input_names : list of str List of input variable names param_file : str, optional Path to parameter file. ctx : Context, default None The context to initialize SymbolBlock on. Returns ------- SymbolBlock SymbolBlock loaded from symbol and parameter files. Examples -------- >>> net1 = gluon.model_zoo.vision.resnet18_v1( ... prefix='resnet', pretrained=True) >>> net1.hybridize() >>> x = mx.nd.random.normal(shape=(1, 3, 32, 32)) >>> out1 = net1(x) >>> net1.export('net1', epoch=1) >>> >>> net2 = gluon.SymbolBlock.imports( ... 'net1-symbol.json', ['data'], 'net1-0001.params') >>> out2 = net2(x) """ sym = symbol.load(symbol_file) if isinstance(input_names, str): input_names = [input_names] inputs = [symbol.var(i) for i in input_names] ret = SymbolBlock(sym, inputs) if param_file is not None: ret.collect_params().load(param_file, ctx=ctx) return ret
python
def imports(symbol_file, input_names, param_file=None, ctx=None): """Import model previously saved by `HybridBlock.export` or `Module.save_checkpoint` as a SymbolBlock for use in Gluon. Parameters ---------- symbol_file : str Path to symbol file. input_names : list of str List of input variable names param_file : str, optional Path to parameter file. ctx : Context, default None The context to initialize SymbolBlock on. Returns ------- SymbolBlock SymbolBlock loaded from symbol and parameter files. Examples -------- >>> net1 = gluon.model_zoo.vision.resnet18_v1( ... prefix='resnet', pretrained=True) >>> net1.hybridize() >>> x = mx.nd.random.normal(shape=(1, 3, 32, 32)) >>> out1 = net1(x) >>> net1.export('net1', epoch=1) >>> >>> net2 = gluon.SymbolBlock.imports( ... 'net1-symbol.json', ['data'], 'net1-0001.params') >>> out2 = net2(x) """ sym = symbol.load(symbol_file) if isinstance(input_names, str): input_names = [input_names] inputs = [symbol.var(i) for i in input_names] ret = SymbolBlock(sym, inputs) if param_file is not None: ret.collect_params().load(param_file, ctx=ctx) return ret
[ "def", "imports", "(", "symbol_file", ",", "input_names", ",", "param_file", "=", "None", ",", "ctx", "=", "None", ")", ":", "sym", "=", "symbol", ".", "load", "(", "symbol_file", ")", "if", "isinstance", "(", "input_names", ",", "str", ")", ":", "input_names", "=", "[", "input_names", "]", "inputs", "=", "[", "symbol", ".", "var", "(", "i", ")", "for", "i", "in", "input_names", "]", "ret", "=", "SymbolBlock", "(", "sym", ",", "inputs", ")", "if", "param_file", "is", "not", "None", ":", "ret", ".", "collect_params", "(", ")", ".", "load", "(", "param_file", ",", "ctx", "=", "ctx", ")", "return", "ret" ]
Import model previously saved by `HybridBlock.export` or `Module.save_checkpoint` as a SymbolBlock for use in Gluon. Parameters ---------- symbol_file : str Path to symbol file. input_names : list of str List of input variable names param_file : str, optional Path to parameter file. ctx : Context, default None The context to initialize SymbolBlock on. Returns ------- SymbolBlock SymbolBlock loaded from symbol and parameter files. Examples -------- >>> net1 = gluon.model_zoo.vision.resnet18_v1( ... prefix='resnet', pretrained=True) >>> net1.hybridize() >>> x = mx.nd.random.normal(shape=(1, 3, 32, 32)) >>> out1 = net1(x) >>> net1.export('net1', epoch=1) >>> >>> net2 = gluon.SymbolBlock.imports( ... 'net1-symbol.json', ['data'], 'net1-0001.params') >>> out2 = net2(x)
[ "Import", "model", "previously", "saved", "by", "HybridBlock", ".", "export", "or", "Module", ".", "save_checkpoint", "as", "a", "SymbolBlock", "for", "use", "in", "Gluon", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/block.py#L985-L1025
train
apache/incubator-mxnet
example/svrg_module/linear_regression/common.py
calc_expectation
def calc_expectation(grad_dict, num_batches): """Calculates the expectation of the gradients per epoch for each parameter w.r.t number of batches Parameters ---------- grad_dict: dict dictionary that maps parameter name to gradients in the mod executor group num_batches: int number of batches Returns ---------- grad_dict: dict dictionary with new keys mapping to gradients expectations """ for key in grad_dict.keys(): grad_dict[str.format(key+"_expectation")] = mx.ndarray.sum(grad_dict[key], axis=0) / num_batches return grad_dict
python
def calc_expectation(grad_dict, num_batches): """Calculates the expectation of the gradients per epoch for each parameter w.r.t number of batches Parameters ---------- grad_dict: dict dictionary that maps parameter name to gradients in the mod executor group num_batches: int number of batches Returns ---------- grad_dict: dict dictionary with new keys mapping to gradients expectations """ for key in grad_dict.keys(): grad_dict[str.format(key+"_expectation")] = mx.ndarray.sum(grad_dict[key], axis=0) / num_batches return grad_dict
[ "def", "calc_expectation", "(", "grad_dict", ",", "num_batches", ")", ":", "for", "key", "in", "grad_dict", ".", "keys", "(", ")", ":", "grad_dict", "[", "str", ".", "format", "(", "key", "+", "\"_expectation\"", ")", "]", "=", "mx", ".", "ndarray", ".", "sum", "(", "grad_dict", "[", "key", "]", ",", "axis", "=", "0", ")", "/", "num_batches", "return", "grad_dict" ]
Calculates the expectation of the gradients per epoch for each parameter w.r.t number of batches Parameters ---------- grad_dict: dict dictionary that maps parameter name to gradients in the mod executor group num_batches: int number of batches Returns ---------- grad_dict: dict dictionary with new keys mapping to gradients expectations
[ "Calculates", "the", "expectation", "of", "the", "gradients", "per", "epoch", "for", "each", "parameter", "w", ".", "r", ".", "t", "number", "of", "batches" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/svrg_module/linear_regression/common.py#L74-L93
train
apache/incubator-mxnet
example/svrg_module/linear_regression/common.py
calc_variance
def calc_variance(grad_dict, num_batches, param_names): """Calculates the variance of the gradients per epoch for each parameter w.r.t number of batches Parameters ---------- grad_dict: dict dictionary that maps parameter name to gradients in the mod executor group num_batches: int number of batches param_names: str parameter name in the module Returns ---------- grad_dict: dict dictionary with new keys mapping to gradients variance """ for i in range(len(param_names)): diff_sqr = mx.ndarray.square(mx.nd.subtract(grad_dict[param_names[i]], grad_dict[str.format(param_names[i]+"_expectation")])) grad_dict[str.format(param_names[i] + "_variance")] = mx.ndarray.sum(diff_sqr, axis=0) / num_batches
python
def calc_variance(grad_dict, num_batches, param_names): """Calculates the variance of the gradients per epoch for each parameter w.r.t number of batches Parameters ---------- grad_dict: dict dictionary that maps parameter name to gradients in the mod executor group num_batches: int number of batches param_names: str parameter name in the module Returns ---------- grad_dict: dict dictionary with new keys mapping to gradients variance """ for i in range(len(param_names)): diff_sqr = mx.ndarray.square(mx.nd.subtract(grad_dict[param_names[i]], grad_dict[str.format(param_names[i]+"_expectation")])) grad_dict[str.format(param_names[i] + "_variance")] = mx.ndarray.sum(diff_sqr, axis=0) / num_batches
[ "def", "calc_variance", "(", "grad_dict", ",", "num_batches", ",", "param_names", ")", ":", "for", "i", "in", "range", "(", "len", "(", "param_names", ")", ")", ":", "diff_sqr", "=", "mx", ".", "ndarray", ".", "square", "(", "mx", ".", "nd", ".", "subtract", "(", "grad_dict", "[", "param_names", "[", "i", "]", "]", ",", "grad_dict", "[", "str", ".", "format", "(", "param_names", "[", "i", "]", "+", "\"_expectation\"", ")", "]", ")", ")", "grad_dict", "[", "str", ".", "format", "(", "param_names", "[", "i", "]", "+", "\"_variance\"", ")", "]", "=", "mx", ".", "ndarray", ".", "sum", "(", "diff_sqr", ",", "axis", "=", "0", ")", "/", "num_batches" ]
Calculates the variance of the gradients per epoch for each parameter w.r.t number of batches Parameters ---------- grad_dict: dict dictionary that maps parameter name to gradients in the mod executor group num_batches: int number of batches param_names: str parameter name in the module Returns ---------- grad_dict: dict dictionary with new keys mapping to gradients variance
[ "Calculates", "the", "variance", "of", "the", "gradients", "per", "epoch", "for", "each", "parameter", "w", ".", "r", ".", "t", "number", "of", "batches" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/svrg_module/linear_regression/common.py#L96-L117
train
apache/incubator-mxnet
python/mxnet/util.py
makedirs
def makedirs(d): """Create directories recursively if they don't exist. os.makedirs(exist_ok=True) is not available in Python2""" if sys.version_info[0] < 3: from distutils.dir_util import mkpath mkpath(d) else: os.makedirs(d, exist_ok=True)
python
def makedirs(d): """Create directories recursively if they don't exist. os.makedirs(exist_ok=True) is not available in Python2""" if sys.version_info[0] < 3: from distutils.dir_util import mkpath mkpath(d) else: os.makedirs(d, exist_ok=True)
[ "def", "makedirs", "(", "d", ")", ":", "if", "sys", ".", "version_info", "[", "0", "]", "<", "3", ":", "from", "distutils", ".", "dir_util", "import", "mkpath", "mkpath", "(", "d", ")", "else", ":", "os", ".", "makedirs", "(", "d", ",", "exist_ok", "=", "True", ")" ]
Create directories recursively if they don't exist. os.makedirs(exist_ok=True) is not available in Python2
[ "Create", "directories", "recursively", "if", "they", "don", "t", "exist", ".", "os", ".", "makedirs", "(", "exist_ok", "=", "True", ")", "is", "not", "available", "in", "Python2" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/util.py#L26-L33
train
apache/incubator-mxnet
python/mxnet/gluon/model_zoo/vision/alexnet.py
alexnet
def alexnet(pretrained=False, ctx=cpu(), root=os.path.join(base.data_dir(), 'models'), **kwargs): r"""AlexNet model from the `"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper. Parameters ---------- pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. root : str, default $MXNET_HOME/models Location for keeping the model parameters. """ net = AlexNet(**kwargs) if pretrained: from ..model_store import get_model_file net.load_parameters(get_model_file('alexnet', root=root), ctx=ctx) return net
python
def alexnet(pretrained=False, ctx=cpu(), root=os.path.join(base.data_dir(), 'models'), **kwargs): r"""AlexNet model from the `"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper. Parameters ---------- pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. root : str, default $MXNET_HOME/models Location for keeping the model parameters. """ net = AlexNet(**kwargs) if pretrained: from ..model_store import get_model_file net.load_parameters(get_model_file('alexnet', root=root), ctx=ctx) return net
[ "def", "alexnet", "(", "pretrained", "=", "False", ",", "ctx", "=", "cpu", "(", ")", ",", "root", "=", "os", ".", "path", ".", "join", "(", "base", ".", "data_dir", "(", ")", ",", "'models'", ")", ",", "*", "*", "kwargs", ")", ":", "net", "=", "AlexNet", "(", "*", "*", "kwargs", ")", "if", "pretrained", ":", "from", ".", ".", "model_store", "import", "get_model_file", "net", ".", "load_parameters", "(", "get_model_file", "(", "'alexnet'", ",", "root", "=", "root", ")", ",", "ctx", "=", "ctx", ")", "return", "net" ]
r"""AlexNet model from the `"One weird trick..." <https://arxiv.org/abs/1404.5997>`_ paper. Parameters ---------- pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. root : str, default $MXNET_HOME/models Location for keeping the model parameters.
[ "r", "AlexNet", "model", "from", "the", "One", "weird", "trick", "...", "<https", ":", "//", "arxiv", ".", "org", "/", "abs", "/", "1404", ".", "5997", ">", "_", "paper", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/model_zoo/vision/alexnet.py#L71-L88
train
apache/incubator-mxnet
example/named_entity_recognition/src/metrics.py
classifer_metrics
def classifer_metrics(label, pred): """ computes f1, precision and recall on the entity class """ prediction = np.argmax(pred, axis=1) label = label.astype(int) pred_is_entity = prediction != not_entity_index label_is_entity = label != not_entity_index corr_pred = (prediction == label) == (pred_is_entity == True) #how many entities are there? num_entities = np.sum(label_is_entity) entity_preds = np.sum(pred_is_entity) #how many times did we correctly predict an entity? correct_entitites = np.sum(corr_pred[pred_is_entity]) #precision: when we predict entity, how often are we right? precision = correct_entitites/entity_preds if entity_preds == 0: precision = np.nan #recall: of the things that were an entity, how many did we catch? recall = correct_entitites / num_entities if num_entities == 0: recall = np.nan f1 = 2 * precision * recall / (precision + recall) return precision, recall, f1
python
def classifer_metrics(label, pred): """ computes f1, precision and recall on the entity class """ prediction = np.argmax(pred, axis=1) label = label.astype(int) pred_is_entity = prediction != not_entity_index label_is_entity = label != not_entity_index corr_pred = (prediction == label) == (pred_is_entity == True) #how many entities are there? num_entities = np.sum(label_is_entity) entity_preds = np.sum(pred_is_entity) #how many times did we correctly predict an entity? correct_entitites = np.sum(corr_pred[pred_is_entity]) #precision: when we predict entity, how often are we right? precision = correct_entitites/entity_preds if entity_preds == 0: precision = np.nan #recall: of the things that were an entity, how many did we catch? recall = correct_entitites / num_entities if num_entities == 0: recall = np.nan f1 = 2 * precision * recall / (precision + recall) return precision, recall, f1
[ "def", "classifer_metrics", "(", "label", ",", "pred", ")", ":", "prediction", "=", "np", ".", "argmax", "(", "pred", ",", "axis", "=", "1", ")", "label", "=", "label", ".", "astype", "(", "int", ")", "pred_is_entity", "=", "prediction", "!=", "not_entity_index", "label_is_entity", "=", "label", "!=", "not_entity_index", "corr_pred", "=", "(", "prediction", "==", "label", ")", "==", "(", "pred_is_entity", "==", "True", ")", "#how many entities are there?", "num_entities", "=", "np", ".", "sum", "(", "label_is_entity", ")", "entity_preds", "=", "np", ".", "sum", "(", "pred_is_entity", ")", "#how many times did we correctly predict an entity?", "correct_entitites", "=", "np", ".", "sum", "(", "corr_pred", "[", "pred_is_entity", "]", ")", "#precision: when we predict entity, how often are we right?", "precision", "=", "correct_entitites", "/", "entity_preds", "if", "entity_preds", "==", "0", ":", "precision", "=", "np", ".", "nan", "#recall: of the things that were an entity, how many did we catch?", "recall", "=", "correct_entitites", "/", "num_entities", "if", "num_entities", "==", "0", ":", "recall", "=", "np", ".", "nan", "f1", "=", "2", "*", "precision", "*", "recall", "/", "(", "precision", "+", "recall", ")", "return", "precision", ",", "recall", ",", "f1" ]
computes f1, precision and recall on the entity class
[ "computes", "f1", "precision", "and", "recall", "on", "the", "entity", "class" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/named_entity_recognition/src/metrics.py#L33-L62
train
apache/incubator-mxnet
example/cnn_text_classification/text_cnn.py
data_iter
def data_iter(batch_size, num_embed, pre_trained_word2vec=False): """Construct data iter Parameters ---------- batch_size: int num_embed: int pre_trained_word2vec: boolean identify the pre-trained layers or not Returns ---------- train_set: DataIter Train DataIter valid: DataIter Valid DataIter sentences_size: int array dimensions embedded_size: int array dimensions vocab_size: int array dimensions """ print('Loading data...') if pre_trained_word2vec: word2vec = data_helpers.load_pretrained_word2vec('data/rt.vec') x, y = data_helpers.load_data_with_word2vec(word2vec) # reshape for convolution input x = np.reshape(x, (x.shape[0], 1, x.shape[1], x.shape[2])) embedded_size = x.shape[-1] sentences_size = x.shape[2] vocabulary_size = -1 else: x, y, vocab, vocab_inv = data_helpers.load_data() embedded_size = num_embed sentences_size = x.shape[1] vocabulary_size = len(vocab) # randomly shuffle data np.random.seed(10) shuffle_indices = np.random.permutation(np.arange(len(y))) x_shuffled = x[shuffle_indices] y_shuffled = y[shuffle_indices] # split train/valid set x_train, x_dev = x_shuffled[:-1000], x_shuffled[-1000:] y_train, y_dev = y_shuffled[:-1000], y_shuffled[-1000:] print('Train/Valid split: %d/%d' % (len(y_train), len(y_dev))) print('train shape:', x_train.shape) print('valid shape:', x_dev.shape) print('sentence max words', sentences_size) print('embedding size', embedded_size) print('vocab size', vocabulary_size) train_set = mx.io.NDArrayIter( x_train, y_train, batch_size, shuffle=True) valid = mx.io.NDArrayIter( x_dev, y_dev, batch_size) return train_set, valid, sentences_size, embedded_size, vocabulary_size
python
def data_iter(batch_size, num_embed, pre_trained_word2vec=False): """Construct data iter Parameters ---------- batch_size: int num_embed: int pre_trained_word2vec: boolean identify the pre-trained layers or not Returns ---------- train_set: DataIter Train DataIter valid: DataIter Valid DataIter sentences_size: int array dimensions embedded_size: int array dimensions vocab_size: int array dimensions """ print('Loading data...') if pre_trained_word2vec: word2vec = data_helpers.load_pretrained_word2vec('data/rt.vec') x, y = data_helpers.load_data_with_word2vec(word2vec) # reshape for convolution input x = np.reshape(x, (x.shape[0], 1, x.shape[1], x.shape[2])) embedded_size = x.shape[-1] sentences_size = x.shape[2] vocabulary_size = -1 else: x, y, vocab, vocab_inv = data_helpers.load_data() embedded_size = num_embed sentences_size = x.shape[1] vocabulary_size = len(vocab) # randomly shuffle data np.random.seed(10) shuffle_indices = np.random.permutation(np.arange(len(y))) x_shuffled = x[shuffle_indices] y_shuffled = y[shuffle_indices] # split train/valid set x_train, x_dev = x_shuffled[:-1000], x_shuffled[-1000:] y_train, y_dev = y_shuffled[:-1000], y_shuffled[-1000:] print('Train/Valid split: %d/%d' % (len(y_train), len(y_dev))) print('train shape:', x_train.shape) print('valid shape:', x_dev.shape) print('sentence max words', sentences_size) print('embedding size', embedded_size) print('vocab size', vocabulary_size) train_set = mx.io.NDArrayIter( x_train, y_train, batch_size, shuffle=True) valid = mx.io.NDArrayIter( x_dev, y_dev, batch_size) return train_set, valid, sentences_size, embedded_size, vocabulary_size
[ "def", "data_iter", "(", "batch_size", ",", "num_embed", ",", "pre_trained_word2vec", "=", "False", ")", ":", "print", "(", "'Loading data...'", ")", "if", "pre_trained_word2vec", ":", "word2vec", "=", "data_helpers", ".", "load_pretrained_word2vec", "(", "'data/rt.vec'", ")", "x", ",", "y", "=", "data_helpers", ".", "load_data_with_word2vec", "(", "word2vec", ")", "# reshape for convolution input", "x", "=", "np", ".", "reshape", "(", "x", ",", "(", "x", ".", "shape", "[", "0", "]", ",", "1", ",", "x", ".", "shape", "[", "1", "]", ",", "x", ".", "shape", "[", "2", "]", ")", ")", "embedded_size", "=", "x", ".", "shape", "[", "-", "1", "]", "sentences_size", "=", "x", ".", "shape", "[", "2", "]", "vocabulary_size", "=", "-", "1", "else", ":", "x", ",", "y", ",", "vocab", ",", "vocab_inv", "=", "data_helpers", ".", "load_data", "(", ")", "embedded_size", "=", "num_embed", "sentences_size", "=", "x", ".", "shape", "[", "1", "]", "vocabulary_size", "=", "len", "(", "vocab", ")", "# randomly shuffle data", "np", ".", "random", ".", "seed", "(", "10", ")", "shuffle_indices", "=", "np", ".", "random", ".", "permutation", "(", "np", ".", "arange", "(", "len", "(", "y", ")", ")", ")", "x_shuffled", "=", "x", "[", "shuffle_indices", "]", "y_shuffled", "=", "y", "[", "shuffle_indices", "]", "# split train/valid set", "x_train", ",", "x_dev", "=", "x_shuffled", "[", ":", "-", "1000", "]", ",", "x_shuffled", "[", "-", "1000", ":", "]", "y_train", ",", "y_dev", "=", "y_shuffled", "[", ":", "-", "1000", "]", ",", "y_shuffled", "[", "-", "1000", ":", "]", "print", "(", "'Train/Valid split: %d/%d'", "%", "(", "len", "(", "y_train", ")", ",", "len", "(", "y_dev", ")", ")", ")", "print", "(", "'train shape:'", ",", "x_train", ".", "shape", ")", "print", "(", "'valid shape:'", ",", "x_dev", ".", "shape", ")", "print", "(", "'sentence max words'", ",", "sentences_size", ")", "print", "(", "'embedding size'", ",", "embedded_size", ")", "print", "(", "'vocab size'", ",", "vocabulary_size", ")", "train_set", "=", "mx", ".", "io", ".", "NDArrayIter", "(", "x_train", ",", "y_train", ",", "batch_size", ",", "shuffle", "=", "True", ")", "valid", "=", "mx", ".", "io", ".", "NDArrayIter", "(", "x_dev", ",", "y_dev", ",", "batch_size", ")", "return", "train_set", ",", "valid", ",", "sentences_size", ",", "embedded_size", ",", "vocabulary_size" ]
Construct data iter Parameters ---------- batch_size: int num_embed: int pre_trained_word2vec: boolean identify the pre-trained layers or not Returns ---------- train_set: DataIter Train DataIter valid: DataIter Valid DataIter sentences_size: int array dimensions embedded_size: int array dimensions vocab_size: int array dimensions
[ "Construct", "data", "iter" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/cnn_text_classification/text_cnn.py#L71-L129
train
apache/incubator-mxnet
example/cnn_text_classification/text_cnn.py
sym_gen
def sym_gen(batch_size, sentences_size, num_embed, vocabulary_size, num_label=2, filter_list=None, num_filter=100, dropout=0.0, pre_trained_word2vec=False): """Generate network symbol Parameters ---------- batch_size: int sentences_size: int num_embed: int vocabulary_size: int num_label: int filter_list: list num_filter: int dropout: int pre_trained_word2vec: boolean identify the pre-trained layers or not Returns ---------- sm: symbol data: list of str data names softmax_label: list of str label names """ input_x = mx.sym.Variable('data') input_y = mx.sym.Variable('softmax_label') # embedding layer if not pre_trained_word2vec: embed_layer = mx.sym.Embedding(data=input_x, input_dim=vocabulary_size, output_dim=num_embed, name='vocab_embed') conv_input = mx.sym.Reshape(data=embed_layer, target_shape=(batch_size, 1, sentences_size, num_embed)) else: conv_input = input_x # create convolution + (max) pooling layer for each filter operation pooled_outputs = [] for i, filter_size in enumerate(filter_list): convi = mx.sym.Convolution(data=conv_input, kernel=(filter_size, num_embed), num_filter=num_filter) relui = mx.sym.Activation(data=convi, act_type='relu') pooli = mx.sym.Pooling(data=relui, pool_type='max', kernel=(sentences_size - filter_size + 1, 1), stride=(1, 1)) pooled_outputs.append(pooli) # combine all pooled outputs total_filters = num_filter * len(filter_list) concat = mx.sym.Concat(*pooled_outputs, dim=1) h_pool = mx.sym.Reshape(data=concat, target_shape=(batch_size, total_filters)) # dropout layer if dropout > 0.0: h_drop = mx.sym.Dropout(data=h_pool, p=dropout) else: h_drop = h_pool # fully connected cls_weight = mx.sym.Variable('cls_weight') cls_bias = mx.sym.Variable('cls_bias') fc = mx.sym.FullyConnected(data=h_drop, weight=cls_weight, bias=cls_bias, num_hidden=num_label) # softmax output sm = mx.sym.SoftmaxOutput(data=fc, label=input_y, name='softmax') return sm, ('data',), ('softmax_label',)
python
def sym_gen(batch_size, sentences_size, num_embed, vocabulary_size, num_label=2, filter_list=None, num_filter=100, dropout=0.0, pre_trained_word2vec=False): """Generate network symbol Parameters ---------- batch_size: int sentences_size: int num_embed: int vocabulary_size: int num_label: int filter_list: list num_filter: int dropout: int pre_trained_word2vec: boolean identify the pre-trained layers or not Returns ---------- sm: symbol data: list of str data names softmax_label: list of str label names """ input_x = mx.sym.Variable('data') input_y = mx.sym.Variable('softmax_label') # embedding layer if not pre_trained_word2vec: embed_layer = mx.sym.Embedding(data=input_x, input_dim=vocabulary_size, output_dim=num_embed, name='vocab_embed') conv_input = mx.sym.Reshape(data=embed_layer, target_shape=(batch_size, 1, sentences_size, num_embed)) else: conv_input = input_x # create convolution + (max) pooling layer for each filter operation pooled_outputs = [] for i, filter_size in enumerate(filter_list): convi = mx.sym.Convolution(data=conv_input, kernel=(filter_size, num_embed), num_filter=num_filter) relui = mx.sym.Activation(data=convi, act_type='relu') pooli = mx.sym.Pooling(data=relui, pool_type='max', kernel=(sentences_size - filter_size + 1, 1), stride=(1, 1)) pooled_outputs.append(pooli) # combine all pooled outputs total_filters = num_filter * len(filter_list) concat = mx.sym.Concat(*pooled_outputs, dim=1) h_pool = mx.sym.Reshape(data=concat, target_shape=(batch_size, total_filters)) # dropout layer if dropout > 0.0: h_drop = mx.sym.Dropout(data=h_pool, p=dropout) else: h_drop = h_pool # fully connected cls_weight = mx.sym.Variable('cls_weight') cls_bias = mx.sym.Variable('cls_bias') fc = mx.sym.FullyConnected(data=h_drop, weight=cls_weight, bias=cls_bias, num_hidden=num_label) # softmax output sm = mx.sym.SoftmaxOutput(data=fc, label=input_y, name='softmax') return sm, ('data',), ('softmax_label',)
[ "def", "sym_gen", "(", "batch_size", ",", "sentences_size", ",", "num_embed", ",", "vocabulary_size", ",", "num_label", "=", "2", ",", "filter_list", "=", "None", ",", "num_filter", "=", "100", ",", "dropout", "=", "0.0", ",", "pre_trained_word2vec", "=", "False", ")", ":", "input_x", "=", "mx", ".", "sym", ".", "Variable", "(", "'data'", ")", "input_y", "=", "mx", ".", "sym", ".", "Variable", "(", "'softmax_label'", ")", "# embedding layer", "if", "not", "pre_trained_word2vec", ":", "embed_layer", "=", "mx", ".", "sym", ".", "Embedding", "(", "data", "=", "input_x", ",", "input_dim", "=", "vocabulary_size", ",", "output_dim", "=", "num_embed", ",", "name", "=", "'vocab_embed'", ")", "conv_input", "=", "mx", ".", "sym", ".", "Reshape", "(", "data", "=", "embed_layer", ",", "target_shape", "=", "(", "batch_size", ",", "1", ",", "sentences_size", ",", "num_embed", ")", ")", "else", ":", "conv_input", "=", "input_x", "# create convolution + (max) pooling layer for each filter operation", "pooled_outputs", "=", "[", "]", "for", "i", ",", "filter_size", "in", "enumerate", "(", "filter_list", ")", ":", "convi", "=", "mx", ".", "sym", ".", "Convolution", "(", "data", "=", "conv_input", ",", "kernel", "=", "(", "filter_size", ",", "num_embed", ")", ",", "num_filter", "=", "num_filter", ")", "relui", "=", "mx", ".", "sym", ".", "Activation", "(", "data", "=", "convi", ",", "act_type", "=", "'relu'", ")", "pooli", "=", "mx", ".", "sym", ".", "Pooling", "(", "data", "=", "relui", ",", "pool_type", "=", "'max'", ",", "kernel", "=", "(", "sentences_size", "-", "filter_size", "+", "1", ",", "1", ")", ",", "stride", "=", "(", "1", ",", "1", ")", ")", "pooled_outputs", ".", "append", "(", "pooli", ")", "# combine all pooled outputs", "total_filters", "=", "num_filter", "*", "len", "(", "filter_list", ")", "concat", "=", "mx", ".", "sym", ".", "Concat", "(", "*", "pooled_outputs", ",", "dim", "=", "1", ")", "h_pool", "=", "mx", ".", "sym", ".", "Reshape", "(", "data", "=", "concat", ",", "target_shape", "=", "(", "batch_size", ",", "total_filters", ")", ")", "# dropout layer", "if", "dropout", ">", "0.0", ":", "h_drop", "=", "mx", ".", "sym", ".", "Dropout", "(", "data", "=", "h_pool", ",", "p", "=", "dropout", ")", "else", ":", "h_drop", "=", "h_pool", "# fully connected", "cls_weight", "=", "mx", ".", "sym", ".", "Variable", "(", "'cls_weight'", ")", "cls_bias", "=", "mx", ".", "sym", ".", "Variable", "(", "'cls_bias'", ")", "fc", "=", "mx", ".", "sym", ".", "FullyConnected", "(", "data", "=", "h_drop", ",", "weight", "=", "cls_weight", ",", "bias", "=", "cls_bias", ",", "num_hidden", "=", "num_label", ")", "# softmax output", "sm", "=", "mx", ".", "sym", ".", "SoftmaxOutput", "(", "data", "=", "fc", ",", "label", "=", "input_y", ",", "name", "=", "'softmax'", ")", "return", "sm", ",", "(", "'data'", ",", ")", ",", "(", "'softmax_label'", ",", ")" ]
Generate network symbol Parameters ---------- batch_size: int sentences_size: int num_embed: int vocabulary_size: int num_label: int filter_list: list num_filter: int dropout: int pre_trained_word2vec: boolean identify the pre-trained layers or not Returns ---------- sm: symbol data: list of str data names softmax_label: list of str label names
[ "Generate", "network", "symbol" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/cnn_text_classification/text_cnn.py#L132-L198
train
apache/incubator-mxnet
example/cnn_text_classification/text_cnn.py
train
def train(symbol_data, train_iterator, valid_iterator, data_column_names, target_names): """Train cnn model Parameters ---------- symbol_data: symbol train_iterator: DataIter Train DataIter valid_iterator: DataIter Valid DataIter data_column_names: list of str Defaults to ('data') for a typical model used in image classification target_names: list of str Defaults to ('softmax_label') for a typical model used in image classification """ devs = mx.cpu() # default setting if args.gpus is not None: for i in args.gpus.split(','): mx.gpu(int(i)) devs = mx.gpu() module = mx.mod.Module(symbol_data, data_names=data_column_names, label_names=target_names, context=devs) module.fit(train_data=train_iterator, eval_data=valid_iterator, eval_metric='acc', kvstore=args.kv_store, optimizer=args.optimizer, optimizer_params={'learning_rate': args.lr}, initializer=mx.initializer.Uniform(0.1), num_epoch=args.num_epochs, batch_end_callback=mx.callback.Speedometer(args.batch_size, args.disp_batches), epoch_end_callback=save_model())
python
def train(symbol_data, train_iterator, valid_iterator, data_column_names, target_names): """Train cnn model Parameters ---------- symbol_data: symbol train_iterator: DataIter Train DataIter valid_iterator: DataIter Valid DataIter data_column_names: list of str Defaults to ('data') for a typical model used in image classification target_names: list of str Defaults to ('softmax_label') for a typical model used in image classification """ devs = mx.cpu() # default setting if args.gpus is not None: for i in args.gpus.split(','): mx.gpu(int(i)) devs = mx.gpu() module = mx.mod.Module(symbol_data, data_names=data_column_names, label_names=target_names, context=devs) module.fit(train_data=train_iterator, eval_data=valid_iterator, eval_metric='acc', kvstore=args.kv_store, optimizer=args.optimizer, optimizer_params={'learning_rate': args.lr}, initializer=mx.initializer.Uniform(0.1), num_epoch=args.num_epochs, batch_end_callback=mx.callback.Speedometer(args.batch_size, args.disp_batches), epoch_end_callback=save_model())
[ "def", "train", "(", "symbol_data", ",", "train_iterator", ",", "valid_iterator", ",", "data_column_names", ",", "target_names", ")", ":", "devs", "=", "mx", ".", "cpu", "(", ")", "# default setting", "if", "args", ".", "gpus", "is", "not", "None", ":", "for", "i", "in", "args", ".", "gpus", ".", "split", "(", "','", ")", ":", "mx", ".", "gpu", "(", "int", "(", "i", ")", ")", "devs", "=", "mx", ".", "gpu", "(", ")", "module", "=", "mx", ".", "mod", ".", "Module", "(", "symbol_data", ",", "data_names", "=", "data_column_names", ",", "label_names", "=", "target_names", ",", "context", "=", "devs", ")", "module", ".", "fit", "(", "train_data", "=", "train_iterator", ",", "eval_data", "=", "valid_iterator", ",", "eval_metric", "=", "'acc'", ",", "kvstore", "=", "args", ".", "kv_store", ",", "optimizer", "=", "args", ".", "optimizer", ",", "optimizer_params", "=", "{", "'learning_rate'", ":", "args", ".", "lr", "}", ",", "initializer", "=", "mx", ".", "initializer", ".", "Uniform", "(", "0.1", ")", ",", "num_epoch", "=", "args", ".", "num_epochs", ",", "batch_end_callback", "=", "mx", ".", "callback", ".", "Speedometer", "(", "args", ".", "batch_size", ",", "args", ".", "disp_batches", ")", ",", "epoch_end_callback", "=", "save_model", "(", ")", ")" ]
Train cnn model Parameters ---------- symbol_data: symbol train_iterator: DataIter Train DataIter valid_iterator: DataIter Valid DataIter data_column_names: list of str Defaults to ('data') for a typical model used in image classification target_names: list of str Defaults to ('softmax_label') for a typical model used in image classification
[ "Train", "cnn", "model" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/cnn_text_classification/text_cnn.py#L201-L231
train
apache/incubator-mxnet
example/vae-gan/convert_data.py
convert_mat_to_images
def convert_mat_to_images(args): '''convert the caltech101 mat file to images Examples -------- python convert_data.py --dataset /home/ubuntu/datasets/caltech101/data/caltech101_silhouettes_28.mat --save_path /home/ubuntu/datasets/caltech101/data/ --invert --height 32 --width 32 ''' dataset = scipy.io.loadmat("{}/{}".format(args.save_path, args.dataset)) # image pixel data X = dataset['X'] # image class labels (not used in this project) Y = dataset['Y'] total_image = X.shape[0] h=args.height w=args.width for i in range(total_image): img = X[i] img = np.reshape(img, (28, 28)) if args.invert: img = (1-img)*255 else: img = img*255 img = Image.fromarray(img, 'L') img = img.rotate(-90) img = img.resize([h, w], Image.BILINEAR) img.save(args.save_path + '/img' + str(i) + '.png')
python
def convert_mat_to_images(args): '''convert the caltech101 mat file to images Examples -------- python convert_data.py --dataset /home/ubuntu/datasets/caltech101/data/caltech101_silhouettes_28.mat --save_path /home/ubuntu/datasets/caltech101/data/ --invert --height 32 --width 32 ''' dataset = scipy.io.loadmat("{}/{}".format(args.save_path, args.dataset)) # image pixel data X = dataset['X'] # image class labels (not used in this project) Y = dataset['Y'] total_image = X.shape[0] h=args.height w=args.width for i in range(total_image): img = X[i] img = np.reshape(img, (28, 28)) if args.invert: img = (1-img)*255 else: img = img*255 img = Image.fromarray(img, 'L') img = img.rotate(-90) img = img.resize([h, w], Image.BILINEAR) img.save(args.save_path + '/img' + str(i) + '.png')
[ "def", "convert_mat_to_images", "(", "args", ")", ":", "dataset", "=", "scipy", ".", "io", ".", "loadmat", "(", "\"{}/{}\"", ".", "format", "(", "args", ".", "save_path", ",", "args", ".", "dataset", ")", ")", "# image pixel data", "X", "=", "dataset", "[", "'X'", "]", "# image class labels (not used in this project)", "Y", "=", "dataset", "[", "'Y'", "]", "total_image", "=", "X", ".", "shape", "[", "0", "]", "h", "=", "args", ".", "height", "w", "=", "args", ".", "width", "for", "i", "in", "range", "(", "total_image", ")", ":", "img", "=", "X", "[", "i", "]", "img", "=", "np", ".", "reshape", "(", "img", ",", "(", "28", ",", "28", ")", ")", "if", "args", ".", "invert", ":", "img", "=", "(", "1", "-", "img", ")", "*", "255", "else", ":", "img", "=", "img", "*", "255", "img", "=", "Image", ".", "fromarray", "(", "img", ",", "'L'", ")", "img", "=", "img", ".", "rotate", "(", "-", "90", ")", "img", "=", "img", ".", "resize", "(", "[", "h", ",", "w", "]", ",", "Image", ".", "BILINEAR", ")", "img", ".", "save", "(", "args", ".", "save_path", "+", "'/img'", "+", "str", "(", "i", ")", "+", "'.png'", ")" ]
convert the caltech101 mat file to images Examples -------- python convert_data.py --dataset /home/ubuntu/datasets/caltech101/data/caltech101_silhouettes_28.mat --save_path /home/ubuntu/datasets/caltech101/data/ --invert --height 32 --width 32
[ "convert", "the", "caltech101", "mat", "file", "to", "images", "Examples", "--------", "python", "convert_data", ".", "py", "--", "dataset", "/", "home", "/", "ubuntu", "/", "datasets", "/", "caltech101", "/", "data", "/", "caltech101_silhouettes_28", ".", "mat", "--", "save_path", "/", "home", "/", "ubuntu", "/", "datasets", "/", "caltech101", "/", "data", "/", "--", "invert", "--", "height", "32", "--", "width", "32" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/vae-gan/convert_data.py#L42-L71
train
apache/incubator-mxnet
dev_menu.py
build
def build(args) -> None: """Build using CMake""" venv_exe = shutil.which('virtualenv') pyexe = shutil.which(args.pyexe) if not venv_exe: logging.warn("virtualenv wasn't found in path, it's recommended to install virtualenv to manage python environments") if not pyexe: logging.warn("Python executable %s not found in path", args.pyexe) if args.cmake_options: cmake = CMake(args.cmake_options) else: cmake = CMake() cmake() create_virtualenv(venv_exe, pyexe, args.venv)
python
def build(args) -> None: """Build using CMake""" venv_exe = shutil.which('virtualenv') pyexe = shutil.which(args.pyexe) if not venv_exe: logging.warn("virtualenv wasn't found in path, it's recommended to install virtualenv to manage python environments") if not pyexe: logging.warn("Python executable %s not found in path", args.pyexe) if args.cmake_options: cmake = CMake(args.cmake_options) else: cmake = CMake() cmake() create_virtualenv(venv_exe, pyexe, args.venv)
[ "def", "build", "(", "args", ")", "->", "None", ":", "venv_exe", "=", "shutil", ".", "which", "(", "'virtualenv'", ")", "pyexe", "=", "shutil", ".", "which", "(", "args", ".", "pyexe", ")", "if", "not", "venv_exe", ":", "logging", ".", "warn", "(", "\"virtualenv wasn't found in path, it's recommended to install virtualenv to manage python environments\"", ")", "if", "not", "pyexe", ":", "logging", ".", "warn", "(", "\"Python executable %s not found in path\"", ",", "args", ".", "pyexe", ")", "if", "args", ".", "cmake_options", ":", "cmake", "=", "CMake", "(", "args", ".", "cmake_options", ")", "else", ":", "cmake", "=", "CMake", "(", ")", "cmake", "(", ")", "create_virtualenv", "(", "venv_exe", ",", "pyexe", ",", "args", ".", "venv", ")" ]
Build using CMake
[ "Build", "using", "CMake" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/dev_menu.py#L199-L212
train
apache/incubator-mxnet
example/svrg_module/api_usage_example/example_api_train.py
create_network
def create_network(batch_size, update_freq): """Create a linear regression network for performing SVRG optimization. Parameters ---------- batch_size: int Size of data split update_freq: int Update Frequency for calculating full gradients Returns ---------- di: mx.io.NDArrayIter Data iterator update_freq: SVRGModule An instance of SVRGModule for performing SVRG optimization """ import logging head = '%(asctime)-15s %(message)s' logging.basicConfig(level=logging.INFO, format=head) train_data = np.random.randint(1, 5, [1000, 2]) weights = np.array([1.0, 2.0]) train_label = train_data.dot(weights) di = mx.io.NDArrayIter(train_data, train_label, batch_size=batch_size, shuffle=True, label_name='lin_reg_label') X = mx.sym.Variable('data') Y = mx.symbol.Variable('lin_reg_label') fully_connected_layer = mx.sym.FullyConnected(data=X, name='fc1', num_hidden=1) lro = mx.sym.LinearRegressionOutput(data=fully_connected_layer, label=Y, name="lro") mod = SVRGModule( symbol=lro, data_names=['data'], label_names=['lin_reg_label'], update_freq=update_freq, logger=logging ) return di, mod
python
def create_network(batch_size, update_freq): """Create a linear regression network for performing SVRG optimization. Parameters ---------- batch_size: int Size of data split update_freq: int Update Frequency for calculating full gradients Returns ---------- di: mx.io.NDArrayIter Data iterator update_freq: SVRGModule An instance of SVRGModule for performing SVRG optimization """ import logging head = '%(asctime)-15s %(message)s' logging.basicConfig(level=logging.INFO, format=head) train_data = np.random.randint(1, 5, [1000, 2]) weights = np.array([1.0, 2.0]) train_label = train_data.dot(weights) di = mx.io.NDArrayIter(train_data, train_label, batch_size=batch_size, shuffle=True, label_name='lin_reg_label') X = mx.sym.Variable('data') Y = mx.symbol.Variable('lin_reg_label') fully_connected_layer = mx.sym.FullyConnected(data=X, name='fc1', num_hidden=1) lro = mx.sym.LinearRegressionOutput(data=fully_connected_layer, label=Y, name="lro") mod = SVRGModule( symbol=lro, data_names=['data'], label_names=['lin_reg_label'], update_freq=update_freq, logger=logging ) return di, mod
[ "def", "create_network", "(", "batch_size", ",", "update_freq", ")", ":", "import", "logging", "head", "=", "'%(asctime)-15s %(message)s'", "logging", ".", "basicConfig", "(", "level", "=", "logging", ".", "INFO", ",", "format", "=", "head", ")", "train_data", "=", "np", ".", "random", ".", "randint", "(", "1", ",", "5", ",", "[", "1000", ",", "2", "]", ")", "weights", "=", "np", ".", "array", "(", "[", "1.0", ",", "2.0", "]", ")", "train_label", "=", "train_data", ".", "dot", "(", "weights", ")", "di", "=", "mx", ".", "io", ".", "NDArrayIter", "(", "train_data", ",", "train_label", ",", "batch_size", "=", "batch_size", ",", "shuffle", "=", "True", ",", "label_name", "=", "'lin_reg_label'", ")", "X", "=", "mx", ".", "sym", ".", "Variable", "(", "'data'", ")", "Y", "=", "mx", ".", "symbol", ".", "Variable", "(", "'lin_reg_label'", ")", "fully_connected_layer", "=", "mx", ".", "sym", ".", "FullyConnected", "(", "data", "=", "X", ",", "name", "=", "'fc1'", ",", "num_hidden", "=", "1", ")", "lro", "=", "mx", ".", "sym", ".", "LinearRegressionOutput", "(", "data", "=", "fully_connected_layer", ",", "label", "=", "Y", ",", "name", "=", "\"lro\"", ")", "mod", "=", "SVRGModule", "(", "symbol", "=", "lro", ",", "data_names", "=", "[", "'data'", "]", ",", "label_names", "=", "[", "'lin_reg_label'", "]", ",", "update_freq", "=", "update_freq", ",", "logger", "=", "logging", ")", "return", "di", ",", "mod" ]
Create a linear regression network for performing SVRG optimization. Parameters ---------- batch_size: int Size of data split update_freq: int Update Frequency for calculating full gradients Returns ---------- di: mx.io.NDArrayIter Data iterator update_freq: SVRGModule An instance of SVRGModule for performing SVRG optimization
[ "Create", "a", "linear", "regression", "network", "for", "performing", "SVRG", "optimization", ".", "Parameters", "----------", "batch_size", ":", "int", "Size", "of", "data", "split", "update_freq", ":", "int", "Update", "Frequency", "for", "calculating", "full", "gradients" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/svrg_module/api_usage_example/example_api_train.py#L73-L109
train
apache/incubator-mxnet
python/mxnet/gluon/model_zoo/vision/squeezenet.py
get_squeezenet
def get_squeezenet(version, pretrained=False, ctx=cpu(), root=os.path.join(base.data_dir(), 'models'), **kwargs): r"""SqueezeNet model from the `"SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and <0.5MB model size" <https://arxiv.org/abs/1602.07360>`_ paper. SqueezeNet 1.1 model from the `official SqueezeNet repo <https://github.com/DeepScale/SqueezeNet/tree/master/SqueezeNet_v1.1>`_. SqueezeNet 1.1 has 2.4x less computation and slightly fewer parameters than SqueezeNet 1.0, without sacrificing accuracy. Parameters ---------- version : str Version of squeezenet. Options are '1.0', '1.1'. pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. root : str, default $MXNET_HOME/models Location for keeping the model parameters. """ net = SqueezeNet(version, **kwargs) if pretrained: from ..model_store import get_model_file net.load_parameters(get_model_file('squeezenet%s'%version, root=root), ctx=ctx) return net
python
def get_squeezenet(version, pretrained=False, ctx=cpu(), root=os.path.join(base.data_dir(), 'models'), **kwargs): r"""SqueezeNet model from the `"SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and <0.5MB model size" <https://arxiv.org/abs/1602.07360>`_ paper. SqueezeNet 1.1 model from the `official SqueezeNet repo <https://github.com/DeepScale/SqueezeNet/tree/master/SqueezeNet_v1.1>`_. SqueezeNet 1.1 has 2.4x less computation and slightly fewer parameters than SqueezeNet 1.0, without sacrificing accuracy. Parameters ---------- version : str Version of squeezenet. Options are '1.0', '1.1'. pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. root : str, default $MXNET_HOME/models Location for keeping the model parameters. """ net = SqueezeNet(version, **kwargs) if pretrained: from ..model_store import get_model_file net.load_parameters(get_model_file('squeezenet%s'%version, root=root), ctx=ctx) return net
[ "def", "get_squeezenet", "(", "version", ",", "pretrained", "=", "False", ",", "ctx", "=", "cpu", "(", ")", ",", "root", "=", "os", ".", "path", ".", "join", "(", "base", ".", "data_dir", "(", ")", ",", "'models'", ")", ",", "*", "*", "kwargs", ")", ":", "net", "=", "SqueezeNet", "(", "version", ",", "*", "*", "kwargs", ")", "if", "pretrained", ":", "from", ".", ".", "model_store", "import", "get_model_file", "net", ".", "load_parameters", "(", "get_model_file", "(", "'squeezenet%s'", "%", "version", ",", "root", "=", "root", ")", ",", "ctx", "=", "ctx", ")", "return", "net" ]
r"""SqueezeNet model from the `"SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and <0.5MB model size" <https://arxiv.org/abs/1602.07360>`_ paper. SqueezeNet 1.1 model from the `official SqueezeNet repo <https://github.com/DeepScale/SqueezeNet/tree/master/SqueezeNet_v1.1>`_. SqueezeNet 1.1 has 2.4x less computation and slightly fewer parameters than SqueezeNet 1.0, without sacrificing accuracy. Parameters ---------- version : str Version of squeezenet. Options are '1.0', '1.1'. pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. root : str, default $MXNET_HOME/models Location for keeping the model parameters.
[ "r", "SqueezeNet", "model", "from", "the", "SqueezeNet", ":", "AlexNet", "-", "level", "accuracy", "with", "50x", "fewer", "parameters", "and", "<0", ".", "5MB", "model", "size", "<https", ":", "//", "arxiv", ".", "org", "/", "abs", "/", "1602", ".", "07360", ">", "_", "paper", ".", "SqueezeNet", "1", ".", "1", "model", "from", "the", "official", "SqueezeNet", "repo", "<https", ":", "//", "github", ".", "com", "/", "DeepScale", "/", "SqueezeNet", "/", "tree", "/", "master", "/", "SqueezeNet_v1", ".", "1", ">", "_", ".", "SqueezeNet", "1", ".", "1", "has", "2", ".", "4x", "less", "computation", "and", "slightly", "fewer", "parameters", "than", "SqueezeNet", "1", ".", "0", "without", "sacrificing", "accuracy", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/model_zoo/vision/squeezenet.py#L113-L137
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
parse_helper
def parse_helper(attrs, attrs_name, alt_value=None): """Helper function to parse operator attributes in required format.""" tuple_re = re.compile('\([0-9L|,| ]+\)') if not attrs: return alt_value attrs_str = None if attrs.get(attrs_name) is None else str(attrs.get(attrs_name)) if attrs_str is None: return alt_value attrs_match = tuple_re.search(attrs_str) if attrs_match is not None: if attrs_match.span() == (0, len(attrs_str)): dims = eval(attrs_str) return dims else: raise AttributeError("Malformed %s dimensions: %s" % (attrs_name, str(attrs_str))) return alt_value
python
def parse_helper(attrs, attrs_name, alt_value=None): """Helper function to parse operator attributes in required format.""" tuple_re = re.compile('\([0-9L|,| ]+\)') if not attrs: return alt_value attrs_str = None if attrs.get(attrs_name) is None else str(attrs.get(attrs_name)) if attrs_str is None: return alt_value attrs_match = tuple_re.search(attrs_str) if attrs_match is not None: if attrs_match.span() == (0, len(attrs_str)): dims = eval(attrs_str) return dims else: raise AttributeError("Malformed %s dimensions: %s" % (attrs_name, str(attrs_str))) return alt_value
[ "def", "parse_helper", "(", "attrs", ",", "attrs_name", ",", "alt_value", "=", "None", ")", ":", "tuple_re", "=", "re", ".", "compile", "(", "'\\([0-9L|,| ]+\\)'", ")", "if", "not", "attrs", ":", "return", "alt_value", "attrs_str", "=", "None", "if", "attrs", ".", "get", "(", "attrs_name", ")", "is", "None", "else", "str", "(", "attrs", ".", "get", "(", "attrs_name", ")", ")", "if", "attrs_str", "is", "None", ":", "return", "alt_value", "attrs_match", "=", "tuple_re", ".", "search", "(", "attrs_str", ")", "if", "attrs_match", "is", "not", "None", ":", "if", "attrs_match", ".", "span", "(", ")", "==", "(", "0", ",", "len", "(", "attrs_str", ")", ")", ":", "dims", "=", "eval", "(", "attrs_str", ")", "return", "dims", "else", ":", "raise", "AttributeError", "(", "\"Malformed %s dimensions: %s\"", "%", "(", "attrs_name", ",", "str", "(", "attrs_str", ")", ")", ")", "return", "alt_value" ]
Helper function to parse operator attributes in required format.
[ "Helper", "function", "to", "parse", "operator", "attributes", "in", "required", "format", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L69-L84
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
transform_padding
def transform_padding(pad_width): """Helper function to convert padding format for pad operator. """ num_pad_values = len(pad_width) onnx_pad_width = [0]*num_pad_values start_index = 0 # num_pad_values will always be multiple of 2 end_index = int(num_pad_values/2) for idx in range(0, num_pad_values): if idx % 2 == 0: onnx_pad_width[start_index] = pad_width[idx] start_index += 1 else: onnx_pad_width[end_index] = pad_width[idx] end_index += 1 return onnx_pad_width
python
def transform_padding(pad_width): """Helper function to convert padding format for pad operator. """ num_pad_values = len(pad_width) onnx_pad_width = [0]*num_pad_values start_index = 0 # num_pad_values will always be multiple of 2 end_index = int(num_pad_values/2) for idx in range(0, num_pad_values): if idx % 2 == 0: onnx_pad_width[start_index] = pad_width[idx] start_index += 1 else: onnx_pad_width[end_index] = pad_width[idx] end_index += 1 return onnx_pad_width
[ "def", "transform_padding", "(", "pad_width", ")", ":", "num_pad_values", "=", "len", "(", "pad_width", ")", "onnx_pad_width", "=", "[", "0", "]", "*", "num_pad_values", "start_index", "=", "0", "# num_pad_values will always be multiple of 2", "end_index", "=", "int", "(", "num_pad_values", "/", "2", ")", "for", "idx", "in", "range", "(", "0", ",", "num_pad_values", ")", ":", "if", "idx", "%", "2", "==", "0", ":", "onnx_pad_width", "[", "start_index", "]", "=", "pad_width", "[", "idx", "]", "start_index", "+=", "1", "else", ":", "onnx_pad_width", "[", "end_index", "]", "=", "pad_width", "[", "idx", "]", "end_index", "+=", "1", "return", "onnx_pad_width" ]
Helper function to convert padding format for pad operator.
[ "Helper", "function", "to", "convert", "padding", "format", "for", "pad", "operator", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L86-L103
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_string_to_list
def convert_string_to_list(string_val): """Helper function to convert string to list. Used to convert shape attribute string to list format. """ result_list = [] list_string = string_val.split(',') for val in list_string: val = str(val.strip()) val = val.replace("(", "") val = val.replace(")", "") val = val.replace("L", "") val = val.replace("[", "") val = val.replace("]", "") if val not in ("", "None"): result_list.append(int(val)) return result_list
python
def convert_string_to_list(string_val): """Helper function to convert string to list. Used to convert shape attribute string to list format. """ result_list = [] list_string = string_val.split(',') for val in list_string: val = str(val.strip()) val = val.replace("(", "") val = val.replace(")", "") val = val.replace("L", "") val = val.replace("[", "") val = val.replace("]", "") if val not in ("", "None"): result_list.append(int(val)) return result_list
[ "def", "convert_string_to_list", "(", "string_val", ")", ":", "result_list", "=", "[", "]", "list_string", "=", "string_val", ".", "split", "(", "','", ")", "for", "val", "in", "list_string", ":", "val", "=", "str", "(", "val", ".", "strip", "(", ")", ")", "val", "=", "val", ".", "replace", "(", "\"(\"", ",", "\"\"", ")", "val", "=", "val", ".", "replace", "(", "\")\"", ",", "\"\"", ")", "val", "=", "val", ".", "replace", "(", "\"L\"", ",", "\"\"", ")", "val", "=", "val", ".", "replace", "(", "\"[\"", ",", "\"\"", ")", "val", "=", "val", ".", "replace", "(", "\"]\"", ",", "\"\"", ")", "if", "val", "not", "in", "(", "\"\"", ",", "\"None\"", ")", ":", "result_list", ".", "append", "(", "int", "(", "val", ")", ")", "return", "result_list" ]
Helper function to convert string to list. Used to convert shape attribute string to list format.
[ "Helper", "function", "to", "convert", "string", "to", "list", ".", "Used", "to", "convert", "shape", "attribute", "string", "to", "list", "format", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L106-L123
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
get_inputs
def get_inputs(node, kwargs): """Helper function to get inputs""" name = node["name"] proc_nodes = kwargs["proc_nodes"] index_lookup = kwargs["index_lookup"] inputs = node["inputs"] attrs = node.get("attrs", {}) input_nodes = [] for ip in inputs: input_node_id = index_lookup[ip[0]] input_nodes.append(proc_nodes[input_node_id].name) return name, input_nodes, attrs
python
def get_inputs(node, kwargs): """Helper function to get inputs""" name = node["name"] proc_nodes = kwargs["proc_nodes"] index_lookup = kwargs["index_lookup"] inputs = node["inputs"] attrs = node.get("attrs", {}) input_nodes = [] for ip in inputs: input_node_id = index_lookup[ip[0]] input_nodes.append(proc_nodes[input_node_id].name) return name, input_nodes, attrs
[ "def", "get_inputs", "(", "node", ",", "kwargs", ")", ":", "name", "=", "node", "[", "\"name\"", "]", "proc_nodes", "=", "kwargs", "[", "\"proc_nodes\"", "]", "index_lookup", "=", "kwargs", "[", "\"index_lookup\"", "]", "inputs", "=", "node", "[", "\"inputs\"", "]", "attrs", "=", "node", ".", "get", "(", "\"attrs\"", ",", "{", "}", ")", "input_nodes", "=", "[", "]", "for", "ip", "in", "inputs", ":", "input_node_id", "=", "index_lookup", "[", "ip", "[", "0", "]", "]", "input_nodes", ".", "append", "(", "proc_nodes", "[", "input_node_id", "]", ".", "name", ")", "return", "name", ",", "input_nodes", ",", "attrs" ]
Helper function to get inputs
[ "Helper", "function", "to", "get", "inputs" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L133-L146
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
create_basic_op_node
def create_basic_op_node(op_name, node, kwargs): """Helper function to create a basic operator node that doesn't contain op specific attrs""" name, input_nodes, _ = get_inputs(node, kwargs) node = onnx.helper.make_node( op_name, input_nodes, [name], name=name ) return [node]
python
def create_basic_op_node(op_name, node, kwargs): """Helper function to create a basic operator node that doesn't contain op specific attrs""" name, input_nodes, _ = get_inputs(node, kwargs) node = onnx.helper.make_node( op_name, input_nodes, [name], name=name ) return [node]
[ "def", "create_basic_op_node", "(", "op_name", ",", "node", ",", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "_", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "op_name", ",", "input_nodes", ",", "[", "name", "]", ",", "name", "=", "name", ")", "return", "[", "node", "]" ]
Helper function to create a basic operator node that doesn't contain op specific attrs
[ "Helper", "function", "to", "create", "a", "basic", "operator", "node", "that", "doesn", "t", "contain", "op", "specific", "attrs" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L148-L159
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_weights_and_inputs
def convert_weights_and_inputs(node, **kwargs): """Helper function to convert weights and inputs. """ name, _, _ = get_inputs(node, kwargs) if kwargs["is_input"] is False: weights = kwargs["weights"] initializer = kwargs["initializer"] np_arr = weights[name] data_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[np_arr.dtype] dims = np.shape(np_arr) tensor_node = onnx.helper.make_tensor_value_info(name, data_type, dims) initializer.append( onnx.helper.make_tensor( name=name, data_type=data_type, dims=dims, vals=np_arr.flatten().tolist(), raw=False, ) ) return [tensor_node] else: tval_node = onnx.helper.make_tensor_value_info(name, kwargs["in_type"], kwargs["in_shape"]) return [tval_node]
python
def convert_weights_and_inputs(node, **kwargs): """Helper function to convert weights and inputs. """ name, _, _ = get_inputs(node, kwargs) if kwargs["is_input"] is False: weights = kwargs["weights"] initializer = kwargs["initializer"] np_arr = weights[name] data_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[np_arr.dtype] dims = np.shape(np_arr) tensor_node = onnx.helper.make_tensor_value_info(name, data_type, dims) initializer.append( onnx.helper.make_tensor( name=name, data_type=data_type, dims=dims, vals=np_arr.flatten().tolist(), raw=False, ) ) return [tensor_node] else: tval_node = onnx.helper.make_tensor_value_info(name, kwargs["in_type"], kwargs["in_shape"]) return [tval_node]
[ "def", "convert_weights_and_inputs", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "_", ",", "_", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "if", "kwargs", "[", "\"is_input\"", "]", "is", "False", ":", "weights", "=", "kwargs", "[", "\"weights\"", "]", "initializer", "=", "kwargs", "[", "\"initializer\"", "]", "np_arr", "=", "weights", "[", "name", "]", "data_type", "=", "onnx", ".", "mapping", ".", "NP_TYPE_TO_TENSOR_TYPE", "[", "np_arr", ".", "dtype", "]", "dims", "=", "np", ".", "shape", "(", "np_arr", ")", "tensor_node", "=", "onnx", ".", "helper", ".", "make_tensor_value_info", "(", "name", ",", "data_type", ",", "dims", ")", "initializer", ".", "append", "(", "onnx", ".", "helper", ".", "make_tensor", "(", "name", "=", "name", ",", "data_type", "=", "data_type", ",", "dims", "=", "dims", ",", "vals", "=", "np_arr", ".", "flatten", "(", ")", ".", "tolist", "(", ")", ",", "raw", "=", "False", ",", ")", ")", "return", "[", "tensor_node", "]", "else", ":", "tval_node", "=", "onnx", ".", "helper", ".", "make_tensor_value_info", "(", "name", ",", "kwargs", "[", "\"in_type\"", "]", ",", "kwargs", "[", "\"in_shape\"", "]", ")", "return", "[", "tval_node", "]" ]
Helper function to convert weights and inputs.
[ "Helper", "function", "to", "convert", "weights", "and", "inputs", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L162-L189
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_convolution
def convert_convolution(node, **kwargs): """Map MXNet's convolution operator attributes to onnx's Conv operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) kernel_dims = list(parse_helper(attrs, "kernel")) stride_dims = list(parse_helper(attrs, "stride", [1, 1])) pad_dims = list(parse_helper(attrs, "pad", [0, 0])) num_group = int(attrs.get("num_group", 1)) dilations = list(parse_helper(attrs, "dilate", [1, 1])) pad_dims = pad_dims + pad_dims conv_node = onnx.helper.make_node( "Conv", inputs=input_nodes, outputs=[name], kernel_shape=kernel_dims, strides=stride_dims, dilations=dilations, pads=pad_dims, group=num_group, name=name ) return [conv_node]
python
def convert_convolution(node, **kwargs): """Map MXNet's convolution operator attributes to onnx's Conv operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) kernel_dims = list(parse_helper(attrs, "kernel")) stride_dims = list(parse_helper(attrs, "stride", [1, 1])) pad_dims = list(parse_helper(attrs, "pad", [0, 0])) num_group = int(attrs.get("num_group", 1)) dilations = list(parse_helper(attrs, "dilate", [1, 1])) pad_dims = pad_dims + pad_dims conv_node = onnx.helper.make_node( "Conv", inputs=input_nodes, outputs=[name], kernel_shape=kernel_dims, strides=stride_dims, dilations=dilations, pads=pad_dims, group=num_group, name=name ) return [conv_node]
[ "def", "convert_convolution", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "kernel_dims", "=", "list", "(", "parse_helper", "(", "attrs", ",", "\"kernel\"", ")", ")", "stride_dims", "=", "list", "(", "parse_helper", "(", "attrs", ",", "\"stride\"", ",", "[", "1", ",", "1", "]", ")", ")", "pad_dims", "=", "list", "(", "parse_helper", "(", "attrs", ",", "\"pad\"", ",", "[", "0", ",", "0", "]", ")", ")", "num_group", "=", "int", "(", "attrs", ".", "get", "(", "\"num_group\"", ",", "1", ")", ")", "dilations", "=", "list", "(", "parse_helper", "(", "attrs", ",", "\"dilate\"", ",", "[", "1", ",", "1", "]", ")", ")", "pad_dims", "=", "pad_dims", "+", "pad_dims", "conv_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Conv\"", ",", "inputs", "=", "input_nodes", ",", "outputs", "=", "[", "name", "]", ",", "kernel_shape", "=", "kernel_dims", ",", "strides", "=", "stride_dims", ",", "dilations", "=", "dilations", ",", "pads", "=", "pad_dims", ",", "group", "=", "num_group", ",", "name", "=", "name", ")", "return", "[", "conv_node", "]" ]
Map MXNet's convolution operator attributes to onnx's Conv operator and return the created node.
[ "Map", "MXNet", "s", "convolution", "operator", "attributes", "to", "onnx", "s", "Conv", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L193-L219
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_deconvolution
def convert_deconvolution(node, **kwargs): """Map MXNet's deconvolution operator attributes to onnx's ConvTranspose operator and return the created node. """ name, inputs, attrs = get_inputs(node, kwargs) kernel_dims = list(parse_helper(attrs, "kernel")) stride_dims = list(parse_helper(attrs, "stride", [1, 1])) pad_dims = list(parse_helper(attrs, "pad", [0, 0])) num_group = int(attrs.get("num_group", 1)) dilations = list(parse_helper(attrs, "dilate", [1, 1])) adj_dims = list(parse_helper(attrs, "adj", [0, 0])) pad_dims = pad_dims + pad_dims deconv_node = onnx.helper.make_node( "ConvTranspose", inputs=inputs, outputs=[name], kernel_shape=kernel_dims, strides=stride_dims, dilations=dilations, output_padding=adj_dims, pads=pad_dims, group=num_group, name=name ) return [deconv_node]
python
def convert_deconvolution(node, **kwargs): """Map MXNet's deconvolution operator attributes to onnx's ConvTranspose operator and return the created node. """ name, inputs, attrs = get_inputs(node, kwargs) kernel_dims = list(parse_helper(attrs, "kernel")) stride_dims = list(parse_helper(attrs, "stride", [1, 1])) pad_dims = list(parse_helper(attrs, "pad", [0, 0])) num_group = int(attrs.get("num_group", 1)) dilations = list(parse_helper(attrs, "dilate", [1, 1])) adj_dims = list(parse_helper(attrs, "adj", [0, 0])) pad_dims = pad_dims + pad_dims deconv_node = onnx.helper.make_node( "ConvTranspose", inputs=inputs, outputs=[name], kernel_shape=kernel_dims, strides=stride_dims, dilations=dilations, output_padding=adj_dims, pads=pad_dims, group=num_group, name=name ) return [deconv_node]
[ "def", "convert_deconvolution", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "inputs", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "kernel_dims", "=", "list", "(", "parse_helper", "(", "attrs", ",", "\"kernel\"", ")", ")", "stride_dims", "=", "list", "(", "parse_helper", "(", "attrs", ",", "\"stride\"", ",", "[", "1", ",", "1", "]", ")", ")", "pad_dims", "=", "list", "(", "parse_helper", "(", "attrs", ",", "\"pad\"", ",", "[", "0", ",", "0", "]", ")", ")", "num_group", "=", "int", "(", "attrs", ".", "get", "(", "\"num_group\"", ",", "1", ")", ")", "dilations", "=", "list", "(", "parse_helper", "(", "attrs", ",", "\"dilate\"", ",", "[", "1", ",", "1", "]", ")", ")", "adj_dims", "=", "list", "(", "parse_helper", "(", "attrs", ",", "\"adj\"", ",", "[", "0", ",", "0", "]", ")", ")", "pad_dims", "=", "pad_dims", "+", "pad_dims", "deconv_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"ConvTranspose\"", ",", "inputs", "=", "inputs", ",", "outputs", "=", "[", "name", "]", ",", "kernel_shape", "=", "kernel_dims", ",", "strides", "=", "stride_dims", ",", "dilations", "=", "dilations", ",", "output_padding", "=", "adj_dims", ",", "pads", "=", "pad_dims", ",", "group", "=", "num_group", ",", "name", "=", "name", ")", "return", "[", "deconv_node", "]" ]
Map MXNet's deconvolution operator attributes to onnx's ConvTranspose operator and return the created node.
[ "Map", "MXNet", "s", "deconvolution", "operator", "attributes", "to", "onnx", "s", "ConvTranspose", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L223-L251
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_crop
def convert_crop(node, **kwargs): """Map MXNet's crop operator attributes to onnx's Crop operator and return the created node. """ name, inputs, attrs = get_inputs(node, kwargs) num_inputs = len(inputs) y, x = list(parse_helper(attrs, "offset", [0, 0])) h, w = list(parse_helper(attrs, "h_w", [0, 0])) if num_inputs > 1: h, w = kwargs["out_shape"][-2:] border = [x, y, x + w, y + h] crop_node = onnx.helper.make_node( "Crop", inputs=[inputs[0]], outputs=[name], border=border, scale=[1, 1], name=name ) logging.warning( "Using an experimental ONNX operator: Crop. " \ "Its definition can change.") return [crop_node]
python
def convert_crop(node, **kwargs): """Map MXNet's crop operator attributes to onnx's Crop operator and return the created node. """ name, inputs, attrs = get_inputs(node, kwargs) num_inputs = len(inputs) y, x = list(parse_helper(attrs, "offset", [0, 0])) h, w = list(parse_helper(attrs, "h_w", [0, 0])) if num_inputs > 1: h, w = kwargs["out_shape"][-2:] border = [x, y, x + w, y + h] crop_node = onnx.helper.make_node( "Crop", inputs=[inputs[0]], outputs=[name], border=border, scale=[1, 1], name=name ) logging.warning( "Using an experimental ONNX operator: Crop. " \ "Its definition can change.") return [crop_node]
[ "def", "convert_crop", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "inputs", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "num_inputs", "=", "len", "(", "inputs", ")", "y", ",", "x", "=", "list", "(", "parse_helper", "(", "attrs", ",", "\"offset\"", ",", "[", "0", ",", "0", "]", ")", ")", "h", ",", "w", "=", "list", "(", "parse_helper", "(", "attrs", ",", "\"h_w\"", ",", "[", "0", ",", "0", "]", ")", ")", "if", "num_inputs", ">", "1", ":", "h", ",", "w", "=", "kwargs", "[", "\"out_shape\"", "]", "[", "-", "2", ":", "]", "border", "=", "[", "x", ",", "y", ",", "x", "+", "w", ",", "y", "+", "h", "]", "crop_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Crop\"", ",", "inputs", "=", "[", "inputs", "[", "0", "]", "]", ",", "outputs", "=", "[", "name", "]", ",", "border", "=", "border", ",", "scale", "=", "[", "1", ",", "1", "]", ",", "name", "=", "name", ")", "logging", ".", "warning", "(", "\"Using an experimental ONNX operator: Crop. \"", "\"Its definition can change.\"", ")", "return", "[", "crop_node", "]" ]
Map MXNet's crop operator attributes to onnx's Crop operator and return the created node.
[ "Map", "MXNet", "s", "crop", "operator", "attributes", "to", "onnx", "s", "Crop", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L255-L281
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_fully_connected
def convert_fully_connected(node, **kwargs): """Map MXNet's FullyConnected operator attributes to onnx's Gemm operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) initializer = kwargs["initializer"] no_bias = get_boolean_attribute_value(attrs, "no_bias") fcnode = [] op_name = "flatten_" + str(kwargs["idx"]) flatten_node = onnx.helper.make_node( 'Flatten', inputs=[input_nodes[0]], outputs=[op_name], name=op_name ) input_nodes[0] = op_name fcnode.append(flatten_node) if no_bias: data_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype('int64')] bias_name = "bias" + str(kwargs["idx"]) tensor_node = onnx.helper.make_tensor_value_info(bias_name, data_type, (1,)) initializer.append( onnx.helper.make_tensor( name=bias_name, data_type=data_type, dims=(1,), vals=[0], raw=False, ) ) input_nodes.append(bias_name) fcnode.append(tensor_node) node = onnx.helper.make_node( "Gemm", input_nodes, # input (A, B, C) - C can be in place [name], # output alpha=1.0, beta=1.0, transA=False, transB=True, name=name ) fcnode.append(node) return fcnode
python
def convert_fully_connected(node, **kwargs): """Map MXNet's FullyConnected operator attributes to onnx's Gemm operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) initializer = kwargs["initializer"] no_bias = get_boolean_attribute_value(attrs, "no_bias") fcnode = [] op_name = "flatten_" + str(kwargs["idx"]) flatten_node = onnx.helper.make_node( 'Flatten', inputs=[input_nodes[0]], outputs=[op_name], name=op_name ) input_nodes[0] = op_name fcnode.append(flatten_node) if no_bias: data_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype('int64')] bias_name = "bias" + str(kwargs["idx"]) tensor_node = onnx.helper.make_tensor_value_info(bias_name, data_type, (1,)) initializer.append( onnx.helper.make_tensor( name=bias_name, data_type=data_type, dims=(1,), vals=[0], raw=False, ) ) input_nodes.append(bias_name) fcnode.append(tensor_node) node = onnx.helper.make_node( "Gemm", input_nodes, # input (A, B, C) - C can be in place [name], # output alpha=1.0, beta=1.0, transA=False, transB=True, name=name ) fcnode.append(node) return fcnode
[ "def", "convert_fully_connected", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "initializer", "=", "kwargs", "[", "\"initializer\"", "]", "no_bias", "=", "get_boolean_attribute_value", "(", "attrs", ",", "\"no_bias\"", ")", "fcnode", "=", "[", "]", "op_name", "=", "\"flatten_\"", "+", "str", "(", "kwargs", "[", "\"idx\"", "]", ")", "flatten_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'Flatten'", ",", "inputs", "=", "[", "input_nodes", "[", "0", "]", "]", ",", "outputs", "=", "[", "op_name", "]", ",", "name", "=", "op_name", ")", "input_nodes", "[", "0", "]", "=", "op_name", "fcnode", ".", "append", "(", "flatten_node", ")", "if", "no_bias", ":", "data_type", "=", "onnx", ".", "mapping", ".", "NP_TYPE_TO_TENSOR_TYPE", "[", "np", ".", "dtype", "(", "'int64'", ")", "]", "bias_name", "=", "\"bias\"", "+", "str", "(", "kwargs", "[", "\"idx\"", "]", ")", "tensor_node", "=", "onnx", ".", "helper", ".", "make_tensor_value_info", "(", "bias_name", ",", "data_type", ",", "(", "1", ",", ")", ")", "initializer", ".", "append", "(", "onnx", ".", "helper", ".", "make_tensor", "(", "name", "=", "bias_name", ",", "data_type", "=", "data_type", ",", "dims", "=", "(", "1", ",", ")", ",", "vals", "=", "[", "0", "]", ",", "raw", "=", "False", ",", ")", ")", "input_nodes", ".", "append", "(", "bias_name", ")", "fcnode", ".", "append", "(", "tensor_node", ")", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Gemm\"", ",", "input_nodes", ",", "# input (A, B, C) - C can be in place", "[", "name", "]", ",", "# output", "alpha", "=", "1.0", ",", "beta", "=", "1.0", ",", "transA", "=", "False", ",", "transB", "=", "True", ",", "name", "=", "name", ")", "fcnode", ".", "append", "(", "node", ")", "return", "fcnode" ]
Map MXNet's FullyConnected operator attributes to onnx's Gemm operator and return the created node.
[ "Map", "MXNet", "s", "FullyConnected", "operator", "attributes", "to", "onnx", "s", "Gemm", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L285-L337
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_batchnorm
def convert_batchnorm(node, **kwargs): """Map MXNet's BatchNorm operator attributes to onnx's BatchNormalization operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) momentum = float(attrs.get("momentum", 0.9)) eps = float(attrs.get("eps", 0.001)) bn_node = onnx.helper.make_node( "BatchNormalization", input_nodes, [name], name=name, epsilon=eps, momentum=momentum, # MXNet computes mean and variance per feature for batchnorm # Default for onnx is across all spatial features. So disabling the parameter. spatial=0 ) return [bn_node]
python
def convert_batchnorm(node, **kwargs): """Map MXNet's BatchNorm operator attributes to onnx's BatchNormalization operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) momentum = float(attrs.get("momentum", 0.9)) eps = float(attrs.get("eps", 0.001)) bn_node = onnx.helper.make_node( "BatchNormalization", input_nodes, [name], name=name, epsilon=eps, momentum=momentum, # MXNet computes mean and variance per feature for batchnorm # Default for onnx is across all spatial features. So disabling the parameter. spatial=0 ) return [bn_node]
[ "def", "convert_batchnorm", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "momentum", "=", "float", "(", "attrs", ".", "get", "(", "\"momentum\"", ",", "0.9", ")", ")", "eps", "=", "float", "(", "attrs", ".", "get", "(", "\"eps\"", ",", "0.001", ")", ")", "bn_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"BatchNormalization\"", ",", "input_nodes", ",", "[", "name", "]", ",", "name", "=", "name", ",", "epsilon", "=", "eps", ",", "momentum", "=", "momentum", ",", "# MXNet computes mean and variance per feature for batchnorm", "# Default for onnx is across all spatial features. So disabling the parameter.", "spatial", "=", "0", ")", "return", "[", "bn_node", "]" ]
Map MXNet's BatchNorm operator attributes to onnx's BatchNormalization operator and return the created node.
[ "Map", "MXNet", "s", "BatchNorm", "operator", "attributes", "to", "onnx", "s", "BatchNormalization", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L341-L361
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_activation
def convert_activation(node, **kwargs): """Map MXNet's Activation operator attributes to onnx's Tanh/Relu operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) act_type = attrs["act_type"] # Creating a dictionary here, but if this titlecase pattern # mxnet_name.title() act_types = { "tanh": "Tanh", "relu": "Relu", "sigmoid": "Sigmoid", "softrelu": "Softplus", "softsign": "Softsign" } act_name = act_types.get(act_type) if act_name: node = onnx.helper.make_node( act_name, input_nodes, [name], name=name ) else: raise AttributeError( "Activation %s not implemented or recognized in the converter" % act_type ) return [node]
python
def convert_activation(node, **kwargs): """Map MXNet's Activation operator attributes to onnx's Tanh/Relu operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) act_type = attrs["act_type"] # Creating a dictionary here, but if this titlecase pattern # mxnet_name.title() act_types = { "tanh": "Tanh", "relu": "Relu", "sigmoid": "Sigmoid", "softrelu": "Softplus", "softsign": "Softsign" } act_name = act_types.get(act_type) if act_name: node = onnx.helper.make_node( act_name, input_nodes, [name], name=name ) else: raise AttributeError( "Activation %s not implemented or recognized in the converter" % act_type ) return [node]
[ "def", "convert_activation", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "act_type", "=", "attrs", "[", "\"act_type\"", "]", "# Creating a dictionary here, but if this titlecase pattern", "# mxnet_name.title()", "act_types", "=", "{", "\"tanh\"", ":", "\"Tanh\"", ",", "\"relu\"", ":", "\"Relu\"", ",", "\"sigmoid\"", ":", "\"Sigmoid\"", ",", "\"softrelu\"", ":", "\"Softplus\"", ",", "\"softsign\"", ":", "\"Softsign\"", "}", "act_name", "=", "act_types", ".", "get", "(", "act_type", ")", "if", "act_name", ":", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "act_name", ",", "input_nodes", ",", "[", "name", "]", ",", "name", "=", "name", ")", "else", ":", "raise", "AttributeError", "(", "\"Activation %s not implemented or recognized in the converter\"", "%", "act_type", ")", "return", "[", "node", "]" ]
Map MXNet's Activation operator attributes to onnx's Tanh/Relu operator and return the created node.
[ "Map", "MXNet", "s", "Activation", "operator", "attributes", "to", "onnx", "s", "Tanh", "/", "Relu", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L429-L460
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_pad
def convert_pad(node, **kwargs): """Map MXNet's pad operator attributes to onnx's Pad operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) mxnet_pad_width = convert_string_to_list(attrs.get("pad_width")) onnx_pad_width = transform_padding(mxnet_pad_width) pad_mode = attrs.get("mode") if pad_mode == "constant": pad_value = float(attrs.get("constant_value")) \ if "constant_value" in attrs else 0.0 node = onnx.helper.make_node( 'Pad', inputs=input_nodes, outputs=[name], mode='constant', value=pad_value, pads=onnx_pad_width, name=name ) else: node = onnx.helper.make_node( 'Pad', inputs=input_nodes, outputs=[name], mode=pad_mode, pads=onnx_pad_width, name=name ) return [node]
python
def convert_pad(node, **kwargs): """Map MXNet's pad operator attributes to onnx's Pad operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) mxnet_pad_width = convert_string_to_list(attrs.get("pad_width")) onnx_pad_width = transform_padding(mxnet_pad_width) pad_mode = attrs.get("mode") if pad_mode == "constant": pad_value = float(attrs.get("constant_value")) \ if "constant_value" in attrs else 0.0 node = onnx.helper.make_node( 'Pad', inputs=input_nodes, outputs=[name], mode='constant', value=pad_value, pads=onnx_pad_width, name=name ) else: node = onnx.helper.make_node( 'Pad', inputs=input_nodes, outputs=[name], mode=pad_mode, pads=onnx_pad_width, name=name ) return [node]
[ "def", "convert_pad", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "mxnet_pad_width", "=", "convert_string_to_list", "(", "attrs", ".", "get", "(", "\"pad_width\"", ")", ")", "onnx_pad_width", "=", "transform_padding", "(", "mxnet_pad_width", ")", "pad_mode", "=", "attrs", ".", "get", "(", "\"mode\"", ")", "if", "pad_mode", "==", "\"constant\"", ":", "pad_value", "=", "float", "(", "attrs", ".", "get", "(", "\"constant_value\"", ")", ")", "if", "\"constant_value\"", "in", "attrs", "else", "0.0", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'Pad'", ",", "inputs", "=", "input_nodes", ",", "outputs", "=", "[", "name", "]", ",", "mode", "=", "'constant'", ",", "value", "=", "pad_value", ",", "pads", "=", "onnx_pad_width", ",", "name", "=", "name", ")", "else", ":", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'Pad'", ",", "inputs", "=", "input_nodes", ",", "outputs", "=", "[", "name", "]", ",", "mode", "=", "pad_mode", ",", "pads", "=", "onnx_pad_width", ",", "name", "=", "name", ")", "return", "[", "node", "]" ]
Map MXNet's pad operator attributes to onnx's Pad operator and return the created node.
[ "Map", "MXNet", "s", "pad", "operator", "attributes", "to", "onnx", "s", "Pad", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L464-L497
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
create_helper_trans_node
def create_helper_trans_node(op_name, input_node, node_name): """create extra transpose node for dot operator""" node_name = op_name + "_" + node_name trans_node = onnx.helper.make_node( 'Transpose', inputs=[input_node], outputs=[node_name], name=node_name ) return trans_node
python
def create_helper_trans_node(op_name, input_node, node_name): """create extra transpose node for dot operator""" node_name = op_name + "_" + node_name trans_node = onnx.helper.make_node( 'Transpose', inputs=[input_node], outputs=[node_name], name=node_name ) return trans_node
[ "def", "create_helper_trans_node", "(", "op_name", ",", "input_node", ",", "node_name", ")", ":", "node_name", "=", "op_name", "+", "\"_\"", "+", "node_name", "trans_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'Transpose'", ",", "inputs", "=", "[", "input_node", "]", ",", "outputs", "=", "[", "node_name", "]", ",", "name", "=", "node_name", ")", "return", "trans_node" ]
create extra transpose node for dot operator
[ "create", "extra", "transpose", "node", "for", "dot", "operator" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L500-L509
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_dot
def convert_dot(node, **kwargs): """Map MXNet's dot operator attributes to onnx's MatMul and Transpose operators based on the values set for transpose_a, transpose_b attributes.""" name, input_nodes, attrs = get_inputs(node, kwargs) input_node_a = input_nodes[0] input_node_b = input_nodes[1] trans_a_node = None trans_b_node = None trans_a = get_boolean_attribute_value(attrs, "transpose_a") trans_b = get_boolean_attribute_value(attrs, "transpose_b") op_name = "transpose" + str(kwargs["idx"]) if trans_a: trans_a_node = create_helper_trans_node(op_name, input_nodes[0], 'a') input_node_a = op_name+"_a" if trans_b: trans_b_node = create_helper_trans_node(op_name, input_nodes[1], 'b') input_node_b = op_name+"_b" matmul_node = onnx.helper.make_node( 'MatMul', inputs=[input_node_a, input_node_b], outputs=[name], name=name ) if not trans_a and not trans_b: return [matmul_node] elif trans_a and not trans_b: return [trans_a_node, matmul_node] elif trans_b and not trans_a: return [trans_b_node, matmul_node] else: return [trans_a_node, trans_b_node, matmul_node]
python
def convert_dot(node, **kwargs): """Map MXNet's dot operator attributes to onnx's MatMul and Transpose operators based on the values set for transpose_a, transpose_b attributes.""" name, input_nodes, attrs = get_inputs(node, kwargs) input_node_a = input_nodes[0] input_node_b = input_nodes[1] trans_a_node = None trans_b_node = None trans_a = get_boolean_attribute_value(attrs, "transpose_a") trans_b = get_boolean_attribute_value(attrs, "transpose_b") op_name = "transpose" + str(kwargs["idx"]) if trans_a: trans_a_node = create_helper_trans_node(op_name, input_nodes[0], 'a') input_node_a = op_name+"_a" if trans_b: trans_b_node = create_helper_trans_node(op_name, input_nodes[1], 'b') input_node_b = op_name+"_b" matmul_node = onnx.helper.make_node( 'MatMul', inputs=[input_node_a, input_node_b], outputs=[name], name=name ) if not trans_a and not trans_b: return [matmul_node] elif trans_a and not trans_b: return [trans_a_node, matmul_node] elif trans_b and not trans_a: return [trans_b_node, matmul_node] else: return [trans_a_node, trans_b_node, matmul_node]
[ "def", "convert_dot", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "input_node_a", "=", "input_nodes", "[", "0", "]", "input_node_b", "=", "input_nodes", "[", "1", "]", "trans_a_node", "=", "None", "trans_b_node", "=", "None", "trans_a", "=", "get_boolean_attribute_value", "(", "attrs", ",", "\"transpose_a\"", ")", "trans_b", "=", "get_boolean_attribute_value", "(", "attrs", ",", "\"transpose_b\"", ")", "op_name", "=", "\"transpose\"", "+", "str", "(", "kwargs", "[", "\"idx\"", "]", ")", "if", "trans_a", ":", "trans_a_node", "=", "create_helper_trans_node", "(", "op_name", ",", "input_nodes", "[", "0", "]", ",", "'a'", ")", "input_node_a", "=", "op_name", "+", "\"_a\"", "if", "trans_b", ":", "trans_b_node", "=", "create_helper_trans_node", "(", "op_name", ",", "input_nodes", "[", "1", "]", ",", "'b'", ")", "input_node_b", "=", "op_name", "+", "\"_b\"", "matmul_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'MatMul'", ",", "inputs", "=", "[", "input_node_a", ",", "input_node_b", "]", ",", "outputs", "=", "[", "name", "]", ",", "name", "=", "name", ")", "if", "not", "trans_a", "and", "not", "trans_b", ":", "return", "[", "matmul_node", "]", "elif", "trans_a", "and", "not", "trans_b", ":", "return", "[", "trans_a_node", ",", "matmul_node", "]", "elif", "trans_b", "and", "not", "trans_a", ":", "return", "[", "trans_b_node", ",", "matmul_node", "]", "else", ":", "return", "[", "trans_a_node", ",", "trans_b_node", ",", "matmul_node", "]" ]
Map MXNet's dot operator attributes to onnx's MatMul and Transpose operators based on the values set for transpose_a, transpose_b attributes.
[ "Map", "MXNet", "s", "dot", "operator", "attributes", "to", "onnx", "s", "MatMul", "and", "Transpose", "operators", "based", "on", "the", "values", "set", "for", "transpose_a", "transpose_b", "attributes", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L513-L550
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_linalg_gemm2
def convert_linalg_gemm2(node, **kwargs): """Map MXNet's _linalg_gemm2 operator attributes to onnx's MatMul and Transpose operators based on the values set for transpose_a, transpose_b attributes. Return multiple nodes created. """ name, input_nodes, attrs = get_inputs(node, kwargs) # Getting the attributes and assigning default values. alpha = float(attrs.get("alpha", 1.0)) trans_a = get_boolean_attribute_value(attrs, "transpose_a") trans_b = get_boolean_attribute_value(attrs, "transpose_b") op_name = "transpose" + str(kwargs["idx"]) if alpha == 1.0 and trans_a == 0 and trans_b == 0: matmul_node = onnx.helper.make_node( 'MatMul', inputs=input_nodes, outputs=[name], name=name ) return [matmul_node] elif trans_a == 1 and trans_b == 0: op_name = "transpose" + str(kwargs["idx"]) node_name = op_name+"_a" trans_a_node = onnx.helper.make_node( 'Transpose', inputs=[input_nodes[0]], outputs=[op_name+"_a"], name=node_name ) matmul_node = onnx.helper.make_node( 'MatMul', inputs=[node_name, input_nodes[1]], outputs=[name], name=name ) return [trans_a_node, matmul_node] elif trans_a == 0 and trans_b == 1: node_name = op_name + "_b" trans_b_node = onnx.helper.make_node( 'Transpose', inputs=[input_nodes[1]], outputs=[op_name+"_b"], name=node_name ) matmul_node = onnx.helper.make_node( 'MatMul', inputs=[input_nodes[0], node_name], outputs=[name], name=name ) return [trans_b_node, matmul_node] else: node_name_a = op_name+"_a" trans_a_node = onnx.helper.make_node( 'Transpose', inputs=[input_nodes[0]], outputs=[op_name+"_a"], name=node_name_a ) node_name_b = op_name + "_b" trans_b_node = onnx.helper.make_node( 'Transpose', inputs=[input_nodes[1]], outputs=[op_name+"_b"], name=node_name_b ) matmul_node = onnx.helper.make_node( 'MatMul', inputs=input_nodes, outputs=[name], name=name ) return [trans_a_node, trans_b_node, matmul_node]
python
def convert_linalg_gemm2(node, **kwargs): """Map MXNet's _linalg_gemm2 operator attributes to onnx's MatMul and Transpose operators based on the values set for transpose_a, transpose_b attributes. Return multiple nodes created. """ name, input_nodes, attrs = get_inputs(node, kwargs) # Getting the attributes and assigning default values. alpha = float(attrs.get("alpha", 1.0)) trans_a = get_boolean_attribute_value(attrs, "transpose_a") trans_b = get_boolean_attribute_value(attrs, "transpose_b") op_name = "transpose" + str(kwargs["idx"]) if alpha == 1.0 and trans_a == 0 and trans_b == 0: matmul_node = onnx.helper.make_node( 'MatMul', inputs=input_nodes, outputs=[name], name=name ) return [matmul_node] elif trans_a == 1 and trans_b == 0: op_name = "transpose" + str(kwargs["idx"]) node_name = op_name+"_a" trans_a_node = onnx.helper.make_node( 'Transpose', inputs=[input_nodes[0]], outputs=[op_name+"_a"], name=node_name ) matmul_node = onnx.helper.make_node( 'MatMul', inputs=[node_name, input_nodes[1]], outputs=[name], name=name ) return [trans_a_node, matmul_node] elif trans_a == 0 and trans_b == 1: node_name = op_name + "_b" trans_b_node = onnx.helper.make_node( 'Transpose', inputs=[input_nodes[1]], outputs=[op_name+"_b"], name=node_name ) matmul_node = onnx.helper.make_node( 'MatMul', inputs=[input_nodes[0], node_name], outputs=[name], name=name ) return [trans_b_node, matmul_node] else: node_name_a = op_name+"_a" trans_a_node = onnx.helper.make_node( 'Transpose', inputs=[input_nodes[0]], outputs=[op_name+"_a"], name=node_name_a ) node_name_b = op_name + "_b" trans_b_node = onnx.helper.make_node( 'Transpose', inputs=[input_nodes[1]], outputs=[op_name+"_b"], name=node_name_b ) matmul_node = onnx.helper.make_node( 'MatMul', inputs=input_nodes, outputs=[name], name=name ) return [trans_a_node, trans_b_node, matmul_node]
[ "def", "convert_linalg_gemm2", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "# Getting the attributes and assigning default values.", "alpha", "=", "float", "(", "attrs", ".", "get", "(", "\"alpha\"", ",", "1.0", ")", ")", "trans_a", "=", "get_boolean_attribute_value", "(", "attrs", ",", "\"transpose_a\"", ")", "trans_b", "=", "get_boolean_attribute_value", "(", "attrs", ",", "\"transpose_b\"", ")", "op_name", "=", "\"transpose\"", "+", "str", "(", "kwargs", "[", "\"idx\"", "]", ")", "if", "alpha", "==", "1.0", "and", "trans_a", "==", "0", "and", "trans_b", "==", "0", ":", "matmul_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'MatMul'", ",", "inputs", "=", "input_nodes", ",", "outputs", "=", "[", "name", "]", ",", "name", "=", "name", ")", "return", "[", "matmul_node", "]", "elif", "trans_a", "==", "1", "and", "trans_b", "==", "0", ":", "op_name", "=", "\"transpose\"", "+", "str", "(", "kwargs", "[", "\"idx\"", "]", ")", "node_name", "=", "op_name", "+", "\"_a\"", "trans_a_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'Transpose'", ",", "inputs", "=", "[", "input_nodes", "[", "0", "]", "]", ",", "outputs", "=", "[", "op_name", "+", "\"_a\"", "]", ",", "name", "=", "node_name", ")", "matmul_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'MatMul'", ",", "inputs", "=", "[", "node_name", ",", "input_nodes", "[", "1", "]", "]", ",", "outputs", "=", "[", "name", "]", ",", "name", "=", "name", ")", "return", "[", "trans_a_node", ",", "matmul_node", "]", "elif", "trans_a", "==", "0", "and", "trans_b", "==", "1", ":", "node_name", "=", "op_name", "+", "\"_b\"", "trans_b_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'Transpose'", ",", "inputs", "=", "[", "input_nodes", "[", "1", "]", "]", ",", "outputs", "=", "[", "op_name", "+", "\"_b\"", "]", ",", "name", "=", "node_name", ")", "matmul_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'MatMul'", ",", "inputs", "=", "[", "input_nodes", "[", "0", "]", ",", "node_name", "]", ",", "outputs", "=", "[", "name", "]", ",", "name", "=", "name", ")", "return", "[", "trans_b_node", ",", "matmul_node", "]", "else", ":", "node_name_a", "=", "op_name", "+", "\"_a\"", "trans_a_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'Transpose'", ",", "inputs", "=", "[", "input_nodes", "[", "0", "]", "]", ",", "outputs", "=", "[", "op_name", "+", "\"_a\"", "]", ",", "name", "=", "node_name_a", ")", "node_name_b", "=", "op_name", "+", "\"_b\"", "trans_b_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'Transpose'", ",", "inputs", "=", "[", "input_nodes", "[", "1", "]", "]", ",", "outputs", "=", "[", "op_name", "+", "\"_b\"", "]", ",", "name", "=", "node_name_b", ")", "matmul_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'MatMul'", ",", "inputs", "=", "input_nodes", ",", "outputs", "=", "[", "name", "]", ",", "name", "=", "name", ")", "return", "[", "trans_a_node", ",", "trans_b_node", ",", "matmul_node", "]" ]
Map MXNet's _linalg_gemm2 operator attributes to onnx's MatMul and Transpose operators based on the values set for transpose_a, transpose_b attributes. Return multiple nodes created.
[ "Map", "MXNet", "s", "_linalg_gemm2", "operator", "attributes", "to", "onnx", "s", "MatMul", "and", "Transpose", "operators", "based", "on", "the", "values", "set", "for", "transpose_a", "transpose_b", "attributes", ".", "Return", "multiple", "nodes", "created", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L554-L636
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_pooling
def convert_pooling(node, **kwargs): """Map MXNet's Pooling operator attributes to onnx's MaxPool/AveragePool/GlobalMaxPool/GlobalAveragePool operators based on the input node's attributes and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) kernel = eval(attrs["kernel"]) pool_type = attrs["pool_type"] if attrs.get("pool_type") else "max" stride = eval(attrs["stride"]) if attrs.get("stride") else (1, 1) global_pool = get_boolean_attribute_value(attrs, "global_pool") p_value = attrs.get('p_value', 'None') pooling_convention = attrs.get('pooling_convention', 'valid') if pooling_convention == 'full': pooling_warning = "Pooling: ONNX currently doesn't support pooling_convention. " \ "This might lead to shape or accuracy issues. " \ "https://github.com/onnx/onnx/issues/549" logging.warning(pooling_warning) pad_dims = list(parse_helper(attrs, "pad", [0, 0])) pad_dims = pad_dims + pad_dims pool_types = {"max": "MaxPool", "avg": "AveragePool", "lp": "LpPool"} global_pool_types = {"max": "GlobalMaxPool", "avg": "GlobalAveragePool", "lp": "GlobalLpPool"} if pool_type == 'lp' and p_value == 'None': raise AttributeError('ONNX requires a p value for LpPool and GlobalLpPool') if global_pool: if pool_type == 'lp': node = onnx.helper.make_node( global_pool_types[pool_type], input_nodes, # input [name], p=int(p_value), name=name ) else: node = onnx.helper.make_node( global_pool_types[pool_type], input_nodes, # input [name], name=name ) else: if pool_type == 'lp': node = onnx.helper.make_node( pool_types[pool_type], input_nodes, # input [name], p=int(p_value), kernel_shape=kernel, pads=pad_dims, strides=stride, name=name ) else: node = onnx.helper.make_node( pool_types[pool_type], input_nodes, # input [name], kernel_shape=kernel, pads=pad_dims, strides=stride, name=name ) return [node]
python
def convert_pooling(node, **kwargs): """Map MXNet's Pooling operator attributes to onnx's MaxPool/AveragePool/GlobalMaxPool/GlobalAveragePool operators based on the input node's attributes and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) kernel = eval(attrs["kernel"]) pool_type = attrs["pool_type"] if attrs.get("pool_type") else "max" stride = eval(attrs["stride"]) if attrs.get("stride") else (1, 1) global_pool = get_boolean_attribute_value(attrs, "global_pool") p_value = attrs.get('p_value', 'None') pooling_convention = attrs.get('pooling_convention', 'valid') if pooling_convention == 'full': pooling_warning = "Pooling: ONNX currently doesn't support pooling_convention. " \ "This might lead to shape or accuracy issues. " \ "https://github.com/onnx/onnx/issues/549" logging.warning(pooling_warning) pad_dims = list(parse_helper(attrs, "pad", [0, 0])) pad_dims = pad_dims + pad_dims pool_types = {"max": "MaxPool", "avg": "AveragePool", "lp": "LpPool"} global_pool_types = {"max": "GlobalMaxPool", "avg": "GlobalAveragePool", "lp": "GlobalLpPool"} if pool_type == 'lp' and p_value == 'None': raise AttributeError('ONNX requires a p value for LpPool and GlobalLpPool') if global_pool: if pool_type == 'lp': node = onnx.helper.make_node( global_pool_types[pool_type], input_nodes, # input [name], p=int(p_value), name=name ) else: node = onnx.helper.make_node( global_pool_types[pool_type], input_nodes, # input [name], name=name ) else: if pool_type == 'lp': node = onnx.helper.make_node( pool_types[pool_type], input_nodes, # input [name], p=int(p_value), kernel_shape=kernel, pads=pad_dims, strides=stride, name=name ) else: node = onnx.helper.make_node( pool_types[pool_type], input_nodes, # input [name], kernel_shape=kernel, pads=pad_dims, strides=stride, name=name ) return [node]
[ "def", "convert_pooling", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "kernel", "=", "eval", "(", "attrs", "[", "\"kernel\"", "]", ")", "pool_type", "=", "attrs", "[", "\"pool_type\"", "]", "if", "attrs", ".", "get", "(", "\"pool_type\"", ")", "else", "\"max\"", "stride", "=", "eval", "(", "attrs", "[", "\"stride\"", "]", ")", "if", "attrs", ".", "get", "(", "\"stride\"", ")", "else", "(", "1", ",", "1", ")", "global_pool", "=", "get_boolean_attribute_value", "(", "attrs", ",", "\"global_pool\"", ")", "p_value", "=", "attrs", ".", "get", "(", "'p_value'", ",", "'None'", ")", "pooling_convention", "=", "attrs", ".", "get", "(", "'pooling_convention'", ",", "'valid'", ")", "if", "pooling_convention", "==", "'full'", ":", "pooling_warning", "=", "\"Pooling: ONNX currently doesn't support pooling_convention. \"", "\"This might lead to shape or accuracy issues. \"", "\"https://github.com/onnx/onnx/issues/549\"", "logging", ".", "warning", "(", "pooling_warning", ")", "pad_dims", "=", "list", "(", "parse_helper", "(", "attrs", ",", "\"pad\"", ",", "[", "0", ",", "0", "]", ")", ")", "pad_dims", "=", "pad_dims", "+", "pad_dims", "pool_types", "=", "{", "\"max\"", ":", "\"MaxPool\"", ",", "\"avg\"", ":", "\"AveragePool\"", ",", "\"lp\"", ":", "\"LpPool\"", "}", "global_pool_types", "=", "{", "\"max\"", ":", "\"GlobalMaxPool\"", ",", "\"avg\"", ":", "\"GlobalAveragePool\"", ",", "\"lp\"", ":", "\"GlobalLpPool\"", "}", "if", "pool_type", "==", "'lp'", "and", "p_value", "==", "'None'", ":", "raise", "AttributeError", "(", "'ONNX requires a p value for LpPool and GlobalLpPool'", ")", "if", "global_pool", ":", "if", "pool_type", "==", "'lp'", ":", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "global_pool_types", "[", "pool_type", "]", ",", "input_nodes", ",", "# input", "[", "name", "]", ",", "p", "=", "int", "(", "p_value", ")", ",", "name", "=", "name", ")", "else", ":", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "global_pool_types", "[", "pool_type", "]", ",", "input_nodes", ",", "# input", "[", "name", "]", ",", "name", "=", "name", ")", "else", ":", "if", "pool_type", "==", "'lp'", ":", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "pool_types", "[", "pool_type", "]", ",", "input_nodes", ",", "# input", "[", "name", "]", ",", "p", "=", "int", "(", "p_value", ")", ",", "kernel_shape", "=", "kernel", ",", "pads", "=", "pad_dims", ",", "strides", "=", "stride", ",", "name", "=", "name", ")", "else", ":", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "pool_types", "[", "pool_type", "]", ",", "input_nodes", ",", "# input", "[", "name", "]", ",", "kernel_shape", "=", "kernel", ",", "pads", "=", "pad_dims", ",", "strides", "=", "stride", ",", "name", "=", "name", ")", "return", "[", "node", "]" ]
Map MXNet's Pooling operator attributes to onnx's MaxPool/AveragePool/GlobalMaxPool/GlobalAveragePool operators based on the input node's attributes and return the created node.
[ "Map", "MXNet", "s", "Pooling", "operator", "attributes", "to", "onnx", "s", "MaxPool", "/", "AveragePool", "/", "GlobalMaxPool", "/", "GlobalAveragePool", "operators", "based", "on", "the", "input", "node", "s", "attributes", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L640-L710
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_instancenorm
def convert_instancenorm(node, **kwargs): """Map MXNet's InstanceNorm operator attributes to onnx's InstanceNormalization operator based on the input node's attributes and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) eps = float(attrs.get("eps", 0.001)) node = onnx.helper.make_node( 'InstanceNormalization', inputs=input_nodes, outputs=[name], name=name, epsilon=eps) return [node]
python
def convert_instancenorm(node, **kwargs): """Map MXNet's InstanceNorm operator attributes to onnx's InstanceNormalization operator based on the input node's attributes and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) eps = float(attrs.get("eps", 0.001)) node = onnx.helper.make_node( 'InstanceNormalization', inputs=input_nodes, outputs=[name], name=name, epsilon=eps) return [node]
[ "def", "convert_instancenorm", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "eps", "=", "float", "(", "attrs", ".", "get", "(", "\"eps\"", ",", "0.001", ")", ")", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'InstanceNormalization'", ",", "inputs", "=", "input_nodes", ",", "outputs", "=", "[", "name", "]", ",", "name", "=", "name", ",", "epsilon", "=", "eps", ")", "return", "[", "node", "]" ]
Map MXNet's InstanceNorm operator attributes to onnx's InstanceNormalization operator based on the input node's attributes and return the created node.
[ "Map", "MXNet", "s", "InstanceNorm", "operator", "attributes", "to", "onnx", "s", "InstanceNormalization", "operator", "based", "on", "the", "input", "node", "s", "attributes", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L735-L750
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_leakyrelu
def convert_leakyrelu(node, **kwargs): """Map MXNet's LeakyReLU operator attributes to onnx's Elu/LeakyRelu/PRelu operators based on the input node's attributes and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) act_type = attrs.get("act_type", "leaky") alpha = float(attrs.get("slope", 0.25)) act_name = {"elu": "Elu", "leaky": "LeakyRelu", "prelu": "PRelu", "selu": "Selu"} if act_type == "prelu" or act_type == "selu": node = onnx.helper.make_node( act_name[act_type], inputs=input_nodes, outputs=[name], name=name) else: node = onnx.helper.make_node( act_name[act_type], inputs=input_nodes, outputs=[name], name=name, alpha=alpha) return [node]
python
def convert_leakyrelu(node, **kwargs): """Map MXNet's LeakyReLU operator attributes to onnx's Elu/LeakyRelu/PRelu operators based on the input node's attributes and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) act_type = attrs.get("act_type", "leaky") alpha = float(attrs.get("slope", 0.25)) act_name = {"elu": "Elu", "leaky": "LeakyRelu", "prelu": "PRelu", "selu": "Selu"} if act_type == "prelu" or act_type == "selu": node = onnx.helper.make_node( act_name[act_type], inputs=input_nodes, outputs=[name], name=name) else: node = onnx.helper.make_node( act_name[act_type], inputs=input_nodes, outputs=[name], name=name, alpha=alpha) return [node]
[ "def", "convert_leakyrelu", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "act_type", "=", "attrs", ".", "get", "(", "\"act_type\"", ",", "\"leaky\"", ")", "alpha", "=", "float", "(", "attrs", ".", "get", "(", "\"slope\"", ",", "0.25", ")", ")", "act_name", "=", "{", "\"elu\"", ":", "\"Elu\"", ",", "\"leaky\"", ":", "\"LeakyRelu\"", ",", "\"prelu\"", ":", "\"PRelu\"", ",", "\"selu\"", ":", "\"Selu\"", "}", "if", "act_type", "==", "\"prelu\"", "or", "act_type", "==", "\"selu\"", ":", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "act_name", "[", "act_type", "]", ",", "inputs", "=", "input_nodes", ",", "outputs", "=", "[", "name", "]", ",", "name", "=", "name", ")", "else", ":", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "act_name", "[", "act_type", "]", ",", "inputs", "=", "input_nodes", ",", "outputs", "=", "[", "name", "]", ",", "name", "=", "name", ",", "alpha", "=", "alpha", ")", "return", "[", "node", "]" ]
Map MXNet's LeakyReLU operator attributes to onnx's Elu/LeakyRelu/PRelu operators based on the input node's attributes and return the created node.
[ "Map", "MXNet", "s", "LeakyReLU", "operator", "attributes", "to", "onnx", "s", "Elu", "/", "LeakyRelu", "/", "PRelu", "operators", "based", "on", "the", "input", "node", "s", "attributes", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L753-L779
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_softmax
def convert_softmax(node, **kwargs): """Map MXNet's softmax operator attributes to onnx's Softmax operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) axis = int(attrs.get("axis", -1)) softmax_node = onnx.helper.make_node( "Softmax", input_nodes, [name], axis=axis, name=name ) return [softmax_node]
python
def convert_softmax(node, **kwargs): """Map MXNet's softmax operator attributes to onnx's Softmax operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) axis = int(attrs.get("axis", -1)) softmax_node = onnx.helper.make_node( "Softmax", input_nodes, [name], axis=axis, name=name ) return [softmax_node]
[ "def", "convert_softmax", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "axis", "=", "int", "(", "attrs", ".", "get", "(", "\"axis\"", ",", "-", "1", ")", ")", "softmax_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Softmax\"", ",", "input_nodes", ",", "[", "name", "]", ",", "axis", "=", "axis", ",", "name", "=", "name", ")", "return", "[", "softmax_node", "]" ]
Map MXNet's softmax operator attributes to onnx's Softmax operator and return the created node.
[ "Map", "MXNet", "s", "softmax", "operator", "attributes", "to", "onnx", "s", "Softmax", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L783-L799
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_softmax_output
def convert_softmax_output(node, **kwargs): """Map MXNet's SoftmaxOutput operator attributes to onnx's Softmax operator and return the created node. """ name = node["name"] input1_idx = kwargs["index_lookup"][node["inputs"][0][0]] input1 = kwargs["proc_nodes"][input1_idx] softmax_node = onnx.helper.make_node( "Softmax", [input1.name], [name], axis=1, name=name ) return [softmax_node]
python
def convert_softmax_output(node, **kwargs): """Map MXNet's SoftmaxOutput operator attributes to onnx's Softmax operator and return the created node. """ name = node["name"] input1_idx = kwargs["index_lookup"][node["inputs"][0][0]] input1 = kwargs["proc_nodes"][input1_idx] softmax_node = onnx.helper.make_node( "Softmax", [input1.name], [name], axis=1, name=name ) return [softmax_node]
[ "def", "convert_softmax_output", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", "=", "node", "[", "\"name\"", "]", "input1_idx", "=", "kwargs", "[", "\"index_lookup\"", "]", "[", "node", "[", "\"inputs\"", "]", "[", "0", "]", "[", "0", "]", "]", "input1", "=", "kwargs", "[", "\"proc_nodes\"", "]", "[", "input1_idx", "]", "softmax_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Softmax\"", ",", "[", "input1", ".", "name", "]", ",", "[", "name", "]", ",", "axis", "=", "1", ",", "name", "=", "name", ")", "return", "[", "softmax_node", "]" ]
Map MXNet's SoftmaxOutput operator attributes to onnx's Softmax operator and return the created node.
[ "Map", "MXNet", "s", "SoftmaxOutput", "operator", "attributes", "to", "onnx", "s", "Softmax", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L805-L822
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_logistic_regression_output
def convert_logistic_regression_output(node, **kwargs): """Map MXNet's SoftmaxOutput operator attributes to onnx's Softmax operator and return the created node. """ name = node["name"] input1_idx = kwargs["index_lookup"][node["inputs"][0][0]] input1 = kwargs["proc_nodes"][input1_idx] sigmoid_node = onnx.helper.make_node( "Sigmoid", [input1.name], [name], name=name ) return [sigmoid_node]
python
def convert_logistic_regression_output(node, **kwargs): """Map MXNet's SoftmaxOutput operator attributes to onnx's Softmax operator and return the created node. """ name = node["name"] input1_idx = kwargs["index_lookup"][node["inputs"][0][0]] input1 = kwargs["proc_nodes"][input1_idx] sigmoid_node = onnx.helper.make_node( "Sigmoid", [input1.name], [name], name=name ) return [sigmoid_node]
[ "def", "convert_logistic_regression_output", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", "=", "node", "[", "\"name\"", "]", "input1_idx", "=", "kwargs", "[", "\"index_lookup\"", "]", "[", "node", "[", "\"inputs\"", "]", "[", "0", "]", "[", "0", "]", "]", "input1", "=", "kwargs", "[", "\"proc_nodes\"", "]", "[", "input1_idx", "]", "sigmoid_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Sigmoid\"", ",", "[", "input1", ".", "name", "]", ",", "[", "name", "]", ",", "name", "=", "name", ")", "return", "[", "sigmoid_node", "]" ]
Map MXNet's SoftmaxOutput operator attributes to onnx's Softmax operator and return the created node.
[ "Map", "MXNet", "s", "SoftmaxOutput", "operator", "attributes", "to", "onnx", "s", "Softmax", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L825-L838
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_concat
def convert_concat(node, **kwargs): """Map MXNet's Concat operator attributes to onnx's Concat operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) axis = int(attrs.get("dim", 1)) concat_node = onnx.helper.make_node( "Concat", input_nodes, [name], axis=axis, name=name ) return [concat_node]
python
def convert_concat(node, **kwargs): """Map MXNet's Concat operator attributes to onnx's Concat operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) axis = int(attrs.get("dim", 1)) concat_node = onnx.helper.make_node( "Concat", input_nodes, [name], axis=axis, name=name ) return [concat_node]
[ "def", "convert_concat", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "axis", "=", "int", "(", "attrs", ".", "get", "(", "\"dim\"", ",", "1", ")", ")", "concat_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Concat\"", ",", "input_nodes", ",", "[", "name", "]", ",", "axis", "=", "axis", ",", "name", "=", "name", ")", "return", "[", "concat_node", "]" ]
Map MXNet's Concat operator attributes to onnx's Concat operator and return the created node.
[ "Map", "MXNet", "s", "Concat", "operator", "attributes", "to", "onnx", "s", "Concat", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L851-L865
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_transpose
def convert_transpose(node, **kwargs): """Map MXNet's transpose operator attributes to onnx's Transpose operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) axes = attrs.get("axes", ()) if axes: axes = tuple(map(int, re.findall(r'\d+', axes))) transpose_node = onnx.helper.make_node( "Transpose", input_nodes, [name], perm=axes, name=name ) else: transpose_node = onnx.helper.make_node( "Transpose", input_nodes, [name], name=name ) return [transpose_node]
python
def convert_transpose(node, **kwargs): """Map MXNet's transpose operator attributes to onnx's Transpose operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) axes = attrs.get("axes", ()) if axes: axes = tuple(map(int, re.findall(r'\d+', axes))) transpose_node = onnx.helper.make_node( "Transpose", input_nodes, [name], perm=axes, name=name ) else: transpose_node = onnx.helper.make_node( "Transpose", input_nodes, [name], name=name ) return [transpose_node]
[ "def", "convert_transpose", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "axes", "=", "attrs", ".", "get", "(", "\"axes\"", ",", "(", ")", ")", "if", "axes", ":", "axes", "=", "tuple", "(", "map", "(", "int", ",", "re", ".", "findall", "(", "r'\\d+'", ",", "axes", ")", ")", ")", "transpose_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Transpose\"", ",", "input_nodes", ",", "[", "name", "]", ",", "perm", "=", "axes", ",", "name", "=", "name", ")", "else", ":", "transpose_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Transpose\"", ",", "input_nodes", ",", "[", "name", "]", ",", "name", "=", "name", ")", "return", "[", "transpose_node", "]" ]
Map MXNet's transpose operator attributes to onnx's Transpose operator and return the created node.
[ "Map", "MXNet", "s", "transpose", "operator", "attributes", "to", "onnx", "s", "Transpose", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L869-L894
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_lrn
def convert_lrn(node, **kwargs): """Map MXNet's LRN operator attributes to onnx's LRN operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) alpha = float(attrs.get("alpha", 0.0001)) beta = float(attrs.get("beta", 0.75)) bias = float(attrs.get("knorm", 1.0)) size = int(attrs.get("nsize")) lrn_node = onnx.helper.make_node( "LRN", inputs=input_nodes, outputs=[name], name=name, alpha=alpha, beta=beta, bias=bias, size=size ) return [lrn_node]
python
def convert_lrn(node, **kwargs): """Map MXNet's LRN operator attributes to onnx's LRN operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) alpha = float(attrs.get("alpha", 0.0001)) beta = float(attrs.get("beta", 0.75)) bias = float(attrs.get("knorm", 1.0)) size = int(attrs.get("nsize")) lrn_node = onnx.helper.make_node( "LRN", inputs=input_nodes, outputs=[name], name=name, alpha=alpha, beta=beta, bias=bias, size=size ) return [lrn_node]
[ "def", "convert_lrn", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "alpha", "=", "float", "(", "attrs", ".", "get", "(", "\"alpha\"", ",", "0.0001", ")", ")", "beta", "=", "float", "(", "attrs", ".", "get", "(", "\"beta\"", ",", "0.75", ")", ")", "bias", "=", "float", "(", "attrs", ".", "get", "(", "\"knorm\"", ",", "1.0", ")", ")", "size", "=", "int", "(", "attrs", ".", "get", "(", "\"nsize\"", ")", ")", "lrn_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"LRN\"", ",", "inputs", "=", "input_nodes", ",", "outputs", "=", "[", "name", "]", ",", "name", "=", "name", ",", "alpha", "=", "alpha", ",", "beta", "=", "beta", ",", "bias", "=", "bias", ",", "size", "=", "size", ")", "return", "[", "lrn_node", "]" ]
Map MXNet's LRN operator attributes to onnx's LRN operator and return the created node.
[ "Map", "MXNet", "s", "LRN", "operator", "attributes", "to", "onnx", "s", "LRN", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L898-L920
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_l2normalization
def convert_l2normalization(node, **kwargs): """Map MXNet's L2Normalization operator attributes to onnx's LpNormalization operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) mode = attrs.get("mode", "instance") if mode != "channel": raise AttributeError("L2Normalization: ONNX currently supports channel mode only") l2norm_node = onnx.helper.make_node( "LpNormalization", input_nodes, [name], axis=1, # channel only name=name ) return [l2norm_node]
python
def convert_l2normalization(node, **kwargs): """Map MXNet's L2Normalization operator attributes to onnx's LpNormalization operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) mode = attrs.get("mode", "instance") if mode != "channel": raise AttributeError("L2Normalization: ONNX currently supports channel mode only") l2norm_node = onnx.helper.make_node( "LpNormalization", input_nodes, [name], axis=1, # channel only name=name ) return [l2norm_node]
[ "def", "convert_l2normalization", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "mode", "=", "attrs", ".", "get", "(", "\"mode\"", ",", "\"instance\"", ")", "if", "mode", "!=", "\"channel\"", ":", "raise", "AttributeError", "(", "\"L2Normalization: ONNX currently supports channel mode only\"", ")", "l2norm_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"LpNormalization\"", ",", "input_nodes", ",", "[", "name", "]", ",", "axis", "=", "1", ",", "# channel only", "name", "=", "name", ")", "return", "[", "l2norm_node", "]" ]
Map MXNet's L2Normalization operator attributes to onnx's LpNormalization operator and return the created node.
[ "Map", "MXNet", "s", "L2Normalization", "operator", "attributes", "to", "onnx", "s", "LpNormalization", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L924-L942
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_dropout
def convert_dropout(node, **kwargs): """Map MXNet's Dropout operator attributes to onnx's Dropout operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) probability = float(attrs.get("p", 0.5)) dropout_node = onnx.helper.make_node( "Dropout", input_nodes, [name], ratio=probability, name=name ) return [dropout_node]
python
def convert_dropout(node, **kwargs): """Map MXNet's Dropout operator attributes to onnx's Dropout operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) probability = float(attrs.get("p", 0.5)) dropout_node = onnx.helper.make_node( "Dropout", input_nodes, [name], ratio=probability, name=name ) return [dropout_node]
[ "def", "convert_dropout", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "probability", "=", "float", "(", "attrs", ".", "get", "(", "\"p\"", ",", "0.5", ")", ")", "dropout_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Dropout\"", ",", "input_nodes", ",", "[", "name", "]", ",", "ratio", "=", "probability", ",", "name", "=", "name", ")", "return", "[", "dropout_node", "]" ]
Map MXNet's Dropout operator attributes to onnx's Dropout operator and return the created node.
[ "Map", "MXNet", "s", "Dropout", "operator", "attributes", "to", "onnx", "s", "Dropout", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L946-L961
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_clip
def convert_clip(node, **kwargs): """Map MXNet's Clip operator attributes to onnx's Clip operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) a_min = np.float(attrs.get('a_min', -np.inf)) a_max = np.float(attrs.get('a_max', np.inf)) clip_node = onnx.helper.make_node( "Clip", input_nodes, [name], name=name, min=a_min, max=a_max ) return [clip_node]
python
def convert_clip(node, **kwargs): """Map MXNet's Clip operator attributes to onnx's Clip operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) a_min = np.float(attrs.get('a_min', -np.inf)) a_max = np.float(attrs.get('a_max', np.inf)) clip_node = onnx.helper.make_node( "Clip", input_nodes, [name], name=name, min=a_min, max=a_max ) return [clip_node]
[ "def", "convert_clip", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "a_min", "=", "np", ".", "float", "(", "attrs", ".", "get", "(", "'a_min'", ",", "-", "np", ".", "inf", ")", ")", "a_max", "=", "np", ".", "float", "(", "attrs", ".", "get", "(", "'a_max'", ",", "np", ".", "inf", ")", ")", "clip_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Clip\"", ",", "input_nodes", ",", "[", "name", "]", ",", "name", "=", "name", ",", "min", "=", "a_min", ",", "max", "=", "a_max", ")", "return", "[", "clip_node", "]" ]
Map MXNet's Clip operator attributes to onnx's Clip operator and return the created node.
[ "Map", "MXNet", "s", "Clip", "operator", "attributes", "to", "onnx", "s", "Clip", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L972-L989
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
scalar_op_helper
def scalar_op_helper(node, op_name, **kwargs): """Helper function for scalar arithmetic operations""" name, input_nodes, attrs = get_inputs(node, kwargs) from onnx import numpy_helper input_type = kwargs["in_type"] scalar_value = np.array([attrs.get("scalar", 1)], dtype=onnx.mapping.TENSOR_TYPE_TO_NP_TYPE[input_type]) initializer = kwargs["initializer"] flag = True # If the input value is in initializer, just multiply with scalar input # and create a new initializer for i in initializer: if i.name == input_nodes[0]: if op_name == 'Mul': new_initializer = numpy_helper.to_array(i) * scalar_value[0] elif op_name == 'Sub': if name.startswith("_rminusscalar"): new_initializer = scalar_value[0] - numpy_helper.to_array(i) else: new_initializer = numpy_helper.to_array(i) - scalar_value[0] elif op_name == 'Add': new_initializer = numpy_helper.to_array(i) + scalar_value[0] elif op_name == 'Div': if name.startswith("_rdivscalar"): new_initializer = scalar_value[0] / numpy_helper.to_array(i) else: new_initializer = numpy_helper.to_array(i) / scalar_value[0] elif op_name == 'Pow': new_initializer = numpy_helper.to_array(i) ** scalar_value[0] flag = False break # else create a new tensor of the scalar value, add it in initializer if flag is True: dims = np.shape(scalar_value) scalar_op_name = "scalar_op" + str(kwargs["idx"]) tensor_node = onnx.helper.make_tensor_value_info(scalar_op_name, input_type, dims) initializer.append( onnx.helper.make_tensor( name=scalar_op_name, data_type=input_type, dims=dims, vals=scalar_value, raw=False, ) ) mul_node = onnx.helper.make_node( op_name, [input_nodes[0], scalar_op_name], [name], name=name ) return [tensor_node, mul_node] else: data_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[new_initializer.dtype] dims = np.shape(new_initializer) new_a_node = input_nodes[0] + str(kwargs["idx"]) tensor_node = onnx.helper.make_tensor_value_info(new_a_node, data_type, dims) initializer.append( onnx.helper.make_tensor( name=new_a_node, data_type=data_type, dims=dims, vals=new_initializer, raw=False, ) ) return [tensor_node]
python
def scalar_op_helper(node, op_name, **kwargs): """Helper function for scalar arithmetic operations""" name, input_nodes, attrs = get_inputs(node, kwargs) from onnx import numpy_helper input_type = kwargs["in_type"] scalar_value = np.array([attrs.get("scalar", 1)], dtype=onnx.mapping.TENSOR_TYPE_TO_NP_TYPE[input_type]) initializer = kwargs["initializer"] flag = True # If the input value is in initializer, just multiply with scalar input # and create a new initializer for i in initializer: if i.name == input_nodes[0]: if op_name == 'Mul': new_initializer = numpy_helper.to_array(i) * scalar_value[0] elif op_name == 'Sub': if name.startswith("_rminusscalar"): new_initializer = scalar_value[0] - numpy_helper.to_array(i) else: new_initializer = numpy_helper.to_array(i) - scalar_value[0] elif op_name == 'Add': new_initializer = numpy_helper.to_array(i) + scalar_value[0] elif op_name == 'Div': if name.startswith("_rdivscalar"): new_initializer = scalar_value[0] / numpy_helper.to_array(i) else: new_initializer = numpy_helper.to_array(i) / scalar_value[0] elif op_name == 'Pow': new_initializer = numpy_helper.to_array(i) ** scalar_value[0] flag = False break # else create a new tensor of the scalar value, add it in initializer if flag is True: dims = np.shape(scalar_value) scalar_op_name = "scalar_op" + str(kwargs["idx"]) tensor_node = onnx.helper.make_tensor_value_info(scalar_op_name, input_type, dims) initializer.append( onnx.helper.make_tensor( name=scalar_op_name, data_type=input_type, dims=dims, vals=scalar_value, raw=False, ) ) mul_node = onnx.helper.make_node( op_name, [input_nodes[0], scalar_op_name], [name], name=name ) return [tensor_node, mul_node] else: data_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[new_initializer.dtype] dims = np.shape(new_initializer) new_a_node = input_nodes[0] + str(kwargs["idx"]) tensor_node = onnx.helper.make_tensor_value_info(new_a_node, data_type, dims) initializer.append( onnx.helper.make_tensor( name=new_a_node, data_type=data_type, dims=dims, vals=new_initializer, raw=False, ) ) return [tensor_node]
[ "def", "scalar_op_helper", "(", "node", ",", "op_name", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "from", "onnx", "import", "numpy_helper", "input_type", "=", "kwargs", "[", "\"in_type\"", "]", "scalar_value", "=", "np", ".", "array", "(", "[", "attrs", ".", "get", "(", "\"scalar\"", ",", "1", ")", "]", ",", "dtype", "=", "onnx", ".", "mapping", ".", "TENSOR_TYPE_TO_NP_TYPE", "[", "input_type", "]", ")", "initializer", "=", "kwargs", "[", "\"initializer\"", "]", "flag", "=", "True", "# If the input value is in initializer, just multiply with scalar input", "# and create a new initializer", "for", "i", "in", "initializer", ":", "if", "i", ".", "name", "==", "input_nodes", "[", "0", "]", ":", "if", "op_name", "==", "'Mul'", ":", "new_initializer", "=", "numpy_helper", ".", "to_array", "(", "i", ")", "*", "scalar_value", "[", "0", "]", "elif", "op_name", "==", "'Sub'", ":", "if", "name", ".", "startswith", "(", "\"_rminusscalar\"", ")", ":", "new_initializer", "=", "scalar_value", "[", "0", "]", "-", "numpy_helper", ".", "to_array", "(", "i", ")", "else", ":", "new_initializer", "=", "numpy_helper", ".", "to_array", "(", "i", ")", "-", "scalar_value", "[", "0", "]", "elif", "op_name", "==", "'Add'", ":", "new_initializer", "=", "numpy_helper", ".", "to_array", "(", "i", ")", "+", "scalar_value", "[", "0", "]", "elif", "op_name", "==", "'Div'", ":", "if", "name", ".", "startswith", "(", "\"_rdivscalar\"", ")", ":", "new_initializer", "=", "scalar_value", "[", "0", "]", "/", "numpy_helper", ".", "to_array", "(", "i", ")", "else", ":", "new_initializer", "=", "numpy_helper", ".", "to_array", "(", "i", ")", "/", "scalar_value", "[", "0", "]", "elif", "op_name", "==", "'Pow'", ":", "new_initializer", "=", "numpy_helper", ".", "to_array", "(", "i", ")", "**", "scalar_value", "[", "0", "]", "flag", "=", "False", "break", "# else create a new tensor of the scalar value, add it in initializer", "if", "flag", "is", "True", ":", "dims", "=", "np", ".", "shape", "(", "scalar_value", ")", "scalar_op_name", "=", "\"scalar_op\"", "+", "str", "(", "kwargs", "[", "\"idx\"", "]", ")", "tensor_node", "=", "onnx", ".", "helper", ".", "make_tensor_value_info", "(", "scalar_op_name", ",", "input_type", ",", "dims", ")", "initializer", ".", "append", "(", "onnx", ".", "helper", ".", "make_tensor", "(", "name", "=", "scalar_op_name", ",", "data_type", "=", "input_type", ",", "dims", "=", "dims", ",", "vals", "=", "scalar_value", ",", "raw", "=", "False", ",", ")", ")", "mul_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "op_name", ",", "[", "input_nodes", "[", "0", "]", ",", "scalar_op_name", "]", ",", "[", "name", "]", ",", "name", "=", "name", ")", "return", "[", "tensor_node", ",", "mul_node", "]", "else", ":", "data_type", "=", "onnx", ".", "mapping", ".", "NP_TYPE_TO_TENSOR_TYPE", "[", "new_initializer", ".", "dtype", "]", "dims", "=", "np", ".", "shape", "(", "new_initializer", ")", "new_a_node", "=", "input_nodes", "[", "0", "]", "+", "str", "(", "kwargs", "[", "\"idx\"", "]", ")", "tensor_node", "=", "onnx", ".", "helper", ".", "make_tensor_value_info", "(", "new_a_node", ",", "data_type", ",", "dims", ")", "initializer", ".", "append", "(", "onnx", ".", "helper", ".", "make_tensor", "(", "name", "=", "new_a_node", ",", "data_type", "=", "data_type", ",", "dims", "=", "dims", ",", "vals", "=", "new_initializer", ",", "raw", "=", "False", ",", ")", ")", "return", "[", "tensor_node", "]" ]
Helper function for scalar arithmetic operations
[ "Helper", "function", "for", "scalar", "arithmetic", "operations" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L992-L1066
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_argmax
def convert_argmax(node, **kwargs): """Map MXNet's argmax operator attributes to onnx's ArgMax operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) axis = int(attrs.get("axis")) keepdims = get_boolean_attribute_value(attrs, "keepdims") node = onnx.helper.make_node( 'ArgMax', inputs=input_nodes, axis=axis, keepdims=keepdims, outputs=[name], name=name ) return [node]
python
def convert_argmax(node, **kwargs): """Map MXNet's argmax operator attributes to onnx's ArgMax operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) axis = int(attrs.get("axis")) keepdims = get_boolean_attribute_value(attrs, "keepdims") node = onnx.helper.make_node( 'ArgMax', inputs=input_nodes, axis=axis, keepdims=keepdims, outputs=[name], name=name ) return [node]
[ "def", "convert_argmax", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "axis", "=", "int", "(", "attrs", ".", "get", "(", "\"axis\"", ")", ")", "keepdims", "=", "get_boolean_attribute_value", "(", "attrs", ",", "\"keepdims\"", ")", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'ArgMax'", ",", "inputs", "=", "input_nodes", ",", "axis", "=", "axis", ",", "keepdims", "=", "keepdims", ",", "outputs", "=", "[", "name", "]", ",", "name", "=", "name", ")", "return", "[", "node", "]" ]
Map MXNet's argmax operator attributes to onnx's ArgMax operator and return the created node.
[ "Map", "MXNet", "s", "argmax", "operator", "attributes", "to", "onnx", "s", "ArgMax", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1131-L1148
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_reshape
def convert_reshape(node, **kwargs): """Map MXNet's Reshape operator attributes to onnx's Reshape operator. Converts output shape attribute to output shape tensor and return multiple created nodes. """ name, input_nodes, attrs = get_inputs(node, kwargs) output_shape_list = convert_string_to_list(attrs["shape"]) initializer = kwargs["initializer"] output_shape_np = np.array(output_shape_list, dtype='int64') data_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[output_shape_np.dtype] dims = np.shape(output_shape_np) output_shape_name = "reshape_attr_tensor" + str(kwargs["idx"]) tensor_node = onnx.helper.make_tensor_value_info(output_shape_name, data_type, dims) initializer.append( onnx.helper.make_tensor( name=output_shape_name, data_type=data_type, dims=dims, vals=output_shape_list, raw=False, ) ) input_nodes.append(output_shape_name) not_supported_shape = [-2, -3, -4] for val in output_shape_list: if val in not_supported_shape: raise AttributeError("Reshape: Shape value not supported in ONNX", val) reshape_node = onnx.helper.make_node( "Reshape", input_nodes, [name], name=name ) return [tensor_node, reshape_node]
python
def convert_reshape(node, **kwargs): """Map MXNet's Reshape operator attributes to onnx's Reshape operator. Converts output shape attribute to output shape tensor and return multiple created nodes. """ name, input_nodes, attrs = get_inputs(node, kwargs) output_shape_list = convert_string_to_list(attrs["shape"]) initializer = kwargs["initializer"] output_shape_np = np.array(output_shape_list, dtype='int64') data_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[output_shape_np.dtype] dims = np.shape(output_shape_np) output_shape_name = "reshape_attr_tensor" + str(kwargs["idx"]) tensor_node = onnx.helper.make_tensor_value_info(output_shape_name, data_type, dims) initializer.append( onnx.helper.make_tensor( name=output_shape_name, data_type=data_type, dims=dims, vals=output_shape_list, raw=False, ) ) input_nodes.append(output_shape_name) not_supported_shape = [-2, -3, -4] for val in output_shape_list: if val in not_supported_shape: raise AttributeError("Reshape: Shape value not supported in ONNX", val) reshape_node = onnx.helper.make_node( "Reshape", input_nodes, [name], name=name ) return [tensor_node, reshape_node]
[ "def", "convert_reshape", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "output_shape_list", "=", "convert_string_to_list", "(", "attrs", "[", "\"shape\"", "]", ")", "initializer", "=", "kwargs", "[", "\"initializer\"", "]", "output_shape_np", "=", "np", ".", "array", "(", "output_shape_list", ",", "dtype", "=", "'int64'", ")", "data_type", "=", "onnx", ".", "mapping", ".", "NP_TYPE_TO_TENSOR_TYPE", "[", "output_shape_np", ".", "dtype", "]", "dims", "=", "np", ".", "shape", "(", "output_shape_np", ")", "output_shape_name", "=", "\"reshape_attr_tensor\"", "+", "str", "(", "kwargs", "[", "\"idx\"", "]", ")", "tensor_node", "=", "onnx", ".", "helper", ".", "make_tensor_value_info", "(", "output_shape_name", ",", "data_type", ",", "dims", ")", "initializer", ".", "append", "(", "onnx", ".", "helper", ".", "make_tensor", "(", "name", "=", "output_shape_name", ",", "data_type", "=", "data_type", ",", "dims", "=", "dims", ",", "vals", "=", "output_shape_list", ",", "raw", "=", "False", ",", ")", ")", "input_nodes", ".", "append", "(", "output_shape_name", ")", "not_supported_shape", "=", "[", "-", "2", ",", "-", "3", ",", "-", "4", "]", "for", "val", "in", "output_shape_list", ":", "if", "val", "in", "not_supported_shape", ":", "raise", "AttributeError", "(", "\"Reshape: Shape value not supported in ONNX\"", ",", "val", ")", "reshape_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Reshape\"", ",", "input_nodes", ",", "[", "name", "]", ",", "name", "=", "name", ")", "return", "[", "tensor_node", ",", "reshape_node", "]" ]
Map MXNet's Reshape operator attributes to onnx's Reshape operator. Converts output shape attribute to output shape tensor and return multiple created nodes.
[ "Map", "MXNet", "s", "Reshape", "operator", "attributes", "to", "onnx", "s", "Reshape", "operator", ".", "Converts", "output", "shape", "attribute", "to", "output", "shape", "tensor", "and", "return", "multiple", "created", "nodes", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1422-L1464
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_cast
def convert_cast(node, **kwargs): """Map MXNet's Cast operator attributes to onnx's Cast operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) dtype = attrs["dtype"] # dtype can be mapped only with types from TensorProto # float32 is mapped to float and float64 to double in onnx # following tensorproto mapping https://github.com/onnx/onnx/blob/master/onnx/mapping.py if dtype == 'float32': dtype = 'float' elif dtype == 'float64': dtype = 'double' node = onnx.helper.make_node( "Cast", input_nodes, [name], to=getattr(onnx.TensorProto, dtype.upper()), name=name, ) return [node]
python
def convert_cast(node, **kwargs): """Map MXNet's Cast operator attributes to onnx's Cast operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) dtype = attrs["dtype"] # dtype can be mapped only with types from TensorProto # float32 is mapped to float and float64 to double in onnx # following tensorproto mapping https://github.com/onnx/onnx/blob/master/onnx/mapping.py if dtype == 'float32': dtype = 'float' elif dtype == 'float64': dtype = 'double' node = onnx.helper.make_node( "Cast", input_nodes, [name], to=getattr(onnx.TensorProto, dtype.upper()), name=name, ) return [node]
[ "def", "convert_cast", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "dtype", "=", "attrs", "[", "\"dtype\"", "]", "# dtype can be mapped only with types from TensorProto", "# float32 is mapped to float and float64 to double in onnx", "# following tensorproto mapping https://github.com/onnx/onnx/blob/master/onnx/mapping.py", "if", "dtype", "==", "'float32'", ":", "dtype", "=", "'float'", "elif", "dtype", "==", "'float64'", ":", "dtype", "=", "'double'", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Cast\"", ",", "input_nodes", ",", "[", "name", "]", ",", "to", "=", "getattr", "(", "onnx", ".", "TensorProto", ",", "dtype", ".", "upper", "(", ")", ")", ",", "name", "=", "name", ",", ")", "return", "[", "node", "]" ]
Map MXNet's Cast operator attributes to onnx's Cast operator and return the created node.
[ "Map", "MXNet", "s", "Cast", "operator", "attributes", "to", "onnx", "s", "Cast", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1467-L1490
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_slice_axis
def convert_slice_axis(node, **kwargs): """Map MXNet's slice_axis operator attributes to onnx's Slice operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) axes = int(attrs.get("axis")) starts = int(attrs.get("begin")) ends = int(attrs.get("end", None)) if not ends: raise ValueError("Slice: ONNX doesnt't support 'None' in 'end' attribute") node = onnx.helper.make_node( "Slice", input_nodes, [name], axes=[axes], starts=[starts], ends=[ends], name=name, ) return [node]
python
def convert_slice_axis(node, **kwargs): """Map MXNet's slice_axis operator attributes to onnx's Slice operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) axes = int(attrs.get("axis")) starts = int(attrs.get("begin")) ends = int(attrs.get("end", None)) if not ends: raise ValueError("Slice: ONNX doesnt't support 'None' in 'end' attribute") node = onnx.helper.make_node( "Slice", input_nodes, [name], axes=[axes], starts=[starts], ends=[ends], name=name, ) return [node]
[ "def", "convert_slice_axis", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "axes", "=", "int", "(", "attrs", ".", "get", "(", "\"axis\"", ")", ")", "starts", "=", "int", "(", "attrs", ".", "get", "(", "\"begin\"", ")", ")", "ends", "=", "int", "(", "attrs", ".", "get", "(", "\"end\"", ",", "None", ")", ")", "if", "not", "ends", ":", "raise", "ValueError", "(", "\"Slice: ONNX doesnt't support 'None' in 'end' attribute\"", ")", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Slice\"", ",", "input_nodes", ",", "[", "name", "]", ",", "axes", "=", "[", "axes", "]", ",", "starts", "=", "[", "starts", "]", ",", "ends", "=", "[", "ends", "]", ",", "name", "=", "name", ",", ")", "return", "[", "node", "]" ]
Map MXNet's slice_axis operator attributes to onnx's Slice operator and return the created node.
[ "Map", "MXNet", "s", "slice_axis", "operator", "attributes", "to", "onnx", "s", "Slice", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1494-L1515
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_slice_channel
def convert_slice_channel(node, **kwargs): """Map MXNet's SliceChannel operator attributes to onnx's Squeeze or Split operator based on squeeze_axis attribute and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) num_outputs = int(attrs.get("num_outputs")) axis = int(attrs.get("axis", 1)) squeeze_axis = int(attrs.get("squeeze_axis", 0)) if squeeze_axis == 1 and num_outputs == 1: node = onnx.helper.make_node( "Squeeze", input_nodes, [name], axes=[axis], name=name, ) return [node] elif squeeze_axis == 0 and num_outputs > 1: in_shape = kwargs.get('in_shape')[0] split = in_shape[axis] // num_outputs node = onnx.helper.make_node( "Split", input_nodes, [name+'_output'+str(i) for i in range(num_outputs)], axis=axis, split=[split for _ in range(num_outputs)], name=name, ) return [node] else: raise NotImplementedError("SliceChannel operator with num_outputs>1 and" "squeeze_axis true is not implemented.")
python
def convert_slice_channel(node, **kwargs): """Map MXNet's SliceChannel operator attributes to onnx's Squeeze or Split operator based on squeeze_axis attribute and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) num_outputs = int(attrs.get("num_outputs")) axis = int(attrs.get("axis", 1)) squeeze_axis = int(attrs.get("squeeze_axis", 0)) if squeeze_axis == 1 and num_outputs == 1: node = onnx.helper.make_node( "Squeeze", input_nodes, [name], axes=[axis], name=name, ) return [node] elif squeeze_axis == 0 and num_outputs > 1: in_shape = kwargs.get('in_shape')[0] split = in_shape[axis] // num_outputs node = onnx.helper.make_node( "Split", input_nodes, [name+'_output'+str(i) for i in range(num_outputs)], axis=axis, split=[split for _ in range(num_outputs)], name=name, ) return [node] else: raise NotImplementedError("SliceChannel operator with num_outputs>1 and" "squeeze_axis true is not implemented.")
[ "def", "convert_slice_channel", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "num_outputs", "=", "int", "(", "attrs", ".", "get", "(", "\"num_outputs\"", ")", ")", "axis", "=", "int", "(", "attrs", ".", "get", "(", "\"axis\"", ",", "1", ")", ")", "squeeze_axis", "=", "int", "(", "attrs", ".", "get", "(", "\"squeeze_axis\"", ",", "0", ")", ")", "if", "squeeze_axis", "==", "1", "and", "num_outputs", "==", "1", ":", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Squeeze\"", ",", "input_nodes", ",", "[", "name", "]", ",", "axes", "=", "[", "axis", "]", ",", "name", "=", "name", ",", ")", "return", "[", "node", "]", "elif", "squeeze_axis", "==", "0", "and", "num_outputs", ">", "1", ":", "in_shape", "=", "kwargs", ".", "get", "(", "'in_shape'", ")", "[", "0", "]", "split", "=", "in_shape", "[", "axis", "]", "//", "num_outputs", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Split\"", ",", "input_nodes", ",", "[", "name", "+", "'_output'", "+", "str", "(", "i", ")", "for", "i", "in", "range", "(", "num_outputs", ")", "]", ",", "axis", "=", "axis", ",", "split", "=", "[", "split", "for", "_", "in", "range", "(", "num_outputs", ")", "]", ",", "name", "=", "name", ",", ")", "return", "[", "node", "]", "else", ":", "raise", "NotImplementedError", "(", "\"SliceChannel operator with num_outputs>1 and\"", "\"squeeze_axis true is not implemented.\"", ")" ]
Map MXNet's SliceChannel operator attributes to onnx's Squeeze or Split operator based on squeeze_axis attribute and return the created node.
[ "Map", "MXNet", "s", "SliceChannel", "operator", "attributes", "to", "onnx", "s", "Squeeze", "or", "Split", "operator", "based", "on", "squeeze_axis", "attribute", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1519-L1553
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_expand_dims
def convert_expand_dims(node, **kwargs): """Map MXNet's expand_dims operator attributes to onnx's Unsqueeze operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) axis = int(attrs.get("axis")) node = onnx.helper.make_node( "Unsqueeze", input_nodes, [name], axes=[axis], name=name, ) return [node]
python
def convert_expand_dims(node, **kwargs): """Map MXNet's expand_dims operator attributes to onnx's Unsqueeze operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) axis = int(attrs.get("axis")) node = onnx.helper.make_node( "Unsqueeze", input_nodes, [name], axes=[axis], name=name, ) return [node]
[ "def", "convert_expand_dims", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "axis", "=", "int", "(", "attrs", ".", "get", "(", "\"axis\"", ")", ")", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Unsqueeze\"", ",", "input_nodes", ",", "[", "name", "]", ",", "axes", "=", "[", "axis", "]", ",", "name", "=", "name", ",", ")", "return", "[", "node", "]" ]
Map MXNet's expand_dims operator attributes to onnx's Unsqueeze operator and return the created node.
[ "Map", "MXNet", "s", "expand_dims", "operator", "attributes", "to", "onnx", "s", "Unsqueeze", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1557-L1572
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_squeeze
def convert_squeeze(node, **kwargs): """Map MXNet's squeeze operator attributes to onnx's squeeze operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) axis = attrs.get("axis", None) if not axis: raise AttributeError("Squeeze: Missing axis attribute: ONNX currently requires axis to " "be specified for squeeze operator") axis = convert_string_to_list(axis) node = onnx.helper.make_node( "Squeeze", input_nodes, [name], axes=axis, name=name, ) return [node]
python
def convert_squeeze(node, **kwargs): """Map MXNet's squeeze operator attributes to onnx's squeeze operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) axis = attrs.get("axis", None) if not axis: raise AttributeError("Squeeze: Missing axis attribute: ONNX currently requires axis to " "be specified for squeeze operator") axis = convert_string_to_list(axis) node = onnx.helper.make_node( "Squeeze", input_nodes, [name], axes=axis, name=name, ) return [node]
[ "def", "convert_squeeze", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "axis", "=", "attrs", ".", "get", "(", "\"axis\"", ",", "None", ")", "if", "not", "axis", ":", "raise", "AttributeError", "(", "\"Squeeze: Missing axis attribute: ONNX currently requires axis to \"", "\"be specified for squeeze operator\"", ")", "axis", "=", "convert_string_to_list", "(", "axis", ")", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Squeeze\"", ",", "input_nodes", ",", "[", "name", "]", ",", "axes", "=", "axis", ",", "name", "=", "name", ",", ")", "return", "[", "node", "]" ]
Map MXNet's squeeze operator attributes to onnx's squeeze operator and return the created node.
[ "Map", "MXNet", "s", "squeeze", "operator", "attributes", "to", "onnx", "s", "squeeze", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1575-L1594
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_depthtospace
def convert_depthtospace(node, **kwargs): """Map MXNet's depth_to_space operator attributes to onnx's DepthToSpace operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) blksize = int(attrs.get("block_size", 0)) node = onnx.helper.make_node( "DepthToSpace", input_nodes, [name], blocksize=blksize, name=name, ) return [node]
python
def convert_depthtospace(node, **kwargs): """Map MXNet's depth_to_space operator attributes to onnx's DepthToSpace operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) blksize = int(attrs.get("block_size", 0)) node = onnx.helper.make_node( "DepthToSpace", input_nodes, [name], blocksize=blksize, name=name, ) return [node]
[ "def", "convert_depthtospace", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "blksize", "=", "int", "(", "attrs", ".", "get", "(", "\"block_size\"", ",", "0", ")", ")", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"DepthToSpace\"", ",", "input_nodes", ",", "[", "name", "]", ",", "blocksize", "=", "blksize", ",", "name", "=", "name", ",", ")", "return", "[", "node", "]" ]
Map MXNet's depth_to_space operator attributes to onnx's DepthToSpace operator and return the created node.
[ "Map", "MXNet", "s", "depth_to_space", "operator", "attributes", "to", "onnx", "s", "DepthToSpace", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1633-L1648
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_square
def convert_square(node, **kwargs): """Map MXNet's square operator attributes to onnx's Pow operator and return the created node. """ name, input_nodes, _ = get_inputs(node, kwargs) initializer = kwargs["initializer"] data_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype('int64')] power2_name = "square_tensor" + str(kwargs["idx"]) tensor_node = onnx.helper.make_tensor_value_info(power2_name, data_type, (1,)) initializer.append( onnx.helper.make_tensor( name=power2_name, data_type=data_type, dims=(1,), vals=[2], raw=False, ) ) input_nodes.append(power2_name) node = onnx.helper.make_node( "Pow", input_nodes, [name], name=name ) return [tensor_node, node]
python
def convert_square(node, **kwargs): """Map MXNet's square operator attributes to onnx's Pow operator and return the created node. """ name, input_nodes, _ = get_inputs(node, kwargs) initializer = kwargs["initializer"] data_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype('int64')] power2_name = "square_tensor" + str(kwargs["idx"]) tensor_node = onnx.helper.make_tensor_value_info(power2_name, data_type, (1,)) initializer.append( onnx.helper.make_tensor( name=power2_name, data_type=data_type, dims=(1,), vals=[2], raw=False, ) ) input_nodes.append(power2_name) node = onnx.helper.make_node( "Pow", input_nodes, [name], name=name ) return [tensor_node, node]
[ "def", "convert_square", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "_", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "initializer", "=", "kwargs", "[", "\"initializer\"", "]", "data_type", "=", "onnx", ".", "mapping", ".", "NP_TYPE_TO_TENSOR_TYPE", "[", "np", ".", "dtype", "(", "'int64'", ")", "]", "power2_name", "=", "\"square_tensor\"", "+", "str", "(", "kwargs", "[", "\"idx\"", "]", ")", "tensor_node", "=", "onnx", ".", "helper", ".", "make_tensor_value_info", "(", "power2_name", ",", "data_type", ",", "(", "1", ",", ")", ")", "initializer", ".", "append", "(", "onnx", ".", "helper", ".", "make_tensor", "(", "name", "=", "power2_name", ",", "data_type", "=", "data_type", ",", "dims", "=", "(", "1", ",", ")", ",", "vals", "=", "[", "2", "]", ",", "raw", "=", "False", ",", ")", ")", "input_nodes", ".", "append", "(", "power2_name", ")", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Pow\"", ",", "input_nodes", ",", "[", "name", "]", ",", "name", "=", "name", ")", "return", "[", "tensor_node", ",", "node", "]" ]
Map MXNet's square operator attributes to onnx's Pow operator and return the created node.
[ "Map", "MXNet", "s", "square", "operator", "attributes", "to", "onnx", "s", "Pow", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1669-L1698
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_sum
def convert_sum(node, **kwargs): """Map MXNet's sum operator attributes to onnx's ReduceSum operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) mx_axis = attrs.get("axis", None) axes = convert_string_to_list(str(mx_axis)) if mx_axis is not None else None keepdims = get_boolean_attribute_value(attrs, "keepdims") if axes: node = onnx.helper.make_node( 'ReduceSum', inputs=input_nodes, outputs=[name], axes=axes, keepdims=keepdims, name=name ) else: node = onnx.helper.make_node( 'ReduceSum', inputs=input_nodes, outputs=[name], keepdims=keepdims, name=name ) return [node]
python
def convert_sum(node, **kwargs): """Map MXNet's sum operator attributes to onnx's ReduceSum operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) mx_axis = attrs.get("axis", None) axes = convert_string_to_list(str(mx_axis)) if mx_axis is not None else None keepdims = get_boolean_attribute_value(attrs, "keepdims") if axes: node = onnx.helper.make_node( 'ReduceSum', inputs=input_nodes, outputs=[name], axes=axes, keepdims=keepdims, name=name ) else: node = onnx.helper.make_node( 'ReduceSum', inputs=input_nodes, outputs=[name], keepdims=keepdims, name=name ) return [node]
[ "def", "convert_sum", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "mx_axis", "=", "attrs", ".", "get", "(", "\"axis\"", ",", "None", ")", "axes", "=", "convert_string_to_list", "(", "str", "(", "mx_axis", ")", ")", "if", "mx_axis", "is", "not", "None", "else", "None", "keepdims", "=", "get_boolean_attribute_value", "(", "attrs", ",", "\"keepdims\"", ")", "if", "axes", ":", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'ReduceSum'", ",", "inputs", "=", "input_nodes", ",", "outputs", "=", "[", "name", "]", ",", "axes", "=", "axes", ",", "keepdims", "=", "keepdims", ",", "name", "=", "name", ")", "else", ":", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'ReduceSum'", ",", "inputs", "=", "input_nodes", ",", "outputs", "=", "[", "name", "]", ",", "keepdims", "=", "keepdims", ",", "name", "=", "name", ")", "return", "[", "node", "]" ]
Map MXNet's sum operator attributes to onnx's ReduceSum operator and return the created node.
[ "Map", "MXNet", "s", "sum", "operator", "attributes", "to", "onnx", "s", "ReduceSum", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1701-L1729
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_hardsigmoid
def convert_hardsigmoid(node, **kwargs): """Map MXNet's hard_sigmoid operator attributes to onnx's HardSigmoid operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) # Converting to float32 alpha = float(attrs.get("alpha", 0.2)) beta = float(attrs.get("beta", 0.5)) node = onnx.helper.make_node( 'HardSigmoid', input_nodes, [name], alpha=alpha, beta=beta, name=name ) return [node]
python
def convert_hardsigmoid(node, **kwargs): """Map MXNet's hard_sigmoid operator attributes to onnx's HardSigmoid operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) # Converting to float32 alpha = float(attrs.get("alpha", 0.2)) beta = float(attrs.get("beta", 0.5)) node = onnx.helper.make_node( 'HardSigmoid', input_nodes, [name], alpha=alpha, beta=beta, name=name ) return [node]
[ "def", "convert_hardsigmoid", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "# Converting to float32", "alpha", "=", "float", "(", "attrs", ".", "get", "(", "\"alpha\"", ",", "0.2", ")", ")", "beta", "=", "float", "(", "attrs", ".", "get", "(", "\"beta\"", ",", "0.5", ")", ")", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'HardSigmoid'", ",", "input_nodes", ",", "[", "name", "]", ",", "alpha", "=", "alpha", ",", "beta", "=", "beta", ",", "name", "=", "name", ")", "return", "[", "node", "]" ]
Map MXNet's hard_sigmoid operator attributes to onnx's HardSigmoid operator and return the created node.
[ "Map", "MXNet", "s", "hard_sigmoid", "operator", "attributes", "to", "onnx", "s", "HardSigmoid", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1741-L1759
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_logsoftmax
def convert_logsoftmax(node, **kwargs): """Map MXNet's log_softmax operator attributes to onnx's LogSoftMax operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) # Converting to int axis = int(attrs.get("axis", -1)) temp = attrs.get("temperature", 'None') if temp != 'None': raise AttributeError("LogSoftMax: ONNX supports only temperature=None") node = onnx.helper.make_node( 'LogSoftmax', input_nodes, [name], axis=axis, name=name ) return [node]
python
def convert_logsoftmax(node, **kwargs): """Map MXNet's log_softmax operator attributes to onnx's LogSoftMax operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) # Converting to int axis = int(attrs.get("axis", -1)) temp = attrs.get("temperature", 'None') if temp != 'None': raise AttributeError("LogSoftMax: ONNX supports only temperature=None") node = onnx.helper.make_node( 'LogSoftmax', input_nodes, [name], axis=axis, name=name ) return [node]
[ "def", "convert_logsoftmax", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "# Converting to int", "axis", "=", "int", "(", "attrs", ".", "get", "(", "\"axis\"", ",", "-", "1", ")", ")", "temp", "=", "attrs", ".", "get", "(", "\"temperature\"", ",", "'None'", ")", "if", "temp", "!=", "'None'", ":", "raise", "AttributeError", "(", "\"LogSoftMax: ONNX supports only temperature=None\"", ")", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'LogSoftmax'", ",", "input_nodes", ",", "[", "name", "]", ",", "axis", "=", "axis", ",", "name", "=", "name", ")", "return", "[", "node", "]" ]
Map MXNet's log_softmax operator attributes to onnx's LogSoftMax operator and return the created node.
[ "Map", "MXNet", "s", "log_softmax", "operator", "attributes", "to", "onnx", "s", "LogSoftMax", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1824-L1843
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_norm
def convert_norm(node, **kwargs): """Map MXNet's norm operator attributes to onnx's ReduceL1 and ReduceL2 operators and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) mx_axis = attrs.get("axis", None) axes = convert_string_to_list(str(mx_axis)) if mx_axis else None keepdims = get_boolean_attribute_value(attrs, "keepdims") ord = int(attrs.get("ord", 2)) onnx_op_name = "ReduceL1" if ord == 1 else "ReduceL2" if axes: reduce_node = onnx.helper.make_node( onnx_op_name, input_nodes, [name], axes=axes, keepdims=keepdims, name=name ) return [reduce_node] else: reduce_node = onnx.helper.make_node( onnx_op_name, input_nodes, [name], keepdims=keepdims, name=name ) return [reduce_node]
python
def convert_norm(node, **kwargs): """Map MXNet's norm operator attributes to onnx's ReduceL1 and ReduceL2 operators and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) mx_axis = attrs.get("axis", None) axes = convert_string_to_list(str(mx_axis)) if mx_axis else None keepdims = get_boolean_attribute_value(attrs, "keepdims") ord = int(attrs.get("ord", 2)) onnx_op_name = "ReduceL1" if ord == 1 else "ReduceL2" if axes: reduce_node = onnx.helper.make_node( onnx_op_name, input_nodes, [name], axes=axes, keepdims=keepdims, name=name ) return [reduce_node] else: reduce_node = onnx.helper.make_node( onnx_op_name, input_nodes, [name], keepdims=keepdims, name=name ) return [reduce_node]
[ "def", "convert_norm", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "mx_axis", "=", "attrs", ".", "get", "(", "\"axis\"", ",", "None", ")", "axes", "=", "convert_string_to_list", "(", "str", "(", "mx_axis", ")", ")", "if", "mx_axis", "else", "None", "keepdims", "=", "get_boolean_attribute_value", "(", "attrs", ",", "\"keepdims\"", ")", "ord", "=", "int", "(", "attrs", ".", "get", "(", "\"ord\"", ",", "2", ")", ")", "onnx_op_name", "=", "\"ReduceL1\"", "if", "ord", "==", "1", "else", "\"ReduceL2\"", "if", "axes", ":", "reduce_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "onnx_op_name", ",", "input_nodes", ",", "[", "name", "]", ",", "axes", "=", "axes", ",", "keepdims", "=", "keepdims", ",", "name", "=", "name", ")", "return", "[", "reduce_node", "]", "else", ":", "reduce_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "onnx_op_name", ",", "input_nodes", ",", "[", "name", "]", ",", "keepdims", "=", "keepdims", ",", "name", "=", "name", ")", "return", "[", "reduce_node", "]" ]
Map MXNet's norm operator attributes to onnx's ReduceL1 and ReduceL2 operators and return the created node.
[ "Map", "MXNet", "s", "norm", "operator", "attributes", "to", "onnx", "s", "ReduceL1", "and", "ReduceL2", "operators", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1846-L1878
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_multinomial
def convert_multinomial(node, **kwargs): """Map MXNet's multinomial operator attributes to onnx's Multinomial operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) dtype = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(attrs.get("dtype", 'int32'))] sample_size = convert_string_to_list(attrs.get("shape", '1')) if len(sample_size) < 2: sample_size = sample_size[-1] else: raise AttributeError("ONNX currently supports integer sample_size only") node = onnx.helper.make_node( "Multinomial", input_nodes, [name], dtype=dtype, sample_size=sample_size, name=name, ) return [node]
python
def convert_multinomial(node, **kwargs): """Map MXNet's multinomial operator attributes to onnx's Multinomial operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) dtype = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(attrs.get("dtype", 'int32'))] sample_size = convert_string_to_list(attrs.get("shape", '1')) if len(sample_size) < 2: sample_size = sample_size[-1] else: raise AttributeError("ONNX currently supports integer sample_size only") node = onnx.helper.make_node( "Multinomial", input_nodes, [name], dtype=dtype, sample_size=sample_size, name=name, ) return [node]
[ "def", "convert_multinomial", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "dtype", "=", "onnx", ".", "mapping", ".", "NP_TYPE_TO_TENSOR_TYPE", "[", "np", ".", "dtype", "(", "attrs", ".", "get", "(", "\"dtype\"", ",", "'int32'", ")", ")", "]", "sample_size", "=", "convert_string_to_list", "(", "attrs", ".", "get", "(", "\"shape\"", ",", "'1'", ")", ")", "if", "len", "(", "sample_size", ")", "<", "2", ":", "sample_size", "=", "sample_size", "[", "-", "1", "]", "else", ":", "raise", "AttributeError", "(", "\"ONNX currently supports integer sample_size only\"", ")", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Multinomial\"", ",", "input_nodes", ",", "[", "name", "]", ",", "dtype", "=", "dtype", ",", "sample_size", "=", "sample_size", ",", "name", "=", "name", ",", ")", "return", "[", "node", "]" ]
Map MXNet's multinomial operator attributes to onnx's Multinomial operator and return the created node.
[ "Map", "MXNet", "s", "multinomial", "operator", "attributes", "to", "onnx", "s", "Multinomial", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1881-L1900
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_random_uniform
def convert_random_uniform(node, **kwargs): """Map MXNet's random_uniform operator attributes to onnx's RandomUniform operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) # Converting to float32 low = float(attrs.get("low", 0)) high = float(attrs.get("high", 1.0)) shape = convert_string_to_list(attrs.get('shape', '[]')) dtype = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(attrs.get('dtype', 'float32'))] node = onnx.helper.make_node( 'RandomUniform', input_nodes, [name], low=low, high=high, dtype=dtype, shape=shape, name=name ) return [node]
python
def convert_random_uniform(node, **kwargs): """Map MXNet's random_uniform operator attributes to onnx's RandomUniform operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) # Converting to float32 low = float(attrs.get("low", 0)) high = float(attrs.get("high", 1.0)) shape = convert_string_to_list(attrs.get('shape', '[]')) dtype = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(attrs.get('dtype', 'float32'))] node = onnx.helper.make_node( 'RandomUniform', input_nodes, [name], low=low, high=high, dtype=dtype, shape=shape, name=name ) return [node]
[ "def", "convert_random_uniform", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "# Converting to float32", "low", "=", "float", "(", "attrs", ".", "get", "(", "\"low\"", ",", "0", ")", ")", "high", "=", "float", "(", "attrs", ".", "get", "(", "\"high\"", ",", "1.0", ")", ")", "shape", "=", "convert_string_to_list", "(", "attrs", ".", "get", "(", "'shape'", ",", "'[]'", ")", ")", "dtype", "=", "onnx", ".", "mapping", ".", "NP_TYPE_TO_TENSOR_TYPE", "[", "np", ".", "dtype", "(", "attrs", ".", "get", "(", "'dtype'", ",", "'float32'", ")", ")", "]", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'RandomUniform'", ",", "input_nodes", ",", "[", "name", "]", ",", "low", "=", "low", ",", "high", "=", "high", ",", "dtype", "=", "dtype", ",", "shape", "=", "shape", ",", "name", "=", "name", ")", "return", "[", "node", "]" ]
Map MXNet's random_uniform operator attributes to onnx's RandomUniform operator and return the created node.
[ "Map", "MXNet", "s", "random_uniform", "operator", "attributes", "to", "onnx", "s", "RandomUniform", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1904-L1926
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_random_normal
def convert_random_normal(node, **kwargs): """Map MXNet's random_normal operator attributes to onnx's RandomNormal operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) # Converting to float32 mean = float(attrs.get("loc", 0)) scale = float(attrs.get("scale", 1.0)) shape = convert_string_to_list(attrs.get('shape', '[]')) dtype = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(attrs.get('dtype', 'float32'))] node = onnx.helper.make_node( 'RandomNormal', input_nodes, [name], mean=mean, scale=scale, dtype=dtype, shape=shape, name=name ) return [node]
python
def convert_random_normal(node, **kwargs): """Map MXNet's random_normal operator attributes to onnx's RandomNormal operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) # Converting to float32 mean = float(attrs.get("loc", 0)) scale = float(attrs.get("scale", 1.0)) shape = convert_string_to_list(attrs.get('shape', '[]')) dtype = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype(attrs.get('dtype', 'float32'))] node = onnx.helper.make_node( 'RandomNormal', input_nodes, [name], mean=mean, scale=scale, dtype=dtype, shape=shape, name=name ) return [node]
[ "def", "convert_random_normal", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "# Converting to float32", "mean", "=", "float", "(", "attrs", ".", "get", "(", "\"loc\"", ",", "0", ")", ")", "scale", "=", "float", "(", "attrs", ".", "get", "(", "\"scale\"", ",", "1.0", ")", ")", "shape", "=", "convert_string_to_list", "(", "attrs", ".", "get", "(", "'shape'", ",", "'[]'", ")", ")", "dtype", "=", "onnx", ".", "mapping", ".", "NP_TYPE_TO_TENSOR_TYPE", "[", "np", ".", "dtype", "(", "attrs", ".", "get", "(", "'dtype'", ",", "'float32'", ")", ")", "]", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'RandomNormal'", ",", "input_nodes", ",", "[", "name", "]", ",", "mean", "=", "mean", ",", "scale", "=", "scale", ",", "dtype", "=", "dtype", ",", "shape", "=", "shape", ",", "name", "=", "name", ")", "return", "[", "node", "]" ]
Map MXNet's random_normal operator attributes to onnx's RandomNormal operator and return the created node.
[ "Map", "MXNet", "s", "random_normal", "operator", "attributes", "to", "onnx", "s", "RandomNormal", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1930-L1952
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_roipooling
def convert_roipooling(node, **kwargs): """Map MXNet's ROIPooling operator attributes to onnx's MaxRoiPool operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) pooled_shape = convert_string_to_list(attrs.get('pooled_size')) scale = float(attrs.get("spatial_scale")) node = onnx.helper.make_node( 'MaxRoiPool', input_nodes, [name], pooled_shape=pooled_shape, spatial_scale=scale, name=name ) return [node]
python
def convert_roipooling(node, **kwargs): """Map MXNet's ROIPooling operator attributes to onnx's MaxRoiPool operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) pooled_shape = convert_string_to_list(attrs.get('pooled_size')) scale = float(attrs.get("spatial_scale")) node = onnx.helper.make_node( 'MaxRoiPool', input_nodes, [name], pooled_shape=pooled_shape, spatial_scale=scale, name=name ) return [node]
[ "def", "convert_roipooling", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "pooled_shape", "=", "convert_string_to_list", "(", "attrs", ".", "get", "(", "'pooled_size'", ")", ")", "scale", "=", "float", "(", "attrs", ".", "get", "(", "\"spatial_scale\"", ")", ")", "node", "=", "onnx", ".", "helper", ".", "make_node", "(", "'MaxRoiPool'", ",", "input_nodes", ",", "[", "name", "]", ",", "pooled_shape", "=", "pooled_shape", ",", "spatial_scale", "=", "scale", ",", "name", "=", "name", ")", "return", "[", "node", "]" ]
Map MXNet's ROIPooling operator attributes to onnx's MaxRoiPool operator and return the created node.
[ "Map", "MXNet", "s", "ROIPooling", "operator", "attributes", "to", "onnx", "s", "MaxRoiPool", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1956-L1973
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_tile
def convert_tile(node, **kwargs): """Map MXNet's Tile operator attributes to onnx's Tile operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) reps_list = convert_string_to_list(attrs["reps"]) initializer = kwargs["initializer"] reps_shape_np = np.array(reps_list, dtype='int64') data_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[reps_shape_np.dtype] dims = np.shape(reps_shape_np) output_shape_name = "reps_attr_tensor" + str(kwargs["idx"]) tensor_node = onnx.helper.make_tensor_value_info(output_shape_name, data_type, dims) initializer.append( onnx.helper.make_tensor( name=output_shape_name, data_type=data_type, dims=dims, vals=reps_list, raw=False, ) ) input_nodes.append(output_shape_name) tile_node = onnx.helper.make_node( "Tile", input_nodes, [name], name=name ) return [tensor_node, tile_node]
python
def convert_tile(node, **kwargs): """Map MXNet's Tile operator attributes to onnx's Tile operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) reps_list = convert_string_to_list(attrs["reps"]) initializer = kwargs["initializer"] reps_shape_np = np.array(reps_list, dtype='int64') data_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[reps_shape_np.dtype] dims = np.shape(reps_shape_np) output_shape_name = "reps_attr_tensor" + str(kwargs["idx"]) tensor_node = onnx.helper.make_tensor_value_info(output_shape_name, data_type, dims) initializer.append( onnx.helper.make_tensor( name=output_shape_name, data_type=data_type, dims=dims, vals=reps_list, raw=False, ) ) input_nodes.append(output_shape_name) tile_node = onnx.helper.make_node( "Tile", input_nodes, [name], name=name ) return [tensor_node, tile_node]
[ "def", "convert_tile", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "reps_list", "=", "convert_string_to_list", "(", "attrs", "[", "\"reps\"", "]", ")", "initializer", "=", "kwargs", "[", "\"initializer\"", "]", "reps_shape_np", "=", "np", ".", "array", "(", "reps_list", ",", "dtype", "=", "'int64'", ")", "data_type", "=", "onnx", ".", "mapping", ".", "NP_TYPE_TO_TENSOR_TYPE", "[", "reps_shape_np", ".", "dtype", "]", "dims", "=", "np", ".", "shape", "(", "reps_shape_np", ")", "output_shape_name", "=", "\"reps_attr_tensor\"", "+", "str", "(", "kwargs", "[", "\"idx\"", "]", ")", "tensor_node", "=", "onnx", ".", "helper", ".", "make_tensor_value_info", "(", "output_shape_name", ",", "data_type", ",", "dims", ")", "initializer", ".", "append", "(", "onnx", ".", "helper", ".", "make_tensor", "(", "name", "=", "output_shape_name", ",", "data_type", "=", "data_type", ",", "dims", "=", "dims", ",", "vals", "=", "reps_list", ",", "raw", "=", "False", ",", ")", ")", "input_nodes", ".", "append", "(", "output_shape_name", ")", "tile_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Tile\"", ",", "input_nodes", ",", "[", "name", "]", ",", "name", "=", "name", ")", "return", "[", "tensor_node", ",", "tile_node", "]" ]
Map MXNet's Tile operator attributes to onnx's Tile operator and return the created node.
[ "Map", "MXNet", "s", "Tile", "operator", "attributes", "to", "onnx", "s", "Tile", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L1977-L2011
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/mx2onnx/_op_translations.py
convert_broadcast_to
def convert_broadcast_to(node, **kwargs): """Map MXNet's broadcast_to operator attributes to onnx's Expand operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) shape_list = convert_string_to_list(attrs["shape"]) initializer = kwargs["initializer"] output_shape_np = np.array(shape_list, dtype='int64') data_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[output_shape_np.dtype] dims = np.shape(output_shape_np) output_shape_name = "expand_attr_tensor" + str(kwargs["idx"]) tensor_node = onnx.helper.make_tensor_value_info(output_shape_name, data_type, dims) initializer.append( onnx.helper.make_tensor( name=output_shape_name, data_type=data_type, dims=dims, vals=shape_list, raw=False, ) ) input_nodes.append(output_shape_name) expand_node = onnx.helper.make_node( "Expand", input_nodes, [name], name=name ) return [tensor_node, expand_node]
python
def convert_broadcast_to(node, **kwargs): """Map MXNet's broadcast_to operator attributes to onnx's Expand operator and return the created node. """ name, input_nodes, attrs = get_inputs(node, kwargs) shape_list = convert_string_to_list(attrs["shape"]) initializer = kwargs["initializer"] output_shape_np = np.array(shape_list, dtype='int64') data_type = onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[output_shape_np.dtype] dims = np.shape(output_shape_np) output_shape_name = "expand_attr_tensor" + str(kwargs["idx"]) tensor_node = onnx.helper.make_tensor_value_info(output_shape_name, data_type, dims) initializer.append( onnx.helper.make_tensor( name=output_shape_name, data_type=data_type, dims=dims, vals=shape_list, raw=False, ) ) input_nodes.append(output_shape_name) expand_node = onnx.helper.make_node( "Expand", input_nodes, [name], name=name ) return [tensor_node, expand_node]
[ "def", "convert_broadcast_to", "(", "node", ",", "*", "*", "kwargs", ")", ":", "name", ",", "input_nodes", ",", "attrs", "=", "get_inputs", "(", "node", ",", "kwargs", ")", "shape_list", "=", "convert_string_to_list", "(", "attrs", "[", "\"shape\"", "]", ")", "initializer", "=", "kwargs", "[", "\"initializer\"", "]", "output_shape_np", "=", "np", ".", "array", "(", "shape_list", ",", "dtype", "=", "'int64'", ")", "data_type", "=", "onnx", ".", "mapping", ".", "NP_TYPE_TO_TENSOR_TYPE", "[", "output_shape_np", ".", "dtype", "]", "dims", "=", "np", ".", "shape", "(", "output_shape_np", ")", "output_shape_name", "=", "\"expand_attr_tensor\"", "+", "str", "(", "kwargs", "[", "\"idx\"", "]", ")", "tensor_node", "=", "onnx", ".", "helper", ".", "make_tensor_value_info", "(", "output_shape_name", ",", "data_type", ",", "dims", ")", "initializer", ".", "append", "(", "onnx", ".", "helper", ".", "make_tensor", "(", "name", "=", "output_shape_name", ",", "data_type", "=", "data_type", ",", "dims", "=", "dims", ",", "vals", "=", "shape_list", ",", "raw", "=", "False", ",", ")", ")", "input_nodes", ".", "append", "(", "output_shape_name", ")", "expand_node", "=", "onnx", ".", "helper", ".", "make_node", "(", "\"Expand\"", ",", "input_nodes", ",", "[", "name", "]", ",", "name", "=", "name", ")", "return", "[", "tensor_node", ",", "expand_node", "]" ]
Map MXNet's broadcast_to operator attributes to onnx's Expand operator and return the created node.
[ "Map", "MXNet", "s", "broadcast_to", "operator", "attributes", "to", "onnx", "s", "Expand", "operator", "and", "return", "the", "created", "node", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_op_translations.py#L2015-L2049
train
apache/incubator-mxnet
example/reinforcement-learning/dqn/base.py
Base.exe
def exe(self): """Get the current executor Returns ------- exe : mxnet.executor.Executor """ return self._buckets[self.curr_bucket_key]['exe'][tuple(self.data_shapes.items())]
python
def exe(self): """Get the current executor Returns ------- exe : mxnet.executor.Executor """ return self._buckets[self.curr_bucket_key]['exe'][tuple(self.data_shapes.items())]
[ "def", "exe", "(", "self", ")", ":", "return", "self", ".", "_buckets", "[", "self", ".", "curr_bucket_key", "]", "[", "'exe'", "]", "[", "tuple", "(", "self", ".", "data_shapes", ".", "items", "(", ")", ")", "]" ]
Get the current executor Returns ------- exe : mxnet.executor.Executor
[ "Get", "the", "current", "executor" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/reinforcement-learning/dqn/base.py#L80-L87
train
apache/incubator-mxnet
example/reinforcement-learning/dqn/base.py
Base.compute_internal
def compute_internal(self, sym_name, bucket_kwargs=None, **arg_dict): """ View the internal symbols using the forward function. :param sym_name: :param bucket_kwargs: :param input_dict: :return: """ data_shapes = {k: v.shape for k, v in arg_dict.items()} self.switch_bucket(bucket_kwargs=bucket_kwargs, data_shapes=data_shapes) internal_sym = self.sym.get_internals()[sym_name] data_inputs = {k: mx.nd.empty(v, ctx=self.ctx) for k, v in self.data_shapes.items() if k in internal_sym.list_arguments()} params = {k: v for k, v in self.params.items() if k in internal_sym.list_arguments()} aux_states = {k: v for k, v in self.aux_states.items() if k in internal_sym.list_auxiliary_states()} exe = internal_sym.bind(ctx=self.ctx, args=dict(params, **data_inputs), args_grad=None, grad_req='null', aux_states=aux_states, shared_exec=self.exe) for k, v in arg_dict.items(): exe.arg_dict[k][:] = v exe.forward(is_train=False) assert 1 == len(exe.outputs) for output in exe.outputs: output.wait_to_read() return exe.outputs[0]
python
def compute_internal(self, sym_name, bucket_kwargs=None, **arg_dict): """ View the internal symbols using the forward function. :param sym_name: :param bucket_kwargs: :param input_dict: :return: """ data_shapes = {k: v.shape for k, v in arg_dict.items()} self.switch_bucket(bucket_kwargs=bucket_kwargs, data_shapes=data_shapes) internal_sym = self.sym.get_internals()[sym_name] data_inputs = {k: mx.nd.empty(v, ctx=self.ctx) for k, v in self.data_shapes.items() if k in internal_sym.list_arguments()} params = {k: v for k, v in self.params.items() if k in internal_sym.list_arguments()} aux_states = {k: v for k, v in self.aux_states.items() if k in internal_sym.list_auxiliary_states()} exe = internal_sym.bind(ctx=self.ctx, args=dict(params, **data_inputs), args_grad=None, grad_req='null', aux_states=aux_states, shared_exec=self.exe) for k, v in arg_dict.items(): exe.arg_dict[k][:] = v exe.forward(is_train=False) assert 1 == len(exe.outputs) for output in exe.outputs: output.wait_to_read() return exe.outputs[0]
[ "def", "compute_internal", "(", "self", ",", "sym_name", ",", "bucket_kwargs", "=", "None", ",", "*", "*", "arg_dict", ")", ":", "data_shapes", "=", "{", "k", ":", "v", ".", "shape", "for", "k", ",", "v", "in", "arg_dict", ".", "items", "(", ")", "}", "self", ".", "switch_bucket", "(", "bucket_kwargs", "=", "bucket_kwargs", ",", "data_shapes", "=", "data_shapes", ")", "internal_sym", "=", "self", ".", "sym", ".", "get_internals", "(", ")", "[", "sym_name", "]", "data_inputs", "=", "{", "k", ":", "mx", ".", "nd", ".", "empty", "(", "v", ",", "ctx", "=", "self", ".", "ctx", ")", "for", "k", ",", "v", "in", "self", ".", "data_shapes", ".", "items", "(", ")", "if", "k", "in", "internal_sym", ".", "list_arguments", "(", ")", "}", "params", "=", "{", "k", ":", "v", "for", "k", ",", "v", "in", "self", ".", "params", ".", "items", "(", ")", "if", "k", "in", "internal_sym", ".", "list_arguments", "(", ")", "}", "aux_states", "=", "{", "k", ":", "v", "for", "k", ",", "v", "in", "self", ".", "aux_states", ".", "items", "(", ")", "if", "k", "in", "internal_sym", ".", "list_auxiliary_states", "(", ")", "}", "exe", "=", "internal_sym", ".", "bind", "(", "ctx", "=", "self", ".", "ctx", ",", "args", "=", "dict", "(", "params", ",", "*", "*", "data_inputs", ")", ",", "args_grad", "=", "None", ",", "grad_req", "=", "'null'", ",", "aux_states", "=", "aux_states", ",", "shared_exec", "=", "self", ".", "exe", ")", "for", "k", ",", "v", "in", "arg_dict", ".", "items", "(", ")", ":", "exe", ".", "arg_dict", "[", "k", "]", "[", ":", "]", "=", "v", "exe", ".", "forward", "(", "is_train", "=", "False", ")", "assert", "1", "==", "len", "(", "exe", ".", "outputs", ")", "for", "output", "in", "exe", ".", "outputs", ":", "output", ".", "wait_to_read", "(", ")", "return", "exe", ".", "outputs", "[", "0", "]" ]
View the internal symbols using the forward function. :param sym_name: :param bucket_kwargs: :param input_dict: :return:
[ "View", "the", "internal", "symbols", "using", "the", "forward", "function", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/reinforcement-learning/dqn/base.py#L190-L222
train
apache/incubator-mxnet
example/fcn-xs/init_fcnxs.py
init_from_fcnxs
def init_from_fcnxs(ctx, fcnxs_symbol, fcnxs_args_from, fcnxs_auxs_from): """ use zero initialization for better convergence, because it tends to oputut 0, and the label 0 stands for background, which may occupy most size of one image. """ fcnxs_args = fcnxs_args_from.copy() fcnxs_auxs = fcnxs_auxs_from.copy() for k,v in fcnxs_args.items(): if(v.context != ctx): fcnxs_args[k] = mx.nd.zeros(v.shape, ctx) v.copyto(fcnxs_args[k]) for k,v in fcnxs_auxs.items(): if(v.context != ctx): fcnxs_auxs[k] = mx.nd.zeros(v.shape, ctx) v.copyto(fcnxs_auxs[k]) data_shape=(1,3,500,500) arg_names = fcnxs_symbol.list_arguments() arg_shapes, _, _ = fcnxs_symbol.infer_shape(data=data_shape) rest_params = {} deconv_params = {} # this is fcn8s init from fcn16s if 'score_pool3_weight' in arg_names: rest_params = dict([(x[0], mx.nd.zeros(x[1], ctx)) for x in zip(arg_names, arg_shapes) if x[0] in ['score_pool3_bias', 'score_pool3_weight']]) deconv_params = dict([(x[0], x[1]) for x in zip(arg_names, arg_shapes) if x[0] \ in ["bigscore_weight", 'score4_weight']]) # this is fcn16s init from fcn32s elif 'score_pool4_weight' in arg_names: rest_params = dict([(x[0], mx.nd.zeros(x[1], ctx)) for x in zip(arg_names, arg_shapes) if x[0] in ['score_pool4_weight', 'score_pool4_bias']]) deconv_params = dict([(x[0], x[1]) for x in zip(arg_names, arg_shapes) if x[0] \ in ["bigscore_weight", 'score2_weight']]) # this is fcn32s init else: logging.error("you are init the fcn32s model, so you should use init_from_vgg16()") sys.exit() fcnxs_args.update(rest_params) for k, v in deconv_params.items(): filt = upsample_filt(v[3]) initw = np.zeros(v) initw[range(v[0]), range(v[1]), :, :] = filt # becareful here is the slice assing fcnxs_args[k] = mx.nd.array(initw, ctx) return fcnxs_args, fcnxs_auxs
python
def init_from_fcnxs(ctx, fcnxs_symbol, fcnxs_args_from, fcnxs_auxs_from): """ use zero initialization for better convergence, because it tends to oputut 0, and the label 0 stands for background, which may occupy most size of one image. """ fcnxs_args = fcnxs_args_from.copy() fcnxs_auxs = fcnxs_auxs_from.copy() for k,v in fcnxs_args.items(): if(v.context != ctx): fcnxs_args[k] = mx.nd.zeros(v.shape, ctx) v.copyto(fcnxs_args[k]) for k,v in fcnxs_auxs.items(): if(v.context != ctx): fcnxs_auxs[k] = mx.nd.zeros(v.shape, ctx) v.copyto(fcnxs_auxs[k]) data_shape=(1,3,500,500) arg_names = fcnxs_symbol.list_arguments() arg_shapes, _, _ = fcnxs_symbol.infer_shape(data=data_shape) rest_params = {} deconv_params = {} # this is fcn8s init from fcn16s if 'score_pool3_weight' in arg_names: rest_params = dict([(x[0], mx.nd.zeros(x[1], ctx)) for x in zip(arg_names, arg_shapes) if x[0] in ['score_pool3_bias', 'score_pool3_weight']]) deconv_params = dict([(x[0], x[1]) for x in zip(arg_names, arg_shapes) if x[0] \ in ["bigscore_weight", 'score4_weight']]) # this is fcn16s init from fcn32s elif 'score_pool4_weight' in arg_names: rest_params = dict([(x[0], mx.nd.zeros(x[1], ctx)) for x in zip(arg_names, arg_shapes) if x[0] in ['score_pool4_weight', 'score_pool4_bias']]) deconv_params = dict([(x[0], x[1]) for x in zip(arg_names, arg_shapes) if x[0] \ in ["bigscore_weight", 'score2_weight']]) # this is fcn32s init else: logging.error("you are init the fcn32s model, so you should use init_from_vgg16()") sys.exit() fcnxs_args.update(rest_params) for k, v in deconv_params.items(): filt = upsample_filt(v[3]) initw = np.zeros(v) initw[range(v[0]), range(v[1]), :, :] = filt # becareful here is the slice assing fcnxs_args[k] = mx.nd.array(initw, ctx) return fcnxs_args, fcnxs_auxs
[ "def", "init_from_fcnxs", "(", "ctx", ",", "fcnxs_symbol", ",", "fcnxs_args_from", ",", "fcnxs_auxs_from", ")", ":", "fcnxs_args", "=", "fcnxs_args_from", ".", "copy", "(", ")", "fcnxs_auxs", "=", "fcnxs_auxs_from", ".", "copy", "(", ")", "for", "k", ",", "v", "in", "fcnxs_args", ".", "items", "(", ")", ":", "if", "(", "v", ".", "context", "!=", "ctx", ")", ":", "fcnxs_args", "[", "k", "]", "=", "mx", ".", "nd", ".", "zeros", "(", "v", ".", "shape", ",", "ctx", ")", "v", ".", "copyto", "(", "fcnxs_args", "[", "k", "]", ")", "for", "k", ",", "v", "in", "fcnxs_auxs", ".", "items", "(", ")", ":", "if", "(", "v", ".", "context", "!=", "ctx", ")", ":", "fcnxs_auxs", "[", "k", "]", "=", "mx", ".", "nd", ".", "zeros", "(", "v", ".", "shape", ",", "ctx", ")", "v", ".", "copyto", "(", "fcnxs_auxs", "[", "k", "]", ")", "data_shape", "=", "(", "1", ",", "3", ",", "500", ",", "500", ")", "arg_names", "=", "fcnxs_symbol", ".", "list_arguments", "(", ")", "arg_shapes", ",", "_", ",", "_", "=", "fcnxs_symbol", ".", "infer_shape", "(", "data", "=", "data_shape", ")", "rest_params", "=", "{", "}", "deconv_params", "=", "{", "}", "# this is fcn8s init from fcn16s", "if", "'score_pool3_weight'", "in", "arg_names", ":", "rest_params", "=", "dict", "(", "[", "(", "x", "[", "0", "]", ",", "mx", ".", "nd", ".", "zeros", "(", "x", "[", "1", "]", ",", "ctx", ")", ")", "for", "x", "in", "zip", "(", "arg_names", ",", "arg_shapes", ")", "if", "x", "[", "0", "]", "in", "[", "'score_pool3_bias'", ",", "'score_pool3_weight'", "]", "]", ")", "deconv_params", "=", "dict", "(", "[", "(", "x", "[", "0", "]", ",", "x", "[", "1", "]", ")", "for", "x", "in", "zip", "(", "arg_names", ",", "arg_shapes", ")", "if", "x", "[", "0", "]", "in", "[", "\"bigscore_weight\"", ",", "'score4_weight'", "]", "]", ")", "# this is fcn16s init from fcn32s", "elif", "'score_pool4_weight'", "in", "arg_names", ":", "rest_params", "=", "dict", "(", "[", "(", "x", "[", "0", "]", ",", "mx", ".", "nd", ".", "zeros", "(", "x", "[", "1", "]", ",", "ctx", ")", ")", "for", "x", "in", "zip", "(", "arg_names", ",", "arg_shapes", ")", "if", "x", "[", "0", "]", "in", "[", "'score_pool4_weight'", ",", "'score_pool4_bias'", "]", "]", ")", "deconv_params", "=", "dict", "(", "[", "(", "x", "[", "0", "]", ",", "x", "[", "1", "]", ")", "for", "x", "in", "zip", "(", "arg_names", ",", "arg_shapes", ")", "if", "x", "[", "0", "]", "in", "[", "\"bigscore_weight\"", ",", "'score2_weight'", "]", "]", ")", "# this is fcn32s init", "else", ":", "logging", ".", "error", "(", "\"you are init the fcn32s model, so you should use init_from_vgg16()\"", ")", "sys", ".", "exit", "(", ")", "fcnxs_args", ".", "update", "(", "rest_params", ")", "for", "k", ",", "v", "in", "deconv_params", ".", "items", "(", ")", ":", "filt", "=", "upsample_filt", "(", "v", "[", "3", "]", ")", "initw", "=", "np", ".", "zeros", "(", "v", ")", "initw", "[", "range", "(", "v", "[", "0", "]", ")", ",", "range", "(", "v", "[", "1", "]", ")", ",", ":", ",", ":", "]", "=", "filt", "# becareful here is the slice assing", "fcnxs_args", "[", "k", "]", "=", "mx", ".", "nd", ".", "array", "(", "initw", ",", "ctx", ")", "return", "fcnxs_args", ",", "fcnxs_auxs" ]
use zero initialization for better convergence, because it tends to oputut 0, and the label 0 stands for background, which may occupy most size of one image.
[ "use", "zero", "initialization", "for", "better", "convergence", "because", "it", "tends", "to", "oputut", "0", "and", "the", "label", "0", "stands", "for", "background", "which", "may", "occupy", "most", "size", "of", "one", "image", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/fcn-xs/init_fcnxs.py#L65-L106
train
apache/incubator-mxnet
example/image-classification/symbols/resnext.py
residual_unit
def residual_unit(data, num_filter, stride, dim_match, name, bottle_neck=True, num_group=32, bn_mom=0.9, workspace=256, memonger=False): """Return ResNet Unit symbol for building ResNet Parameters ---------- data : str Input data num_filter : int Number of output channels bnf : int Bottle neck channels factor with regard to num_filter stride : tuple Stride used in convolution dim_match : Boolean True means channel number between input and output is the same, otherwise means differ name : str Base name of the operators workspace : int Workspace used in convolution operator """ if bottle_neck: # the same as https://github.com/facebook/fb.resnet.torch#notes, a bit difference with origin paper conv1 = mx.sym.Convolution(data=data, num_filter=int(num_filter*0.5), kernel=(1,1), stride=(1,1), pad=(0,0), no_bias=True, workspace=workspace, name=name + '_conv1') bn1 = mx.sym.BatchNorm(data=conv1, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn1') act1 = mx.sym.Activation(data=bn1, act_type='relu', name=name + '_relu1') conv2 = mx.sym.Convolution(data=act1, num_filter=int(num_filter*0.5), num_group=num_group, kernel=(3,3), stride=stride, pad=(1,1), no_bias=True, workspace=workspace, name=name + '_conv2') bn2 = mx.sym.BatchNorm(data=conv2, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn2') act2 = mx.sym.Activation(data=bn2, act_type='relu', name=name + '_relu2') conv3 = mx.sym.Convolution(data=act2, num_filter=num_filter, kernel=(1,1), stride=(1,1), pad=(0,0), no_bias=True, workspace=workspace, name=name + '_conv3') bn3 = mx.sym.BatchNorm(data=conv3, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn3') if dim_match: shortcut = data else: shortcut_conv = mx.sym.Convolution(data=data, num_filter=num_filter, kernel=(1,1), stride=stride, no_bias=True, workspace=workspace, name=name+'_sc') shortcut = mx.sym.BatchNorm(data=shortcut_conv, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_sc_bn') if memonger: shortcut._set_attr(mirror_stage='True') eltwise = bn3 + shortcut return mx.sym.Activation(data=eltwise, act_type='relu', name=name + '_relu') else: conv1 = mx.sym.Convolution(data=data, num_filter=num_filter, kernel=(3,3), stride=stride, pad=(1,1), no_bias=True, workspace=workspace, name=name + '_conv1') bn1 = mx.sym.BatchNorm(data=conv1, fix_gamma=False, momentum=bn_mom, eps=2e-5, name=name + '_bn1') act1 = mx.sym.Activation(data=bn1, act_type='relu', name=name + '_relu1') conv2 = mx.sym.Convolution(data=act1, num_filter=num_filter, kernel=(3,3), stride=(1,1), pad=(1,1), no_bias=True, workspace=workspace, name=name + '_conv2') bn2 = mx.sym.BatchNorm(data=conv2, fix_gamma=False, momentum=bn_mom, eps=2e-5, name=name + '_bn2') if dim_match: shortcut = data else: shortcut_conv = mx.sym.Convolution(data=data, num_filter=num_filter, kernel=(1,1), stride=stride, no_bias=True, workspace=workspace, name=name+'_sc') shortcut = mx.sym.BatchNorm(data=shortcut_conv, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_sc_bn') if memonger: shortcut._set_attr(mirror_stage='True') eltwise = bn2 + shortcut return mx.sym.Activation(data=eltwise, act_type='relu', name=name + '_relu')
python
def residual_unit(data, num_filter, stride, dim_match, name, bottle_neck=True, num_group=32, bn_mom=0.9, workspace=256, memonger=False): """Return ResNet Unit symbol for building ResNet Parameters ---------- data : str Input data num_filter : int Number of output channels bnf : int Bottle neck channels factor with regard to num_filter stride : tuple Stride used in convolution dim_match : Boolean True means channel number between input and output is the same, otherwise means differ name : str Base name of the operators workspace : int Workspace used in convolution operator """ if bottle_neck: # the same as https://github.com/facebook/fb.resnet.torch#notes, a bit difference with origin paper conv1 = mx.sym.Convolution(data=data, num_filter=int(num_filter*0.5), kernel=(1,1), stride=(1,1), pad=(0,0), no_bias=True, workspace=workspace, name=name + '_conv1') bn1 = mx.sym.BatchNorm(data=conv1, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn1') act1 = mx.sym.Activation(data=bn1, act_type='relu', name=name + '_relu1') conv2 = mx.sym.Convolution(data=act1, num_filter=int(num_filter*0.5), num_group=num_group, kernel=(3,3), stride=stride, pad=(1,1), no_bias=True, workspace=workspace, name=name + '_conv2') bn2 = mx.sym.BatchNorm(data=conv2, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn2') act2 = mx.sym.Activation(data=bn2, act_type='relu', name=name + '_relu2') conv3 = mx.sym.Convolution(data=act2, num_filter=num_filter, kernel=(1,1), stride=(1,1), pad=(0,0), no_bias=True, workspace=workspace, name=name + '_conv3') bn3 = mx.sym.BatchNorm(data=conv3, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn3') if dim_match: shortcut = data else: shortcut_conv = mx.sym.Convolution(data=data, num_filter=num_filter, kernel=(1,1), stride=stride, no_bias=True, workspace=workspace, name=name+'_sc') shortcut = mx.sym.BatchNorm(data=shortcut_conv, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_sc_bn') if memonger: shortcut._set_attr(mirror_stage='True') eltwise = bn3 + shortcut return mx.sym.Activation(data=eltwise, act_type='relu', name=name + '_relu') else: conv1 = mx.sym.Convolution(data=data, num_filter=num_filter, kernel=(3,3), stride=stride, pad=(1,1), no_bias=True, workspace=workspace, name=name + '_conv1') bn1 = mx.sym.BatchNorm(data=conv1, fix_gamma=False, momentum=bn_mom, eps=2e-5, name=name + '_bn1') act1 = mx.sym.Activation(data=bn1, act_type='relu', name=name + '_relu1') conv2 = mx.sym.Convolution(data=act1, num_filter=num_filter, kernel=(3,3), stride=(1,1), pad=(1,1), no_bias=True, workspace=workspace, name=name + '_conv2') bn2 = mx.sym.BatchNorm(data=conv2, fix_gamma=False, momentum=bn_mom, eps=2e-5, name=name + '_bn2') if dim_match: shortcut = data else: shortcut_conv = mx.sym.Convolution(data=data, num_filter=num_filter, kernel=(1,1), stride=stride, no_bias=True, workspace=workspace, name=name+'_sc') shortcut = mx.sym.BatchNorm(data=shortcut_conv, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_sc_bn') if memonger: shortcut._set_attr(mirror_stage='True') eltwise = bn2 + shortcut return mx.sym.Activation(data=eltwise, act_type='relu', name=name + '_relu')
[ "def", "residual_unit", "(", "data", ",", "num_filter", ",", "stride", ",", "dim_match", ",", "name", ",", "bottle_neck", "=", "True", ",", "num_group", "=", "32", ",", "bn_mom", "=", "0.9", ",", "workspace", "=", "256", ",", "memonger", "=", "False", ")", ":", "if", "bottle_neck", ":", "# the same as https://github.com/facebook/fb.resnet.torch#notes, a bit difference with origin paper", "conv1", "=", "mx", ".", "sym", ".", "Convolution", "(", "data", "=", "data", ",", "num_filter", "=", "int", "(", "num_filter", "*", "0.5", ")", ",", "kernel", "=", "(", "1", ",", "1", ")", ",", "stride", "=", "(", "1", ",", "1", ")", ",", "pad", "=", "(", "0", ",", "0", ")", ",", "no_bias", "=", "True", ",", "workspace", "=", "workspace", ",", "name", "=", "name", "+", "'_conv1'", ")", "bn1", "=", "mx", ".", "sym", ".", "BatchNorm", "(", "data", "=", "conv1", ",", "fix_gamma", "=", "False", ",", "eps", "=", "2e-5", ",", "momentum", "=", "bn_mom", ",", "name", "=", "name", "+", "'_bn1'", ")", "act1", "=", "mx", ".", "sym", ".", "Activation", "(", "data", "=", "bn1", ",", "act_type", "=", "'relu'", ",", "name", "=", "name", "+", "'_relu1'", ")", "conv2", "=", "mx", ".", "sym", ".", "Convolution", "(", "data", "=", "act1", ",", "num_filter", "=", "int", "(", "num_filter", "*", "0.5", ")", ",", "num_group", "=", "num_group", ",", "kernel", "=", "(", "3", ",", "3", ")", ",", "stride", "=", "stride", ",", "pad", "=", "(", "1", ",", "1", ")", ",", "no_bias", "=", "True", ",", "workspace", "=", "workspace", ",", "name", "=", "name", "+", "'_conv2'", ")", "bn2", "=", "mx", ".", "sym", ".", "BatchNorm", "(", "data", "=", "conv2", ",", "fix_gamma", "=", "False", ",", "eps", "=", "2e-5", ",", "momentum", "=", "bn_mom", ",", "name", "=", "name", "+", "'_bn2'", ")", "act2", "=", "mx", ".", "sym", ".", "Activation", "(", "data", "=", "bn2", ",", "act_type", "=", "'relu'", ",", "name", "=", "name", "+", "'_relu2'", ")", "conv3", "=", "mx", ".", "sym", ".", "Convolution", "(", "data", "=", "act2", ",", "num_filter", "=", "num_filter", ",", "kernel", "=", "(", "1", ",", "1", ")", ",", "stride", "=", "(", "1", ",", "1", ")", ",", "pad", "=", "(", "0", ",", "0", ")", ",", "no_bias", "=", "True", ",", "workspace", "=", "workspace", ",", "name", "=", "name", "+", "'_conv3'", ")", "bn3", "=", "mx", ".", "sym", ".", "BatchNorm", "(", "data", "=", "conv3", ",", "fix_gamma", "=", "False", ",", "eps", "=", "2e-5", ",", "momentum", "=", "bn_mom", ",", "name", "=", "name", "+", "'_bn3'", ")", "if", "dim_match", ":", "shortcut", "=", "data", "else", ":", "shortcut_conv", "=", "mx", ".", "sym", ".", "Convolution", "(", "data", "=", "data", ",", "num_filter", "=", "num_filter", ",", "kernel", "=", "(", "1", ",", "1", ")", ",", "stride", "=", "stride", ",", "no_bias", "=", "True", ",", "workspace", "=", "workspace", ",", "name", "=", "name", "+", "'_sc'", ")", "shortcut", "=", "mx", ".", "sym", ".", "BatchNorm", "(", "data", "=", "shortcut_conv", ",", "fix_gamma", "=", "False", ",", "eps", "=", "2e-5", ",", "momentum", "=", "bn_mom", ",", "name", "=", "name", "+", "'_sc_bn'", ")", "if", "memonger", ":", "shortcut", ".", "_set_attr", "(", "mirror_stage", "=", "'True'", ")", "eltwise", "=", "bn3", "+", "shortcut", "return", "mx", ".", "sym", ".", "Activation", "(", "data", "=", "eltwise", ",", "act_type", "=", "'relu'", ",", "name", "=", "name", "+", "'_relu'", ")", "else", ":", "conv1", "=", "mx", ".", "sym", ".", "Convolution", "(", "data", "=", "data", ",", "num_filter", "=", "num_filter", ",", "kernel", "=", "(", "3", ",", "3", ")", ",", "stride", "=", "stride", ",", "pad", "=", "(", "1", ",", "1", ")", ",", "no_bias", "=", "True", ",", "workspace", "=", "workspace", ",", "name", "=", "name", "+", "'_conv1'", ")", "bn1", "=", "mx", ".", "sym", ".", "BatchNorm", "(", "data", "=", "conv1", ",", "fix_gamma", "=", "False", ",", "momentum", "=", "bn_mom", ",", "eps", "=", "2e-5", ",", "name", "=", "name", "+", "'_bn1'", ")", "act1", "=", "mx", ".", "sym", ".", "Activation", "(", "data", "=", "bn1", ",", "act_type", "=", "'relu'", ",", "name", "=", "name", "+", "'_relu1'", ")", "conv2", "=", "mx", ".", "sym", ".", "Convolution", "(", "data", "=", "act1", ",", "num_filter", "=", "num_filter", ",", "kernel", "=", "(", "3", ",", "3", ")", ",", "stride", "=", "(", "1", ",", "1", ")", ",", "pad", "=", "(", "1", ",", "1", ")", ",", "no_bias", "=", "True", ",", "workspace", "=", "workspace", ",", "name", "=", "name", "+", "'_conv2'", ")", "bn2", "=", "mx", ".", "sym", ".", "BatchNorm", "(", "data", "=", "conv2", ",", "fix_gamma", "=", "False", ",", "momentum", "=", "bn_mom", ",", "eps", "=", "2e-5", ",", "name", "=", "name", "+", "'_bn2'", ")", "if", "dim_match", ":", "shortcut", "=", "data", "else", ":", "shortcut_conv", "=", "mx", ".", "sym", ".", "Convolution", "(", "data", "=", "data", ",", "num_filter", "=", "num_filter", ",", "kernel", "=", "(", "1", ",", "1", ")", ",", "stride", "=", "stride", ",", "no_bias", "=", "True", ",", "workspace", "=", "workspace", ",", "name", "=", "name", "+", "'_sc'", ")", "shortcut", "=", "mx", ".", "sym", ".", "BatchNorm", "(", "data", "=", "shortcut_conv", ",", "fix_gamma", "=", "False", ",", "eps", "=", "2e-5", ",", "momentum", "=", "bn_mom", ",", "name", "=", "name", "+", "'_sc_bn'", ")", "if", "memonger", ":", "shortcut", ".", "_set_attr", "(", "mirror_stage", "=", "'True'", ")", "eltwise", "=", "bn2", "+", "shortcut", "return", "mx", ".", "sym", ".", "Activation", "(", "data", "=", "eltwise", ",", "act_type", "=", "'relu'", ",", "name", "=", "name", "+", "'_relu'", ")" ]
Return ResNet Unit symbol for building ResNet Parameters ---------- data : str Input data num_filter : int Number of output channels bnf : int Bottle neck channels factor with regard to num_filter stride : tuple Stride used in convolution dim_match : Boolean True means channel number between input and output is the same, otherwise means differ name : str Base name of the operators workspace : int Workspace used in convolution operator
[ "Return", "ResNet", "Unit", "symbol", "for", "building", "ResNet", "Parameters", "----------", "data", ":", "str", "Input", "data", "num_filter", ":", "int", "Number", "of", "output", "channels", "bnf", ":", "int", "Bottle", "neck", "channels", "factor", "with", "regard", "to", "num_filter", "stride", ":", "tuple", "Stride", "used", "in", "convolution", "dim_match", ":", "Boolean", "True", "means", "channel", "number", "between", "input", "and", "output", "is", "the", "same", "otherwise", "means", "differ", "name", ":", "str", "Base", "name", "of", "the", "operators", "workspace", ":", "int", "Workspace", "used", "in", "convolution", "operator" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/image-classification/symbols/resnext.py#L28-L99
train
apache/incubator-mxnet
example/image-classification/symbols/resnext.py
resnext
def resnext(units, num_stages, filter_list, num_classes, num_group, image_shape, bottle_neck=True, bn_mom=0.9, workspace=256, dtype='float32', memonger=False): """Return ResNeXt symbol of Parameters ---------- units : list Number of units in each stage num_stages : int Number of stage filter_list : list Channel size of each stage num_classes : int Ouput size of symbol num_groupes: int Number of conv groups dataset : str Dataset type, only cifar10 and imagenet supports workspace : int Workspace used in convolution operator dtype : str Precision (float32 or float16) """ num_unit = len(units) assert(num_unit == num_stages) data = mx.sym.Variable(name='data') if dtype == 'float32': data = mx.sym.identity(data=data, name='id') else: if dtype == 'float16': data = mx.sym.Cast(data=data, dtype=np.float16) data = mx.sym.BatchNorm(data=data, fix_gamma=True, eps=2e-5, momentum=bn_mom, name='bn_data') (nchannel, height, width) = image_shape if height <= 32: # such as cifar10 body = mx.sym.Convolution(data=data, num_filter=filter_list[0], kernel=(3, 3), stride=(1,1), pad=(1, 1), no_bias=True, name="conv0", workspace=workspace) else: # often expected to be 224 such as imagenet body = mx.sym.Convolution(data=data, num_filter=filter_list[0], kernel=(7, 7), stride=(2,2), pad=(3, 3), no_bias=True, name="conv0", workspace=workspace) body = mx.sym.BatchNorm(data=body, fix_gamma=False, eps=2e-5, momentum=bn_mom, name='bn0') body = mx.sym.Activation(data=body, act_type='relu', name='relu0') body = mx.sym.Pooling(data=body, kernel=(3, 3), stride=(2,2), pad=(1,1), pool_type='max') for i in range(num_stages): body = residual_unit(body, filter_list[i+1], (1 if i==0 else 2, 1 if i==0 else 2), False, name='stage%d_unit%d' % (i + 1, 1), bottle_neck=bottle_neck, num_group=num_group, bn_mom=bn_mom, workspace=workspace, memonger=memonger) for j in range(units[i]-1): body = residual_unit(body, filter_list[i+1], (1,1), True, name='stage%d_unit%d' % (i + 1, j + 2), bottle_neck=bottle_neck, num_group=num_group, bn_mom=bn_mom, workspace=workspace, memonger=memonger) pool1 = mx.sym.Pooling(data=body, global_pool=True, kernel=(7, 7), pool_type='avg', name='pool1') flat = mx.sym.Flatten(data=pool1) fc1 = mx.sym.FullyConnected(data=flat, num_hidden=num_classes, name='fc1') if dtype == 'float16': fc1 = mx.sym.Cast(data=fc1, dtype=np.float32) return mx.sym.SoftmaxOutput(data=fc1, name='softmax')
python
def resnext(units, num_stages, filter_list, num_classes, num_group, image_shape, bottle_neck=True, bn_mom=0.9, workspace=256, dtype='float32', memonger=False): """Return ResNeXt symbol of Parameters ---------- units : list Number of units in each stage num_stages : int Number of stage filter_list : list Channel size of each stage num_classes : int Ouput size of symbol num_groupes: int Number of conv groups dataset : str Dataset type, only cifar10 and imagenet supports workspace : int Workspace used in convolution operator dtype : str Precision (float32 or float16) """ num_unit = len(units) assert(num_unit == num_stages) data = mx.sym.Variable(name='data') if dtype == 'float32': data = mx.sym.identity(data=data, name='id') else: if dtype == 'float16': data = mx.sym.Cast(data=data, dtype=np.float16) data = mx.sym.BatchNorm(data=data, fix_gamma=True, eps=2e-5, momentum=bn_mom, name='bn_data') (nchannel, height, width) = image_shape if height <= 32: # such as cifar10 body = mx.sym.Convolution(data=data, num_filter=filter_list[0], kernel=(3, 3), stride=(1,1), pad=(1, 1), no_bias=True, name="conv0", workspace=workspace) else: # often expected to be 224 such as imagenet body = mx.sym.Convolution(data=data, num_filter=filter_list[0], kernel=(7, 7), stride=(2,2), pad=(3, 3), no_bias=True, name="conv0", workspace=workspace) body = mx.sym.BatchNorm(data=body, fix_gamma=False, eps=2e-5, momentum=bn_mom, name='bn0') body = mx.sym.Activation(data=body, act_type='relu', name='relu0') body = mx.sym.Pooling(data=body, kernel=(3, 3), stride=(2,2), pad=(1,1), pool_type='max') for i in range(num_stages): body = residual_unit(body, filter_list[i+1], (1 if i==0 else 2, 1 if i==0 else 2), False, name='stage%d_unit%d' % (i + 1, 1), bottle_neck=bottle_neck, num_group=num_group, bn_mom=bn_mom, workspace=workspace, memonger=memonger) for j in range(units[i]-1): body = residual_unit(body, filter_list[i+1], (1,1), True, name='stage%d_unit%d' % (i + 1, j + 2), bottle_neck=bottle_neck, num_group=num_group, bn_mom=bn_mom, workspace=workspace, memonger=memonger) pool1 = mx.sym.Pooling(data=body, global_pool=True, kernel=(7, 7), pool_type='avg', name='pool1') flat = mx.sym.Flatten(data=pool1) fc1 = mx.sym.FullyConnected(data=flat, num_hidden=num_classes, name='fc1') if dtype == 'float16': fc1 = mx.sym.Cast(data=fc1, dtype=np.float32) return mx.sym.SoftmaxOutput(data=fc1, name='softmax')
[ "def", "resnext", "(", "units", ",", "num_stages", ",", "filter_list", ",", "num_classes", ",", "num_group", ",", "image_shape", ",", "bottle_neck", "=", "True", ",", "bn_mom", "=", "0.9", ",", "workspace", "=", "256", ",", "dtype", "=", "'float32'", ",", "memonger", "=", "False", ")", ":", "num_unit", "=", "len", "(", "units", ")", "assert", "(", "num_unit", "==", "num_stages", ")", "data", "=", "mx", ".", "sym", ".", "Variable", "(", "name", "=", "'data'", ")", "if", "dtype", "==", "'float32'", ":", "data", "=", "mx", ".", "sym", ".", "identity", "(", "data", "=", "data", ",", "name", "=", "'id'", ")", "else", ":", "if", "dtype", "==", "'float16'", ":", "data", "=", "mx", ".", "sym", ".", "Cast", "(", "data", "=", "data", ",", "dtype", "=", "np", ".", "float16", ")", "data", "=", "mx", ".", "sym", ".", "BatchNorm", "(", "data", "=", "data", ",", "fix_gamma", "=", "True", ",", "eps", "=", "2e-5", ",", "momentum", "=", "bn_mom", ",", "name", "=", "'bn_data'", ")", "(", "nchannel", ",", "height", ",", "width", ")", "=", "image_shape", "if", "height", "<=", "32", ":", "# such as cifar10", "body", "=", "mx", ".", "sym", ".", "Convolution", "(", "data", "=", "data", ",", "num_filter", "=", "filter_list", "[", "0", "]", ",", "kernel", "=", "(", "3", ",", "3", ")", ",", "stride", "=", "(", "1", ",", "1", ")", ",", "pad", "=", "(", "1", ",", "1", ")", ",", "no_bias", "=", "True", ",", "name", "=", "\"conv0\"", ",", "workspace", "=", "workspace", ")", "else", ":", "# often expected to be 224 such as imagenet", "body", "=", "mx", ".", "sym", ".", "Convolution", "(", "data", "=", "data", ",", "num_filter", "=", "filter_list", "[", "0", "]", ",", "kernel", "=", "(", "7", ",", "7", ")", ",", "stride", "=", "(", "2", ",", "2", ")", ",", "pad", "=", "(", "3", ",", "3", ")", ",", "no_bias", "=", "True", ",", "name", "=", "\"conv0\"", ",", "workspace", "=", "workspace", ")", "body", "=", "mx", ".", "sym", ".", "BatchNorm", "(", "data", "=", "body", ",", "fix_gamma", "=", "False", ",", "eps", "=", "2e-5", ",", "momentum", "=", "bn_mom", ",", "name", "=", "'bn0'", ")", "body", "=", "mx", ".", "sym", ".", "Activation", "(", "data", "=", "body", ",", "act_type", "=", "'relu'", ",", "name", "=", "'relu0'", ")", "body", "=", "mx", ".", "sym", ".", "Pooling", "(", "data", "=", "body", ",", "kernel", "=", "(", "3", ",", "3", ")", ",", "stride", "=", "(", "2", ",", "2", ")", ",", "pad", "=", "(", "1", ",", "1", ")", ",", "pool_type", "=", "'max'", ")", "for", "i", "in", "range", "(", "num_stages", ")", ":", "body", "=", "residual_unit", "(", "body", ",", "filter_list", "[", "i", "+", "1", "]", ",", "(", "1", "if", "i", "==", "0", "else", "2", ",", "1", "if", "i", "==", "0", "else", "2", ")", ",", "False", ",", "name", "=", "'stage%d_unit%d'", "%", "(", "i", "+", "1", ",", "1", ")", ",", "bottle_neck", "=", "bottle_neck", ",", "num_group", "=", "num_group", ",", "bn_mom", "=", "bn_mom", ",", "workspace", "=", "workspace", ",", "memonger", "=", "memonger", ")", "for", "j", "in", "range", "(", "units", "[", "i", "]", "-", "1", ")", ":", "body", "=", "residual_unit", "(", "body", ",", "filter_list", "[", "i", "+", "1", "]", ",", "(", "1", ",", "1", ")", ",", "True", ",", "name", "=", "'stage%d_unit%d'", "%", "(", "i", "+", "1", ",", "j", "+", "2", ")", ",", "bottle_neck", "=", "bottle_neck", ",", "num_group", "=", "num_group", ",", "bn_mom", "=", "bn_mom", ",", "workspace", "=", "workspace", ",", "memonger", "=", "memonger", ")", "pool1", "=", "mx", ".", "sym", ".", "Pooling", "(", "data", "=", "body", ",", "global_pool", "=", "True", ",", "kernel", "=", "(", "7", ",", "7", ")", ",", "pool_type", "=", "'avg'", ",", "name", "=", "'pool1'", ")", "flat", "=", "mx", ".", "sym", ".", "Flatten", "(", "data", "=", "pool1", ")", "fc1", "=", "mx", ".", "sym", ".", "FullyConnected", "(", "data", "=", "flat", ",", "num_hidden", "=", "num_classes", ",", "name", "=", "'fc1'", ")", "if", "dtype", "==", "'float16'", ":", "fc1", "=", "mx", ".", "sym", ".", "Cast", "(", "data", "=", "fc1", ",", "dtype", "=", "np", ".", "float32", ")", "return", "mx", ".", "sym", ".", "SoftmaxOutput", "(", "data", "=", "fc1", ",", "name", "=", "'softmax'", ")" ]
Return ResNeXt symbol of Parameters ---------- units : list Number of units in each stage num_stages : int Number of stage filter_list : list Channel size of each stage num_classes : int Ouput size of symbol num_groupes: int Number of conv groups dataset : str Dataset type, only cifar10 and imagenet supports workspace : int Workspace used in convolution operator dtype : str Precision (float32 or float16)
[ "Return", "ResNeXt", "symbol", "of", "Parameters", "----------", "units", ":", "list", "Number", "of", "units", "in", "each", "stage", "num_stages", ":", "int", "Number", "of", "stage", "filter_list", ":", "list", "Channel", "size", "of", "each", "stage", "num_classes", ":", "int", "Ouput", "size", "of", "symbol", "num_groupes", ":", "int", "Number", "of", "conv", "groups", "dataset", ":", "str", "Dataset", "type", "only", "cifar10", "and", "imagenet", "supports", "workspace", ":", "int", "Workspace", "used", "in", "convolution", "operator", "dtype", ":", "str", "Precision", "(", "float32", "or", "float16", ")" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/image-classification/symbols/resnext.py#L101-L155
train
apache/incubator-mxnet
example/image-classification/symbols/resnext.py
get_symbol
def get_symbol(num_classes, num_layers, image_shape, num_group=32, conv_workspace=256, dtype='float32', **kwargs): """ Adapted from https://github.com/tornadomeet/ResNet/blob/master/train_resnet.py Original author Wei Wu """ image_shape = [int(l) for l in image_shape.split(',')] (nchannel, height, width) = image_shape if height <= 32: num_stages = 3 if (num_layers-2) % 9 == 0 and num_layers >= 164: per_unit = [(num_layers-2)//9] filter_list = [16, 64, 128, 256] bottle_neck = True elif (num_layers-2) % 6 == 0 and num_layers < 164: per_unit = [(num_layers-2)//6] filter_list = [16, 16, 32, 64] bottle_neck = False else: raise ValueError("no experiments done on num_layers {}, you can do it yourself".format(num_layers)) units = per_unit * num_stages else: if num_layers >= 50: filter_list = [64, 256, 512, 1024, 2048] bottle_neck = True else: filter_list = [64, 64, 128, 256, 512] bottle_neck = False num_stages = 4 if num_layers == 18: units = [2, 2, 2, 2] elif num_layers == 34: units = [3, 4, 6, 3] elif num_layers == 50: units = [3, 4, 6, 3] elif num_layers == 101: units = [3, 4, 23, 3] elif num_layers == 152: units = [3, 8, 36, 3] elif num_layers == 200: units = [3, 24, 36, 3] elif num_layers == 269: units = [3, 30, 48, 8] else: raise ValueError("no experiments done on num_layers {}, you can do it yourself".format(num_layers)) return resnext(units = units, num_stages = num_stages, filter_list = filter_list, num_classes = num_classes, num_group = num_group, image_shape = image_shape, bottle_neck = bottle_neck, workspace = conv_workspace, dtype = dtype)
python
def get_symbol(num_classes, num_layers, image_shape, num_group=32, conv_workspace=256, dtype='float32', **kwargs): """ Adapted from https://github.com/tornadomeet/ResNet/blob/master/train_resnet.py Original author Wei Wu """ image_shape = [int(l) for l in image_shape.split(',')] (nchannel, height, width) = image_shape if height <= 32: num_stages = 3 if (num_layers-2) % 9 == 0 and num_layers >= 164: per_unit = [(num_layers-2)//9] filter_list = [16, 64, 128, 256] bottle_neck = True elif (num_layers-2) % 6 == 0 and num_layers < 164: per_unit = [(num_layers-2)//6] filter_list = [16, 16, 32, 64] bottle_neck = False else: raise ValueError("no experiments done on num_layers {}, you can do it yourself".format(num_layers)) units = per_unit * num_stages else: if num_layers >= 50: filter_list = [64, 256, 512, 1024, 2048] bottle_neck = True else: filter_list = [64, 64, 128, 256, 512] bottle_neck = False num_stages = 4 if num_layers == 18: units = [2, 2, 2, 2] elif num_layers == 34: units = [3, 4, 6, 3] elif num_layers == 50: units = [3, 4, 6, 3] elif num_layers == 101: units = [3, 4, 23, 3] elif num_layers == 152: units = [3, 8, 36, 3] elif num_layers == 200: units = [3, 24, 36, 3] elif num_layers == 269: units = [3, 30, 48, 8] else: raise ValueError("no experiments done on num_layers {}, you can do it yourself".format(num_layers)) return resnext(units = units, num_stages = num_stages, filter_list = filter_list, num_classes = num_classes, num_group = num_group, image_shape = image_shape, bottle_neck = bottle_neck, workspace = conv_workspace, dtype = dtype)
[ "def", "get_symbol", "(", "num_classes", ",", "num_layers", ",", "image_shape", ",", "num_group", "=", "32", ",", "conv_workspace", "=", "256", ",", "dtype", "=", "'float32'", ",", "*", "*", "kwargs", ")", ":", "image_shape", "=", "[", "int", "(", "l", ")", "for", "l", "in", "image_shape", ".", "split", "(", "','", ")", "]", "(", "nchannel", ",", "height", ",", "width", ")", "=", "image_shape", "if", "height", "<=", "32", ":", "num_stages", "=", "3", "if", "(", "num_layers", "-", "2", ")", "%", "9", "==", "0", "and", "num_layers", ">=", "164", ":", "per_unit", "=", "[", "(", "num_layers", "-", "2", ")", "//", "9", "]", "filter_list", "=", "[", "16", ",", "64", ",", "128", ",", "256", "]", "bottle_neck", "=", "True", "elif", "(", "num_layers", "-", "2", ")", "%", "6", "==", "0", "and", "num_layers", "<", "164", ":", "per_unit", "=", "[", "(", "num_layers", "-", "2", ")", "//", "6", "]", "filter_list", "=", "[", "16", ",", "16", ",", "32", ",", "64", "]", "bottle_neck", "=", "False", "else", ":", "raise", "ValueError", "(", "\"no experiments done on num_layers {}, you can do it yourself\"", ".", "format", "(", "num_layers", ")", ")", "units", "=", "per_unit", "*", "num_stages", "else", ":", "if", "num_layers", ">=", "50", ":", "filter_list", "=", "[", "64", ",", "256", ",", "512", ",", "1024", ",", "2048", "]", "bottle_neck", "=", "True", "else", ":", "filter_list", "=", "[", "64", ",", "64", ",", "128", ",", "256", ",", "512", "]", "bottle_neck", "=", "False", "num_stages", "=", "4", "if", "num_layers", "==", "18", ":", "units", "=", "[", "2", ",", "2", ",", "2", ",", "2", "]", "elif", "num_layers", "==", "34", ":", "units", "=", "[", "3", ",", "4", ",", "6", ",", "3", "]", "elif", "num_layers", "==", "50", ":", "units", "=", "[", "3", ",", "4", ",", "6", ",", "3", "]", "elif", "num_layers", "==", "101", ":", "units", "=", "[", "3", ",", "4", ",", "23", ",", "3", "]", "elif", "num_layers", "==", "152", ":", "units", "=", "[", "3", ",", "8", ",", "36", ",", "3", "]", "elif", "num_layers", "==", "200", ":", "units", "=", "[", "3", ",", "24", ",", "36", ",", "3", "]", "elif", "num_layers", "==", "269", ":", "units", "=", "[", "3", ",", "30", ",", "48", ",", "8", "]", "else", ":", "raise", "ValueError", "(", "\"no experiments done on num_layers {}, you can do it yourself\"", ".", "format", "(", "num_layers", ")", ")", "return", "resnext", "(", "units", "=", "units", ",", "num_stages", "=", "num_stages", ",", "filter_list", "=", "filter_list", ",", "num_classes", "=", "num_classes", ",", "num_group", "=", "num_group", ",", "image_shape", "=", "image_shape", ",", "bottle_neck", "=", "bottle_neck", ",", "workspace", "=", "conv_workspace", ",", "dtype", "=", "dtype", ")" ]
Adapted from https://github.com/tornadomeet/ResNet/blob/master/train_resnet.py Original author Wei Wu
[ "Adapted", "from", "https", ":", "//", "github", ".", "com", "/", "tornadomeet", "/", "ResNet", "/", "blob", "/", "master", "/", "train_resnet", ".", "py", "Original", "author", "Wei", "Wu" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/image-classification/symbols/resnext.py#L157-L210
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
var
def var(name, attr=None, shape=None, lr_mult=None, wd_mult=None, dtype=None, init=None, stype=None, **kwargs): """Creates a symbolic variable with specified name. Example ------- >>> data = mx.sym.Variable('data', attr={'a': 'b'}) >>> data <Symbol data> >>> csr_data = mx.sym.Variable('csr_data', stype='csr') >>> csr_data <Symbol csr_data> >>> row_sparse_weight = mx.sym.Variable('weight', stype='row_sparse') >>> row_sparse_weight <Symbol weight> Parameters ---------- name : str Variable name. attr : Dict of strings Additional attributes to set on the variable. Format {string : string}. shape : tuple The shape of a variable. If specified, this will be used during the shape inference. If one has specified a different shape for this variable using a keyword argument when calling shape inference, this shape information will be ignored. lr_mult : float The learning rate multiplier for input variable. wd_mult : float Weight decay multiplier for input variable. dtype : str or numpy.dtype The dtype for input variable. If not specified, this value will be inferred. init : initializer (mxnet.init.*) Initializer for this variable to (optionally) override the default initializer. stype : str The storage type of the variable, such as 'row_sparse', 'csr', 'default', etc kwargs : Additional attribute variables Additional attributes must start and end with double underscores. Returns ------- variable : Symbol A symbol corresponding to an input to the computation graph. """ if not isinstance(name, string_types): raise TypeError('Expect a string for variable `name`') handle = SymbolHandle() check_call(_LIB.MXSymbolCreateVariable(c_str(name), ctypes.byref(handle))) ret = Symbol(handle) if not hasattr(AttrScope._current, "value"): AttrScope._current.value = AttrScope() attr = AttrScope._current.value.get(attr) attr = {} if attr is None else attr if shape is not None: attr['__shape__'] = str(shape) if lr_mult is not None: attr['__lr_mult__'] = str(lr_mult) if wd_mult is not None: attr['__wd_mult__'] = str(wd_mult) if dtype is not None: attr['__dtype__'] = str(_DTYPE_NP_TO_MX[_numpy.dtype(dtype).type]) if init is not None: if not isinstance(init, string_types): init = init.dumps() attr['__init__'] = init if stype is not None: attr['__storage_type__'] = str(_STORAGE_TYPE_STR_TO_ID[stype]) for k, v in kwargs.items(): if k.startswith('__') and k.endswith('__'): attr[k] = str(v) else: raise ValueError('Attribute name=%s is not supported.' ' Additional attributes must start and end with double underscores,' ' e.g, __yourattr__' % k) ret._set_attr(**attr) return ret
python
def var(name, attr=None, shape=None, lr_mult=None, wd_mult=None, dtype=None, init=None, stype=None, **kwargs): """Creates a symbolic variable with specified name. Example ------- >>> data = mx.sym.Variable('data', attr={'a': 'b'}) >>> data <Symbol data> >>> csr_data = mx.sym.Variable('csr_data', stype='csr') >>> csr_data <Symbol csr_data> >>> row_sparse_weight = mx.sym.Variable('weight', stype='row_sparse') >>> row_sparse_weight <Symbol weight> Parameters ---------- name : str Variable name. attr : Dict of strings Additional attributes to set on the variable. Format {string : string}. shape : tuple The shape of a variable. If specified, this will be used during the shape inference. If one has specified a different shape for this variable using a keyword argument when calling shape inference, this shape information will be ignored. lr_mult : float The learning rate multiplier for input variable. wd_mult : float Weight decay multiplier for input variable. dtype : str or numpy.dtype The dtype for input variable. If not specified, this value will be inferred. init : initializer (mxnet.init.*) Initializer for this variable to (optionally) override the default initializer. stype : str The storage type of the variable, such as 'row_sparse', 'csr', 'default', etc kwargs : Additional attribute variables Additional attributes must start and end with double underscores. Returns ------- variable : Symbol A symbol corresponding to an input to the computation graph. """ if not isinstance(name, string_types): raise TypeError('Expect a string for variable `name`') handle = SymbolHandle() check_call(_LIB.MXSymbolCreateVariable(c_str(name), ctypes.byref(handle))) ret = Symbol(handle) if not hasattr(AttrScope._current, "value"): AttrScope._current.value = AttrScope() attr = AttrScope._current.value.get(attr) attr = {} if attr is None else attr if shape is not None: attr['__shape__'] = str(shape) if lr_mult is not None: attr['__lr_mult__'] = str(lr_mult) if wd_mult is not None: attr['__wd_mult__'] = str(wd_mult) if dtype is not None: attr['__dtype__'] = str(_DTYPE_NP_TO_MX[_numpy.dtype(dtype).type]) if init is not None: if not isinstance(init, string_types): init = init.dumps() attr['__init__'] = init if stype is not None: attr['__storage_type__'] = str(_STORAGE_TYPE_STR_TO_ID[stype]) for k, v in kwargs.items(): if k.startswith('__') and k.endswith('__'): attr[k] = str(v) else: raise ValueError('Attribute name=%s is not supported.' ' Additional attributes must start and end with double underscores,' ' e.g, __yourattr__' % k) ret._set_attr(**attr) return ret
[ "def", "var", "(", "name", ",", "attr", "=", "None", ",", "shape", "=", "None", ",", "lr_mult", "=", "None", ",", "wd_mult", "=", "None", ",", "dtype", "=", "None", ",", "init", "=", "None", ",", "stype", "=", "None", ",", "*", "*", "kwargs", ")", ":", "if", "not", "isinstance", "(", "name", ",", "string_types", ")", ":", "raise", "TypeError", "(", "'Expect a string for variable `name`'", ")", "handle", "=", "SymbolHandle", "(", ")", "check_call", "(", "_LIB", ".", "MXSymbolCreateVariable", "(", "c_str", "(", "name", ")", ",", "ctypes", ".", "byref", "(", "handle", ")", ")", ")", "ret", "=", "Symbol", "(", "handle", ")", "if", "not", "hasattr", "(", "AttrScope", ".", "_current", ",", "\"value\"", ")", ":", "AttrScope", ".", "_current", ".", "value", "=", "AttrScope", "(", ")", "attr", "=", "AttrScope", ".", "_current", ".", "value", ".", "get", "(", "attr", ")", "attr", "=", "{", "}", "if", "attr", "is", "None", "else", "attr", "if", "shape", "is", "not", "None", ":", "attr", "[", "'__shape__'", "]", "=", "str", "(", "shape", ")", "if", "lr_mult", "is", "not", "None", ":", "attr", "[", "'__lr_mult__'", "]", "=", "str", "(", "lr_mult", ")", "if", "wd_mult", "is", "not", "None", ":", "attr", "[", "'__wd_mult__'", "]", "=", "str", "(", "wd_mult", ")", "if", "dtype", "is", "not", "None", ":", "attr", "[", "'__dtype__'", "]", "=", "str", "(", "_DTYPE_NP_TO_MX", "[", "_numpy", ".", "dtype", "(", "dtype", ")", ".", "type", "]", ")", "if", "init", "is", "not", "None", ":", "if", "not", "isinstance", "(", "init", ",", "string_types", ")", ":", "init", "=", "init", ".", "dumps", "(", ")", "attr", "[", "'__init__'", "]", "=", "init", "if", "stype", "is", "not", "None", ":", "attr", "[", "'__storage_type__'", "]", "=", "str", "(", "_STORAGE_TYPE_STR_TO_ID", "[", "stype", "]", ")", "for", "k", ",", "v", "in", "kwargs", ".", "items", "(", ")", ":", "if", "k", ".", "startswith", "(", "'__'", ")", "and", "k", ".", "endswith", "(", "'__'", ")", ":", "attr", "[", "k", "]", "=", "str", "(", "v", ")", "else", ":", "raise", "ValueError", "(", "'Attribute name=%s is not supported.'", "' Additional attributes must start and end with double underscores,'", "' e.g, __yourattr__'", "%", "k", ")", "ret", ".", "_set_attr", "(", "*", "*", "attr", ")", "return", "ret" ]
Creates a symbolic variable with specified name. Example ------- >>> data = mx.sym.Variable('data', attr={'a': 'b'}) >>> data <Symbol data> >>> csr_data = mx.sym.Variable('csr_data', stype='csr') >>> csr_data <Symbol csr_data> >>> row_sparse_weight = mx.sym.Variable('weight', stype='row_sparse') >>> row_sparse_weight <Symbol weight> Parameters ---------- name : str Variable name. attr : Dict of strings Additional attributes to set on the variable. Format {string : string}. shape : tuple The shape of a variable. If specified, this will be used during the shape inference. If one has specified a different shape for this variable using a keyword argument when calling shape inference, this shape information will be ignored. lr_mult : float The learning rate multiplier for input variable. wd_mult : float Weight decay multiplier for input variable. dtype : str or numpy.dtype The dtype for input variable. If not specified, this value will be inferred. init : initializer (mxnet.init.*) Initializer for this variable to (optionally) override the default initializer. stype : str The storage type of the variable, such as 'row_sparse', 'csr', 'default', etc kwargs : Additional attribute variables Additional attributes must start and end with double underscores. Returns ------- variable : Symbol A symbol corresponding to an input to the computation graph.
[ "Creates", "a", "symbolic", "variable", "with", "specified", "name", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L2574-L2649
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
Group
def Group(symbols): """Creates a symbol that contains a collection of other symbols, grouped together. Example ------- >>> a = mx.sym.Variable('a') >>> b = mx.sym.Variable('b') >>> mx.sym.Group([a,b]) <Symbol Grouped> Parameters ---------- symbols : list List of symbols to be grouped. Returns ------- sym : Symbol A group symbol. """ if not symbols or any(not isinstance(sym, Symbol) for sym in symbols): raise TypeError('Expected a list of symbols as input') handle = SymbolHandle() check_call(_LIB.MXSymbolCreateGroup( mx_uint(len(symbols)), c_handle_array(symbols), ctypes.byref(handle))) return Symbol(handle)
python
def Group(symbols): """Creates a symbol that contains a collection of other symbols, grouped together. Example ------- >>> a = mx.sym.Variable('a') >>> b = mx.sym.Variable('b') >>> mx.sym.Group([a,b]) <Symbol Grouped> Parameters ---------- symbols : list List of symbols to be grouped. Returns ------- sym : Symbol A group symbol. """ if not symbols or any(not isinstance(sym, Symbol) for sym in symbols): raise TypeError('Expected a list of symbols as input') handle = SymbolHandle() check_call(_LIB.MXSymbolCreateGroup( mx_uint(len(symbols)), c_handle_array(symbols), ctypes.byref(handle))) return Symbol(handle)
[ "def", "Group", "(", "symbols", ")", ":", "if", "not", "symbols", "or", "any", "(", "not", "isinstance", "(", "sym", ",", "Symbol", ")", "for", "sym", "in", "symbols", ")", ":", "raise", "TypeError", "(", "'Expected a list of symbols as input'", ")", "handle", "=", "SymbolHandle", "(", ")", "check_call", "(", "_LIB", ".", "MXSymbolCreateGroup", "(", "mx_uint", "(", "len", "(", "symbols", ")", ")", ",", "c_handle_array", "(", "symbols", ")", ",", "ctypes", ".", "byref", "(", "handle", ")", ")", ")", "return", "Symbol", "(", "handle", ")" ]
Creates a symbol that contains a collection of other symbols, grouped together. Example ------- >>> a = mx.sym.Variable('a') >>> b = mx.sym.Variable('b') >>> mx.sym.Group([a,b]) <Symbol Grouped> Parameters ---------- symbols : list List of symbols to be grouped. Returns ------- sym : Symbol A group symbol.
[ "Creates", "a", "symbol", "that", "contains", "a", "collection", "of", "other", "symbols", "grouped", "together", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L2656-L2682
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
load
def load(fname): """Loads symbol from a JSON file. You can also use pickle to do the job if you only work on python. The advantage of load/save is the file is language agnostic. This means the file saved using save can be loaded by other language binding of mxnet. You also get the benefit being able to directly load/save from cloud storage(S3, HDFS). Parameters ---------- fname : str The name of the file, examples: - `s3://my-bucket/path/my-s3-symbol` - `hdfs://my-bucket/path/my-hdfs-symbol` - `/path-to/my-local-symbol` Returns ------- sym : Symbol The loaded symbol. See Also -------- Symbol.save : Used to save symbol into file. """ if not isinstance(fname, string_types): raise TypeError('fname need to be string') handle = SymbolHandle() check_call(_LIB.MXSymbolCreateFromFile(c_str(fname), ctypes.byref(handle))) return Symbol(handle)
python
def load(fname): """Loads symbol from a JSON file. You can also use pickle to do the job if you only work on python. The advantage of load/save is the file is language agnostic. This means the file saved using save can be loaded by other language binding of mxnet. You also get the benefit being able to directly load/save from cloud storage(S3, HDFS). Parameters ---------- fname : str The name of the file, examples: - `s3://my-bucket/path/my-s3-symbol` - `hdfs://my-bucket/path/my-hdfs-symbol` - `/path-to/my-local-symbol` Returns ------- sym : Symbol The loaded symbol. See Also -------- Symbol.save : Used to save symbol into file. """ if not isinstance(fname, string_types): raise TypeError('fname need to be string') handle = SymbolHandle() check_call(_LIB.MXSymbolCreateFromFile(c_str(fname), ctypes.byref(handle))) return Symbol(handle)
[ "def", "load", "(", "fname", ")", ":", "if", "not", "isinstance", "(", "fname", ",", "string_types", ")", ":", "raise", "TypeError", "(", "'fname need to be string'", ")", "handle", "=", "SymbolHandle", "(", ")", "check_call", "(", "_LIB", ".", "MXSymbolCreateFromFile", "(", "c_str", "(", "fname", ")", ",", "ctypes", ".", "byref", "(", "handle", ")", ")", ")", "return", "Symbol", "(", "handle", ")" ]
Loads symbol from a JSON file. You can also use pickle to do the job if you only work on python. The advantage of load/save is the file is language agnostic. This means the file saved using save can be loaded by other language binding of mxnet. You also get the benefit being able to directly load/save from cloud storage(S3, HDFS). Parameters ---------- fname : str The name of the file, examples: - `s3://my-bucket/path/my-s3-symbol` - `hdfs://my-bucket/path/my-hdfs-symbol` - `/path-to/my-local-symbol` Returns ------- sym : Symbol The loaded symbol. See Also -------- Symbol.save : Used to save symbol into file.
[ "Loads", "symbol", "from", "a", "JSON", "file", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L2685-L2715
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
load_json
def load_json(json_str): """Loads symbol from json string. Parameters ---------- json_str : str A JSON string. Returns ------- sym : Symbol The loaded symbol. See Also -------- Symbol.tojson : Used to save symbol into json string. """ if not isinstance(json_str, string_types): raise TypeError('fname required to be string') handle = SymbolHandle() check_call(_LIB.MXSymbolCreateFromJSON(c_str(json_str), ctypes.byref(handle))) return Symbol(handle)
python
def load_json(json_str): """Loads symbol from json string. Parameters ---------- json_str : str A JSON string. Returns ------- sym : Symbol The loaded symbol. See Also -------- Symbol.tojson : Used to save symbol into json string. """ if not isinstance(json_str, string_types): raise TypeError('fname required to be string') handle = SymbolHandle() check_call(_LIB.MXSymbolCreateFromJSON(c_str(json_str), ctypes.byref(handle))) return Symbol(handle)
[ "def", "load_json", "(", "json_str", ")", ":", "if", "not", "isinstance", "(", "json_str", ",", "string_types", ")", ":", "raise", "TypeError", "(", "'fname required to be string'", ")", "handle", "=", "SymbolHandle", "(", ")", "check_call", "(", "_LIB", ".", "MXSymbolCreateFromJSON", "(", "c_str", "(", "json_str", ")", ",", "ctypes", ".", "byref", "(", "handle", ")", ")", ")", "return", "Symbol", "(", "handle", ")" ]
Loads symbol from json string. Parameters ---------- json_str : str A JSON string. Returns ------- sym : Symbol The loaded symbol. See Also -------- Symbol.tojson : Used to save symbol into json string.
[ "Loads", "symbol", "from", "json", "string", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L2718-L2739
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
pow
def pow(base, exp): """Returns element-wise result of base element raised to powers from exp element. Both inputs can be Symbol or scalar number. Broadcasting is not supported. Use `broadcast_pow` instead. `sym.pow` is being deprecated, please use `sym.power` instead. Parameters --------- base : Symbol or scalar The base symbol exp : Symbol or scalar The exponent symbol Returns ------- Symbol or scalar The bases in x raised to the exponents in y. Examples -------- >>> mx.sym.pow(2, 3) 8 >>> x = mx.sym.Variable('x') >>> y = mx.sym.Variable('y') >>> z = mx.sym.pow(x, 2) >>> z.eval(x=mx.nd.array([1,2]))[0].asnumpy() array([ 1., 4.], dtype=float32) >>> z = mx.sym.pow(3, y) >>> z.eval(y=mx.nd.array([2,3]))[0].asnumpy() array([ 9., 27.], dtype=float32) >>> z = mx.sym.pow(x, y) >>> z.eval(x=mx.nd.array([3,4]), y=mx.nd.array([2,3]))[0].asnumpy() array([ 9., 64.], dtype=float32) """ if isinstance(base, Symbol) and isinstance(exp, Symbol): return _internal._Power(base, exp) if isinstance(base, Symbol) and isinstance(exp, Number): return _internal._PowerScalar(base, scalar=exp) if isinstance(base, Number) and isinstance(exp, Symbol): return _internal._RPowerScalar(exp, scalar=base) if isinstance(base, Number) and isinstance(exp, Number): return base**exp else: raise TypeError('types (%s, %s) not supported' % (str(type(base)), str(type(exp))))
python
def pow(base, exp): """Returns element-wise result of base element raised to powers from exp element. Both inputs can be Symbol or scalar number. Broadcasting is not supported. Use `broadcast_pow` instead. `sym.pow` is being deprecated, please use `sym.power` instead. Parameters --------- base : Symbol or scalar The base symbol exp : Symbol or scalar The exponent symbol Returns ------- Symbol or scalar The bases in x raised to the exponents in y. Examples -------- >>> mx.sym.pow(2, 3) 8 >>> x = mx.sym.Variable('x') >>> y = mx.sym.Variable('y') >>> z = mx.sym.pow(x, 2) >>> z.eval(x=mx.nd.array([1,2]))[0].asnumpy() array([ 1., 4.], dtype=float32) >>> z = mx.sym.pow(3, y) >>> z.eval(y=mx.nd.array([2,3]))[0].asnumpy() array([ 9., 27.], dtype=float32) >>> z = mx.sym.pow(x, y) >>> z.eval(x=mx.nd.array([3,4]), y=mx.nd.array([2,3]))[0].asnumpy() array([ 9., 64.], dtype=float32) """ if isinstance(base, Symbol) and isinstance(exp, Symbol): return _internal._Power(base, exp) if isinstance(base, Symbol) and isinstance(exp, Number): return _internal._PowerScalar(base, scalar=exp) if isinstance(base, Number) and isinstance(exp, Symbol): return _internal._RPowerScalar(exp, scalar=base) if isinstance(base, Number) and isinstance(exp, Number): return base**exp else: raise TypeError('types (%s, %s) not supported' % (str(type(base)), str(type(exp))))
[ "def", "pow", "(", "base", ",", "exp", ")", ":", "if", "isinstance", "(", "base", ",", "Symbol", ")", "and", "isinstance", "(", "exp", ",", "Symbol", ")", ":", "return", "_internal", ".", "_Power", "(", "base", ",", "exp", ")", "if", "isinstance", "(", "base", ",", "Symbol", ")", "and", "isinstance", "(", "exp", ",", "Number", ")", ":", "return", "_internal", ".", "_PowerScalar", "(", "base", ",", "scalar", "=", "exp", ")", "if", "isinstance", "(", "base", ",", "Number", ")", "and", "isinstance", "(", "exp", ",", "Symbol", ")", ":", "return", "_internal", ".", "_RPowerScalar", "(", "exp", ",", "scalar", "=", "base", ")", "if", "isinstance", "(", "base", ",", "Number", ")", "and", "isinstance", "(", "exp", ",", "Number", ")", ":", "return", "base", "**", "exp", "else", ":", "raise", "TypeError", "(", "'types (%s, %s) not supported'", "%", "(", "str", "(", "type", "(", "base", ")", ")", ",", "str", "(", "type", "(", "exp", ")", ")", ")", ")" ]
Returns element-wise result of base element raised to powers from exp element. Both inputs can be Symbol or scalar number. Broadcasting is not supported. Use `broadcast_pow` instead. `sym.pow` is being deprecated, please use `sym.power` instead. Parameters --------- base : Symbol or scalar The base symbol exp : Symbol or scalar The exponent symbol Returns ------- Symbol or scalar The bases in x raised to the exponents in y. Examples -------- >>> mx.sym.pow(2, 3) 8 >>> x = mx.sym.Variable('x') >>> y = mx.sym.Variable('y') >>> z = mx.sym.pow(x, 2) >>> z.eval(x=mx.nd.array([1,2]))[0].asnumpy() array([ 1., 4.], dtype=float32) >>> z = mx.sym.pow(3, y) >>> z.eval(y=mx.nd.array([2,3]))[0].asnumpy() array([ 9., 27.], dtype=float32) >>> z = mx.sym.pow(x, y) >>> z.eval(x=mx.nd.array([3,4]), y=mx.nd.array([2,3]))[0].asnumpy() array([ 9., 64.], dtype=float32)
[ "Returns", "element", "-", "wise", "result", "of", "base", "element", "raised", "to", "powers", "from", "exp", "element", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L2744-L2789
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
maximum
def maximum(left, right): """Returns element-wise maximum of the input elements. Both inputs can be Symbol or scalar number. Broadcasting is not supported. Parameters --------- left : Symbol or scalar First symbol to be compared. right : Symbol or scalar Second symbol to be compared. Returns ------- Symbol or scalar The element-wise maximum of the input symbols. Examples -------- >>> mx.sym.maximum(2, 3.5) 3.5 >>> x = mx.sym.Variable('x') >>> y = mx.sym.Variable('y') >>> z = mx.sym.maximum(x, 4) >>> z.eval(x=mx.nd.array([3,5,2,10]))[0].asnumpy() array([ 4., 5., 4., 10.], dtype=float32) >>> z = mx.sym.maximum(x, y) >>> z.eval(x=mx.nd.array([3,4]), y=mx.nd.array([10,2]))[0].asnumpy() array([ 10., 4.], dtype=float32) """ if isinstance(left, Symbol) and isinstance(right, Symbol): return _internal._Maximum(left, right) if isinstance(left, Symbol) and isinstance(right, Number): return _internal._MaximumScalar(left, scalar=right) if isinstance(left, Number) and isinstance(right, Symbol): return _internal._MaximumScalar(right, scalar=left) if isinstance(left, Number) and isinstance(right, Number): return left if left > right else right else: raise TypeError('types (%s, %s) not supported' % (str(type(left)), str(type(right))))
python
def maximum(left, right): """Returns element-wise maximum of the input elements. Both inputs can be Symbol or scalar number. Broadcasting is not supported. Parameters --------- left : Symbol or scalar First symbol to be compared. right : Symbol or scalar Second symbol to be compared. Returns ------- Symbol or scalar The element-wise maximum of the input symbols. Examples -------- >>> mx.sym.maximum(2, 3.5) 3.5 >>> x = mx.sym.Variable('x') >>> y = mx.sym.Variable('y') >>> z = mx.sym.maximum(x, 4) >>> z.eval(x=mx.nd.array([3,5,2,10]))[0].asnumpy() array([ 4., 5., 4., 10.], dtype=float32) >>> z = mx.sym.maximum(x, y) >>> z.eval(x=mx.nd.array([3,4]), y=mx.nd.array([10,2]))[0].asnumpy() array([ 10., 4.], dtype=float32) """ if isinstance(left, Symbol) and isinstance(right, Symbol): return _internal._Maximum(left, right) if isinstance(left, Symbol) and isinstance(right, Number): return _internal._MaximumScalar(left, scalar=right) if isinstance(left, Number) and isinstance(right, Symbol): return _internal._MaximumScalar(right, scalar=left) if isinstance(left, Number) and isinstance(right, Number): return left if left > right else right else: raise TypeError('types (%s, %s) not supported' % (str(type(left)), str(type(right))))
[ "def", "maximum", "(", "left", ",", "right", ")", ":", "if", "isinstance", "(", "left", ",", "Symbol", ")", "and", "isinstance", "(", "right", ",", "Symbol", ")", ":", "return", "_internal", ".", "_Maximum", "(", "left", ",", "right", ")", "if", "isinstance", "(", "left", ",", "Symbol", ")", "and", "isinstance", "(", "right", ",", "Number", ")", ":", "return", "_internal", ".", "_MaximumScalar", "(", "left", ",", "scalar", "=", "right", ")", "if", "isinstance", "(", "left", ",", "Number", ")", "and", "isinstance", "(", "right", ",", "Symbol", ")", ":", "return", "_internal", ".", "_MaximumScalar", "(", "right", ",", "scalar", "=", "left", ")", "if", "isinstance", "(", "left", ",", "Number", ")", "and", "isinstance", "(", "right", ",", "Number", ")", ":", "return", "left", "if", "left", ">", "right", "else", "right", "else", ":", "raise", "TypeError", "(", "'types (%s, %s) not supported'", "%", "(", "str", "(", "type", "(", "left", ")", ")", ",", "str", "(", "type", "(", "right", ")", ")", ")", ")" ]
Returns element-wise maximum of the input elements. Both inputs can be Symbol or scalar number. Broadcasting is not supported. Parameters --------- left : Symbol or scalar First symbol to be compared. right : Symbol or scalar Second symbol to be compared. Returns ------- Symbol or scalar The element-wise maximum of the input symbols. Examples -------- >>> mx.sym.maximum(2, 3.5) 3.5 >>> x = mx.sym.Variable('x') >>> y = mx.sym.Variable('y') >>> z = mx.sym.maximum(x, 4) >>> z.eval(x=mx.nd.array([3,5,2,10]))[0].asnumpy() array([ 4., 5., 4., 10.], dtype=float32) >>> z = mx.sym.maximum(x, y) >>> z.eval(x=mx.nd.array([3,4]), y=mx.nd.array([10,2]))[0].asnumpy() array([ 10., 4.], dtype=float32)
[ "Returns", "element", "-", "wise", "maximum", "of", "the", "input", "elements", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L2831-L2870
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
minimum
def minimum(left, right): """Returns element-wise minimum of the input elements. Both inputs can be Symbol or scalar number. Broadcasting is not supported. Parameters --------- left : Symbol or scalar First symbol to be compared. right : Symbol or scalar Second symbol to be compared. Returns ------- Symbol or scalar The element-wise minimum of the input symbols. Examples -------- >>> mx.sym.minimum(2, 3.5) 2 >>> x = mx.sym.Variable('x') >>> y = mx.sym.Variable('y') >>> z = mx.sym.minimum(x, 4) >>> z.eval(x=mx.nd.array([3,5,2,10]))[0].asnumpy() array([ 3., 4., 2., 4.], dtype=float32) >>> z = mx.sym.minimum(x, y) >>> z.eval(x=mx.nd.array([3,4]), y=mx.nd.array([10,2]))[0].asnumpy() array([ 3., 2.], dtype=float32) """ if isinstance(left, Symbol) and isinstance(right, Symbol): return _internal._Minimum(left, right) if isinstance(left, Symbol) and isinstance(right, Number): return _internal._MinimumScalar(left, scalar=right) if isinstance(left, Number) and isinstance(right, Symbol): return _internal._MinimumScalar(right, scalar=left) if isinstance(left, Number) and isinstance(right, Number): return left if left < right else right else: raise TypeError('types (%s, %s) not supported' % (str(type(left)), str(type(right))))
python
def minimum(left, right): """Returns element-wise minimum of the input elements. Both inputs can be Symbol or scalar number. Broadcasting is not supported. Parameters --------- left : Symbol or scalar First symbol to be compared. right : Symbol or scalar Second symbol to be compared. Returns ------- Symbol or scalar The element-wise minimum of the input symbols. Examples -------- >>> mx.sym.minimum(2, 3.5) 2 >>> x = mx.sym.Variable('x') >>> y = mx.sym.Variable('y') >>> z = mx.sym.minimum(x, 4) >>> z.eval(x=mx.nd.array([3,5,2,10]))[0].asnumpy() array([ 3., 4., 2., 4.], dtype=float32) >>> z = mx.sym.minimum(x, y) >>> z.eval(x=mx.nd.array([3,4]), y=mx.nd.array([10,2]))[0].asnumpy() array([ 3., 2.], dtype=float32) """ if isinstance(left, Symbol) and isinstance(right, Symbol): return _internal._Minimum(left, right) if isinstance(left, Symbol) and isinstance(right, Number): return _internal._MinimumScalar(left, scalar=right) if isinstance(left, Number) and isinstance(right, Symbol): return _internal._MinimumScalar(right, scalar=left) if isinstance(left, Number) and isinstance(right, Number): return left if left < right else right else: raise TypeError('types (%s, %s) not supported' % (str(type(left)), str(type(right))))
[ "def", "minimum", "(", "left", ",", "right", ")", ":", "if", "isinstance", "(", "left", ",", "Symbol", ")", "and", "isinstance", "(", "right", ",", "Symbol", ")", ":", "return", "_internal", ".", "_Minimum", "(", "left", ",", "right", ")", "if", "isinstance", "(", "left", ",", "Symbol", ")", "and", "isinstance", "(", "right", ",", "Number", ")", ":", "return", "_internal", ".", "_MinimumScalar", "(", "left", ",", "scalar", "=", "right", ")", "if", "isinstance", "(", "left", ",", "Number", ")", "and", "isinstance", "(", "right", ",", "Symbol", ")", ":", "return", "_internal", ".", "_MinimumScalar", "(", "right", ",", "scalar", "=", "left", ")", "if", "isinstance", "(", "left", ",", "Number", ")", "and", "isinstance", "(", "right", ",", "Number", ")", ":", "return", "left", "if", "left", "<", "right", "else", "right", "else", ":", "raise", "TypeError", "(", "'types (%s, %s) not supported'", "%", "(", "str", "(", "type", "(", "left", ")", ")", ",", "str", "(", "type", "(", "right", ")", ")", ")", ")" ]
Returns element-wise minimum of the input elements. Both inputs can be Symbol or scalar number. Broadcasting is not supported. Parameters --------- left : Symbol or scalar First symbol to be compared. right : Symbol or scalar Second symbol to be compared. Returns ------- Symbol or scalar The element-wise minimum of the input symbols. Examples -------- >>> mx.sym.minimum(2, 3.5) 2 >>> x = mx.sym.Variable('x') >>> y = mx.sym.Variable('y') >>> z = mx.sym.minimum(x, 4) >>> z.eval(x=mx.nd.array([3,5,2,10]))[0].asnumpy() array([ 3., 4., 2., 4.], dtype=float32) >>> z = mx.sym.minimum(x, y) >>> z.eval(x=mx.nd.array([3,4]), y=mx.nd.array([10,2]))[0].asnumpy() array([ 3., 2.], dtype=float32)
[ "Returns", "element", "-", "wise", "minimum", "of", "the", "input", "elements", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L2875-L2914
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
hypot
def hypot(left, right): """Given the "legs" of a right triangle, returns its hypotenuse. Equivalent to :math:`\\sqrt(left^2 + right^2)`, element-wise. Both inputs can be Symbol or scalar number. Broadcasting is not supported. Parameters --------- left : Symbol or scalar First leg of the triangle(s). right : Symbol or scalar Second leg of the triangle(s). Returns ------- Symbol or scalar The hypotenuse of the triangle(s) Examples -------- >>> mx.sym.hypot(3, 4) 5.0 >>> x = mx.sym.Variable('x') >>> y = mx.sym.Variable('y') >>> z = mx.sym.hypot(x, 4) >>> z.eval(x=mx.nd.array([3,5,2]))[0].asnumpy() array([ 5., 6.40312433, 4.47213602], dtype=float32) >>> z = mx.sym.hypot(x, y) >>> z.eval(x=mx.nd.array([3,4]), y=mx.nd.array([10,2]))[0].asnumpy() array([ 10.44030666, 4.47213602], dtype=float32) """ if isinstance(left, Symbol) and isinstance(right, Symbol): return _internal._Hypot(left, right) if isinstance(left, Symbol) and isinstance(right, Number): return _internal._HypotScalar(left, scalar=right) if isinstance(left, Number) and isinstance(right, Symbol): return _internal._HypotScalar(right, scalar=left) if isinstance(left, Number) and isinstance(right, Number): return _numpy.hypot(left, right) else: raise TypeError('types (%s, %s) not supported' % (str(type(left)), str(type(right))))
python
def hypot(left, right): """Given the "legs" of a right triangle, returns its hypotenuse. Equivalent to :math:`\\sqrt(left^2 + right^2)`, element-wise. Both inputs can be Symbol or scalar number. Broadcasting is not supported. Parameters --------- left : Symbol or scalar First leg of the triangle(s). right : Symbol or scalar Second leg of the triangle(s). Returns ------- Symbol or scalar The hypotenuse of the triangle(s) Examples -------- >>> mx.sym.hypot(3, 4) 5.0 >>> x = mx.sym.Variable('x') >>> y = mx.sym.Variable('y') >>> z = mx.sym.hypot(x, 4) >>> z.eval(x=mx.nd.array([3,5,2]))[0].asnumpy() array([ 5., 6.40312433, 4.47213602], dtype=float32) >>> z = mx.sym.hypot(x, y) >>> z.eval(x=mx.nd.array([3,4]), y=mx.nd.array([10,2]))[0].asnumpy() array([ 10.44030666, 4.47213602], dtype=float32) """ if isinstance(left, Symbol) and isinstance(right, Symbol): return _internal._Hypot(left, right) if isinstance(left, Symbol) and isinstance(right, Number): return _internal._HypotScalar(left, scalar=right) if isinstance(left, Number) and isinstance(right, Symbol): return _internal._HypotScalar(right, scalar=left) if isinstance(left, Number) and isinstance(right, Number): return _numpy.hypot(left, right) else: raise TypeError('types (%s, %s) not supported' % (str(type(left)), str(type(right))))
[ "def", "hypot", "(", "left", ",", "right", ")", ":", "if", "isinstance", "(", "left", ",", "Symbol", ")", "and", "isinstance", "(", "right", ",", "Symbol", ")", ":", "return", "_internal", ".", "_Hypot", "(", "left", ",", "right", ")", "if", "isinstance", "(", "left", ",", "Symbol", ")", "and", "isinstance", "(", "right", ",", "Number", ")", ":", "return", "_internal", ".", "_HypotScalar", "(", "left", ",", "scalar", "=", "right", ")", "if", "isinstance", "(", "left", ",", "Number", ")", "and", "isinstance", "(", "right", ",", "Symbol", ")", ":", "return", "_internal", ".", "_HypotScalar", "(", "right", ",", "scalar", "=", "left", ")", "if", "isinstance", "(", "left", ",", "Number", ")", "and", "isinstance", "(", "right", ",", "Number", ")", ":", "return", "_numpy", ".", "hypot", "(", "left", ",", "right", ")", "else", ":", "raise", "TypeError", "(", "'types (%s, %s) not supported'", "%", "(", "str", "(", "type", "(", "left", ")", ")", ",", "str", "(", "type", "(", "right", ")", ")", ")", ")" ]
Given the "legs" of a right triangle, returns its hypotenuse. Equivalent to :math:`\\sqrt(left^2 + right^2)`, element-wise. Both inputs can be Symbol or scalar number. Broadcasting is not supported. Parameters --------- left : Symbol or scalar First leg of the triangle(s). right : Symbol or scalar Second leg of the triangle(s). Returns ------- Symbol or scalar The hypotenuse of the triangle(s) Examples -------- >>> mx.sym.hypot(3, 4) 5.0 >>> x = mx.sym.Variable('x') >>> y = mx.sym.Variable('y') >>> z = mx.sym.hypot(x, 4) >>> z.eval(x=mx.nd.array([3,5,2]))[0].asnumpy() array([ 5., 6.40312433, 4.47213602], dtype=float32) >>> z = mx.sym.hypot(x, y) >>> z.eval(x=mx.nd.array([3,4]), y=mx.nd.array([10,2]))[0].asnumpy() array([ 10.44030666, 4.47213602], dtype=float32)
[ "Given", "the", "legs", "of", "a", "right", "triangle", "returns", "its", "hypotenuse", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L2919-L2959
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
eye
def eye(N, M=0, k=0, dtype=None, **kwargs): """Returns a new symbol of 2-D shpae, filled with ones on the diagonal and zeros elsewhere. Parameters ---------- N: int Number of rows in the output. M: int, optional Number of columns in the output. If 0, defaults to N. k: int, optional Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal. dtype : str or numpy.dtype, optional The value type of the inner value, default to ``np.float32``. Returns ------- out : Symbol The created Symbol. """ if dtype is None: dtype = _numpy.float32 return _internal._eye(N, M, k, dtype=dtype, **kwargs)
python
def eye(N, M=0, k=0, dtype=None, **kwargs): """Returns a new symbol of 2-D shpae, filled with ones on the diagonal and zeros elsewhere. Parameters ---------- N: int Number of rows in the output. M: int, optional Number of columns in the output. If 0, defaults to N. k: int, optional Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal. dtype : str or numpy.dtype, optional The value type of the inner value, default to ``np.float32``. Returns ------- out : Symbol The created Symbol. """ if dtype is None: dtype = _numpy.float32 return _internal._eye(N, M, k, dtype=dtype, **kwargs)
[ "def", "eye", "(", "N", ",", "M", "=", "0", ",", "k", "=", "0", ",", "dtype", "=", "None", ",", "*", "*", "kwargs", ")", ":", "if", "dtype", "is", "None", ":", "dtype", "=", "_numpy", ".", "float32", "return", "_internal", ".", "_eye", "(", "N", ",", "M", ",", "k", ",", "dtype", "=", "dtype", ",", "*", "*", "kwargs", ")" ]
Returns a new symbol of 2-D shpae, filled with ones on the diagonal and zeros elsewhere. Parameters ---------- N: int Number of rows in the output. M: int, optional Number of columns in the output. If 0, defaults to N. k: int, optional Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal. dtype : str or numpy.dtype, optional The value type of the inner value, default to ``np.float32``. Returns ------- out : Symbol The created Symbol.
[ "Returns", "a", "new", "symbol", "of", "2", "-", "D", "shpae", "filled", "with", "ones", "on", "the", "diagonal", "and", "zeros", "elsewhere", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L2962-L2985
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
zeros
def zeros(shape, dtype=None, **kwargs): """Returns a new symbol of given shape and type, filled with zeros. Parameters ---------- shape : int or sequence of ints Shape of the new array. dtype : str or numpy.dtype, optional The value type of the inner value, default to ``np.float32``. Returns ------- out : Symbol The created Symbol. """ if dtype is None: dtype = _numpy.float32 return _internal._zeros(shape=shape, dtype=dtype, **kwargs)
python
def zeros(shape, dtype=None, **kwargs): """Returns a new symbol of given shape and type, filled with zeros. Parameters ---------- shape : int or sequence of ints Shape of the new array. dtype : str or numpy.dtype, optional The value type of the inner value, default to ``np.float32``. Returns ------- out : Symbol The created Symbol. """ if dtype is None: dtype = _numpy.float32 return _internal._zeros(shape=shape, dtype=dtype, **kwargs)
[ "def", "zeros", "(", "shape", ",", "dtype", "=", "None", ",", "*", "*", "kwargs", ")", ":", "if", "dtype", "is", "None", ":", "dtype", "=", "_numpy", ".", "float32", "return", "_internal", ".", "_zeros", "(", "shape", "=", "shape", ",", "dtype", "=", "dtype", ",", "*", "*", "kwargs", ")" ]
Returns a new symbol of given shape and type, filled with zeros. Parameters ---------- shape : int or sequence of ints Shape of the new array. dtype : str or numpy.dtype, optional The value type of the inner value, default to ``np.float32``. Returns ------- out : Symbol The created Symbol.
[ "Returns", "a", "new", "symbol", "of", "given", "shape", "and", "type", "filled", "with", "zeros", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L2987-L3004
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
ones
def ones(shape, dtype=None, **kwargs): """Returns a new symbol of given shape and type, filled with ones. Parameters ---------- shape : int or sequence of ints Shape of the new array. dtype : str or numpy.dtype, optional The value type of the inner value, default to ``np.float32``. Returns ------- out : Symbol The created Symbol """ if dtype is None: dtype = _numpy.float32 return _internal._ones(shape=shape, dtype=dtype, **kwargs)
python
def ones(shape, dtype=None, **kwargs): """Returns a new symbol of given shape and type, filled with ones. Parameters ---------- shape : int or sequence of ints Shape of the new array. dtype : str or numpy.dtype, optional The value type of the inner value, default to ``np.float32``. Returns ------- out : Symbol The created Symbol """ if dtype is None: dtype = _numpy.float32 return _internal._ones(shape=shape, dtype=dtype, **kwargs)
[ "def", "ones", "(", "shape", ",", "dtype", "=", "None", ",", "*", "*", "kwargs", ")", ":", "if", "dtype", "is", "None", ":", "dtype", "=", "_numpy", ".", "float32", "return", "_internal", ".", "_ones", "(", "shape", "=", "shape", ",", "dtype", "=", "dtype", ",", "*", "*", "kwargs", ")" ]
Returns a new symbol of given shape and type, filled with ones. Parameters ---------- shape : int or sequence of ints Shape of the new array. dtype : str or numpy.dtype, optional The value type of the inner value, default to ``np.float32``. Returns ------- out : Symbol The created Symbol
[ "Returns", "a", "new", "symbol", "of", "given", "shape", "and", "type", "filled", "with", "ones", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L3007-L3024
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
full
def full(shape, val, dtype=None, **kwargs): """Returns a new array of given shape and type, filled with the given value `val`. Parameters ---------- shape : int or sequence of ints Shape of the new array. val : scalar Fill value. dtype : str or numpy.dtype, optional The value type of the inner value, default to ``np.float32``. Returns ------- out : Symbol The created Symbol """ if dtype is None: dtype = _numpy.float32 return _internal._full(shape=shape, dtype=dtype, value=float(val), **kwargs)
python
def full(shape, val, dtype=None, **kwargs): """Returns a new array of given shape and type, filled with the given value `val`. Parameters ---------- shape : int or sequence of ints Shape of the new array. val : scalar Fill value. dtype : str or numpy.dtype, optional The value type of the inner value, default to ``np.float32``. Returns ------- out : Symbol The created Symbol """ if dtype is None: dtype = _numpy.float32 return _internal._full(shape=shape, dtype=dtype, value=float(val), **kwargs)
[ "def", "full", "(", "shape", ",", "val", ",", "dtype", "=", "None", ",", "*", "*", "kwargs", ")", ":", "if", "dtype", "is", "None", ":", "dtype", "=", "_numpy", ".", "float32", "return", "_internal", ".", "_full", "(", "shape", "=", "shape", ",", "dtype", "=", "dtype", ",", "value", "=", "float", "(", "val", ")", ",", "*", "*", "kwargs", ")" ]
Returns a new array of given shape and type, filled with the given value `val`. Parameters ---------- shape : int or sequence of ints Shape of the new array. val : scalar Fill value. dtype : str or numpy.dtype, optional The value type of the inner value, default to ``np.float32``. Returns ------- out : Symbol The created Symbol
[ "Returns", "a", "new", "array", "of", "given", "shape", "and", "type", "filled", "with", "the", "given", "value", "val", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L3027-L3046
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
arange
def arange(start, stop=None, step=1.0, repeat=1, infer_range=False, name=None, dtype=None): """Returns evenly spaced values within a given interval. Values are generated within the half-open interval [`start`, `stop`). In other words, the interval includes `start` but excludes `stop`. The function is similar to the built-in Python function `range` and to `numpy.arange`, but returns a `Symbol`. Parameters ---------- start : number, optional Start of interval. The interval includes this value. The default start value is 0. stop : number End of interval. The interval does not include this value. step : number, optional Spacing between values. repeat : int, optional "The repeating time of all elements. E.g repeat=3, the element a will be repeated three times --> a, a, a. infer_range : boolean, optional When set to True, infer the stop position from the start, step, repeat, and output tensor size. dtype : str or numpy.dtype, optional The value type of the inner value, default to ``np.float32``. Returns ------- out : Symbol The created Symbol """ if dtype is None: dtype = _numpy.float32 return _internal._arange(start=start, stop=stop, step=step, repeat=repeat, infer_range=infer_range, name=name, dtype=dtype)
python
def arange(start, stop=None, step=1.0, repeat=1, infer_range=False, name=None, dtype=None): """Returns evenly spaced values within a given interval. Values are generated within the half-open interval [`start`, `stop`). In other words, the interval includes `start` but excludes `stop`. The function is similar to the built-in Python function `range` and to `numpy.arange`, but returns a `Symbol`. Parameters ---------- start : number, optional Start of interval. The interval includes this value. The default start value is 0. stop : number End of interval. The interval does not include this value. step : number, optional Spacing between values. repeat : int, optional "The repeating time of all elements. E.g repeat=3, the element a will be repeated three times --> a, a, a. infer_range : boolean, optional When set to True, infer the stop position from the start, step, repeat, and output tensor size. dtype : str or numpy.dtype, optional The value type of the inner value, default to ``np.float32``. Returns ------- out : Symbol The created Symbol """ if dtype is None: dtype = _numpy.float32 return _internal._arange(start=start, stop=stop, step=step, repeat=repeat, infer_range=infer_range, name=name, dtype=dtype)
[ "def", "arange", "(", "start", ",", "stop", "=", "None", ",", "step", "=", "1.0", ",", "repeat", "=", "1", ",", "infer_range", "=", "False", ",", "name", "=", "None", ",", "dtype", "=", "None", ")", ":", "if", "dtype", "is", "None", ":", "dtype", "=", "_numpy", ".", "float32", "return", "_internal", ".", "_arange", "(", "start", "=", "start", ",", "stop", "=", "stop", ",", "step", "=", "step", ",", "repeat", "=", "repeat", ",", "infer_range", "=", "infer_range", ",", "name", "=", "name", ",", "dtype", "=", "dtype", ")" ]
Returns evenly spaced values within a given interval. Values are generated within the half-open interval [`start`, `stop`). In other words, the interval includes `start` but excludes `stop`. The function is similar to the built-in Python function `range` and to `numpy.arange`, but returns a `Symbol`. Parameters ---------- start : number, optional Start of interval. The interval includes this value. The default start value is 0. stop : number End of interval. The interval does not include this value. step : number, optional Spacing between values. repeat : int, optional "The repeating time of all elements. E.g repeat=3, the element a will be repeated three times --> a, a, a. infer_range : boolean, optional When set to True, infer the stop position from the start, step, repeat, and output tensor size. dtype : str or numpy.dtype, optional The value type of the inner value, default to ``np.float32``. Returns ------- out : Symbol The created Symbol
[ "Returns", "evenly", "spaced", "values", "within", "a", "given", "interval", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L3049-L3082
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
histogram
def histogram(a, bins=10, range=None, **kwargs): """Compute the histogram of the input data. Parameters ---------- a : NDArray Input data. The histogram is computed over the flattened array. bins : int or sequence of scalars If bins is an int, it defines the number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths. range : (float, float), required if bins is an integer The lower and upper range of the bins. If not provided, range is simply (a.min(), a.max()). Values outside the range are ignored. The first element of the range must be less than or equal to the second. range affects the automatic bin computation as well, the range will be equally divided by the number of bins. Returns ------- out : Symbol The created Symbol """ if isinstance(bins, Symbol): return _internal._histogram(data=a, bins=bins, **kwargs) elif isinstance(bins, integer_types): if range is None: raise ValueError("null range is not supported in symbol mode") return _internal._histogram(data=a, bin_cnt=bins, range=range, **kwargs) raise ValueError("bins argument should be either an integer or an NDArray")
python
def histogram(a, bins=10, range=None, **kwargs): """Compute the histogram of the input data. Parameters ---------- a : NDArray Input data. The histogram is computed over the flattened array. bins : int or sequence of scalars If bins is an int, it defines the number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths. range : (float, float), required if bins is an integer The lower and upper range of the bins. If not provided, range is simply (a.min(), a.max()). Values outside the range are ignored. The first element of the range must be less than or equal to the second. range affects the automatic bin computation as well, the range will be equally divided by the number of bins. Returns ------- out : Symbol The created Symbol """ if isinstance(bins, Symbol): return _internal._histogram(data=a, bins=bins, **kwargs) elif isinstance(bins, integer_types): if range is None: raise ValueError("null range is not supported in symbol mode") return _internal._histogram(data=a, bin_cnt=bins, range=range, **kwargs) raise ValueError("bins argument should be either an integer or an NDArray")
[ "def", "histogram", "(", "a", ",", "bins", "=", "10", ",", "range", "=", "None", ",", "*", "*", "kwargs", ")", ":", "if", "isinstance", "(", "bins", ",", "Symbol", ")", ":", "return", "_internal", ".", "_histogram", "(", "data", "=", "a", ",", "bins", "=", "bins", ",", "*", "*", "kwargs", ")", "elif", "isinstance", "(", "bins", ",", "integer_types", ")", ":", "if", "range", "is", "None", ":", "raise", "ValueError", "(", "\"null range is not supported in symbol mode\"", ")", "return", "_internal", ".", "_histogram", "(", "data", "=", "a", ",", "bin_cnt", "=", "bins", ",", "range", "=", "range", ",", "*", "*", "kwargs", ")", "raise", "ValueError", "(", "\"bins argument should be either an integer or an NDArray\"", ")" ]
Compute the histogram of the input data. Parameters ---------- a : NDArray Input data. The histogram is computed over the flattened array. bins : int or sequence of scalars If bins is an int, it defines the number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths. range : (float, float), required if bins is an integer The lower and upper range of the bins. If not provided, range is simply (a.min(), a.max()). Values outside the range are ignored. The first element of the range must be less than or equal to the second. range affects the automatic bin computation as well, the range will be equally divided by the number of bins. Returns ------- out : Symbol The created Symbol
[ "Compute", "the", "histogram", "of", "the", "input", "data", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L3084-L3112
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
split_v2
def split_v2(ary, indices_or_sections, axis=0, squeeze_axis=False): """Split an array into multiple sub-arrays. Parameters ---------- ary : NDArray Array to be divided into sub-arrays. indices_or_sections : int or tuple of ints If `indices_or_sections` is an integer, N, the array will be divided into N equal arrays along `axis`. If such a split is not possible, an error is raised. If `indices_or_sections` is a 1-D array of sorted integers, the entries indicate where along `axis` the array is split. For example, ``[2, 3]`` would, for ``axis=0``, result in - ary[:2] - ary[2:3] - ary[3:] If an index exceeds the dimension of the array along `axis`, an empty sub-array is returned correspondingly. axis : int, optional The axis along which to split, default is 0. squeeze_axis: boolean, optional Whether to squeeze the axis of sub-arrays or not, only useful when size of the sub-arrays are 1 on the `axis`. Default is False. Returns ------- out : Symbol The created Symbol """ indices = [] sections = 0 if isinstance(indices_or_sections, int): sections = indices_or_sections elif isinstance(indices_or_sections, tuple): indices = [0] + list(indices_or_sections) else: raise ValueError('indices_or_sections must either int or tuple of ints') return _internal._split_v2(ary, indices, axis, squeeze_axis, sections)
python
def split_v2(ary, indices_or_sections, axis=0, squeeze_axis=False): """Split an array into multiple sub-arrays. Parameters ---------- ary : NDArray Array to be divided into sub-arrays. indices_or_sections : int or tuple of ints If `indices_or_sections` is an integer, N, the array will be divided into N equal arrays along `axis`. If such a split is not possible, an error is raised. If `indices_or_sections` is a 1-D array of sorted integers, the entries indicate where along `axis` the array is split. For example, ``[2, 3]`` would, for ``axis=0``, result in - ary[:2] - ary[2:3] - ary[3:] If an index exceeds the dimension of the array along `axis`, an empty sub-array is returned correspondingly. axis : int, optional The axis along which to split, default is 0. squeeze_axis: boolean, optional Whether to squeeze the axis of sub-arrays or not, only useful when size of the sub-arrays are 1 on the `axis`. Default is False. Returns ------- out : Symbol The created Symbol """ indices = [] sections = 0 if isinstance(indices_or_sections, int): sections = indices_or_sections elif isinstance(indices_or_sections, tuple): indices = [0] + list(indices_or_sections) else: raise ValueError('indices_or_sections must either int or tuple of ints') return _internal._split_v2(ary, indices, axis, squeeze_axis, sections)
[ "def", "split_v2", "(", "ary", ",", "indices_or_sections", ",", "axis", "=", "0", ",", "squeeze_axis", "=", "False", ")", ":", "indices", "=", "[", "]", "sections", "=", "0", "if", "isinstance", "(", "indices_or_sections", ",", "int", ")", ":", "sections", "=", "indices_or_sections", "elif", "isinstance", "(", "indices_or_sections", ",", "tuple", ")", ":", "indices", "=", "[", "0", "]", "+", "list", "(", "indices_or_sections", ")", "else", ":", "raise", "ValueError", "(", "'indices_or_sections must either int or tuple of ints'", ")", "return", "_internal", ".", "_split_v2", "(", "ary", ",", "indices", ",", "axis", ",", "squeeze_axis", ",", "sections", ")" ]
Split an array into multiple sub-arrays. Parameters ---------- ary : NDArray Array to be divided into sub-arrays. indices_or_sections : int or tuple of ints If `indices_or_sections` is an integer, N, the array will be divided into N equal arrays along `axis`. If such a split is not possible, an error is raised. If `indices_or_sections` is a 1-D array of sorted integers, the entries indicate where along `axis` the array is split. For example, ``[2, 3]`` would, for ``axis=0``, result in - ary[:2] - ary[2:3] - ary[3:] If an index exceeds the dimension of the array along `axis`, an empty sub-array is returned correspondingly. axis : int, optional The axis along which to split, default is 0. squeeze_axis: boolean, optional Whether to squeeze the axis of sub-arrays or not, only useful when size of the sub-arrays are 1 on the `axis`. Default is False. Returns ------- out : Symbol The created Symbol
[ "Split", "an", "array", "into", "multiple", "sub", "-", "arrays", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L3114-L3152
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
Symbol.name
def name(self): """Gets name string from the symbol, this function only works for non-grouped symbol. Returns ------- value : str The name of this symbol, returns ``None`` for grouped symbol. """ ret = ctypes.c_char_p() success = ctypes.c_int() check_call(_LIB.MXSymbolGetName( self.handle, ctypes.byref(ret), ctypes.byref(success))) if success.value != 0: return py_str(ret.value) else: return None
python
def name(self): """Gets name string from the symbol, this function only works for non-grouped symbol. Returns ------- value : str The name of this symbol, returns ``None`` for grouped symbol. """ ret = ctypes.c_char_p() success = ctypes.c_int() check_call(_LIB.MXSymbolGetName( self.handle, ctypes.byref(ret), ctypes.byref(success))) if success.value != 0: return py_str(ret.value) else: return None
[ "def", "name", "(", "self", ")", ":", "ret", "=", "ctypes", ".", "c_char_p", "(", ")", "success", "=", "ctypes", ".", "c_int", "(", ")", "check_call", "(", "_LIB", ".", "MXSymbolGetName", "(", "self", ".", "handle", ",", "ctypes", ".", "byref", "(", "ret", ")", ",", "ctypes", ".", "byref", "(", "success", ")", ")", ")", "if", "success", ".", "value", "!=", "0", ":", "return", "py_str", "(", "ret", ".", "value", ")", "else", ":", "return", "None" ]
Gets name string from the symbol, this function only works for non-grouped symbol. Returns ------- value : str The name of this symbol, returns ``None`` for grouped symbol.
[ "Gets", "name", "string", "from", "the", "symbol", "this", "function", "only", "works", "for", "non", "-", "grouped", "symbol", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L534-L549
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
Symbol.attr
def attr(self, key): """Returns the attribute string for corresponding input key from the symbol. This function only works for non-grouped symbols. Example ------- >>> data = mx.sym.Variable('data', attr={'mood': 'angry'}) >>> data.attr('mood') 'angry' Parameters ---------- key : str The key corresponding to the desired attribute. Returns ------- value : str The desired attribute value, returns ``None`` if the attribute does not exist. """ ret = ctypes.c_char_p() success = ctypes.c_int() check_call(_LIB.MXSymbolGetAttr( self.handle, c_str(key), ctypes.byref(ret), ctypes.byref(success))) if success.value != 0: return py_str(ret.value) else: return None
python
def attr(self, key): """Returns the attribute string for corresponding input key from the symbol. This function only works for non-grouped symbols. Example ------- >>> data = mx.sym.Variable('data', attr={'mood': 'angry'}) >>> data.attr('mood') 'angry' Parameters ---------- key : str The key corresponding to the desired attribute. Returns ------- value : str The desired attribute value, returns ``None`` if the attribute does not exist. """ ret = ctypes.c_char_p() success = ctypes.c_int() check_call(_LIB.MXSymbolGetAttr( self.handle, c_str(key), ctypes.byref(ret), ctypes.byref(success))) if success.value != 0: return py_str(ret.value) else: return None
[ "def", "attr", "(", "self", ",", "key", ")", ":", "ret", "=", "ctypes", ".", "c_char_p", "(", ")", "success", "=", "ctypes", ".", "c_int", "(", ")", "check_call", "(", "_LIB", ".", "MXSymbolGetAttr", "(", "self", ".", "handle", ",", "c_str", "(", "key", ")", ",", "ctypes", ".", "byref", "(", "ret", ")", ",", "ctypes", ".", "byref", "(", "success", ")", ")", ")", "if", "success", ".", "value", "!=", "0", ":", "return", "py_str", "(", "ret", ".", "value", ")", "else", ":", "return", "None" ]
Returns the attribute string for corresponding input key from the symbol. This function only works for non-grouped symbols. Example ------- >>> data = mx.sym.Variable('data', attr={'mood': 'angry'}) >>> data.attr('mood') 'angry' Parameters ---------- key : str The key corresponding to the desired attribute. Returns ------- value : str The desired attribute value, returns ``None`` if the attribute does not exist.
[ "Returns", "the", "attribute", "string", "for", "corresponding", "input", "key", "from", "the", "symbol", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L551-L579
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
Symbol.list_attr
def list_attr(self, recursive=False): """Gets all attributes from the symbol. Example ------- >>> data = mx.sym.Variable('data', attr={'mood': 'angry'}) >>> data.list_attr() {'mood': 'angry'} Returns ------- ret : Dict of str to str A dictionary mapping attribute keys to values. """ if recursive: raise DeprecationWarning("Symbol.list_attr with recursive=True has been deprecated. " "Please use attr_dict instead.") size = mx_uint() pairs = ctypes.POINTER(ctypes.c_char_p)() f_handle = _LIB.MXSymbolListAttrShallow check_call(f_handle(self.handle, ctypes.byref(size), ctypes.byref(pairs))) return {py_str(pairs[i * 2]): py_str(pairs[i * 2 + 1]) for i in range(size.value)}
python
def list_attr(self, recursive=False): """Gets all attributes from the symbol. Example ------- >>> data = mx.sym.Variable('data', attr={'mood': 'angry'}) >>> data.list_attr() {'mood': 'angry'} Returns ------- ret : Dict of str to str A dictionary mapping attribute keys to values. """ if recursive: raise DeprecationWarning("Symbol.list_attr with recursive=True has been deprecated. " "Please use attr_dict instead.") size = mx_uint() pairs = ctypes.POINTER(ctypes.c_char_p)() f_handle = _LIB.MXSymbolListAttrShallow check_call(f_handle(self.handle, ctypes.byref(size), ctypes.byref(pairs))) return {py_str(pairs[i * 2]): py_str(pairs[i * 2 + 1]) for i in range(size.value)}
[ "def", "list_attr", "(", "self", ",", "recursive", "=", "False", ")", ":", "if", "recursive", ":", "raise", "DeprecationWarning", "(", "\"Symbol.list_attr with recursive=True has been deprecated. \"", "\"Please use attr_dict instead.\"", ")", "size", "=", "mx_uint", "(", ")", "pairs", "=", "ctypes", ".", "POINTER", "(", "ctypes", ".", "c_char_p", ")", "(", ")", "f_handle", "=", "_LIB", ".", "MXSymbolListAttrShallow", "check_call", "(", "f_handle", "(", "self", ".", "handle", ",", "ctypes", ".", "byref", "(", "size", ")", ",", "ctypes", ".", "byref", "(", "pairs", ")", ")", ")", "return", "{", "py_str", "(", "pairs", "[", "i", "*", "2", "]", ")", ":", "py_str", "(", "pairs", "[", "i", "*", "2", "+", "1", "]", ")", "for", "i", "in", "range", "(", "size", ".", "value", ")", "}" ]
Gets all attributes from the symbol. Example ------- >>> data = mx.sym.Variable('data', attr={'mood': 'angry'}) >>> data.list_attr() {'mood': 'angry'} Returns ------- ret : Dict of str to str A dictionary mapping attribute keys to values.
[ "Gets", "all", "attributes", "from", "the", "symbol", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L581-L602
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
Symbol.attr_dict
def attr_dict(self): """Recursively gets all attributes from the symbol and its children. Example ------- >>> a = mx.sym.Variable('a', attr={'a1':'a2'}) >>> b = mx.sym.Variable('b', attr={'b1':'b2'}) >>> c = a+b >>> c.attr_dict() {'a': {'a1': 'a2'}, 'b': {'b1': 'b2'}} Returns ------- ret : Dict of str to dict There is a key in the returned dict for every child with non-empty attribute set. For each symbol, the name of the symbol is its key in the dict and the correspond value is that symbol's attribute list (itself a dictionary). """ size = mx_uint() pairs = ctypes.POINTER(ctypes.c_char_p)() f_handle = _LIB.MXSymbolListAttr check_call(f_handle(self.handle, ctypes.byref(size), ctypes.byref(pairs))) ret = {} for i in range(size.value): name, key = py_str(pairs[i * 2]).split('$') val = py_str(pairs[i * 2 + 1]) if name not in ret: ret[name] = {} ret[name][key] = val return ret
python
def attr_dict(self): """Recursively gets all attributes from the symbol and its children. Example ------- >>> a = mx.sym.Variable('a', attr={'a1':'a2'}) >>> b = mx.sym.Variable('b', attr={'b1':'b2'}) >>> c = a+b >>> c.attr_dict() {'a': {'a1': 'a2'}, 'b': {'b1': 'b2'}} Returns ------- ret : Dict of str to dict There is a key in the returned dict for every child with non-empty attribute set. For each symbol, the name of the symbol is its key in the dict and the correspond value is that symbol's attribute list (itself a dictionary). """ size = mx_uint() pairs = ctypes.POINTER(ctypes.c_char_p)() f_handle = _LIB.MXSymbolListAttr check_call(f_handle(self.handle, ctypes.byref(size), ctypes.byref(pairs))) ret = {} for i in range(size.value): name, key = py_str(pairs[i * 2]).split('$') val = py_str(pairs[i * 2 + 1]) if name not in ret: ret[name] = {} ret[name][key] = val return ret
[ "def", "attr_dict", "(", "self", ")", ":", "size", "=", "mx_uint", "(", ")", "pairs", "=", "ctypes", ".", "POINTER", "(", "ctypes", ".", "c_char_p", ")", "(", ")", "f_handle", "=", "_LIB", ".", "MXSymbolListAttr", "check_call", "(", "f_handle", "(", "self", ".", "handle", ",", "ctypes", ".", "byref", "(", "size", ")", ",", "ctypes", ".", "byref", "(", "pairs", ")", ")", ")", "ret", "=", "{", "}", "for", "i", "in", "range", "(", "size", ".", "value", ")", ":", "name", ",", "key", "=", "py_str", "(", "pairs", "[", "i", "*", "2", "]", ")", ".", "split", "(", "'$'", ")", "val", "=", "py_str", "(", "pairs", "[", "i", "*", "2", "+", "1", "]", ")", "if", "name", "not", "in", "ret", ":", "ret", "[", "name", "]", "=", "{", "}", "ret", "[", "name", "]", "[", "key", "]", "=", "val", "return", "ret" ]
Recursively gets all attributes from the symbol and its children. Example ------- >>> a = mx.sym.Variable('a', attr={'a1':'a2'}) >>> b = mx.sym.Variable('b', attr={'b1':'b2'}) >>> c = a+b >>> c.attr_dict() {'a': {'a1': 'a2'}, 'b': {'b1': 'b2'}} Returns ------- ret : Dict of str to dict There is a key in the returned dict for every child with non-empty attribute set. For each symbol, the name of the symbol is its key in the dict and the correspond value is that symbol's attribute list (itself a dictionary).
[ "Recursively", "gets", "all", "attributes", "from", "the", "symbol", "and", "its", "children", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L604-L633
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
Symbol._set_attr
def _set_attr(self, **kwargs): """Sets an attribute of the symbol. For example. A._set_attr(foo="bar") adds the mapping ``"{foo: bar}"`` to the symbol's attribute dictionary. Parameters ---------- **kwargs The attributes to set """ for key, value in kwargs.items(): if not isinstance(value, string_types): raise ValueError("Set Attr only accepts string values") check_call(_LIB.MXSymbolSetAttr( self.handle, c_str(key), c_str(str(value))))
python
def _set_attr(self, **kwargs): """Sets an attribute of the symbol. For example. A._set_attr(foo="bar") adds the mapping ``"{foo: bar}"`` to the symbol's attribute dictionary. Parameters ---------- **kwargs The attributes to set """ for key, value in kwargs.items(): if not isinstance(value, string_types): raise ValueError("Set Attr only accepts string values") check_call(_LIB.MXSymbolSetAttr( self.handle, c_str(key), c_str(str(value))))
[ "def", "_set_attr", "(", "self", ",", "*", "*", "kwargs", ")", ":", "for", "key", ",", "value", "in", "kwargs", ".", "items", "(", ")", ":", "if", "not", "isinstance", "(", "value", ",", "string_types", ")", ":", "raise", "ValueError", "(", "\"Set Attr only accepts string values\"", ")", "check_call", "(", "_LIB", ".", "MXSymbolSetAttr", "(", "self", ".", "handle", ",", "c_str", "(", "key", ")", ",", "c_str", "(", "str", "(", "value", ")", ")", ")", ")" ]
Sets an attribute of the symbol. For example. A._set_attr(foo="bar") adds the mapping ``"{foo: bar}"`` to the symbol's attribute dictionary. Parameters ---------- **kwargs The attributes to set
[ "Sets", "an", "attribute", "of", "the", "symbol", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L635-L650
train
apache/incubator-mxnet
python/mxnet/symbol/symbol.py
Symbol.get_internals
def get_internals(self): """Gets a new grouped symbol `sgroup`. The output of `sgroup` is a list of outputs of all of the internal nodes. Consider the following code: Example ------- >>> a = mx.sym.var('a') >>> b = mx.sym.var('b') >>> c = a + b >>> d = c.get_internals() >>> d <Symbol Grouped> >>> d.list_outputs() ['a', 'b', '_plus4_output'] Returns ------- sgroup : Symbol A symbol group containing all internal and leaf nodes of the computation graph used to compute the symbol. """ handle = SymbolHandle() check_call(_LIB.MXSymbolGetInternals( self.handle, ctypes.byref(handle))) return Symbol(handle=handle)
python
def get_internals(self): """Gets a new grouped symbol `sgroup`. The output of `sgroup` is a list of outputs of all of the internal nodes. Consider the following code: Example ------- >>> a = mx.sym.var('a') >>> b = mx.sym.var('b') >>> c = a + b >>> d = c.get_internals() >>> d <Symbol Grouped> >>> d.list_outputs() ['a', 'b', '_plus4_output'] Returns ------- sgroup : Symbol A symbol group containing all internal and leaf nodes of the computation graph used to compute the symbol. """ handle = SymbolHandle() check_call(_LIB.MXSymbolGetInternals( self.handle, ctypes.byref(handle))) return Symbol(handle=handle)
[ "def", "get_internals", "(", "self", ")", ":", "handle", "=", "SymbolHandle", "(", ")", "check_call", "(", "_LIB", ".", "MXSymbolGetInternals", "(", "self", ".", "handle", ",", "ctypes", ".", "byref", "(", "handle", ")", ")", ")", "return", "Symbol", "(", "handle", "=", "handle", ")" ]
Gets a new grouped symbol `sgroup`. The output of `sgroup` is a list of outputs of all of the internal nodes. Consider the following code: Example ------- >>> a = mx.sym.var('a') >>> b = mx.sym.var('b') >>> c = a + b >>> d = c.get_internals() >>> d <Symbol Grouped> >>> d.list_outputs() ['a', 'b', '_plus4_output'] Returns ------- sgroup : Symbol A symbol group containing all internal and leaf nodes of the computation graph used to compute the symbol.
[ "Gets", "a", "new", "grouped", "symbol", "sgroup", ".", "The", "output", "of", "sgroup", "is", "a", "list", "of", "outputs", "of", "all", "of", "the", "internal", "nodes", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/symbol/symbol.py#L652-L678
train