2017-07-24 41 views
0

我試圖使用Rstudio Keras軟件包實現連體網絡。我試圖實現的網絡與this post中的網絡相同。使用Rstudio Keras的連體網絡

所以,基本上,我將代碼移植到R並使用Rstudio Keras實現。到目前爲止,我的代碼如下所示:

library(keras) 

    inputShape <- c(105, 105, 1) 
    leftInput <- layer_input(inputShape) 
    rightInput <- layer_input(inputShape) 

    model<- keras_model_sequential() 

    model %>% 
     layer_conv_2d(filter=64, 
        kernel_size=c(10,10), 
        activation = "relu", 
        input_shape=inputShape, 
        kernel_initializer = initializer_random_normal(0, 1e-2), 
        kernel_regularizer = regularizer_l2(2e-4)) %>% 
     layer_max_pooling_2d() %>% 

     layer_conv_2d(filter=128, 
        kernel_size=c(7,7), 
        activation = "relu", 
        kernel_initializer = initializer_random_normal(0, 1e-2), 
        kernel_regularizer = regularizer_l2(2e-4), 
        bias_initializer = initializer_random_normal(0.5, 1e-2)) %>% 
     layer_max_pooling_2d() %>% 

     layer_conv_2d(filter=128, 
        kernel_size=c(4,4), 
        activation = "relu", 
        kernel_initializer = initializer_random_normal(0, 1e-2), 
        kernel_regularizer = regularizer_l2(2e-4), 
        bias_initializer = initializer_random_normal(0.5, 1e-2)) %>% 
     layer_max_pooling_2d() %>% 

     layer_conv_2d(filter=256, 
        kernel_size=c(4,4), 
        activation = "relu", 
        kernel_initializer = initializer_random_normal(0, 1e-2), 
        kernel_regularizer = regularizer_l2(2e-4), 
        bias_initializer = initializer_random_normal(0.5, 1e-2)) %>% 

     layer_flatten() %>% 
     layer_dense(4096, 
        activation = "sigmoid", 
        kernel_initializer = initializer_random_normal(0, 1e-2), 
        kernel_regularizer = regularizer_l2(1e-3), 
        bias_initializer = initializer_random_normal(0.5, 1e-2)) 

    encoded_left <- leftInput %>% model 
    encoded_right <- rightInput %>% model 

但是,在運行的最後兩行的時候,我得到以下錯誤:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
    AttributeError: 'Model' object has no attribute '_losses' 

Detailed traceback: 
    File "/home/rstudio/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/contrib/keras/python/keras/engine/topology.py", line 432, in __call__ 
    output = super(Layer, self).__call__(inputs, **kwargs) 
    File "/home/rstudio/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 441, in __call__ 
    outputs = self.call(inputs, *args, **kwargs) 
    File "/home/rstudio/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/contrib/keras/python/keras/models.py", line 560, in call 
    return self.model.call(inputs, mask) 
    File "/home/rstudio/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/contrib/keras/python/keras/engine/topology.py", line 1743, in call 
    output_tensors, _, _ = self.run_internal_graph(inputs, masks) 
    File "/home/rstudio/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/contrib/keras/python 

我一直在尋找類似的實現和問題都在StackOverflow上,但我找不到解決方案。我想我可能會錯過一些非常明顯的東西。

任何想法如何解決這個問題?

+1

這在我的電腦上運行良好。嘗試更新R-keras軟件包和tensorflow安裝。 –

+0

哦,快點。感謝您花時間對其進行測試。雖然我的安裝很新,但我會嘗試更新並查看是否可以運行它:) –

+0

解決!謝謝丹尼爾。如果您可以自己重新發布您的建議作爲答案,那麼我可以將其標記爲解決方案。我還沒有幾個名聲,但我應該能夠做到這一點... :) –

回答

0

正如Daniel Falbel在他的評論中指出的,解決方案是更新R-keras軟件包,然後更新tensorflow安裝。

但是,R中的tensorflow軟件包並未安裝最新的1.3 tensorflow版本(它是重新安裝1.2版本)。

要解決此問題,可以將正確版本的URL提供給install_tensorflow函數。可以找到不同實現的URL here。在這種情況下,我正在使用Linux。運行此命令應解決遇到同一問題的任何人的問題:

library(tensorflow) 
install_tensorflow(package_url = "https://pypi.python.org/packages/b8/d6/af3d52dd52150ec4a6ceb7788bfeb2f62ecb6aa2d1172211c4db39b349a2/tensorflow-1.3.0rc0-cp27-cp27mu-manylinux1_x86_64.whl#md5=1cf77a2360ae2e38dd3578618eacc03b") 
1

我試過了GAN,並且也出現了這個錯誤。當我在tensorflow的CPU版本上使用相同的代碼時沒問題,但在GPU版本上沒有。

我發現,這個問題是通過使用GPU上的版本kernel_regularizer參數引起的。您可以刪除參數並再次嘗試。我不知道爲什麼這解決了這個問題。我想這可能是一個錯誤時處理重用模型。