title: Keras set output of intermediate layer to 0 or 1 based on threshold
body: <p>I have a model that has "classification" and "regression" like parts. I merge them using multiplication layer. Before performing multiplication I want to set outputs of classification part to 0 or 1 based on threshold. I tried to use Lambda layer with custom function as below, however I am facing various errors, and I have no clue about those errors. Resolving them one by one as I go does not add to my understanding. Can anyone explain how to define custom Lambda layer function that modifies the values?</p>
  
  <p><strong>My current Lambda layer function:</strong> (not working due to <code>FailedPreconditionError: Attempting to use uninitialized value lstm_32/bias</code>)</p>
  
  <pre><code>def func(x):    
      a = x.eval(session=tf.Session())
      a[x &lt; 0.5] = 0
      a[x &gt;= 0.5] = 1
      return K.variable(a)
  </code></pre>
  
  <p><strong>Regression part:</strong></p>
  
  <pre><code>input1 = Input(shape=(1, ))
  model = Sequential()
  model.add(Embedding(vocab_size + 1, embedding, input_length=1))
  model.add(LSTM(hidden, recurrent_dropout=0.1, return_sequences=True))
  model.add(LSTM(6))
  model.add(Reshape((3,2)))
  model.add(Activation('linear'))
  </code></pre>
  
  <p><strong>Classification part:</strong></p>
  
  <pre><code>input2 = Input(shape=(1, ))
  model2 = Sequential()
  model2.add(Embedding(vocab_size + 1, embedding, input_length=1))
  model2.add(LSTM(hidden, recurrent_dropout=0.1, return_sequences=True))
  model2.add(LSTM(1))
  model2.add(Activation('sigmoid'))
  model2.add(???)  # need to add 0-1 thresholding here
  </code></pre>
  
  <p><strong>Merge two parts:</strong></p>
  
  <pre><code>reg_head = model(input1)
  clf_head = model2(input2)    
  merge_head = multiply(inputs=[clf_head, reg_head])
  m2 = Model(inputs=[input1, input2], outputs=merge_head)
  </code></pre>