NNDL 作业11 LSTM

习题6-4 推导LSTM网络中参数的梯度, 并分析其避免梯度消失的效果

LSTM网络的参数梯度计算过程可以分为两个步骤:前向传播和反向传播。

前向传播

  1. 遗忘门(Forget Gate): f_t 

  2. 输入门(Input Gate): i_t 

  3. 新的候选细胞状态(New Candidate Cell State): C_t~

  4. 细胞状态(Cell State)更新: C_t

  5. 输出门(Output Gate): O_t 

  6. 隐藏状态(Hidden State)更新: h_t 

反向传播

又有:△ct=ot+1⊙[1−tanh2(ct+1)]

所以

所以

避免梯度消失:

  1. cell的记忆单元可以存储并传递长期的信息。通过使用门控机制,LSTM能够选择性地遗忘或更新记忆单元中的信息,从而更好地保留重要的信息,减少信息的丢失。

  2. 门控单元使用sigmoid函数来控制信息的流动,并且通过乘法操作将信息进行筛选,可以有效地限制信息的传递,避免梯度过小或过大。

习题6-3P 编程实现下图LSTM运行过程

 1. 使用Numpy实现LSTM算子

import numpy as np
 
x = np.array([[1, 0, 0, 1],
              [3, 1, 0, 1],
              [2, 0, 0, 1],
              [4, 1, 0, 1],
              [2, 0, 0, 1],
              [1, 0, 1, 1],
              [3, -1, 0, 1],
              [6, 1, 0, 1],
              [1, 0, 1, 1]])
# x = np.array([
#               [3, 1, 0, 1],
#
#               [4, 1, 0, 1],
#               [2, 0, 0, 1],
#               [1, 0, 1, 1],
#               [3, -1, 0, 1]])
inputGate_W = np.array([0, 100, 0, -10])
outputGate_W = np.array([0, 0, 100, -10])
forgetGate_W = np.array([0, 100, 0, 10])
c_W = np.array([1, 0, 0, 0])
 
 
def sigmoid(x):
    y = 1 / (1 + np.exp(-x))
    if y >= 0.5:
        return 1
    else:
        return 0
 
 
temp = 0
y = []
c = []
for input in x:
    c.append(temp)
    temp_c = np.sum(np.multiply(input, c_W))
    temp_input = sigmoid(np.sum(np.multiply(input, inputGate_W)))
    temp_forget = sigmoid(np.sum(np.multiply(input, forgetGate_W)))
    temp_output = sigmoid(np.sum(np.multiply(input, outputGate_W)))
    temp = temp_c * temp_input + temp_forget * temp
    y.append(temp_output * temp)
print("memory:",c)
print("y     :",y)

2. 使用nn.LSTMCell实现

PyTorch - torch.nn.LSTMCell

import  torch
from  torch import nn
import numpy as np
 
print('one layer lstm')
cell=nn.LSTMCell(input_size=100, hidden_size=20)
h=torch.zeros(3,20)
c=torch.zeros(3,20)
x = torch.randn(10,3,100)
for xt in x: 
	h,c = cell(xt, [h,c])
 
print('h.shape: ',h.shape)
print('c.shape: ',c.shape)

3. 使用nn.LSTM实现

PyTorch - torch.nn.LSTM

 

import  torch
from  torch import nn
 
lstm = nn.LSTM(input_size=512, hidden_size=256, num_layers=2, batch_first=True)
print(lstm)
x = torch.randn(40,25,512)
output,(h_n,c_n) = lstm(x)
print(output.shape,h_n.shape,c_n.shape)

李宏毅机器学习笔记:RNN循环神经网络_李宏毅rnn笔记_ZEERO~的博客-CSDN博客​编辑https://blog.csdn.net/weixin_43249038/article/details/132650998

L5W1作业1 手把手实现循环神经网络-CSDN博客​编辑https://blog.csdn.net/segegse/article/details/127708468[干货]深入浅出LSTM及其Python代码实现-腾讯云开发者社区-腾讯云 (tencent.com)

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值