Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binary threshold activation function in tensorflow

I have a piece of code that uses sigmoid activation function for classification that outputs [0,1]. But I need a activation function that outputs binary values either 0 or 1.

        x = tf.placeholder("float", [None, COLUMN])
        Wh = tf.Variable(tf.random_normal([COLUMN, UNITS_OF_HIDDEN_LAYER], mean=0.0, stddev=0.05))
        h = tf.nn.sigmoid(tf.matmul(x, Wh))

        Wo = tf.Variable(tf.random_normal([UNITS_OF_HIDDEN_LAYER, COLUMN], mean=0.0, stddev=0.05))
        y = tf.nn.sigmoid(tf.matmul(h, Wo))

        # Objective functions
        y_ = tf.placeholder("float", [None, COLUMN])
        correct_prediction = tf.equal(tf.argmax(y, 1),tf.argmax(y, 1))
        cost = tf.reduce_sum(tf.cast(correct_prediction, "float"))/BATCH_SIZE

Can you please tell me how to replace sigmoid function with binary step one.

like image 244
user3104352 Avatar asked Dec 10 '22 09:12

user3104352


2 Answers

y = tf.round(tf.nn.sigmoid(tf.matmul(h,Wo))

that will give you 0 or 1 output.

like image 69
Ryan Jay Avatar answered Dec 20 '22 18:12

Ryan Jay


You don't need sigmoid in this case. Try relu(sign(x))

like image 37
Hongyu Sun Avatar answered Dec 20 '22 17:12

Hongyu Sun