1. 程式人生 > 其它 >深度學習與R語言

深度學習與R語言

對於R語言使用者來說,深度學習還沒有生產級的解決方案(除了MXNET)。這篇文章介紹了R語言的Keras介面,以及如何使用它來執行影象分類。文章結尾會通過提供一些程式碼片段顯示Keras的直觀和強大

Tensorflow

去年1月,R語言中的Tensorflow 釋出了,它提供了從R語言中獲得的Tensorflow API的方法。這是很重要的,因為Tensorflow是最受歡迎的深度學習庫。然而,對於大多數R語言使用者來說,R語言的Tensorflow介面和R語言並不是很像。下面是訓練模型的程式碼塊。

cross_entropy <- tf$reduce_mean(-tf$reduce_sum(y_* tf$log(y_conv), reduction_indices=1L))
train_step <- tf$train$AdamOptimizer(1e-4)$minimize(cross_entropy)
correct_prediction <- tf$equal(tf$argmax(y_conv,1L), tf$argmax(y_,1L))
accuracy <- tf$reduce_mean(tf$cast(correct_prediction, tf$float32))
sess$run(tf$global_variables_initializer())

for (iin 1:20000) {
  batch <- mnist$train$next_batch(50L)
  if (i%% 100 == 0) {
    train_accuracy <- accuracy$eval(feed_dict= dict(
        x= batch[[1]], y_= batch[[2]], keep_prob= 1.0))
    cat(sprintf("step %d, training accuracy %gn", i, train_accuracy))
  }
  train_step$run(feed_dict= dict(
    x= batch[[1]], y_= batch[[2]], keep_prob= 0.5))
}

test_accuracy <- accuracy$eval(feed_dict= dict(
     x= mnist$test$images, y_= mnist$test$labels, keep_prob= 1.0))
cat(sprintf("test accuracy %g", test_accuracy))

除非你熟悉Tensorflow,,否則你可能不清楚發生了什麼。Github的快速搜尋發現使用tensorflow為R語言提供的程式碼不到100個。

Keras

所有這一切都將隨著Keras和R語言而改變。

Keras是一個用於實驗的高階神經網路API,可以在Tensorflow上執行。Keras是科學家們喜歡使用的資料。Keras越來越受歡迎,並在越來越多的平臺上得到支援,包括Tensorflow,CNTK,Apple的CoreML,Theano。在深度學習中越來越重要。

舉一個簡單的例子,在Keras中訓練模型的程式碼如下:

model_top%>% fit(
        x= train_x, y= train_y,
        epochs=epochs,
        batch_size=batch_size,
        validation_data=valid)

用Keras進行影象分類

讓我告訴你如何使用R語言、Keras和Tensorflow構建深度學習模型。你會發現一個Github repo在https://github.com/rajshah4/image_keras/,其中包含你需要的程式碼和資料。通過R notebook(和Python notebooks)構建一個影象分類器,可以很容易地應用到其他影象上。用於構建深度學習工作的高階方法包括:

增加的資料

使用預先訓練的網路的瓶頸特徵

對預先訓練的網路頂層進行微調

儲存模型的權重

Keras的程式碼片段

Keras的R語言介面確實可以很容易地在R語言中構建深度學習模型,這裡有基於構建影象分類器一些程式碼片段,以說明R語言中Keras的直觀和有用

載入folder:

train_generator <- flow_images_from_directory(train_directory, generator= image_data_generator(), target_size= c(img_width, img_height), color_mode= "rgb",
  class_mode= "binary", batch_size= batch_size, shuffle= TRUE,
  seed= 123)

定義一個簡單的卷積神經網路:

model <- keras_model_sequential()

model%>%
  layer_conv_2d(filter = 32, kernel_size= c(3,3), input_shape= c(img_width, img_height,3))%>%
  layer_activation("relu")%>%
  layer_max_pooling_2d(pool_size= c(2,2))%>%

  layer_conv_2d(filter = 32, kernel_size= c(3,3))%>%
  layer_activation("relu")%>%
  layer_max_pooling_2d(pool_size= c(2,2))%>%

  layer_conv_2d(filter = 64, kernel_size= c(3,3))%>%
  layer_activation("relu")%>%
  layer_max_pooling_2d(pool_size= c(2,2))%>%

  layer_flatten()%>%
  layer_dense(64)%>%
  layer_activation("relu")%>%
  layer_dropout(0.5)%>%
  layer_dense(1)%>%
  layer_activation("sigmoid")

增加資料:

augment <- image_data_generator(rescale=1./255,
                               shear_range=0.2,
                               zoom_range=0.2,
                               horizontal_flip=TRUE)

載入預先訓練的網路:

model_vgg <- application_vgg16(include_top= FALSE, weights= "imagenet")

儲存模型權重:

save_model_weights_hdf5(model_ft,'finetuning_30epochs_vggR.h5', overwrite= TRUE)

R語言中的Keras介面使R語言使用者更容易構建和細化深度學習模型。不再強迫使用Python構建、精煉和測試深度學習模型。這應該向對使用python有點擔心的受眾開放。

首先,您可以應用我的repo,啟動RStudio(或者您選擇的IDE),然後使用Keras構建一個簡單的分類器。

本文為編譯文章,作者Rajiv Shah,原網址為

http://projects.rajivshah.com/blog/2017/06/04/deeplearningR/