1

I am trying to define a custom layer in Keras, where the data values are first quantized as -1, 0, or 1. Then, every -1 is transformed to 0. For example, The input tensor is x = [-0.95, -0.85,0.1,0.9]. It will be quantized to x1 = [-1,-1,0,1]. Then, x2 is transformed to x3 = [0,0,0,1]. I have completed the quantization part. However, I don't know how to implement the if-else control flow to map -1 to 0. Thank you for any helpful suggestions.

Quicky2357
  • 11
  • 1
  • I've never used Keras, but by taking a look at their [documentation](https://keras.io/api/layers/base_layer/#layer-class), it seems that the if-else logic should go into your activation function. The layer weights should be all set to 1, since you don't want to change the input. `non_trainable_weights` might do the job for you, since you don't want the weights to change. You might also want to read [this SO post](https://stackoverflow.com/questions/45142706/how-to-write-step-function-as-an-activation-function-in-keras), as it might contain some useful information. Good luck! – Nicholas Obert Aug 16 '22 at 05:47
  • Just apply a ReLU activation that maps everything < 0 to 0. – xdurch0 Aug 16 '22 at 06:23

0 Answers0