R语言中不能举办深度进修?
当前位置:以往代写 > 其他教程 >R语言中不能举办深度进修?
2019-06-14

R语言中不能举办深度进修?

R语言中不能举办深度进修?

众所周知,R语言是统计阐明较好用的语言。但在Keras和TensorFlow的辅佐下,R语言也可以举办深度进修了。
在呆板进修的语言的选择上,R和Python之间选择一直是一个有争议的话题。但跟着深度进修的爆炸性增长,越来越多的人选择了Python,因为它有一个很大的深度进修库和框架,而R却没有(直到此刻)。
可是我就是想利用R语言进入深度进修空间,所以我就从Python规模转入到了R规模,继承我的深度进修的研究了。这大概看起来险些不行能的。可是本日这酿成了大概。
跟着Keras在R上的推出,R与Python的斗争回到了中心。Python逐步成为了最风行的深度进修模子。可是,跟着Keras库在R后端的宣布,而且在靠山还可以利用张力流(TensorFlow)(CPU和GPU兼容性),所以在深度进修规模,R将再次与Python打成平手。
下面我们将看到如何利用Tensorflow在R中安装Keras,并在RStudio的经典MNIST数据集上构建我们的第一个神经网络模子。
目次:
1.在后端安装带有张量的Keras。
2.利用Keras可以在R中构建差异范例的模子。
3.在R中利用MLP对MNIST手写数字举办分类。
4.将MNIST功效与Python中的等效代码举办较量。
5.竣事条记。
1.在后端安装带有TensorFlow的Keras。在RStudio中安装Keras的步调很是简朴。只需凭据以下步调,您将很顺利的在R中建设您的第一个神经网络模子。
install.packages(“devtools”)devtools::install_github(“rstudio/keras”)上述步调将从GitHub客栈加载keras库。此刻是将keras加载到R并安装TensorFlow的时候了。
library(keras)
默认环境下,RStudio加载TensorFlow的CPU版本。利用以下呼吁下载TensorFlow的CPU版本。
install_tensorflow()
要为单个用户/桌面系统安装具有GPU支持的TensorFlow版本,请利用以下呼吁。
install_tensorflow(gpu=TRUE)有关更多的用户安装,请参阅本安装指南。
此刻我们在RStudio中安装了keras和TensorFlow,让我们在R中启动和构建我们的第一个神经网络来办理MNIST数据集
2.利用keras可以在R中构建的差异范例的模子
以下是利用Keras可以在R中构建的模子列表。
1.多层感知器
2.卷积神经网络
3.轮回神经网络
4.Skip-Gram模子
5.利用预先练习的模子,如VGG16,RESNET等
6.微调预先练习的模子。
让我们开始构建一个很是简朴的MLP模子,只需一个埋没的层来实验分类手写数字。
3.利用R中的MLP对MNIST手写数字举办分类#loading keras librarylibrary(keras)#loading the keras inbuilt mnist datasetdata<-dataset_mnist()#separating train and test filetrain_x<-data$train$xtrain_y<-data$train$ytest_x<-data$test$xtest_y<-data$test$yrm(data)# converting a 2D array into a 1D array for feeding into the MLP and normalising the matrixtrain_x <- array(train_x, dim = c(dim(train_x)[1], prod(dim(train_x)[-1]))) / 255test_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 functiontrain_y<-to_categorical(train_y,10)test_y<-to_categorical(test_y,10)#defining a keras sequential modelmodel <- 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 9model %>% 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 datasetmodel %>% fit(train_x, train_y, epochs = 100, batch_size = 128)#Evaluating model on the cross validation datasetloss_and_metrics <- model %>% evaluate(test_x, test_y, batch_size = 128)上述代码的练习精度为99.14,验证精确率为96.89。代码在i5处理惩罚器上运行,运行时间为13.5秒,而在TITANx GPU上,验证精度为98.44,平均运行时间为2秒。
4.MLP利用keras–R VS Python为了较量起见,我也在Python中实现了上述的MNIST问题。我以为在keras-R和Python中应该没有任何区别,因为R中的keras建设了一个conda实例并在个中运行keras。你可以实验运行一下下面等效的python代码。
#importing the required libraries for the MLP modelimport  kerasfrom  keras.models  import Sequentialimport  numpy  as  np#loading  the  MNIST dataset  from  kerasfrom  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 dimensionsx_train=np.reshape(x_train,(x_train.shape[0],-1))/255x_test=np.reshape(x_test,(x_test.shape[0],-1))/255import pandas as pdy_train=pd.get_dummies(y_train)y_test=pd.get_dummies(y_test)#performing one-hot encoding on target variables for train and testy_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 Densemodel.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 metricmodel.compile(loss=’categorical_crossentropy’, optimizer=”adam”, metrics=[‘accuracy’])# fitting model and performing validationmodel.fit(x_train,y_train,epochs=50,batch_size=128,validation_data=(x_test,y_test))上述模子在同一GPU上实现了98.42的验证精度。所以,我们最初猜到的功效是正确的。
5.竣事条记假如这是你在R的第一个深度进修模子,我但愿你喜欢它。通过一个很是简朴的代码,您可以有98%位精确率对是否为手写数字举办分类。这应该是足够的动力让你开始深度进修。
假如您已经在Python中利用keras深度进修库,那么您将在R中找到keras库的语法和布局与Python中相似的处所。事实上,R中的keras包建设了一个conda情况,并安装了在该情况中运行keras所需的一切。可是,让我更为感动的是,此刻看到数据科学家在R中成立现实糊口中的深条理的进修模子。听说 – 竞争应该永远不会遏制。我也想听听你对这一新成长概念的观点。你可以在下面留言分享你的观点。
接待插手本站果真乐趣群贸易智能与数据阐明群乐趣范畴包罗各类让数据发生代价的步伐,实际应用案例分享与接头,阐明东西,ETL东西,数据客栈,数据挖掘东西,报表系统等全方位常识QQ群:81035754

    关键字:

在线提交作业