DeepLearing学习笔记-Sigmoid函数的梯度

背景:

求解 z=σ(z) 的梯度
由于 sigmoid(x)=11+ex
在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

求对应的导数

sigmoid_derivative(x)=σ(x)=σ(x)(1σ(x))(1)

那这个是怎么推导的呢?
σ(x)=11+ex
另临时变量 t=1+ex ,通过复合函数的求导法则,所以 σ(x)=(t1)t=t2(ex)=1(1+ex)2ex=11+ex(ex1+ex)=11+ex(1+ex11+ex)=11+ex(111+ex)=σ(x)(1σ(x))
得证!

python实现

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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值