Keras: Custom Keras Layer: Pass external parameter to a custom layer during training

Created on 21 Jun 2016  路  5Comments  路  Source: keras-team/keras

Please make sure that the boxes below are checked before you submit your issue. Thank you!

  • [x] Check that you are up-to-date with the master branch of Keras. You can update with:
    pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps
  • [x] If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with:
    pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps
  • [ ] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).

I'm trying to implement a custom layer in Keras and need some parameters to be passed to the call() function. These parameters depend on the specific training instance. How can I pass these parameters to the layer during training?

Most helpful comment

I finally found a way through. Made a Lambda layer which took extra Input parameter
custom_layer = Lambda(custom_layer, output_shape=custom_layer_output_shape, arguments={'extra_parameter':extra_parameter})
where extra_parameter was an extra input to the model.

All 5 comments

Recently, I'm defining a own layer, and suffer a similar question.
I want to ask your quetion. How do you get the value of x in call(x) function.
Is there a existing method or It need me to define a function?

The value x is the input to the custom layer you've defined.

from keras.layers import Input, Dense
from keras.models import Model

inputs = Input(shape=(784,))

dense = Dense(64, activation='relu')(inputs)
custom_layer = CustomLayer(32, activation='relu')(dense)

In the above example, the 'dense' variable output from the Dense layer is passed to the call(x) function of your custom layer. You just need to define the model appropriately.

I finally found a way through. Made a Lambda layer which took extra Input parameter
custom_layer = Lambda(custom_layer, output_shape=custom_layer_output_shape, arguments={'extra_parameter':extra_parameter})
where extra_parameter was an extra input to the model.

@stillbreeze This extra parameter would be constant. Is there any way we could be able to pass different parameter values based on the point that we are processing at the current time.

@KiriteeGak : Just pass it as an input to the graph in the functional API.

Was this page helpful?
0 / 5 - 0 ratings