How to optimize a function using rmsprop in R

This recipe helps you optimize a function using rmsprop in R

Recipe Objective -How to optimize a function using rmsprop in R?

The RMSprop optimizer is defined as an optimizer which is similar to the gradient descent algorithm with a momentum. The RMSprop optimizer restricts the oscillations in vertical direction in a neural network model. Therefore, It helps in increasing the learning rate thus an algorithm will be able to take larger steps in the horizontal direction that is converging faster. The only difference between the RMSprop and the gradient descent algorithm is that how gradients are calculated. The RMSprop divides the learning rate by the exponentially decaying average of squared gradients. It is suggested "?" to be set to 0.9 value while a default value for the learning rate "?" is 0.001. The RMSprop is the widely and most used optimizer in neural networks.

This recipe explains what is RMSprop optimizer is, what are its benefits and how it can be executed.

Learn to Build a Multi Class Image Classification Model in Python from Scratch

Implementing RMSprop optimizer.

Step 1: Installing and loading keras package to build neural network using keras.

# Installing Packages
install.packages("keras")
# Loading packages
library(keras)

Step 2: Loading MNIST handwritten digit dataset which comes pre-loaded in keras package.

# Loading the data
mnist <- dataset_mnist()

Step 3: Train and test dataset containing images are prepared using MNIST dataset.

# Preparing train data and test data
training_images <- mnist$train$x
training_labels <- mnist$train$y
testing_images <- mnist$test$x
testing_labels <- mnist$test$y

Step 4: Transform the train images data and test image data into the double array of [0, 255] shape (60000, 28 * 28) with values between 0 and 1.

# Reshaping train data and test data
training_images <- array_reshape(training_images, c(60000, 28 * 28))
training_images <- training_images / 255
testing_images <- array_reshape(testing_images, c(10000, 28 * 28))
testing_images <- testing_images / 255

Step 5: Labels are prepared by categorically encoding them.

# Preparing Labels
training_labels <- to_categorical(training_labels)
testing_labels <- to_categorical(testing_labels)

Step 6: Model is build using dense layers with relu and softmax activation.

# Model Buidling
network <- keras_model_sequential() %>%
layer_dense(units = 512, activation = "relu", input_shape = c(28 * 28)) %>%
layer_dense(units = 10, activation = "softmax")

Step 7: Model is compiled with optimizer rmsprop, loss as categorical entorpy and accuracy as metrics.

# Model Compiling
neural_network %>% compile(
optimizer = "rmsprop",
loss = "categorical_crossentropy",
metrics = c("accuracy")

Step 8: Model is fitted. neural_network %>% fit(training_images, training_labels, epochs = 3, batch_size = 64)

Step 9: Model performance is evaluated on testing dataset of images and test dataset of labels.

# Model performance
metric <- neural_network %>% evaluate(testing_images, testing_labels)

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python