Start learning free

LESSON 66 OF 165 · MACHINE LEARNING: A NEURAL NETWORK FROM SCRATCH

Squeeze It: Sigmoid in Python

Practice

Your neuron adds up weighted inputs plus a bias. For this customer it gives 1.0, but with other weights the same sum could be 6 or -3: a sum has no limits. To read it as a chance that the customer buys again, you need a number between 0 and 1. The sigmoid function does that squeeze: sigmoid(z) = 1 / (1 + e-z), where e is a fixed number, about 2.718.

A new numpy function: np.exp(a) gives back e raised to the power a. On a single number it gives back one number; on an array it works on every item and gives back an array of the same shape.

print(np.exp(0.0))                                # 1.0
print(np.round(np.exp(np.array([1.0, 2.0])), 3))  # [2.718 7.389]

Work one value by hand: for z = 2, e-2 is about 0.135, so sigmoid(2) = 1 / 1.135, about 0.881. A very negative z makes e-z huge, so the sigmoid is close to 0. A very positive z makes it tiny, so the sigmoid is close to 1, and z = 0 gives exactly 0.5. Because np.exp works item by item, your sigmoid will too; np.round(a, 3) rounds every item of an array.

Your code must print these lines:

neuron output: 1.0
as a chance: 0.731
sigmoid of [-5.  0.  5.] is [0.007 0.5   0.993]

Below them, the check prints lines of its own: it calls your sigmoid on -20 and 20, on 2 and -2, and on a small table.

Your task: below neuron, write a function sigmoid(z), print the neuron's sum as a chance, then squeeze an array of -5, 0 and 5 in one call.


Assignment
Follow the Coding Tutorial to write sigmoid and print the chances for the neuron's sum and for -5, 0 and 5.


Hint
Put 1 + np.exp(-z) in parentheses: without them Python divides 1 by 1 first and adds afterwards. Round one number with round(…, 3) and an array with np.round(…, 3).


STEP 1 OF 5
    Ctrl + Enter to check

    Nn Sigmoid in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free