Siamese Network Using Rstudio Keras

I am trying to implement a Siamese network using the Rstudio Keras package. The network I am trying to implement is the same network as in this post .

So, basically, I am porting the code to R and using the Keras Rstudio implementation. So far, my code looks like this:

    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

      

However, when running the last two lines, the following error appears:

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

      

I've looked at similar implementations and questions all over StackOverflow, but I couldn't find a solution. I think I might be missing something really obvious.

Any ideas how to solve this?

0


source to share


2 answers


As Daniel Falbel pointed out in his comment, the solution was updating the R-keras package and then updating the tensorflow installation.

However, the tensorflow package in R did not install the latest version 1.3 of tensorflow (it was reinstalling version 1.2).



To fix this issue, the correct version URL can be provided to the install_tensorflow function. URLs for different implementations can be found here . In this case, I used Linux. Running this command should fix the problem for anyone else facing the same problem:

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")

      

0


source


I tried GAN and got this error. When I use the same code in the CPU version it is ok for tensorflow, but it was not in the GPU version.



I found this problem was caused by using a parameter in the GPU version. You can delete the parameter and try again. I don't know why this solved the problem. I think it might be a bug when handling reusable models. kernel_regularizer

+1


source







All Articles