-1

So, I'm using Keras to implement a convolutional neural network. At the end of my decoding topology there's a Conv2D layer with sigmoid activation.

decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

Basically, I want to change the sigmoid implementation, my goal is to make it a binary-type activation, returning 0 if sigmoid function get values below 0.5 and 1 if it gets values equal or above 0.5.

Searching inside Tensorflow implementations, I found sigmoid's to be something like this:

def sigmoid(x, name=None):
    with ops.name_scope(name, "Sigmoid", [x]) as name:
        x = ops.convert_to_tensor(x, name="x")
        return gen_math_ops._sigmoid(x, name=name)

I'm having trouble in manipulating gen_math_ops return, to compare it's values with the 0.5 threshold. I know usual if's can't be used because of tensor-type restrictions, so how should I solve this?

Mateus Hora
  • 117
  • 1
  • 6

1 Answers1

1

just round your output.

def hardsigmoid(x): 
  return tf.round(tf.nn.sigmoid(x))

Remember that such hard sigmoid has zero derivatives everywhere so you won't be able to train it with any sort of gradient based technique.

lejlot
  • 64,777
  • 8
  • 131
  • 164