Shubh_20
Shubh_20

Reputation: 31

Inverse Sigmoid Function in Python for Neural Networks?

I am trying to implement an Inverse Sigmoid function to the last layer of my Convolutional Neural Network? I am trying to build the network in Pytorch and I want to take the output from the last Convolutional Layer and then apply Inverse Sigmoid Function to it.

I have read that the logit function is the opposite of sigmoid function and I tried implementing it but its not working. I used the logit function from the scipy library and used it in the function.

def InverseSigmoid(self, x):

        x = logit(x)
        return x

Upvotes: 3

Views: 8759

Answers (2)

Mateen Ulhaq
Mateen Ulhaq

Reputation: 27271

The inverse sigmoid may be computed via:

inv_sigmoid(x) = ln(x) - ln(1 - x)

Proof:

sigmoid(x) = 1 / (1 + exp(-x))
inv_sigmoid(sigmoid(x))
  = ln(sigmoid(x)) - ln(1 - sigmoid(x))
  = ln(sigmoid(x) / (1 - sigmoid(x)))
  = -ln((1 - sigmoid(x)) / sigmoid(x))
  = -ln(1 / sigmoid(x) - 1)
  = -ln(1 + exp(-x) - 1)
  = x

Upvotes: 3

abe
abe

Reputation: 987

Sigmoid is just 1 / (1 + e**-x). So if you want to invert it you can just -ln((1 / x) - 1). For numerical stability purposes, you can also do -ln((1 / (x + 1e-8)) - 1). This is the inverse function of sigmoid, implementation is straightforward.

Upvotes: 3

Related Questions