深度学习、sigmoid函数求导

本文详细解析了Sigmoid函数的求导过程,利用两种不同的方法进行了严谨的数学推导,为理解神经网络中反向传播算法的数学基础提供了帮助。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习神经网络的反向传播过程中,涉及到对sigmoid函数进行求导、出来混总要还的。。丢掉的高数还是得捡起来,记录一下sigmoid函数的推导过程吧。

前置准备
  1. 求导的倒数法则:若有 g ( x ) = 1 f ( x ) g(x)=\frac{1}{f(x)} g(x)=f(x)1,则 g ′ ( x ) = − f ′ ( x ) f ( x ) 2 g'(x)=-\frac{f'(x)}{f(x)^2} g(x)=f(x)2f(x)
  2. f ( x ) = e x , f ′ ( x ) = e x f(x) = e^x,f'(x) = e^x f(x)=ex,f(x)=ex
证明1:

g ′ ( x ) = ( 1 f ( x ) ) ′ = lim ⁡ Δ x → 0 1 f ( x + Δ x ) − 1 f ( x ) Δ x = lim ⁡ Δ x → 0 f ( x + Δ x ) − f ( x ) f ( x + Δ x ) f ( x ) Δ x = ( lim ⁡ Δ x → 0 f ( x + Δ x ) − f ( x ) Δ x ) ( lim ⁡ Δ x → 0 1 f ( x + Δ x ) f ( x ) ) g'(x) = (\frac{1}{f(x)})'=\lim_{\Delta x\rightarrow0}\frac{\frac{1}{f(x+\Delta x)}-\frac{1}{f(x)}}{\Delta x}=\lim_{\Delta x\rightarrow0}\frac{f(x+\Delta x)-f(x)}{f(x+\Delta x)f(x)\Delta x}=(\lim_{\Delta x\rightarrow0}\frac{f(x+\Delta x) - f(x)}{\Delta x})(\lim_{\Delta x\rightarrow0}\frac{1}{f(x+\Delta x)f(x)}) g(x)=(f(x)1)=limΔx0Δxf(x+Δx)1f(x)1=limΔx0f(x+Δx)f(x)Δxf(x+Δx)f(x)=(limΔx0Δxf(x+Δx)f(x))(limΔx0f(x+Δx)f(x)1)

由于 x + Δ x x+\Delta x x+Δx 在 点 x x x处连续当 Δ x → 0 \Delta x \rightarrow 0 Δx0的时候、 f ( x + Δ x ) = f ( x ) f(x+\Delta x) = f(x) f(x+Δx)=f(x),有:

1 f ( x + Δ x ) 1 f ( x ) = 1 f ( x ) 2 \frac{1}{f(x+\Delta x)}\frac{1}{f(x)}=\frac{1}{f(x)^2} f(x+Δx)1f(x)1=f(x)21

即:

g ′ ( x ) = ( 1 f ( x ) ) ′ = ( lim ⁡ Δ x → 0 − f ( x + Δ x ) − f ( x ) Δ x ) ( lim ⁡ Δ x → 0 1 f ( x + Δ x ) f ( x ) ) = − f ′ ( x ) f ( x ) 2 g'(x) = (\frac{1}{f(x)})'=(\lim_{\Delta x\rightarrow0}-\frac{f(x+\Delta x) - f(x)}{\Delta x})(\lim_{\Delta x\rightarrow0}\frac{1}{f(x+\Delta x)f(x)})=-\frac{f'(x)}{f(x)^2} g(x)=(f(x)1)=(limΔx0Δxf(x+Δx)f(x))(limΔx0f(x+Δx)f(x)1)=f(x)2f(x)

证明2:

OTL、太难了,推导比较复杂,直接贴大佬的推导过程了,讲的很好的。
参考文章:知乎:为什么e^x 的导数是还是其自身?

好了、开始推导sigmoid函数求导公式

根据公式: S ( x ) = 1 1 + e − x S(x)=\frac{1}{1+e^{-x}} S(x)=1+ex1
S ′ ( x ) = ( 1 1 + e − x ) ′ = − ( 1 + e − x ) ′ ( 1 + e − x ) 2 = 1 1 + e − x 1 + e − x − 1 1 + e − x = 1 1 + e − x ( 1 − 1 1 + e − x ) = S ( x ) ( 1 − S ( x ) ) S'(x)=(\frac{1}{1+e^{-x}})'=-\frac{(1+e^{-x})'}{(1+e^{-x})^2}=\frac{1}{1+e^{-x}}\frac{1+e^{-x}-1}{1+e^{-x}}=\frac{1}{1+e^{-x}}(1-\frac{1}{1+e^{-x}})=S(x)(1-S(x)) S(x)=(1+ex1)=(1+ex)2(1+ex)=1+ex11+ex1+ex1=1+ex1(11+ex1)=S(x)(1S(x))

至此推导完成
### 链式求导法则在深度学习中的解释 链式求导法则是指在一个复合函数中,计算其相对于某个自变量的导数的方法。具体来说,在多层神经网络中,每一层可以看作是一个函数,该函数接受来自前一层的数据并将其传递给下一层。为了更新权重参数,需要知道损失函数关于各层权重的梯度。 #### 反向传播算法利用了这一原则: 当执行反向传播时,从最后一层开始逐步向前推进,每次迭代都会依据当前层误差对本层权重做调整,并将此误差沿网络传播回更早的层次[^1]。这种机制允许有效计算复杂模型结构下的梯度信息而不必显式展开整个表达式树形图。 #### 数学表示形式如下所示: 假设有一个简单的两层感知器作为例子,则第\(l\)层输出\(\mathbf{z}^{(l)}=\sigma (\mathbf{W}^{(l)} \cdot \mathbf{x})+\mathbf{b}\),其中\(\sigma ()\)是非线性激活函数;如果定义最终预测结果为\(\hat{\mathbf{y}}=f_{L}(... f_2(f_1(\mathbf{x}; W); ... ; W))\) ,那么对于任意中间层\(i<L\)处的一个特定单元\(j\)而言,其对应的局部梯度可以通过下面的方式获得: \[ \frac {\partial L}{\partial w_j}= \sum_k \left[\frac {\partial z_k }{\partial a_j }\right]\times \left [\prod _m ^k \frac {da_m }{dz_m }\right ]\times \frac {\partial L}{\partial z_K }, \] 这里 \(K>L, k>m>j,\) 并且 \(a,b,z,W,L\)分别代表激活值、偏差项、带权输入、权重矩阵以及损失函数[^2]。 ```python import numpy as np def sigmoid(x): return 1 / (1 + np.exp(-x)) def derivative_sigmoid(x): fx = sigmoid(x) return fx * (1 - fx) # 假设这是我们的简单三层网络(不考虑bias) weights_input_hidden = np.random.randn(3, 4) weights_hidden_output = np.random.randn(4, 2) input_data = np.array([0.5, 0.7, 0.9]) target_output = np.array([0.1, 0.8]) hidden_layer_activation = sigmoid(np.dot(input_data, weights_input_hidden)) predicted_output = sigmoid(np.dot(hidden_layer_activation, weights_hidden_output)) error = target_output - predicted_output d_predicted_output = error * derivative_sigmoid(predicted_output) # 应用链式法则来计算隐藏层的误差贡献 error_hidden_layer = d_predicted_output.dot(weights_hidden_output.T) d_hidden_layer = error_hidden_layer * derivative_sigmoid(hidden_layer_activation) learning_rate = 0.05 # 更新权重 weights_hidden_output += hidden_layer_activation.T.dot(d_predicted_output) * learning_rate weights_input_hidden += input_data.reshape((3,1)).dot(d_hidden_layer.reshape((1,4))) * learning_rate ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逆行的小白菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值