# Importing A Neural Network From Mathematica For Use In R

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],
"attrs":{
"mxnet_version":["int",10400]
}
}


Likewise, using FilePrint the parameters of the network can be seen.

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

Is it possible to use the imported untrained network from Mathematica, to then be used in R to train on some data? What should i use to train the data, Keras, MXNet etc?

Here is my code in R

suppressMessages(library(keras))
suppressMessages(library(DT))

###Importing The Data
col_names = TRUE)
datatable(data.set[sample(nrow(data.set),
replace = FALSE,
size = 0.01 * nrow(data.set)), ])

### Transformation into a matrix
# Cast dataframe as a matrix
data.set <- as.matrix(data.set)
# Remove column names
dimnames(data.set) = NULL

###Training and test data

# Split for train and test data
set.seed(123)
indx <- sample(2,
nrow(data.set),
replace = TRUE,
prob = c(0.8, 0.2)) # Makes index with values 1 and 2

# Select only the feature variables
# Take rows with index = 1
x_train <- data.set[indx == 1, 1:24]
x_test <- data.set[indx == 2, 1:24]

###Rescaling the data

mean.train <- apply(x_train,
2,
mean)
sd.train <- apply(x_train,
2,
sd)
x_test <- scale(x_test,
center = mean.train,
scale = sd.train)
x_train <- scale(x_train)

### Processing the target variable
y_test_actual <- data.set[indx == 2, 25]

# Using similar indices to correspond to the training and test set
y_train <- to_categorical(data.set[indx == 1, 25])
y_test <- to_categorical(data.set[indx == 2, 25])

cbind(y_test_actual[1:24],
y_test[1:24, ])


At this point the data has been split between training and test data. This is where im unsure how to proceed.

How do i use the imported network in MXNet format to train on the data?