Open In App

How to Save a Model When Using MXNet in R

Last Updated : 08 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

MXNet is a versatile and efficient deep learning framework that supports multiple programming languages, including R. When training machine learning or deep learning models, saving your model for later use is an essential step.

This article will guide you through the process of saving a model in MXNet using R.

Step 1: Load and Prepare the Iris Dataset

The Iris dataset is a widely used dataset for classification tasks. We'll preprocess it to create training and testing datasets.

R
# Load the iris dataset
data(iris)

# Convert Species to a numeric label (0, 1, 2)
iris$Species <- as.numeric(as.factor(iris$Species)) - 1

# Split data into features and labels
data <- as.matrix(iris[, 1:4])
labels <- as.numeric(iris$Species)

# Normalize data
data <- scale(data)

# Split into training and testing sets
set.seed(123)
train_idx <- sample(1:nrow(data), 0.7 * nrow(data))
train_data <- data[train_idx, ]
train_labels <- labels[train_idx]
test_data <- data[-train_idx, ]
test_labels <- labels[-train_idx]

Output:

Start training with 1 devices

[1] Train-accuracy=0.25
[2] Train-accuracy=0.357142857142857
[3] Train-accuracy=0.357142857142857
[4] Train-accuracy=0.357142857142857
[5] Train-accuracy=0.357142857142857
[6] Train-accuracy=0.357142857142857
[7] Train-accuracy=0.357142857142857
[8] Train-accuracy=0.375
.
.
.
[44] Train-accuracy=0.9375
[45] Train-accuracy=0.9375
[46] Train-accuracy=0.9375
[47] Train-accuracy=0.946428571428571
[48] Train-accuracy=0.946428571428571
[49] Train-accuracy=0.955357142857143
[50] Train-accuracy=0.955357142857143

Step 2: Define and Train the Neural Network

Define a simple neural network with one hidden layer and train it using MXNet.

R
library(mxnet)

# Define the network structure
data <- mx.symbol.Variable("data")
hidden_layer <- mx.symbol.FullyConnected(data = data, num_hidden = 10)
activation <- mx.symbol.Activation(data = hidden_layer, act_type = "relu")
output_layer <- mx.symbol.FullyConnected(data = activation, num_hidden = 3)
softmax <- mx.symbol.SoftmaxOutput(data = output_layer, name = "softmax")

# Train the model
model <- mx.model.FeedForward.create(
  symbol = softmax,
  X = train_data,
  y = train_labels,
  ctx = mx.cpu(),
  num.round = 50,
  array.batch.size = 16,
  learning.rate = 0.01,
  momentum = 0.9,
  eval.metric = mx.metric.accuracy
)

Step 3: Save the Trained Model

Once the model is trained, save it to files for future use.

R
# Save the model
model_prefix <- "iris_model"
mx.model.save(model, prefix = model_prefix, iteration = 50)

Step 4: Load the Saved Model

To use the model later for inference or further training, you can load it back into your environment.

Python
# Load the model
loaded_model <- mx.model.load(prefix = model_prefix, iteration = 50)

# Make predictions on the test dataset
predictions <- predict(loaded_model, test_data)
predicted_labels <- max.col(t(predictions)) - 1

# Calculate accuracy
accuracy <- mean(predicted_labels == test_labels)
print(paste("Test Accuracy:", accuracy))

Output:

[1] "Test Accuracy: 0.977777777777778"

In this guide, we used the Iris dataset to train a neural network with MXNet in R. We covered how to save the model's structure and parameters, reload it, and use it for predictions. This process ensures your trained models can be reused without retraining, saving time and resources in practical applications.

By following these steps, you can efficiently manage your models in R using MXNet.


Next Article

Similar Reads