title: how to create a keras layer that takes effect only during evaluation phase (and that is transparent during training)?
body: <p>I want to add to my model a layer that, during evaluation, takes the input, applies some transformations (a quantization in this case, but can be whatever) and return it as the output. This layer must, however, be completely transparent during training, meaning that it must return the same input tensor.</p>
  
  <p>I have written the following function</p>
  
  <pre class="lang-py prettyprint-override"><code>from keras.layers import Lambda
  import keras.backend as K
  
  def myquantize(x):
      return K.in_test_phase( K.clip(K.round(x*(2**5))/(2**5),-3.9,3.9) , x)
  </code></pre>
  
  <p>which I then use via a Lambda layer</p>
  
  <pre class="lang-py prettyprint-override"><code>y = keras.layers.Conv1D(**args1)
  y = keras.layers.AveragePooling1D(pool_size=2)(y)
  y = keras.layers.Lambda(myquantize)(y)
  y = keras.layers.Conv1D(**args2)
  #...
  </code></pre>
  
  <p>Now, in principle the K.in_test_phase should return x during training, and that expression during test.
  However, training the network with such layer prevent the network from learning (i.e. the train loss stops decreasing after 3 epochs), while if I remove it the network keeps training normally. I assume this layer is not actually transparent during training as expected.</p>