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 = 
  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


which returns


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:

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


###Importing The Data
data.set <- read_csv("KidneyTestData2.csv",
                 col_names = TRUE)
                          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
indx <- sample(2,
               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,
sd.train <- apply(x_train,
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])

      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?

Thank you for reading.


Your Answer

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

Browse other questions tagged or ask your own question.