Cross post here
Problem remains in Mathematica 11.2
First defining the net
SeedRandom[1234];
net = NetInitialize@NetChain[{5, 3}, "Input" -> 2];
{net[{1, 2}], net[{0, 0}], net[{0.1, 0.3}]}
{{1.27735, -1.21455, -1.02647}, {0., 0., 0.}, {0.141054, -0.145882, -0.099945}}
Exporting it to net of MXNet.
Export["simple model-symbol.json", net, "MXNet"]
The JSON file(It defines the net structure) is
From the code, we can see the name of the parameter is the same between params
and JSON
file.
import mxnet as mx
mx.nd.load('simple model-0000.params')
It gives an error ValueError: need more than 1 value to unpack
when importing the net.
import mxnet as mx
sym, arg_params, aux_params = mx.model.load_checkpoint('simple model', 0)
Complete code to make prediction.(data_names
and label_names
can be shown in JSON file.)
import mxnet as mx
import numpy as np
sym, arg_params, aux_params = mx.model.load_checkpoint('simple model', 0)
mod = mx.mod.Module(symbol=sym,data_names=['Input'],label_names=['2'])
mod.bind(for_training=False, data_shapes=[('Input', (1,2))])
mod.set_params(arg_params, aux_params)
input_data=np.array([[1,2]])
array = mx.nd.array(input_data)
from collections import namedtuple
Batch = namedtuple('Batch', ['Input'])
mod.forward(Batch([array]))
prob = mod.get_outputs()[0].asnumpy()
prob = np.squeeze(prob)
print prob
Version:
Mathematica "11.2.0 for Microsoft Windows (64-bit) (September 11, 2017)"
MXNet 0.11.1 (the latest version 20170905_mxnet_x64_vc14_cpu.7z)
How to fix it?
PS: Thank you @Sebastian, but still have a problem when using RNN.
SeedRandom[1234];
net = NetInitialize@NetChain[{10, Ramp, ReshapeLayer[{5, 2}],
LongShortTermMemoryLayer[5], 3}, "Input" -> 2]
net[{1, 2}]
Export["example.json", net, "MXNet"];
In MXNet, when executing this code e = sym.bind(mx.cpu(), nd)
, it gives the following errors.
raise ValueError('key
%s
is missing in%s
' % (name, arg_key))ValueError: key
4.State
is missing inargs
mod.bind(for_training=False, data_shapes=[('data', (1,2))])
withmod.bind(for_training=False, data_shapes=[('Input', (1,2))])
. Also, for future reference, in 11.2 the export step will be pretty simple: justExport["net.json", net, "MXNet"]
. $\endgroup$