OpenersPrimaryExamplesSection(Spacer1)Examples  (16)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Define an initialized (RefLink[NetChain, paclet:ref/NetChain]):

net=NetInitialize@NetChain[{2,3,4},"Input"->1]

Export the net to the MXNet format:

path=Export[FileNameJoin[{$TemporaryDirectory,"netChain.json"}],net,"MXNet"]

Import the net from the MXNet format:

Import[path,"MXNet"]

OpenersExampleSection1(Spacer1)Import Elements  (11)

OpenersExampleSubsection0(Spacer1)InputNames  (1)

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

Import["ExampleData/mxnet_example2.json",{"MXNet","InputNames"}]

OpenersExampleSubsection1(Spacer1)LayerAssociation  (1)

Obtain an association of layers for a given MXNet net:

Import["ExampleData/mxnet_example2.json",{"MXNet","LayerAssociation"}]

OpenersExampleSubsection2(Spacer1)Net  (2)

Import an MXNet net as a (RefLink[NetGraph, paclet:ref/NetGraph]):

Import["ExampleData/mxnet_example2.json",{"MXNet","Net"}]

This is equivalent to the following:

Import["ExampleData/mxnet_example2.json","MXNet"]

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

FindFile["ExampleData/mxnet_example1.params"]

Import["ExampleData/mxnet_example1.json",{"MXNet","Net"}]

The "ArrayPath" option can be used to avoid importing the parameter file:

Import["ExampleData/mxnet_example1.json",{"MXNet","Net"},"ArrayPath"->None]

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

Import["ExampleData/mxnet_example1.json",{"MXNet","UninitializedNet"}]

OpenersExampleSubsection3(Spacer1)NodeDataset  (1)

View the nodes of an MXNet symbol:

Import["ExampleData/mxnet_example2.json",{"MXNet","NodeDataset"}]

OpenersExampleSubsection4(Spacer1)NodeGraph  (1)

Obtain a graph of the nodes in an MXNet symbol:

graph=Import["ExampleData/mxnet_example2.json",{"MXNet","NodeGraph"}]

Get a list of vertices in the graph:

VertexList[graph]

OpenersExampleSubsection5(Spacer1)NodeGraphPlot  (1)

Obtain a plot of the nodes in an MXNet symbol:

graph=Import["ExampleData/mxnet_example2.json",{"MXNet","NodeGraphPlot"}]

OpenersExampleSubsection6(Spacer1)UninitializedNet  (1)

"UninitializedNet" avoids importing any net parameters:

Import["ExampleData/mxnet_example1.json",{"MXNet","UninitializedNet"},"ArrayPath"->FindFile["ExampleData/mxnet_example1.params"]]

OpenersExampleSubsection7(Spacer1)ArrayAssociation  (1)

Import the arrays in an MXNet net as an association:

Import["ExampleData/mxnet_example1.json",{"MXNet","ArrayAssociation"}]

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

Import["ExampleData/mxnet_example1.json",{"MXNet","ArrayAssociation"},"ArrayPath"->FindFile["ExampleData/mxnet_example1.params"]]

OpenersExampleSubsection8(Spacer1)ArrayList  (1)

Import the arrays in an MXNet net as a list:

Import["ExampleData/mxnet_example1.json",{"MXNet","ArrayList"}]

OpenersExampleSubsection9(Spacer1)ArrayNames  (1)

Obtain the names of the arrays in an MXNet net:

Import["ExampleData/mxnet_example1.json",{"MXNet","ArrayNames"}]

This is equivalent to:

Keys@Import["ExampleData/mxnet_example1.json",{"MXNet","ArrayAssociation"}]

OpenersExampleSection2(Spacer1)Import Options  (1)

OpenersExampleSubsection10(Spacer1)"ArrayPath"  (1)

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

file=FindFile["ExampleData/mxnet_example1.params"]

Import["ExampleData/mxnet_example1.json","MXNet","ArrayPath"->file]

When it is set to (RefLink[None, paclet:ref/None]), no parameters are imported:

Import["ExampleData/mxnet_example1.json","MXNet","ArrayPath"->None]

This is equivalent to the following:

Import["ExampleData/mxnet_example1.json",{"MXNet","UninitializedNet"}]

OpenersExampleSection3(Spacer1)Possible Issues  (3)

Any (RefLink[NetEncoder, paclet:ref/NetEncoder]) or (RefLink[NetDecoder, paclet:ref/NetDecoder]) attached to a net is not exported:

net=ConvolutionLayer[2,3,"Input"->NetEncoder["Image"]]

path=Export[FileNameJoin[{$TemporaryDirectory,"conv.json"}],net,"MXNet"]

The imported net no longer has a (RefLink[NetEncoder, paclet:ref/NetEncoder]):

net2=Import[path,"MXNet"]

MXNet model definitions do not preserve information about input and output dimensions. Define a (RefLink[NetChain, paclet:ref/NetChain]) with the dimensions of "Input" specified:

net=NetInitialize@NetChain[{ConvolutionLayer[3,2],ElementwiseLayer[Ramp]},"Input"->{4,5,6}]

Export the net to the MXNet format:

path=Export[FileNameJoin[{$TemporaryDirectory,"net.json"}],net,"MXNet"]

The dimensions of the input are no longer known:

net2=Import[path,"MXNet"]

The dimensions can be added manually using (RefLink[NetReplacePart, paclet:ref/NetReplacePart]):

NetReplacePart[net2,"Input"->{4,5,6}]

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

net=BasicRecurrentLayer[3,"Input"->{"Varying",2}]

path=Export[FileNameJoin[{$TemporaryDirectory,"net.json"}],net,"MXNet"]

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

net=BasicRecurrentLayer[3,"Input"->{3,2}]

path=Export[FileNameJoin[{$TemporaryDirectory,"net.json"}],net,"MXNet"]

Recurrent layers can often not be re-imported:

Import[path,"MXNet"]

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

Import[path,{"MXNet","NodeGraphPlot"}]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[NetGraph, paclet:ref/NetGraph])  ▪  (RefLink[NetChain, paclet:ref/NetChain])  ▪  (RefLink[NetTrain, paclet:ref/NetTrain])}, {Related Formats:  (RefLink[WMLF, paclet:ref/format/WMLF])  ▪  (RefLink[WLNet, paclet:ref/format/WLNet])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Neural Networks in the Wolfram Language, paclet:tutorial/NeuralNetworksOverview])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Neural Networks, paclet:guide/NeuralNetworks])}, {▪(RefLinkPlain[Importing & Exporting, paclet:guide/ImportingAndExporting])}})}})