user1076808
user1076808

Reputation: 149

Sigmoid going out of bound

def sigmoid(z):
# complete the code
z = np.asarray(z)
if z.ndim == 0:
    return(1/(1+np.exp(-z)))
else:
    d = np.array([])
    for item in z:
        d= np.append(d,1/(1+np.exp(-item)))
    return d

print(sigmoid([(1,3), (4,30)]))

why is this returning [ 0.73105858 0.95257413 0.98201379 1. ] as the function is bound from 0 to 1 for example [q= 1/1+np.exp(-30)][1] returns 1.0000000000000935

why is this happening and how to correct it? for example image of a weird looking output

Upvotes: 0

Views: 278

Answers (2)

yjcrocks
yjcrocks

Reputation: 126

Your sigmoid implementation looks fine.

The reason print(1 / 1 + np.exp(-30)) returns 1.0000000000000935 is due to operator precedence.

# Your example
1 / 1 + np.exp(-30)

# How it will be computed
(1 / 1) + np.exp(-30)

# What you actually wanted
1 / (1 + np.exp(-30))

P.S. numpy supports broadcasting. Your function can be simplified into:

def sigmoid(z):
    z = np.asarray(z)
    return 1 / (1 + np.exp(-z))

Use ravel function to flatten the multi-dimensional array, if it's what you want.

Upvotes: 2

Ashutosh Chapagain
Ashutosh Chapagain

Reputation: 926

oh BODMAS rule.

q= 1/(1+np.exp(-30))

returns 0.9999999999999065

which is less than 1

It is rounded off to 1 in the console

Upvotes: 0

Related Questions