赞
踩
求解z=σ(z)
由于sigmoid(x)=11+e−x
在python中利用numpy
模块实现:
# GRADED FUNCTION: sigmoid
import numpy as np
# this means you can access numpy functions by writing np.function() instead of numpy.function()
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 = None
s = 1/(1+np.exp(-x))
### END CODE HERE ###
return s

def sigmoid_derivative(x):
"""
Compute the gradient (also called the slope or derivative) of the sigmoid function with respect to its input x.
You can store the output of the sigmoid function into variables and then use it to calculate the gradient.
Arguments:
x -- A scalar or numpy array
Return:
ds -- Your computed gradient.
"""
### START CODE HERE ### (≈ 2 lines of code)
s = 1 / ( 1 + 1 / np.exp(x))
ds = s * (1 - s)
### END CODE HERE ###
return ds
x = np.array([1, 2, 3])
print ("sigmoid_derivative(x) = " + str(sigmoid_derivative(x)))

输出结果:
sigmoid_derivative(x) = [ 0.19661193 0.10499359 0.04517666]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。