title: Using a custom step activation function in Keras results in "An operation has `None` for gradient." error. How to resolve this?
body: <p>I am building auto-encoder and I want to encode my values into a logical matrix. However, when I'm using my custom step activation function in one of the intermediate layers (all other layers are using 'relu'), keras raises this error:</p>
  
  <pre><code>An operation has `None` for gradient.
  </code></pre>
  
  <p>I've tried using <a href="https://www.tensorflow.org/api_docs/python/tf/keras/backend/hard_sigmoid" rel="nofollow noreferrer">hard-sigmoid</a> function, but it doesn't fit my problem, because it still produces intermediate values, when I only need binary. I am aware, that at most points my function has no gradient, but is it possible to use some other function for gradient calculation and still use step function for accuracy and loss calculations?</p>
  
  <p>My activation function:</p>
  
  <pre><code>def binary_activation(x):
      ones = tf.ones(tf.shape(x), dtype=x.dtype.base_dtype)
      zeros = tf.zeros(tf.shape(x), dtype=x.dtype.base_dtype)
      return keras.backend.switch(x &gt; 0.5, ones, zeros)
  </code></pre>
  
  <p>I expect to be able to use binary step activation function to train the network and then use it as a typical auto-encoder. Something simmilar to binary feature map used in <a href="https://arxiv.org/pdf/1703.10553.pdf" rel="nofollow noreferrer">this paper</a>.</p>