手把手构建LSTM的向前传播(Building a LSTM step by step)
- 阀门和状态描述
- LSTM cell
- LSTM整个过程
- 遗忘门,更新门,输出门的作用是什么,它们是怎么发挥作用的。
- 单元状态 cell state 是如何来选择性保留信息。

- 假设我们正在阅读一段文本中的单词,并计划使用LSTM跟踪语法结构,例如判断主体是单数(“ puppy”)还是复数(“ puppies”)。
- 如果主体更改其状态(从单数词更改为复数词),那么先前的记忆状态将过时,因此我们“忘记”过时的状态。
- “遗忘门”是一个张量,它包含介于0和1之间的值。
- 如果遗忘门中的一个单元的值接近于0,则LSTM将“忘记”之前单元状态相应单位的存储值。
- 如果遗忘门中的一个单元的值接近于1,则LSTM将记住大部分相应的值。

- 包含控制遗忘门行为的权重。
- 之前时间步长的隐藏状态和当前时间步长的输入连接在一起乘以。
- sigmoid函数让每个门的张量值在0到1之间。
- 遗忘门和之前的单元状态有相同的shape。
- 这就意味着它们可以按照元素相乘。
- 将张量和相乘相当于在之前的单元状态应用一层蒙版。
- 如果中的单个值是0或者接近于0,那么乘积就接近0.
- 这就是使得存储在对应单位的值在下一个时间步长不会被记住。
- 同样,如果中的1个值接近于1,那么乘积就接近之前单元状态的原始值。
- LSTM就会在下一个时间步长中保留对应单位的值。
- Wf: 遗忘门的权重
- Wb: 遗忘门的偏差
- ft: 遗忘门
- 候选值是包含当前时间步长信息的张量,它可能会存储在当前单元状态中。
- 传递候选值的哪些部分取决于更新门。
- 候选值是一个张量,它的范围从-1到1。
- 代字号“〜”用于将候选值与单元状态区分开。

- 'tanh'函数产生的值介于-1和+1之间。
- cct: 候选值
- 我们使用更新门来确定候选的哪些部分要添加到单元状态中。
- 更新门是包含0到1之间值的张量。
- 当更新门中的单位接近于0时,它将阻止候选值中的相应值传递到。
- 当更新门中的单位接近1时,它允许将候选的值传递到。
- 注意,我们使用下标“i”而不是“u”来遵循文献中使用的约定。

- 类似于遗忘门(此处为),用sigmoid函数乘后值就落在了0到1之间。
- 将更新门与候选元素逐元素相乘,并将此乘积()用于确定单元状态。
- wi是更新门的权重
- bi是更新门的偏差
- it是更新门
- 单元状态是传递到未来时间步长的“记忆/内存(memory)”。
- 新单元状态是先前单元状态和候选值的组合。

- 之前的单元状态通过遗忘门调整(加权)。
- 候选值通过更新门调整(加权)。
- c: 单元状态,包含所有的时间步长,c的shape是(na, m, T)
- c_next: 下一个时间步长的单元状态,的shape (na, m)
- c_prev: 之前的单元状态,的shape (na, m)

- 输出门决定时间步长要输出的预测值。
- 输出门与其他门一样,它包含从0到1的值。
- 输出门由之前的隐藏状态和当前的输入 决定。
- sigmoid函数让值的范围在0到1之间。
- wo: 输出门的权重
- bo: 输出门的偏差
- ot: 输出门
- 隐藏状态将传递到LSTM单元的下一个时间步长。
- 它用于确定下一个时间步长的三个门()。
- 隐藏状态也用于预测。

- 隐藏状态由单元状态结合输出门确定。
- 单元状态通过“ tanh”函数把值缩放到-1和+1之间。
- 输出门的作用就像一个“掩码mask”,它既可以保留的值,也可以使这些值不包含在隐藏状态中。
- a: 隐藏状态,包含时间步长,shape (na, m, Tx)
- a_prev: 前一步的隐藏状态,的shape (na, m)
- a_next: 下一步的隐藏状态,的shape (na, m)
- 此用例的预测是分类,所以我们用softmax。

- y_pred: 预测,包含所有的时间步长,的shape (ny, m, Tx),注意,本例中Tx=Ty。
- yt_pred: 当前时间步长t的预测值,shape是(ny, m)
def lstm_cell_forward(xt, a_prev, c_prev, parameters):
"""
Implement a single forward step of the LSTM-cell as described in Figure (4)
Arguments:
xt -- your input data at timestep "t", numpy array of shape (n_x, m).
a_prev -- Hidden state at timestep "t-1", numpy array of shape (n_a, m)
c_prev -- Memory state at timestep "t-1", numpy array of shape (n_a, m)
parameters -- python dictionary containing:
Wf -- Weight matrix of the forget gate, numpy array of shape (n_a, n_a + n_x)
bf -- Bias of the forget gate, numpy array of shape (n_a, 1)
Wi -- Weight matrix of the update gate, numpy array of shape (n_a, n_a + n_x)
bi -- Bias of the update gate, numpy array of shape (n_a, 1)
Wc -- Weight matrix of the first "tanh", numpy array of shape (n_a, n_a + n_x)
bc -- Bias of the first "tanh", numpy array of shape (n_a, 1)
Wo -- Weight matrix of the output gate, numpy array of shape (n_a, n_a + n_x)
bo -- Bias of the output gate, numpy array of shape (n_a, 1)
Wy -- Weight matrix relating the hidden-state to the output, numpy array of shape (n_y, n_a)
by -- Bias relating the hidden-state to the output, numpy array of shape (n_y, 1)
Returns:
a_next -- next hidden state, of shape (n_a, m)
c_next -- next memory state, of shape (n_a, m)
yt_pred -- prediction at timestep "t", numpy array of shape (n_y, m)
cache -- tuple of values needed for the backward pass, contains (a_next, c_next, a_prev, c_prev, xt, parameters)
Note: ft/it/ot stand for the forget/update/output gates, cct stands for the candidate value (c tilde),
c stands for the cell state (memory)
"""
# 从 "parameters" 中取出参数。
Wf = parameters["Wf"] # 遗忘门权重
bf = parameters["bf"]
Wi = parameters["Wi"] # 更新门权重 (注意变量名下标是i不是u哦)
bi = parameters["bi"] # (notice the variable name)
Wc = parameters["Wc"] # 候选值权重
bc = parameters["bc"]
Wo = parameters["Wo"] # 输出门权重
bo = parameters["bo"]
Wy = parameters["Wy"] # 预测值权重
by = parameters["by"]
# 连接 a_prev 和 xt
concat = np.concatenate((a_prev, xt), axis=0)
# 等价于下面代码
# 从 xt 和 Wy 中取出维度
# n_x, m = xt.shape
# n_y, n_a = Wy.shape
# concat = np.zeros((n_a + n_x, m))
# concat[: n_a, :] = a_prev
# concat[n_a :, :] = xt
# 计算 ft (遗忘门), it (更新门)的值
# cct (候选值), c_next (单元状态),
# ot (输出门), a_next (隐藏单元)
ft = sigmoid(np.dot(Wf, concat) + bf) # 遗忘门
it = sigmoid(np.dot(Wi, concat) + bi) # 更新门
cct = np.tanh(np.dot(Wc, concat) + bc) # 候选值
c_next = ft * c_prev + it * cct # 单元状态
ot = sigmoid(np.dot(Wo, concat) + bo) # 输出门
a_next = ot * np.tanh(c_next) # 隐藏状态
# 计算LSTM的预测值
yt_pred = softmax(np.dot(Wy, a_next) + by)
# 用于反向传播的缓存
cache = (a_next, c_next, a_prev, c_prev, ft, it, cct, ot, xt, parameters)
return a_next, c_next, yt_pred, cache

- 从变量x 和 parameters中获得 的维度。
- 初始化三维张量 , 和 .
- : 隐藏状态, shape
- : 单元状态, shape
- : 预测, shape (注意在这个例子里 ).
- 注意 将一个变量设置来和另一个变量相等是"按引用复制". 换句话说,就是不用使用c = a, 否则这两个变量指的是同一个变量,更改任何其中一个变量另一个变量的值都会跟着变。
- 初始化二维张量
- 储存了t时间步长的隐藏状态,它的变量名是a_next。
- , 时间步长0时候的初始隐藏状态,调用该函数时候传入的值,它的变量名是a0。
- 和 代表单个时间步长,所以他们的shape都是
- 通过传入函数的初始化隐藏状态来初始化 。
- 用0来初始化 。
- 变量名是 c_next.
- 表示单个时间步长, 所以它的shape是
- 注意: create c_next as its own variable with its own location in memory. 不要将它通过3维张量的切片来初始化,换句话说, 不要 c_next = c[:,:,0].
- 对每个时间步长,做以下事情:
- 从3维的张量 中, 获取在时间步长t处的2维切片 。
- 调用你之前定义的 lstm_cell_forward 函数,获得隐藏状态,单元状态,预测值。
- 存储隐藏状态,单元状态,预测值到3维张量中。
- 把缓存加入到缓存列表。
def lstm_forward(x, a0, parameters):
"""
Implement the forward propagation of the recurrent neural network using an LSTM-cell described in Figure (4).
Arguments:
x -- Input data for every time-step, of shape (n_x, m, T_x).
a0 -- Initial hidden state, of shape (n_a, m)
parameters -- python dictionary containing:
Wf -- Weight matrix of the forget gate, numpy array of shape (n_a, n_a + n_x)
bf -- Bias of the forget gate, numpy array of shape (n_a, 1)
Wi -- Weight matrix of the update gate, numpy array of shape (n_a, n_a + n_x)
bi -- Bias of the update gate, numpy array of shape (n_a, 1)
Wc -- Weight matrix of the first "tanh", numpy array of shape (n_a, n_a + n_x)
bc -- Bias of the first "tanh", numpy array of shape (n_a, 1)
Wo -- Weight matrix of the output gate, numpy array of shape (n_a, n_a + n_x)
bo -- Bias of the output gate, numpy array of shape (n_a, 1)
Wy -- Weight matrix relating the hidden-state to the output, numpy array of shape (n_y, n_a)
by -- Bias relating the hidden-state to the output, numpy array of shape (n_y, 1)
Returns:
a -- Hidden states for every time-step, numpy array of shape (n_a, m, T_x)
y -- Predictions for every time-step, numpy array of shape (n_y, m, T_x)
c -- The value of the cell state, numpy array of shape (n_a, m, T_x)
caches -- tuple of values needed for the backward pass, contains (list of all the caches, x)
"""
# 初始化 "caches", 用来存储每个时间步长的cache值的
caches = [] Wy = parameters['Wy']
# 从 x 和 parameters['Wy'] 的shape中获取纬度值
n_x, m, T_x = x.shape
n_y, n_a = Wy.shape
# 初始化 "a", "c" and "y"
a = np.zeros((n_a, m, T_x))
c = np.zeros((n_a, m, T_x))
y = np.zeros((n_y, m, T_x))
# 初始化 a_next and c_next
a_next = a0
c_next = np.zeros(a_next.shape)
# loop over all time-steps
for t in range(T_x):
# 从3维张量x中获取t时间步长的2维张量xt
xt = x[:, :, t]
# 更新下一个时间步长的隐藏状态, 下一个单元状态, 计算预测值
a_next, c_next, yt, cache = lstm_cell_forward(xt, a_next, c_next, parameters)
# 把下一个时间步长长的隐藏状态保存起来
a[:,:,t] = a_next
# 把下一个时间步长长的单元状态保存起来
c[:,:,t] = c_next
# 把预测值保存起来
y[:,:,t] = yt
# 保存缓存值
caches.append(cache) # 用于向后传播
caches = (caches, x)
return a, y, c, caches
手把手构建LSTM的向前传播(Building a LSTM step by step)的更多相关文章
- 用纯Python实现循环神经网络RNN向前传播过程(吴恩达DeepLearning.ai作业)
Google TensorFlow程序员点赞的文章! 前言 目录: - 向量表示以及它的维度 - rnn cell - rnn 向前传播 重点关注: - 如何把数据向量化的,它们的维度是怎么来的 ...
- 使用PyTorch构建神经网络以及反向传播计算
使用PyTorch构建神经网络以及反向传播计算 前一段时间南京出现了疫情,大概原因是因为境外飞机清洁处理不恰当,导致清理人员感染.话说国外一天不消停,国内就得一直严防死守.沈阳出现了一例感染人员,我在 ...
- 课程五(Sequence Models),第一 周(Recurrent Neural Networks) —— 1.Programming assignments:Building a recurrent neural network - step by step
Building your Recurrent Neural Network - Step by Step Welcome to Course 5's first assignment! In thi ...
- 课程一(Neural Networks and Deep Learning),第四周(Deep Neural Networks)——2.Programming Assignments: Building your Deep Neural Network: Step by Step
Building your Deep Neural Network: Step by Step Welcome to your third programming exercise of the de ...
- Sequence Models Week 1 Building a recurrent neural network - step by step
Building your Recurrent Neural Network - Step by Step Welcome to Course 5's first assignment! In thi ...
- Neural Networks and Deep Learning(week4)Building your Deep Neural Network: Step by Step
Building your Deep Neural Network: Step by Step 你将使用下面函数来构建一个深层神经网络来实现图像分类. 使用像relu这的非线性单元来改进你的模型 构建 ...
- 精通initramfs构建step by step
(一)hello world 一.initramfs是什么 在2.6版本的linux内核中,都包含一个压缩过的cpio格式 的打包文件.当内核启动时,会从这个打包文件中导出文件到内核的rootfs ...
- DeepLearningDTU: Building a RNN step by step
exercise 5: Week 5 - Recurrent Neural Networks Building your Recurrent Neural Network - Step by Step
- [2] TensorFlow 向前传播算法(forward-propagation)与反向传播算法(back-propagation)
TensorFlow Playground http://playground.tensorflow.org 帮助更好的理解,游乐场Playground可以实现可视化训练过程的工具 TensorFlo ...
随机推荐
- Windows server 2008 r2下安装sqlserver2012
在微软官网上下载sqlserver2012镜像文件:用Alcohol 120%软件进行驱动安装. 如果C盘的容量不够的话,上边的路径可以修改
- 【转】Android Monkey 命令行可用的全部选项
常规 事件 约束限制 调试 原文参见:http://www.douban.com/note/257030384/ 常规 –help 列出简单的用法. -v 命令行的每一个 -v 将增加反馈信息的级别. ...
- 在Docker中运行gocd
gocd是一个持续集成的工具,可视化效果非常好 运行gocd-server 12345 docker run -d --name server -p8153:8153 -p8154:8154 -v / ...
- 初学Qt——程序打包(环境vs2012+qt5.1.0)
说来可笑,网上那么多的教程,偏偏结尾的时候就没有一个能详细的讲下关于程序的发布.开发Qt是这样,刚开始做web也是这样,因为是自学的,所以都没人可以教下,结果到了项目完成的最后总是要花费成吨的时间去查 ...
- 简单的员工管理系统(Mysql+jdbc+Servlet+JSP)
员工管理系统 因为学业要求,需要完成一个过关检测,但是因为检测之前没有做好准备,且想到之前用mysql+jdbc+Struts2+bootstrap做成了一个ATM系统(主要有对数据的增删改查操作), ...
- Java入门教程十二(集合与泛型)
在 Java 中数组的长度是不可修改的.然而在实际应用的很多情况下,无法确定数据数量.这些数据不适合使用数组来保存,这时候就需要使用集合. Java 的集合就像一个容器,用来存储 Java 类的对象. ...
- python自己做计算器
题目: exp = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) ...
- Springboot 2.2.x 默认不支持put、delete等请求方式
springboot 2.2.x 默认不支持put delete等请 原因:springboot默认关闭了对它们的支持,只要在application.properties里面打开即可 spring.m ...
- Flutter01-学习准备
1. 简介: Flutter是谷歌的移动UI框架,可以快速在iOS和Android上构建高质量的原生用户界面. Flutter可以与现有的代码一起工作.在全世界,Flutter正在被越来越多的开发者和 ...
- 不要写很酷但同事看不懂的Java代码
你好呀,我是沉默王二,一个和黄家驹一样身高,和刘德华一样颜值的程序员.为了提高 Java 编程的技艺,我最近在 GitHub 上学习一些高手编写的代码.下面这一行代码(出自大牛之手)据说可以征服你的朋 ...