# gluon.SymbolBlock¶

class mxnet.gluon.SymbolBlock(outputs, inputs, params=None)[source]

Bases: mxnet.gluon.block.HybridBlock

Construct block from symbol. This is useful for using pre-trained models as feature extractors. For example, you may want to extract the output from fc2 layer in AlexNet.

Parameters
• outputs (Symbol or list of Symbol) – The desired output for SymbolBlock.

• inputs (Symbol or list of Symbol) – The Variables in output’s argument that should be used as inputs.

• params (ParameterDict) – Parameter dictionary for arguments and auxililary states of outputs that are not inputs.

Methods

 apply(fn) Applies fn recursively to every child block as well as self. cast(dtype) Cast this Block to use another data type. collect_params([select]) Returns a ParameterDict containing this Block and all of its children’s Parameters(default), also can returns the select ParameterDict which match some given regular expressions. export(path[, epoch, remove_amp_cast]) Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports, mxnet.mod.Module or the C++ interface. forward(x, *args) Defines the forward computation. hybrid_forward(F, x, *args, **kwargs) Overrides to construct symbolic graph for this Block. hybridize([active, backend, backend_opts]) Activates or deactivates HybridBlock s recursively. imports(symbol_file, input_names[, …]) Import model previously saved by gluon.HybridBlock.export or Module.save_checkpoint as a gluon.SymbolBlock for use in Gluon. infer_shape(*args) Infers shape of Parameters from inputs. infer_type(*args) Infers data type of Parameters from inputs. initialize([init, ctx, verbose, force_reinit]) Initializes Parameter s of this Block and its children. load_parameters(filename[, ctx, …]) Load parameters from file previously saved by save_parameters. load_params(filename[, ctx, allow_missing, …]) [Deprecated] Please use load_parameters. Returns a name space object managing a child Block and parameter names. optimize_for(x, *args[, backend, backend_opts]) Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. register_child(block[, name]) Registers block as a child of self. Registers a forward hook on the block. Registers a forward pre-hook on the block. register_op_hook(callback[, monitor_all]) Install op hook for block recursively. save_parameters(filename[, deduplicate]) Save parameters to file. save_params(filename) [Deprecated] Please use save_parameters. Note that if you want load summary(*inputs) Print the summary of the model’s output and parameters.

Attributes

 name Name of this Block, without ‘_’ in the end. params Returns this Block’s parameter dictionary (does not include its children’s parameters). prefix Prefix of this Block.

Examples

>>> # To extract the feature from fc1 and fc2 layers of AlexNet:
>>> alexnet = gluon.model_zoo.vision.alexnet(pretrained=True, ctx=mx.cpu(),
prefix='model_')
>>> inputs = mx.sym.var('data')
>>> out = alexnet(inputs)
>>> internals = out.get_internals()
>>> print(internals.list_outputs())
['data', ..., 'model_dense0_relu_fwd_output', ..., 'model_dense1_relu_fwd_output', ...]
>>> outputs = [internals['model_dense0_relu_fwd_output'],
internals['model_dense1_relu_fwd_output']]
>>> # Create SymbolBlock that shares parameters with alexnet
>>> feat_model = gluon.SymbolBlock(outputs, inputs, params=alexnet.collect_params())
>>> x = mx.nd.random.normal(shape=(16, 3, 224, 224))
>>> print(feat_model(x))

apply(fn)

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

Return type

this block

cast(dtype)[source]

Cast this Block to use another data type.

Parameters

dtype (str or numpy.dtype) – The new data type.

collect_params(select=None)

Returns a ParameterDict containing this Block and all of its children’s Parameters(default), also can returns the select ParameterDict which match some given regular expressions.

For example, collect the specified parameters in [‘conv1_weight’, ‘conv1_bias’, ‘fc_weight’, ‘fc_bias’]:

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


or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:

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

Parameters

select (str) – regular expressions

Returns

Return type

The selected ParameterDict

export(path, epoch=0, remove_amp_cast=True)

Export HybridBlock to json format that can be loaded by gluon.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.

forward(x, *args)[source]

Defines the forward computation. Arguments can be either NDArray or Symbol.

hybrid_forward(F, x, *args, **kwargs)[source]

Overrides to construct symbolic graph for this Block.

Parameters
• x (Symbol or NDArray) – The first input tensor.

• *args (list of Symbol or list of NDArray) – Additional input tensors.

hybridize(active=True, backend=None, backend_opts=None, **kwargs)

Activates or deactivates HybridBlock s recursively. Has no effect on non-hybrid children.

Parameters
• active (bool, default True) – Whether to turn hybrid on or off.

• backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None

• backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty

• 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.

static imports(symbol_file, input_names, param_file=None, ctx=None)[source]

Import model previously saved by gluon.HybridBlock.export or Module.save_checkpoint as a gluon.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 gluon.SymbolBlock on.

Returns

gluon.SymbolBlock loaded from symbol and parameter files.

Return type

gluon.SymbolBlock

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)

infer_shape(*args)

Infers shape of Parameters from inputs.

infer_type(*args)

Infers data type of Parameters from inputs.

initialize(init=<mxnet.initializer.Uniform object>, ctx=None, verbose=False, force_reinit=False)

Initializes Parameter s of this Block and its children. Equivalent to block.collect_params().initialize(...)

Parameters
• init (Initializer) – Global default Initializer to be used when Parameter.init() is None. Otherwise, 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.

load_parameters(filename, ctx=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')

Load parameters from file previously saved by save_parameters.

Parameters
• 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.

• cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.

• dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters

References

load_params(filename, ctx=None, allow_missing=False, ignore_extra=False)

filenamestr

Path to parameter file.

ctxContext or list of Context, default cpu()

Context(s) to initialize loaded parameters on.

allow_missingbool, default False

ignore_extrabool, default False

Whether to silently ignore parameters from the file that are not present in this Block.

property name

Name of this Block, without ‘_’ in the end.

name_scope()

Returns a name space object managing a child Block and parameter names. Should be used within a with statement:

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


optimize_for(x, *args, backend=None, backend_opts=None, **kwargs)

Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.

Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.

Examples

# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)

# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)

Parameters
• x (NDArray) – first input to model

• *args (NDArray) – other inputs to model

• backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None

• backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty

• 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.

property params

Returns this Block’s parameter dictionary (does not include its children’s parameters).

property prefix

Prefix of this Block.

register_child(block, name=None)

Registers block as a child of self. Block s assigned to self as attributes will be registered automatically.

register_forward_hook(hook)

Registers a forward hook on the block.

The hook function is called immediately after forward(). It should not modify the input or output.

Parameters

hook (callable) – The forward hook function of form hook(block, input, output) -> None.

Returns

Return type

mxnet.gluon.utils.HookHandle

register_forward_pre_hook(hook)

Registers a forward pre-hook on the block.

The hook function is called immediately before forward(). It should not modify the input or output.

Parameters

hook (callable) – The forward hook function of form hook(block, input) -> None.

Returns

Return type

mxnet.gluon.utils.HookHandle

register_op_hook(callback, monitor_all=False)

Install op hook for block recursively.

Parameters
• callback (function) – Takes a string and a NDArrayHandle.

• monitor_all (bool, default False) – If true, monitor both input and output, otherwise monitor output only.

save_parameters(filename, deduplicate=False)

Save parameters to file.

Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use HybridBlock.export().

Parameters
• filename (str) – Path to file.

• deduplicate (bool, default False) – If True, save shared parameters only once. Otherwise, if a Block contains multiple sub-blocks that share parameters, each of the shared parameters will be separately saved for every sub-block.

References

save_params(filename)

Save parameters to file.

filenamestr

Path to file.

summary(*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 mxnet.ndarray.NDArray is supported.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.16389571130275726, "perplexity": 13087.506683355608}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-04/segments/1610703519784.35/warc/CC-MAIN-20210119201033-20210119231033-00210.warc.gz"}