activation function for binarized neural networks
Create a custom function
As explained in Keras doc for custom Actiation function
Creating custom activations
You can also use a TensorFlow callable as an activation (in this case it should take a tensor and return a tensor of the same shape and type):
model.add(layers.Dense(64, activation=tf.nn.tanh))

def custom_bin(x):
    return tf.where(x &gt;= 0, 1.0 , -1)

import tensorflow as tf
from tensorflow import keras
model = keras.Sequential()

model.add(keras.layers.Dense( 3, activation=&quot;linear&quot;,input_shape=(5,)))
model.add(keras.layers.Dense( 3, activation=&quot;linear&quot;))
model.add(keras.layers.Dense( 4, activation=custom_bin))
model.add(keras.layers.Dense( 2, activation=&quot;linear&quot;))

model.summary()


A toy NN to check the Activation output
A simple neural net with all weights = 1 and bias = 0. So, it will keep the output sign same as that of the input i.e.(+Ve, 0, -Ve).Then apply the respective Activation function in the output layer. Then predict using the 3 use-cases
def custom_bin(x):
    return tf.where(x &gt;= 0, 1.0 , -1) 
   
import tensorflow as tf
from tensorflow import keras

#Case I - tf.math.sign 
initializer = tf.keras.initializers.Ones()
model = keras.Sequential()
model.add(keras.layers.Dense( 2, activation=&quot;linear&quot;,input_shape=(2,),kernel_initializer=initializer))
model.add(keras.layers.Dense( 1, activation=tf.math.sign,kernel_initializer=initializer))
print(model.predict([[-5,-5],[0,0],[5, 5]]))

#Case II - custom if-then-else 
initializer = tf.keras.initializers.Ones()
model = keras.Sequential()
model.add(keras.layers.Dense( 2, activation=&quot;linear&quot;,input_shape=(2,),kernel_initializer=initializer))
model.add(keras.layers.Dense( 1, activation=custom_bin, kernel_initializer=initializer))
print(model.predict([[-5,-5],[0,0],[5, 5]]))


Output
[[-1.] [ 0.] [ 1.]]
[[-1.] [ 1.] [ 1.]]