Differentiable round function in Tensorflow?

前端 未结 6 1043
面向向阳花
面向向阳花 2021-02-04 20:27

So the output of my network is a list of propabilities, which I then round using tf.round() to be either 0 or 1, this is crucial for this project. I then found out that tf.roun

6条回答
  •  走了就别回头了
    2021-02-04 20:58

    You can use the fact that tf.maximum() and tf.minimum() are differentiable, and the inputs are probabilities from 0 to 1

    # round numbers less than 0.5 to zero;
    # by making them negative and taking the maximum with 0
    differentiable_round = tf.maximum(x-0.499,0)
    # scale the remaining numbers (0 to 0.5) to greater than 1
    # the other half (zeros) is not affected by multiplication
    differentiable_round = differentiable_round * 10000
    # take the minimum with 1
    differentiable_round = tf.minimum(differentiable_round, 1)
    

    Example:

    [0.1,       0.5,     0.7]
    [-0.0989, 0.001, 0.20099] # x - 0.499
    [0,       0.001, 0.20099] # max(x-0.499, 0)
    [0,          10,  2009.9] # max(x-0.499, 0) * 10000
    [0,         1.0,     1.0] # min(max(x-0.499, 0) * 10000, 1)
    

提交回复
热议问题