TheHedge
TheHedge

Reputation: 127

Sigmoid Function in Numpy

For fast computations, I have to implement my sigmoid function in Numpy this is the code below

   def sigmoid(Z):
    """
    Implements the sigmoid activation in bumpy

    Arguments:
    Z -- numpy array of any shape

    Returns:
    A -- output of sigmoid(z), same shape as Z
    cache -- returns Z, useful during backpropagation
    """

    cache=Z

    print(type(Z))
    print(Z)
    A=1/(1+(np.exp((-Z))))

    return A, cache

Also some relevant information:

  Z=(np.matmul(W,A)+b)

and the type of Z is:

  <class 'numpy.ndarray'>

Sadly I am getting a: "bad operand type for unary -: 'tuple' " I have tried to work around this problem without any luck.I appreciate any suggestions. Best

Upvotes: 6

Views: 49825

Answers (3)

Chris Idzerda
Chris Idzerda

Reputation: 348

You are seeing this error most likely because your Z, while it is of type numpy.ndarray, contains a tuple. For instance, consider this definition of Z.

Z = np.array([1, (1, 2, 3), 3])

If you run your sigmoid function on this Z, it will print that Z is a numpy.ndarray but produce the TypeError you get because numpy broadcasts the unary negation across the members of Z, one of which is a tuple, and tuple does not implement unary negation.

Upvotes: 2

hansrajswapnil
hansrajswapnil

Reputation: 639

While implementing sigmoid function is quite easy, sometimes the argument passed in the function might cause errors.

Code snippet

def sigmoid_function(z):
""" this function implements the sigmoid function, and 
expects a numpy array as argument """
    
    if isinstance(z, numpy.ndarray):
        continue
    
    sigmoid = 1.0/(1.0 + np.exp(-z))
    return sigmoid 

Few important points to keep in mind:-

  • using 1.0 in value of sigmoid will result in a float type output
  • checking the type of argument before performing your computations over them is a good programming practice. It can help avoid confusions later

  • Why is sigmoid function used so often?
    Sigmoid function is used for squishing the range of values into a range (0, 1). There are multiple other function which can do that, but a very important point boosting its popularity is how simply it can express its derivatives, which comes handy in backpropagation

    Implementating derivative of sigmoid

    def sigmoid_derivative(z):
        """ this function implements the derivative of a sigmoid function """
        
        return sigmoid_function(z) * (1.0 - sigmoid_function(z))
    

    derivative of sigmoid can be expressed in terms of sigmoid!!

    Upvotes: 1

    user9238250
    user9238250

    Reputation:

    This worked for me. I think no need to use cache because you already initialized it. Try this code below.

    import matplotlib.pyplot as plt 
    import numpy as np 
    
    z = np.linspace(-10, 10, 100) 
    def sigmoid(z):
        return 1/(1 + np.exp(-z))
    
    a = sigmoid(z)
    plt.plot(z, a) 
    plt.xlabel("z") 
    plt.ylabel("sigmoid(z)")
    

    Upvotes: 18

    Related Questions