![](https://csdnimg.cn/release/download_crawler_static/86734815/bg3.jpg)
Furthermore, if x is a vector, then a Python operation such as or will output s as a vector of the same size
as x.
Any time you need more info on a numpy function, we encourage you to look at the official documentation
(https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.exp.html).
You can also create a new cell in the notebook and write np.exp? (for example) to get quick access to the documentation.
Exercise: Implement the sigmoid function using numpy.
Instructions: x could now be either a real number, a vector, or a matrix. The data structures we use in numpy to represent
these shapes (vectors, matrices...) are called numpy arrays. You don't need to know more for now.
For
𝑥
∈
,
𝑠𝑖𝑔𝑚𝑜𝑖𝑑
(
𝑥
) =
𝑠𝑖𝑔𝑚𝑜𝑖𝑑
=
Expected Output:
**sigmoid([1,2,3])** array([ 0.73105858, 0.88079708, 0.95257413])
1.2 - Sigmoid gradient
As you've seen in lecture, you will need to compute gradients to optimize loss functions using backpropagation. Let's code
your first gradient function.
[ 2.71828183 7.3890561 20.08553692]
array([0.73105858, 0.88079708, 0.95257413])
import numpy as np
# example of np.exp
x = np.array([1, 2, 3])
print(np.exp(x)) # result is (exp(1), exp(2), exp(3))
# example of vector operation
x = np.array([1, 2, 3])
print (x + 3)
# GRADED FUNCTION: sigmoid
import numpy as np # this means you can access numpy functions by writing np.function() instead of numpy.functio
def sigmoid(x):
"""
Compute the sigmoid of x
Arguments:
x -- A scalar or numpy array of any size
Return:
s -- sigmoid(x)
"""
### START CODE HERE ### (≈ 1 line of code)
s = 1/(1+np.exp(-x))
### END CODE HERE ###
return s
x = np.array([1, 2, 3])
sigmoid(x)