#loading keras library
library(keras)
#loading the keras inbuilt mnist dataset
data
#separating train and test file
train_x
train_y
test_x
test_y
rm(data)
# converting a 2D array into a 1D array for feeding into the MLP and normalising the matrix
train_x
test_x
#converting the target variable to once hot encoded vectors using keras inbuilt function
train_y
test_y
#defining a keras sequential model
model
#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',
optimizer = 'adam',
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 % evaluate(test_x, test_y, batch_size = 128)
以上的代码获得了99.14%的训练精度和96.89%的验证精度。在我的i5处理器上跑这段代码完整训练一次用时13.5秒,而在TITANx GPU上,验证精度可以达到98.44%,训练一次平均用时2秒。
四、使用keras来构建MLP模型——R Vs. Python
为了更好地比较,我同样使用Python来实现解决以上的MINIST归类问题。结果不应当有任何差别,因为R会创建一个进程(conda instance)并在其中运行keras。但你仍然可以尝试以下同等的Python代码。
#importing the required libraries for the MLP model
import keras
from keras.models import Sequential
import numpy as np
#loading the MNIST dataset from keras
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
model.add(Dense(784, input_dim=784, activation='relu'))
keras.layers.core.Dropout(rate=0.4)
model.add(Dense(10,input_dim=784,activation='softmax'))
# compiling model using adam optimiser and accuracy as metric
model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy'])
# fitting model and performing validation
model.fit(x_train,y_train,epochs=50,batch_size=128,validation_data=(x_test,y_test))
以上模型在同样的GPU上达到了98.42%的验证精度。所以,就像我们在一开始猜测的那样,结果是相同的。
如果这是你用R构建的第一个深度学习模型,我希望你很享受这个过程。使用很简单的代码,你就可以对手写数值进行精确度达到98%的分类。这应该可以给你足够的动力让你在机器学习的领域探索。
如果你已经在Python中使用过keras深度学习框架,那么你会发现R中keras框架的句式和结构跟其在Python中非常相似。事实上,R中的keras安装包创造了一个conda环境而且安装了在该环境下运行keras所需要的所有东西。但是,更让我兴奋的是:看到现在数据科学家们使用R构建有关现实生活的深度学习模型。就像有句话说的一样,竞争永不停歇。
原文链接:
https://www.analyticsvidhya.com/blog/2017/06/getting-started-with-deep-learning-using-keras-in-r/