MXNet (.json, .params)

Background & Context

    • Underlying format of the MXNet deep learning framework, used by the Wolfram Language.
    • Translation is performed automatically between the low-level layers used by MXNet and corresponding high-level layers used by the Wolfram Language.
    • The mapping between Wolfram Language layers and MXNet layers is not always invertible.
    • MXNet nets are saved as separate .json files, specifying the network topology, and .params files, specifying the numeric arrays used in the network. The Wolfram Language will automatically look for an appropriate .params file when loading a .json file.
    • By default, Import and Export use the "JSON" format for files ending in .json, rather than the "MXNet" format.

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.json",{"MXNet",elem}] imports the net with the specified element elem.
  • Import["file.params","MXNet"] imports the MXNet parameter file saved as a .params file as an association of weights.
  • Import["file.params",{"MXNet",elem}] imports the parameter file with the specified element elem.
  • Export["file.json",net,"MXNet"] exports a trained neural network as a .json file and a corresponding .params file.

Import Elements

  • Net Import elements:
  • "InputNames"names of the net inputs
    "LayerAssociation"list of rules for options, properties and settings
    "Net"import the net as a NetGraph or NetChain
    "NodeDataset"dataset of MXNet symbol nodes
    "NodeGraph"graph of nodes of MXNet symbol
    "NodeGraphPlot"plot of nodes of MXNet symbol
    "UninitializedNet"import the net without any parameters
  • Parameter Import elements:
  • "ArrayAssociation"import the parameter arrays as an association
    "ArrayList"import the parameter arrays as a list
    "ArrayNames"the names of the parameter arrays

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 an initialized NetChain:

Export the net to the MXNet format:

Import the net from the MXNet format:

Import Elements  (11)

InputNames  (1)

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

LayerAssociation  (1)

Obtain an association of layers for a given MXNet net:

Net  (2)

Import an MXNet net as a NetGraph:

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:

This is equivalent to using the "UninitializedNet" content element:

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:

UninitializedNet  (1)

"UninitializedNet" avoids importing any net parameters:

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:

This is equivalent to the following:

Possible Issues  (3)

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

The imported net no longer has a NetEncoder:

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 input are no longer known:

The dimensions can be added manually using NetReplacePart:

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

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

Recurrent layers can often not be re-imported:

The reason is that they are not exported to a single MXNet operator, but rather as a computational graph unrolled over the sequence length:

Introduced in 2017
 (11.2)