8
$\begingroup$

This is a simple DNN from Fast, portable neural networks with Gluon HybridBlocks:

This is json file and params file of Gluon(MXNet)

import mxnet as mx
from mxnet.gluon import nn
print("mxnet version: "+mx.__version__)

mx.random.seed(1234)  #Getting the same result everytime
def get_net():
    # construct a MLP
    net = nn.HybridSequential()
    with net.name_scope():
        net.add(nn.Dense(256, activation="relu"))
        net.add(nn.Dense(128, activation="relu"))
        net.add(nn.Dense(2))
    # initialize the parameters
    net.collect_params().initialize()
    return net

# forward
x = mx.nd.array([[0.1,0.2,0.3]])
net = get_net()
net.hybridize()
print('=== net(x) ==={}'.format(net(x)))

from mxnet import sym
x = sym.var('data')
net(x).save('model.json')
net.collect_params().save('model.params')

##############   Re-importing the net  ##############
sym = mx.symbol.load('model.json') 
nd = mx.nd.load('model.params')
inputND = mx.nd.array([[0.1,0.2,0.3]])
nd["data"] = inputND
e = sym.bind(mx.cpu(), nd)
out = e.forward()
print(out[0])

mxnet version: 0.11.1

=== net(x) ===

[[ 0.00051653 -0.00040585]]

[[ 0.00051653 -0.00040585]]

In Mathematica11.2,this code will crash the kernel,I think because mma use old version of MXNet.At that time,there isn't gluon actually, a new interface of MXNet.

Import["model.json","MXNet"]

And this code also can't work

<< NeuralNetworks`;
net = ImportMXNetModel["model.json", "model.params"];
$\endgroup$
  • $\begingroup$ Links do not work. $\endgroup$ – b.gates.you.know.what Oct 10 '17 at 6:47
  • $\begingroup$ @b.gatessucks fixed. $\endgroup$ – partida Oct 10 '17 at 6:59
4
$\begingroup$

In Mathematica 11.2, this code will crash the kernel, I think because mma use old version of MXNet

This hypothesis is correct. I have verified that this works for internal 11.3 builds which use the latest version of MXNet, so it will automatically be fixed when 11.3 is released. It is a bug though that it crashes the kernel. Have reported this.

$\endgroup$
  • $\begingroup$ Yes,MMA use version 0.9.5.But MXNet has released the 0.12.0 $\endgroup$ – partida Oct 30 '17 at 1:52
  • $\begingroup$ MXNet support the new functionexport to export the net that make it easy using MXNet in C++ $\endgroup$ – partida Oct 30 '17 at 1:54
4
$\begingroup$

Bug fixed in 11.3.0

net = Import[
             "C:\\Users\\xzhou\\Desktop\\mxnet_prediction\\SE\\model.json", 
             "MXNet"]
net[{0.1, 0.2, 0.3}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.