MXNet (.json, .params)

Background & Context

    • Underlying format of the MXNet deep learning framework, used by the Wolfram Language.
    • Networks saved as MXNet are stored as two separate file: a .json file specifying the network topology and a .params file specifying the numeric arrays used in the network.

Import & Export

  • Import["file.json","MXNet"] imports the MXNet network saved as a .json file, loading weights from a corresponding .params file if available.
  • The parameter file corresponding to "file.json" is assumed to match the pattern "file*.params" and be located in the same folder.
  • Import["file.params","MXNet"] can also be used to directly import network parameters.
  • Import["file.json",{"MXNet",elem}] and Import["file.params",{"MXNet",elem}] import the specified element elem from the corresponding file.
  • By default, Import["file.json"] is imported as a "JSON" format rather than "MXNet".
  • See the following reference pages for full general information:
  • Import, Exportimport from or export to a file
    CloudImport, CloudExportimport from or export to a cloud object
    ImportString, ExportStringimport from or export to a string
    ImportByteArray, ExportByteArrayimport from or export to a byte array

Import Elements

  • "Elements" list of elements and options available in this file
    "Summary"summary of the file
    "Rules"list of rules for all available elements
  • Net Import elements, available when importing the .json file:
  • "InputNames"names of the net inputs
    "NetExternalObject"import the net as a NetExternalObject
    "NodeDataset"dataset of MXNet symbol nodes
    "NodeGraph"graph of nodes of MXNet symbol
    "NodeGraphPlot"plot of nodes of MXNet symbol
  • Import["file.json","MXNet"] uses the "NetExternalObject" element by default.
  • Parameter Import elements, available when importing the .params file:
  • "ArrayAssociation"import the parameter arrays as an association
    "ArrayList"import the parameter arrays as a list
    "ArrayNames"the names of the parameter arrays
  • Import["file.params","MXNet"] uses the "ArrayAssociation" element by default.

Options

  • Import option:
  • "ArrayPath"Automaticpath to the MXNet parameter file
  • Export options:
  • "ArrayPath"Automaticpath to save the parameter arrays to
    "SaveArrays"Truewhether to export the net parameter arrays

Examples

open allclose all

Basic Examples  (1)

Define a NetChain:

Export the net to the MXNet format:

Import the net from the MXNet format:

Import Elements  (9)

InputNames  (1)

Obtain a list of net inputs for a given MXNet net:

NetExternalObject  (2)

Import an MXNet net as a NetExternalObject:

This is equivalent to the following:

If an MXNet net has a parameter file with the same name, it is automatically imported as well:

The "ArrayPath" option can be used to avoid importing the parameter file, producing the equivalent of an uninitialized net:

Note that the resulting NetExternalObject has additional inputs where values for the missing arrays have to be provided.

NodeDataset  (1)

View the nodes of an MXNet symbol:

NodeGraph  (1)

Obtain a graph of the nodes in an MXNet symbol:

Get a list of vertices in the graph:

NodeGraphPlot  (1)

Obtain a plot of the nodes in an MXNet symbol:

ArrayAssociation  (1)

Import the arrays in an MXNet net as an association:

This is equivalent to explicitly specifying the path to the parameter file:

ArrayList  (1)

Import the arrays in an MXNet net as a list:

ArrayNames  (1)

Obtain the names of the arrays in an MXNet net:

This is equivalent to:

Import Options  (1)

"ArrayPath"  (1)

"ArrayPath" allows for an explicit path to a parameter file to be specified:

When it is set to None, no parameters are imported, producing the equivalent of an uninitialized net:

Note that the resulting NetExternalObject has additional inputs where values for the missing arrays have to be provided.

Possible Issues  (4)

MXNet model definitions do not preserve information about input and output dimensions. Define a NetChain with the dimensions of "Input" specified:

Export the net to the MXNet format:

The dimensions of the inputs have to be provided during import; otherwise, the procedure fails:

Providing inconsistent dimensions will trigger a shape inference error from MXNet:

A minimal set of consistent dimensions must be provided. In this case, providing the shape for a single input is enough to infer the rest of the net:

Nets containing layers that translate to MXNet operators acting on batches of elements need an additional input dimension when roundtripped. Create a small convolutional net:

Export the net to the MXNet format:

Providing the same input shape to the original net triggers an MXNet error:

An additional input dimension, the batch dimension, must be explicitly provided. It can usually have any value:

Any NetEncoder or NetDecoder attached to a net is not exported:

The imported NetExternalObject no longer has a NetEncoder:

Recurrent layers that operate on variable-length sequences cannot be exported:

Fixing the sequence length allows for the net to be exported: