title: Keras Custom Layer Working with Constants and Variables
body: <p>i have some problems with Keras custom layer and lambda layers/function. I will be glad if somebody can help.</p>
  <p>I try to build a custom layer to make some calculations and save/reuse old values in a variable. Keras-Documentation advises to use subclasses indeed lamda functions if you work with variables.</p>
  <pre><code># Define Constante outside of the Model
  
  c = tf.constant(5., dtype=tf.float32)
  
  # Define Custom Layer
  
  class MyLayer(Layer):
  
      def __init__(self, input_dim):
          super(MyLayer, self).__init__()
          self.prev_input = tf.Variable(initial_value=tf.zeros(input_dim),
                                        trainable=False)
          self.prev_output = tf.Variable(initial_value=tf.zeros(input_dim),
                                         trainable=False)
  
      def get_config(self):
          config = super().get_config()
          config.update({
              &quot;prev_input&quot;: self.prev_input,
              &quot;prev_output&quot;: self.prev_output,
          })
          return config
  
      def call(self, inputs_, training=False):
          a = inputs_[0]
          b = inputs_[1]
          t = K.get_value(c)
          output = self.prev_output*a*b / t 
          self.prev_input = inputs_
          self.prev_output = output
          return output
  
      def compute_output_shape(self, input_shape):
          return tf.TensorShape([None, 1])
  </code></pre>
  <p>Thank you</p>