当前位置:   article > 正文

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

sigmoid的梯度

背景:

求解z=σ(z)z=σ(z)的梯度
由于sigmoid(x)=11+exsigmoid(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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

求对应的导数

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

那这个是怎么推导的呢?
σ(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)))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

输出结果:

sigmoid_derivative(x) = [ 0.19661193 0.10499359 0.04517666]
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/351791
推荐阅读
相关标签
  

闽ICP备14008679号