Auto Byte

Science AI

# 教你在R中使用Keras和TensorFlow构建深度学习模型

NSS是一个数据分析和数据科学的爱好者，他经常在Analytics Vidhya上面分享数据科学方面的教程。本文是他所撰写的关于如何在R中使用Keras构建手写数字分类模型的入门教程。

### 引言

• 以TensorFlow为后端的Keras框架安装
• 在R中可以使用Keras来构建模型的不同类型
• 在R中使用MLP将MNIST手写数字进行归类
• 将MNIST结果与Python中同等代码结果进行比较
• 结语

### 一、以TensorFlow为后端的Keras框架安装

install.packages("devtools")

devtools::install_github("rstudio/keras")

library(keras)

install_tensorflow()

install_tensorflow(gpu=TRUE)

### 二、在R中可以使用keras来构建模型的不同类型

1. 多层感知器(Multi-Layer Perceptrons)
2. 卷积神经网络(Convoluted Neural Networks)
3. 递归神经网络(Recurrent Neural Networks)
4. Skip-Gram模型
5. 使用预训练的模型（比如VGG16、RESNET等）
6. 微调预训练的模型

### 三、在R中使用MLP将MNIST手写数字进行归类

library(keras)

data<-dataset_mnist()

#separating train and test file

train_x<-data\$train\$x

train_y<-data\$train\$y

test_x<-data\$test\$x

test_y<-data\$test\$y

rm(data)

# converting a 2D array into a 1D array for feeding into the MLP and normalising the matrix

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

#converting the target variable to once hot encoded vectors using keras inbuilt function

train_y<-to_categorical(train_y,10)

test_y<-to_categorical(test_y,10)

#defining a keras sequential model

model <- keras_model_sequential()

#defining the model with 1 input layer[784 neurons], 1 hidden layer[784 neurons] with dropout rate 0.4 and 1 output layer[10 neurons]

#i.e number of digits from 0 to 9

model %>%

layer_dense(units = 784, input_shape = 784) %>%

layer_dropout(rate=0.4)%>%

layer_activation(activation = 'relu') %>%

layer_dense(units = 10) %>%

layer_activation(activation = 'softmax')

#compiling the defined model with metric = accuracy and optimiser as adam.

model %>% compile(

loss = 'categorical_crossentropy',

metrics = c('accuracy')

)

#fitting the model on the training dataset

model %>% fit(train_x, train_y, epochs = 100, batch_size = 128)

#Evaluating model on the cross validation dataset

loss_and_metrics <- model %>% evaluate(test_x, test_y, batch_size = 128)

### 四、使用keras来构建MLP模型——R Vs. Python

#importing the required libraries for the MLP model

import keras

from keras.models import Sequential

import numpy as np

from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

#reshaping the x_train, y_train, x_test and y_test to conform to MLP input and output dimensions

x_train=np.reshape(x_train,(x_train.shape[0],-1))/255

x_test=np.reshape(x_test,(x_test.shape[0],-1))/255

import pandas as pd

y_train=pd.get_dummies(y_train)

y_test=pd.get_dummies(y_test)

#performing one-hot encoding on target variables for train and test

y_train=np.array(y_train)

y_test=np.array(y_test)

#defining model with one input layer[784 neurons], 1 hidden layer[784 neurons] with dropout rate 0.4 and 1 output layer [10 #neurons]

model=Sequential()

from keras.layers import Dense

keras.layers.core.Dropout(rate=0.4)

# compiling model using adam optimiser and accuracy as metric

# fitting model and performing validation

model.fit(x_train,y_train,epochs=50,batch_size=128,validation_data=(x_test,y_test))

### 五、结语

https://www.analyticsvidhya.com/blog/2017/06/getting-started-with-deep-learning-using-keras-in-r/