1
$\begingroup$

I am experimenting with platform interoperability between Mathematica and R.

My aim is to create an untrained Neural Network using Mathematica, export this network in MXNet format as a .json file, and import this network into R for a classification problem.

Creating the Network in Mathematica

Here i have created a basic neural network - this network is untrained. I have exported the network alongside the network parameters.

In mathematica the code is as follows.

dec=NetDecoder["Class",{"Chronic Kidney Disease","No Kidney Disease"}]

net = 
 NetInitialize@
  NetChain[{BatchNormalizationLayer[], LinearLayer[20], Ramp, 
    DropoutLayer[0.1], LinearLayer[2], SoftmaxLayer[]},
   "Input" -> 24, "Output" -> dec
   ]

There are 24 feature variables for the input and the output is the netdecoder. I then export this network.

Export["net.json", net, "MXNet"]

This produces two files, one with the network, and another with the parameters. By using FilePrint we can visualise this

FilePrint["net.json"]

which returns

{
    "nodes":[
        {"op":"null","name":"Input","inputs":[]},
        {"op":"null","name":"1.Scaling","inputs":[]},
        {"op":"null","name":"1.Biases","inputs":[]},
        {"op":"null","name":"1.MovingMean","inputs":[]},
        {"op":"null","name":"1.MovingVariance","inputs":[]},
        {"op":"BatchNorm","name":"1","attrs":{"eps":"0.001","momentum":"0.9","fix_gamma":"false","use_global_stats":"false","axis":"1","cudnn_off":"0"},"inputs":[[0,0,0],[1,0,0],[2,0,0],[3,0,0],[4,0,0]]},
        {"op":"null","name":"2.Weights","inputs":[]},
        {"op":"null","name":"2.Biases","inputs":[]},
        {"op":"FullyConnected","name":"2","attrs":{"num_hidden":"20","no_bias":"False"},"inputs":[[5,0,0],[6,0,0],[7,0,0]]},
        {"op":"relu","name":"3$0","inputs":[[8,0,0]]},
        {"op":"Dropout","name":"4$0","attrs":{"p":"0.1","mode":"always","axes":"()"},"inputs":[[9,0,0]]},
        {"op":"null","name":"5.Weights","inputs":[]},
        {"op":"null","name":"5.Biases","inputs":[]},
        {"op":"FullyConnected","name":"5","attrs":{"num_hidden":"2","no_bias":"False"},"inputs":[[10,0,0],[11,0,0],[12,0,0]]},
        {"op":"softmax","name":"6$0","attrs":{"axis":"1"},"inputs":[[13,0,0]]},
        {"op":"identity","name":"Output","inputs":[[14,0,0]]}
    ],
    "arg_nodes":[0,1,2,3,4,6,7,11,12],
    "heads":[[15,0,0]],
    "attrs":{
        "mxnet_version":["int",10400]
    }
}

Importing the Network into R

Now we have an untrained network as a .json file in MXNet format.

We can import this using:

library(rjson)
mydata <- fromJSON(file="net.json")

The Problem

Im not sure how to process the exported net in R. Is it possible to use the imported untrained network from Mathematica, to then be used in R to train on some data?

$\endgroup$
3
  • 1
    $\begingroup$ Isn't your question about R more than about Mathematica? From the description of your aims, it seems that you have already completed the MMA parts successfully. $\endgroup$
    – MarcoB
    Jun 10, 2020 at 16:42
  • $\begingroup$ Yes it is, I was hoping someone had encountered this cross-platform problem before when using MMA. $\endgroup$
    – isaac5122
    Jun 10, 2020 at 17:08
  • $\begingroup$ You may have more luck e.g. on StackOverflow with R-specific questions. See for instance stackoverflow.com/search?q=%5Br%5D+neural+network+train $\endgroup$
    – MarcoB
    Jun 10, 2020 at 18:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.