Is there a logit function in tensorflow?
Asked Answered
F

3

6

Is there a logit function in tensorflow, i.e. the inverse of sigmoid function? I have searched google but have not found any.

Farthest answered 11/6, 2018 at 9:14 Comment(1)
removed @joseph-budinAbduce
T
5

tf.log_sigmoid() is not a logit function. It's the log of the logistic function.

From the TF doc:

y = log(1 / (1 + exp(-x)))

As far as I can tell, TF doesn't have a logit function, so you have to make your own, as the first answer originally suggested.

Timoteo answered 25/1, 2019 at 22:32 Comment(0)
C
1

Given the definition of the logit function (as inverse of the sigmoidal logistic function), it is rather straightforward to implement it yourself (c.f. Wikipedia "Logit" article):

As sigmoid(x) = 1 / (1 + exp(-x)),

logit(y) = sigmoid(x)^-1 = log(y / (1 - p)) = -log( 1 / p - 1)


Implementation:

import tensorflow as tf

def logit(x):
    """ Computes the logit function, i.e. the logistic sigmoid inverse. """
    return - tf.log(1. / x - 1.)

x = tf.random_uniform((5, ), minval=-10., maxval=10., dtype=tf.float64)

# sigmoid(x):
x_sig = tf.sigmoid(x)

# logit(sigmoid(x)) = x:
x_id = logit(x_sig)

# Verifying the equality:
comp = tf.abs(x - x_id)

with tf.Session() as sess:
    a, a_id, co = sess.run([x, x_id, comp])
    print(a)
    # [ 0.99629643  1.4082849   6.6794731   7.64434123  6.99725702]
    print(a_id)
    # [ 0.99629643  1.4082849   6.6794731   7.64434123  6.99725702]
    print(co)
    # [  2.22044605e-16   0.00000000e+00   7.28306304e-14   4.35207426e-14 7.81597009e-14]

Note: The equality holds for rather small values of x (i.e. small values of n for x in [-n, n]) as sigmoid(x) converges quickly towards its asymptote limits:

import tensorflow as tf

def logit(x):
    """ Computes the logit function, i.e. the logistic sigmoid inverse. """
    return - tf.log(1. / x - 1.)

x = tf.constant([-1000, -100, -10, -1, 0, 1, 10, 100, 1000], dtype=tf.float64)

# sigmoid(x):
x_sig = tf.sigmoid(x)
# logit(sigmoid(x)) = x:
x_id = logit(x_sig)

with tf.Session() as sess:
    a, a_id = sess.run([x, x_id])
    print(a)
    # [-1000.  -100.   -10.    -1.     0.     1.    10.   100.  1000.]
    print(a_id)
    # [ -inf   -100.   -10.    -1.     0.     1.    10.   inf   inf  ]
Cristicristian answered 11/6, 2018 at 9:56 Comment(3)
There is a logistic sigmoid function already in tensorflow :tf.math.log_sigmoid. It is not asked to develop a new oneSchematic
@Schematic I somehow missed this method. Thanks for pointing out. The answer has been edited accordingly.Cristicristian
@AGP: The log_sigmoid is not the same as the logit, and therefore it is not the inverse of the sigmoid either. benjaminplanche: please remove that incorrect and misleading edit.Margaretemargaretha
S
1

No, the function tf.log_sigmoid is not the inverse of sigmoid.

The first answer by @benjaminplanche was very correct.

import tensorflow as tf
logit = lambda x: -tf.math.log(1/x-1)
assert logit(tf.math.sigmoid(0.4))==0.4

This is also implemented in scipy.special.logit. `

Swatch answered 27/1, 2022 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.