从rnn到lstm,再到seq2seq(一)
rnn的的公式很简单:

对于每个时刻,输入上一个时刻的隐层s和这个时刻的文本x,然后输出这个时刻的隐层s。对于输出的隐层s 做个ws+b就是这个时刻的输出y。
tf.scan(fn, elems, initializer) # scan operation
def fn(st_1, xt): # recurrent function
st = f(st_1, xt)
return st
rnn的实现:
def step(hprev, x):
# initializer
xav_init = tf.contrib.layers.xavier_initializer
# params
W = tf.get_variable('W', shape=[state_size, state_size], initializer=xav_init())
U = tf.get_variable('U', shape=[state_size, state_size], initializer=xav_init())
b = tf.get_variable('b', shape=[state_size], initializer=tf.constant_initializer(0.))
# current hidden state
h = tf.tanh(tf.matmul(hprev, W) + tf.matmul(x,U) + b)
return h
states = tf.scan(step,
tf.transpose(rnn_inputs, [1,0,2]),
initializer=init_state)
lstm只是网络结构上个对rnn进行改进,它同时增加一个单元叫做state状态,每个lstm有个hidden和一个state。
下面图中h就是隐层,下面图中的c就是状态。首先根据这个时刻的输入x和上个时刻的隐层算出三个门,f(forget),i(input),o(ouput)
激活函数是sigmoid函数,输出0或者1。算出来的f门是来控制上个状态多少被忘记。算出来的i门来控制这个时刻状态的多少被输入。
本时刻的状态由这个时刻的输入x和上个时刻的隐层算出然后用tan函数激活(对应第四行公式)。
本时刻隐层的输出h是由本时刻的状态用tan来激活,然后乘以输出门

看看lstm的实现:
def step(prev, x):
# gather previous internal state and output state
st_1, ct_1 = tf.unpack(prev)
####
# GATES
#
# input gate
i = tf.sigmoid(tf.matmul(x,U[0]) + tf.matmul(st_1,W[0]))
# forget gate
f = tf.sigmoid(tf.matmul(x,U[1]) + tf.matmul(st_1,W[1]))
# output gate
o = tf.sigmoid(tf.matmul(x,U[2]) + tf.matmul(st_1,W[2]))
# gate weights
g = tf.tanh(tf.matmul(x,U[3]) + tf.matmul(st_1,W[3]))
###
# new internal cell state
ct = ct_1*f + g*i
# output state
st = tf.tanh(ct)*o
return tf.pack([st, ct])
###
# here comes the scan operation; wake up!
# tf.scan(fn, elems, initializer)
states = tf.scan(step,
tf.transpose(rnn_inputs, [1,0,2]),
initializer=init_state)
在来看下gru
gru里面没有state这个东西,它有两个门,一个是z,遗忘门,一个是r,就是reset门
跟lstm。算出遗忘门,来控制上个时刻的多少隐层被遗忘,另一半(1-z)就是本时刻多少隐层被输入。
本时刻多少隐层,跟lstm也很相似,只是在上个时刻的h上加了个reset门,就是:根据上个时刻的h加上reset门,和本时刻的输入x,通过tan来激活

看看gru的实现:
def step(st_1, x):
####
# GATES
#
# update gate
z = tf.sigmoid(tf.matmul(x,U[0]) + tf.matmul(st_1,W[0]))
# reset gate
r = tf.sigmoid(tf.matmul(x,U[1]) + tf.matmul(st_1,W[1]))
# intermediate
h = tf.tanh(tf.matmul(x,U[2]) + tf.matmul( (r*st_1),W[2]))
###
# new state
st = (1-z)*h + (z*st_1)
return st
###
# here comes the scan operation; wake up!
# tf.scan(fn, elems, initializer)
states = tf.scan(step,
tf.transpose(rnn_inputs, [1,0,2]),
initializer=init_state)
参考文章:
http://colah.github.io/posts/2015-08-Understanding-LSTMs/
http://suriyadeepan.github.io/2017-02-13-unfolding-rnn-2/
https://github.com/suriyadeepan/rnn-from-scratch
http://karpathy.github.io/2015/05/21/rnn-effectiveness/
从rnn到lstm,再到seq2seq(一)的更多相关文章
- RNN、LSTM、Seq2Seq、Attention、Teacher forcing、Skip thought模型总结
RNN RNN的发源: 单层的神经网络(只有一个细胞,f(wx+b),只有输入,没有输出和hidden state) 多个神经细胞(增加细胞个数和hidden state,hidden是f(wx+b) ...
- 3. RNN神经网络-LSTM模型结构
1. RNN神经网络模型原理 2. RNN神经网络模型的不同结构 3. RNN神经网络-LSTM模型结构 1. 前言 之前我们对RNN模型做了总结.由于RNN也有梯度消失的问题,因此很难处理长序列的数 ...
- RNN以及LSTM的介绍和公式梳理
前言 好久没用正儿八经地写博客了,csdn居然也有了markdown的编辑器了,最近花了不少时间看RNN以及LSTM的论文,在组内『夜校』分享过了,再在这里总结一下发出来吧,按照我讲解的思路,理解RN ...
- RNN、LSTM、Char-RNN 学习系列(一)
RNN.LSTM.Char-RNN 学习系列(一) zoerywzhou@gmail.com http://www.cnblogs.com/swje/ 作者:Zhouw 2016-3-15 版权声明 ...
- 机器学习- RNN以及LSTM的原理分析
概述 RNN是递归神经网络,它提供了一种解决深度学习的另一个思路,那就是每一步的输出不仅仅跟当前这一步的输入有关,而且还跟前面和后面的输入输出有关,尤其是在一些NLP的应用中,经常会用到,例如在NLP ...
- RNN and LSTM saliency Predection Scene Label
http://handong1587.github.io/deep_learning/2015/10/09/rnn-and-lstm.html //RNN and LSTM http://hando ...
- RNN 与 LSTM 的应用
之前已经介绍过关于 Recurrent Neural Nnetwork 与 Long Short-Trem Memory 的网络结构与参数求解算法( 递归神经网络(Recurrent Neural N ...
- Naive RNN vs LSTM vs GRU
0 Recurrent Neural Network 1 Naive RNN 2 LSTM peephole Naive RNN vs LSTM 记忆更新部分的操作,Naive RNN为乘法,LSTM ...
- TensorFlow之RNN:堆叠RNN、LSTM、GRU及双向LSTM
RNN(Recurrent Neural Networks,循环神经网络)是一种具有短期记忆能力的神经网络模型,可以处理任意长度的序列,在自然语言处理中的应用非常广泛,比如机器翻译.文本生成.问答系统 ...
- RNN和LSTM
一.RNN 全称为Recurrent Neural Network,意为循环神经网络,用于处理序列数据. 序列数据是指在不同时间点上收集到的数据,反映了某一事物.现象等随时间的变化状态或程度.即数据之 ...
随机推荐
- REST风格的5条关键原则
REST风格的5条关键原则包括: (1)网络上的所有事物都被抽象为资源. (2)每个资源对应一个唯一的资源标识. (3)通过通用的连接件接口对资源进行操作. (4)对资源的各种操作不会改变资源标识. ...
- Oracle单行函数
一.尽管各个数据库都是支持sql语句的.可是每一个数据库也有每一个数据库所支持的操作函数,这些就是单行函数.假设想进行数据库开发的话.除了要回使用sql语句外,就是要多学习函数. 1.单行函数的分类: ...
- 为什么python中没有switch case语句
终于知道了python中映射的设计哲学. 我自己写的code : class order_status_switcher(object): def get_order_status(self,argu ...
- spring 事务注解
在spring中使用事务需要遵守一些规范和了解一些坑点,别想当然.列举一下一些注意点. 在需要事务管理的地方加@Transactional 注解.@Transactional 注解可以被应用于接口定义 ...
- Java读取resource文件/路径的几种方式
方式一: String fileName = this.getClass().getClassLoader().getResource("文件名").getPath();//获取文 ...
- 使用new和newInstance()创建类的区别
在初始化一个类,生成一个实例的时候,newInstance()方法和new关键字除了一个是方法,一个是关键字外,最主要有什么区别?它们的区别在于创建对象的方式不一样,前者是使用类加载机制,后者是创建一 ...
- 152. Maximum Product Subarray(动态规划)
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- Fluxion无线攻击
使用步骤 github地址 https://github.com/deltaxflux/fluxion 进入到fluxion目录下 ./fluxion 启动fluxion 启动之后会先检测没有安装的 ...
- Poj3984 迷宫问题 (BFS + 路径还原)
Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, ...
- nodejs笔记之初识node
1.安装node; node -v //检测node是否安装成功 node可以做什么: 搭建服务器: 读写文件: 连接数据库: 爬虫: node的模块系统: 原生模块(如http,fs); 自定义模 ...