| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 1 个月前 | ||
| 1 个月前 | ||
| 1 个月前 | ||
| 1 个月前 | ||
| 2 个月前 | ||
| 6 个月前 | ||
| 2 个月前 |
SingleLayerLstmGrad
产品支持情况
| 产品 | 是否支持 |
|---|---|
| Ascend 950PR/Ascend 950DT | × |
| Atlas A3 训练系列产品/Atlas A3 推理系列产品 | √ |
| Atlas A2 训练系列产品/Atlas A2 推理系列产品 | √ |
| Atlas 200I/500 A2 推理产品 | × |
| Atlas 推理系列产品 | × |
| Atlas 训练系列产品 | × |
功能说明
-
算子功能:单层单向LSTM的反向传播,计算正向输入x、权重w、偏置b、初始隐藏状态initH与初始细胞状态initC的梯度。
-
计算公式:
单层LSTM反向传播计算
前向传播公式
| 组件 | 公式 |
|---|---|
| 输入拼接 | zt=[ht−1xt]\mathbf{z}_t = \begin{bmatrix} \mathbf{h}_{t-1} \\ \mathbf{x}_t \end{bmatrix} |
| 遗忘门 | ft=σ(Wfzt+bf)\mathbf{f}_t = \sigma(\mathbf{W}_f \mathbf{z}_t + \mathbf{b}_f) |
| 输入门 | it=σ(Wizt+bi)\mathbf{i}_t = \sigma(\mathbf{W}_i \mathbf{z}_t + \mathbf{b}_i) |
| 候选状态 | gt=tanh(Wgzt+bc)\mathbf{g}_t = \tanh(\mathbf{W}_g \mathbf{z}_t + \mathbf{b}_c) |
| 输出门 | ot=σ(Wozt+bo)\mathbf{o}_t = \sigma(\mathbf{W}_o \mathbf{z}_t + \mathbf{b}_o) |
| 细胞状态 | ct=ft⊙ct−1+it⊙gt\mathbf{c}_t = \mathbf{f}_t \odot \mathbf{c}_{t-1} + \mathbf{i}_t \odot \mathbf{g}_t |
| 隐藏状态 | ht=ot⊙tanh(ct)\mathbf{h}_t = \mathbf{o}_t \odot \tanh(\mathbf{c}_t) |
其中:
- σ\sigma 是sigmoid函数
- ⊙\odot 表示逐元素乘法(Hadamard product)
- W∗W_* 是可学习的权重矩阵
- b∗b_* 是可学习的偏置项
反向传播变量定义
- 总损失:L=∑t=1TLtL = \sum_{t=1}^{T} L_t
- 隐藏状态梯度:δht=∂L∂ht\delta\mathbf{h}_t = \frac{\partial L}{\partial \mathbf{h}_t}
- 细胞状态梯度:δct=∂L∂ct\delta\mathbf{c}_t = \frac{\partial L}{\partial \mathbf{c}_t}
反向传播算法(时间步 t→t−1t \rightarrow t-1)
初始化
δhT=0,δcT=0,fT=0\delta\mathbf{h}_{T} = \mathbf{0}, \quad \delta\mathbf{c}_{T} = \mathbf{0}, \quad \mathbf{f}_{T} = \mathbf{0}
循环 t=T−1t = T - 1 到 00
-
当前隐藏状态梯度
δht=∂Lt∂ht+δhnext\delta\mathbf{h}_t = \frac{\partial L_t}{\partial \mathbf{h}_t} + \delta\mathbf{h}_{\text{next}}
-
当前细胞状态梯度
δct=δht⊙ot⊙(1−tanh2(ct))+δcnext⊙fnext\delta\mathbf{c}_t = \delta\mathbf{h}_t \odot \mathbf{o}_t \odot (1 - \tanh^2(\mathbf{c}_t)) + \delta\mathbf{c}_{\text{next}} \odot \mathbf{f}_{\text{next}}
-
门控梯度计算
δot=δht⊙tanh(ct)⊙ot⊙(1−ot)\delta\mathbf{o}_t = \delta\mathbf{h}_t \odot \tanh(\mathbf{c}_t) \odot \mathbf{o}_t \odot (1 - \mathbf{o}_t)
δgt=δct⊙it⊙(1−gt2)\delta\mathbf{g}_t = \delta\mathbf{c}_t \odot \mathbf{i}_t \odot (1 - \mathbf{g}_t^2)
δit=δct⊙gt⊙it⊙(1−it)\delta\mathbf{i}_t = \delta\mathbf{c}_t \odot \mathbf{g}_t \odot \mathbf{i}_t \odot (1 - \mathbf{i}_t)
δft=δct⊙ct−1⊙ft⊙(1−ft)\delta\mathbf{f}_t = \delta\mathbf{c}_t \odot \mathbf{c}_{t-1} \odot \mathbf{f}_t \odot (1 - \mathbf{f}_t)
-
参数梯度累加
∂L∂Wf+=δftzt⊤\frac{\partial L}{\partial \mathbf{W}_f} \mathrel{+}= \delta\mathbf{f}_t \mathbf{z}_t^\top
∂L∂bf+=δft\frac{\partial L}{\partial \mathbf{b}_f} \mathrel{+}= \delta\mathbf{f}_t
∂L∂Wi+=δitzt⊤\frac{\partial L}{\partial \mathbf{W}_i} \mathrel{+}= \delta\mathbf{i}_t \mathbf{z}_t^\top
∂L∂bi+=δit\frac{\partial L}{\partial \mathbf{b}_i} \mathrel{+}= \delta\mathbf{i}_t
∂L∂Wg+=δgtzt⊤\frac{\partial L}{\partial \mathbf{W}_g} \mathrel{+}= \delta\mathbf{g}_t \mathbf{z}_t^\top
∂L∂bg+=δgt\frac{\partial L}{\partial \mathbf{b}_g} \mathrel{+}= \delta\mathbf{g}_t
∂L∂Wo+=δotzt⊤\frac{\partial L}{\partial \mathbf{W}_o} \mathrel{+}= \delta\mathbf{o}_t \mathbf{z}_t^\top
∂L∂bo+=δot\frac{\partial L}{\partial \mathbf{b}_o} \mathrel{+}= \delta\mathbf{o}_t
-
传播到前一时刻
δzt=Wf⊤δft+Wi⊤δit+Wg⊤δgt+Wo⊤δot \delta\mathbf{z}_t = \mathbf{W}_f^\top \delta\mathbf{f}_t + \mathbf{W}_i^\top \delta\mathbf{i}_t + \mathbf{W}_g^\top \delta\mathbf{g}_t + \mathbf{W}_o^\top \delta\mathbf{o}_t
δhprev=δzt[1:dim(ht−1)]\delta\mathbf{h}_{\text{prev}} = \delta\mathbf{z}_t[1:\dim(\mathbf{h}_{t-1})]
δcprev=δct⊙ft\delta\mathbf{c}_{\text{prev}} = \delta\mathbf{c}_t \odot \mathbf{f}_t
-
更新传播变量
δhnext←δhprev\delta\mathbf{h}_{\text{next}} \leftarrow \delta\mathbf{h}_{\text{prev}}
δcnext←δcprev\delta\mathbf{c}_{\text{next}} \leftarrow \delta\mathbf{c}_{\text{prev}}
fnext←ft\mathbf{f}_{\text{next}} \leftarrow \mathbf{f}_t
梯度计算原理
细胞状态梯度推导
δct=∂L∂ht∂ht∂ct+∂L∂ct+1∂ct+1∂ct\delta\mathbf{c}_t = \frac{\partial L}{\partial \mathbf{h}_t} \frac{\partial \mathbf{h}_t}{\partial \mathbf{c}_t} + \frac{\partial L}{\partial \mathbf{c}_{t+1}} \frac{\partial \mathbf{c}_{t+1}}{\partial \mathbf{c}_t}
其中:
∂ht∂ct=ot⊙(1−tanh2(ct))\frac{\partial \mathbf{h}_t}{\partial \mathbf{c}_t} = \mathbf{o}_t \odot (1 - \tanh^2(\mathbf{c}_t))
∂ct+1∂ct=ft+1\frac{\partial \mathbf{c}_{t+1}}{\partial \mathbf{c}_t} = \mathbf{f}_{t+1}
遗忘门梯度推导
δft=∂L∂aft=δct⊙ct−1⊙ft⊙(1−ft)\delta\mathbf{f}_t = \frac{\partial L}{\partial \mathbf{a}_f^t} = \delta\mathbf{c}_t \odot \mathbf{c}_{t-1} \odot \mathbf{f}_t \odot (1 - \mathbf{f}_t)
参数梯度推导
∂L∂Wf=∑t=1Tδftzt⊤\frac{\partial L}{\partial \mathbf{W}_f} = \sum_{t=1}^{T} \delta\mathbf{f}_t \mathbf{z}_t^\top
LSTM梯度流动特性 长程依赖处理
∂cT∂c1=∏k=2Tfk(对角矩阵)\frac{\partial \mathbf{c}_T}{\partial \mathbf{c}_1} = \prod_{k=2}^{T} \mathbf{f}_k \quad \text{(对角矩阵)}
- 参数说明:
参数名 输入/输出/属性 描述 数据类型 数据格式 x 输入 - 表示LSTM输入的序列,公式中的x。
- shape为[T,batch,input_size]。
FLOAT、FLOAT16 ND w 输入 - 表示LSTM的权重,对应公式中的W。
- 包含wi与wh;shape为[4*hidden_size, input_size+hidden_size]。
FLOAT、FLOAT16 ND b 可选输入 - 表示LSTM的偏置,对应公式中的b。
- shape为[4*hidden_size]。
FLOAT、FLOAT16 ND inith 输入 - 表示LSTM的初始hidden状态,对应公式中在t=0时的h(t-1)。
- shape为[1, Batch, hidden_size]。
FLOAT、FLOAT16 ND initc 输入 - 表示LSTM的初始cell状态,对应公式中在t=0时的c(t-1)
- shape为[1, Batch, hidden_size]。
FLOAT、FLOAT16 ND dy 输入 - 表示LSTM正向中输出隐藏状态hidden的梯度,对应公式中的δh。
- shape为[T, Batch, hidden_size]。
FLOAT、FLOAT16 ND dh 输入 - 表示LSTM正向中输出隐藏状态hidden在T时刻的梯度,对应公式中T时刻的δh(t)。
- shape为[1, Batch, hidden_size]。
FLOAT、FLOAT16 ND dc 可选输入 - 表示LSTM正向中输出细胞状态cell的梯度,对应公式中T时刻的δc(t)。
- shape为[1, Batch, hidden_size]。
FLOAT、FLOAT16 ND i 输入 - 表示LSTM正向中输出的输入门的激活值,对应公式中的i。
- shape为[T, Batch, hidden_size]。
FLOAT、FLOAT16 ND j 输入 - 表示LSTM正向中输出的候选cell状态的tanh输出,对应公式中的g。
- shape为[T, Batch, hidden_size]。
FLOAT、FLOAT16 ND f 输入 - 表示LSTM正向中输出的遗忘门的激活值,对应公式中的f。
- shape为[T, Batch, hidden_size]。
FLOAT、FLOAT16 ND o 输入 - 表示LSTM正向中输出的输出门的激活值,对应公式中的o。
- shape为[T, Batch, hidden_size]。
FLOAT、FLOAT16 ND h 输入 - 表示LSTM正向中输出的隐藏hidden状态,对应公式中的h。
- shape为[T, Batch, hidden_size]。
FLOAT16、FLOAT32 ND c 输入 - 表示LSTM正向中输出的最终cell状态,对应公式中的c。
- shape为[T, Batch, hidden_size]。
FLOAT16、FLOAT32 ND tanhc 输入 - 表示LSTM正向中输出的最终cell状态经过tanh激活函数后的输出,对应公式中的tanh(c)。
- shape为[T, Batch, hidden_size]。
FLOAT16、FLOAT32 ND seq_length 可选输入 - 表示实际序列长度对应的掩码矩阵。
- shape为[T, Batch, hidden_size]。
FLOAT16、FLOAT32 ND dw 输出 - 表示LSTM输入权重上的梯度,对应公式中的∂L/∂W。
- shape为[4 * hidden_size,input_size+hidden_size]。
FLOAT16、FLOAT32 ND db 输出 - 表示LSTM输入偏置上的梯度,对应公式中的∂L/∂b。
- shape为[4, hidden_size]。
FLOAT16、FLOAT32 ND dx 输出 - 表示LSTM输入序列x上的梯度,对应公式中的δx。
- shape为[T, Batch, input_size]。
FLOAT16、FLOAT32 ND dh_prev 输出 - 表示LSTM输入inith的梯度,对应公式中在t=0时的δh_prev。
- shape为[1, Batch, hidden_size]。
FLOAT16、FLOAT32 ND dc_prev 输出 - 表示LSTM输入initc的梯度,对应公式中在t=0时的δc_prev。
- shape为[1, Batch, hidden_size]。
FLOAT16、FLOAT32 ND direction 属性 LSTM循环迭代方向。仅支持"UNIDIRECTIONAL"与"REDIRECTIONAL"。默认"UNIDIRECTIONAL"表示前向,"REDIRECTIONAL"表示反向。 STRING - gate_order 属性 ijfo的排布顺序。支持"ijfo"或"ifjo"。 STRING -
约束说明
无
调用说明
| 调用方式 | 样例代码 | 说明 |
|---|---|---|
| aclnn接口 | test_aclnn_single_layer_lstm_grad.cpp | 通过aclnnLstmBackward接口方式调用SingleLayerLstmGrad算子。 |