使用Rstudio Keras的Siamese网络

时间:2017-07-24 12:08:44

标签: r tensorflow rstudio keras conv-neural-network

我正在尝试使用Rstudio Keras软件包实现一个连体网络。我尝试实施的网络与您在this post中可以看到的网络相同。

因此,基本上,我将代码移植到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上的类似实现和问题,但我找不到解决方案。我想我可能会遗漏一些非常明显的东西。

任何想法如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

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 :(得分:1)

我尝试过GAN并且也遇到了这个错误。当我在张量流的CPU版本上使用相同的代码是可以的,但在GPU版本上没有。

我发现此问题是由GPU版本上的 kernel_regularizer 参数引起的。您可以删除参数并再次尝试。我不知道为什么这解决了这个问题。我想在处理重用模型时可能会出错。