SoFunction
Updated on 2025-04-10

R language based on Keras's MLP neural network and environment construction

Intro

R is the first computer language I use and one of the current mainstream data analysis languages. It is often compared with python. In EDA, mapping and machine learning, R language has many packages to choose from. However, in deep learning, due to the lack of learning libraries and appropriate frameworks, it was surpassed by python. But Keras's release on R has led to the two languages ​​tied again.

This article uses Tensorflow and the Keras library to recognize handwritten numbers in R language. The code is partly derived from "Getting started with Deep Learning using Keras and TensorFlow in R", author: NSS: /blog/2017/06/getting-started-with-deep-learning-using-keras-in-r/?spm=5176.100239.blogcont109827.13.QVuLG8. For novices, these codes may not be easy to understand. The omission of the computer configuration part in the previous computer has allowed me to go through a lot of pitfalls. In this article, I will explain the background installation and code part in detail.

Environment construction

This computer configuration

Computer model: MacBook Air
System: Windows 8.1 Professional Edition 64-bit operating system
Processor: Intel® Core™ i5-5250U CPU @ 1.60GHz
Installation Memory (RAM): 8.0 GB
Graphics card: HD 4000 core graphics card

Install TensorFlow and Keras

First, please install Python 3.5 or 3.6. Download address: /getit/. The latest one is 3.7.1, but the tensorflow win8 version currently only supports python 3.5 and 3.6. IOS or LINUX may support versions. We use this Python to install tensorflow.

My installation version is Python 3.6.7 64bit. It is necessary to install C Runtime Update (KB2999226), that is, it needs to be updated or installed. If your computer does not have this patch, go to the official website to find a version suitable for your computer and download and update it. Note that you need to install KB2919355 first before you can install it later.

When installing, choose to install all the pips and so on. Then enter python into cmd to view the version. (As a computer novice, I didn't know what cmd was at first... Actually it is the 'terminal' in the ios system, and it is called the command prompt in Chinese in win system)

C:\Users\user>python

After that, you need to download Visual C++2015 redistributable on the official website to perform pip install tensorflow. The download address is: /en-us/download/?id=53587.

After the installation is complete, enter it in cmd

C:\Users\user>pip install tensorflow

If the display cannot access xxxxx, add -user

C:\Users\user>pip install --user tensorflow

Similarly, continue to install keras

C:\Users\user>pip install --user keras

Now some installation is ready, you can enter the following code in cmd to see if it is installed. If no error is displayed, the installation is successful~~

C:\Users\user>python
import tensorflow
import keras

Next, enter the R language part!

Install R and Rstudio

If you have used R before, please ignore this paragraph.
Installing R is very simple, download it directly on the official website: /CRAN/

Then download Rstudio, which is equivalent to the R language open version. The interface is very friendly compared to R and has many auxiliary functions. Download address: /products/rstudio/download/

#Note that Rstudio is based on R language, and you need to download and install R language before you can install it.

Deep Learning MLP based on R language

Install Tensorflow and Keras in Rstudio

This part of the code comes from: /blog/2017/06/getting-started-with-deep-learning-using-keras-in-r/?spm=5176.100239.blogcont109827.13.QVuLG8. I've added some comments for reference.

First install Keras and tensorflow in RStudio

("devtools") #InstallRDevelopment Toolkit
devtools::install_github("rstudio/keras") #fromgithubdownloadkerasarriveR
("keras")#也可以直接downloadCRANofKerasBag
#以上两种Install方法选其一就可以
library(keras) #loadkerasBag
install_tensorflow()#Create aR语言oftensorflowenvironment,Default isCPUVersion
install_tensorflow(gpu=TRUE) #如果想要自定义Install,For example, useGPU,Use this line of code

Preprocessing of MNIST datasets

After configuring the environment, we start building the neural network and use the dataset_mnist() dataset. The MNIST dataset is composed of 60,000 grayscale images of ten handwritten numbers from 0 to 9, and provides a test set of 10,000 images.

First we download the dataset and create variables for the test and training data. Where x is a 3D array of grayscale values ​​(image, width, height), and y is an integer classification vector with numbers 0 to 9.

#Put the training set,Test sets separate and create variables
#This step is very common in deep learning,Can effectively prevent data fraud,And make your data and steps look clearer
train_x<-data$train$x 
train_y<-data$train$y
test_x<-data$test$x
test_y<-data$test$y
rm(data) #This step is to remove the original data。。。No use

Then we convert the width and height in the 3D data of x data (grayscale value) into one-dimensional through array() (the pixel value of 28x28 becomes a vector of length 784) to convert it into matrix form. At the same time, the grayscale value of the integer between 0 and 255 is converted into a numerical value between 0 and 1.

train_x <- array(train_x, dim = c(dim(train_x)[1], prod(dim(train_x)[-1]))) / 255
test_x <- array(test_x, dim = c(dim(test_x)[1], prod(dim(test_x)[-1]))) / 255

Then let’s look at the y data. We use the to_categorical() function in the Keras package to convert the previous classification vector into binary class matrix (binary class matrix)

train_y<-to_categorical(train_y,10)
test_y<-to_categorical(test_y,10)

Deep Learning MLP Model

Now that the data has been processed, we can start modeling. First, create a sequential model of keras, which is a linear stack of multiple network layers. We can construct the model by passing a layer list to the Sequential model.

model &lt;- keras_model_sequential() #Define the model

Add and define the network layer

#The original author's code creates an input layer(784A neuron),A fully connected layer(784A neuron)and an output layer(10A neuron)
model %&gt;%
  #Full connection layer,unitsRepresents the output latitude,input_shapeRepresents the input tensorshape。
  layer_dense(units = 784, input_shape = 784) %&gt;%
  
  #Random stop40%Feature Test,Used to improve model generalization capabilities。
  layer_dropout(rate=0.4)%&gt;% 
  
  #Select the hidden layer activation functionRELU
  layer_activation(activation = 'relu') %&gt;%  
  
  #Output layer(Total10Numbers,So the output latitude is10)
  layer_dense(units = 10) %&gt;% 
  
  #Select the hidden layer activation functionRELU
  layer_activation(activation = 'softmax') 
  
summary(model) # usesummary()View model details

Select loss functions, optimizers, and metrics to compile the model

model %&gt;% compile(
loss = 'categorical_crossentropy', #Loss function
optimizer = 'adam', #Optimizer
metrics = c('accuracy') #index
)

Training and evaluating models

#usefit()Functions to train models,epochsfor100,batch_sizefor128
model %&gt;% fit(train_x, train_y, epochs = 100, batch_size = 128) 
#Evaluate model performance by testing data
loss_and_metrics &lt;- model %&gt;% evaluate(test_x, test_y, batch_size = 128)

When training the model, the above code directly draws the values ​​of loss and acc in each step of epoch. You can also define the model, such as mymodel<-model, and then use plot (mymodel) to view the drawing process.

After that, you can view the results of the predicted training set through the following code.

model %>% predict_classes(x_test)

My computer runs at 12s/epoch, loss=0.1076, acc=0.9857 on the test set. It can be said to be a very good result.

Summary and study notes

I did not use python to compare the results, but the NSS article made a comparison. The data shows that R is not much different from Python in all aspects. Although this is just a simple multi-layer perceptron, the application of deep learning in R language will be developed from this, and both parties will once again compete with the same level of online. The merger of Keras with other packages in R language may bring some unprecedented experiences to deep learning.

There are many shortcomings in this article, and I will continue to learn and update.

Welcome to discuss together~

Email zhaotian151@
Please note the source for reprinting
by zt

The following are some reference materials:
Quick Start Sequential Model: /en/latest/getting_started/sequential_model/

The functions and usages of various Keras layers: /p/86d667ee3c62

Getting started with Deep Learning using Keras and TensorFlow in R:/blog/2017/06/getting-started-with-deep-learning-using-keras-in-r/?spm=5176.100239.blogcont109827.13.QVuLG8

How to choose the optimizer optimizer /p/d99b83f4c1a6/

This is the end of this article about the MLP neural network based on R language, which is about Keras. For more related content on R language, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!