深度学习(九)--RNN
0. 传统神经网络缺点
1)不能处理输入样本是连续序列且长度不统一的情况;
2)网络不能共享(学习到的知识不能应用于后边的学习)
1. RNN( 循环神经网络)
1)特点:处理序列数据的神经网络(序列数据,指的是数据会随着时间、空间等某一维度,前后数据存在一定关联性),网络可以处理单元之间存在的内部反馈连接,同时可以处理前馈连接;
2)主要的RNN网络
Hopfield网络、Elman networks、长短记忆网络
3)网络结构
4)网络前向传播过程
5)反向传播过程
基于时间的反向传播,核心仍是梯度下降
这里所有的U,W,V,b,c在序列的各个位置是共享的,反向传播时我们更新的是相同的参数。
网络存在问题:梯度消失或梯度爆炸
改进方法:
1)修改**函数:tanh-->Relu
2)修改网络结构
2. LSTM(长短时记忆网络)
1)RNN由于梯度消失的原因只能有短期记忆,LSTM网络通过精妙的门控制将短期记忆与长期记忆结合起来,并且一定程度上解决了梯度消失的问题;
2)结构
3)介绍
LSTM 有通过精心设计的称作为“门”的结构来删除或者增加信息到细胞状态的能力。门是一种让信息选择式通过的方法。他们包含一个 sigmoid 神经网络层和一个 pointwise 乘法操作。
遗忘门:决定之前的知识记住什么,**函数为sigmoid;
"更新门":将旧状态乘以一个比例加上新状态的信息输出;
"输出门":决定最终将什么信息输出;
输出:当前时序输入的部分内容和应用之前知识学习到的新知识的一部分;
3. GRU
将LSTM中的输入门和遗忘门结合成一个单独的"更新门",合并细胞状态和隐含状态;
网络结构如图:
4. 手写数字识别
# coding=utf-8
'''
RNN中的LSTM实现手写数字识别
'''
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#载入数据
mnist = input_data.read_data_sets('data/MNIST_data/',one_hot=True)
#输入图片为28*28
n_inputs = 28#输入一行数据,每行28
max_time = 28#一张图片28行
lstm_size = 100#隐层数量
n_classes = 10#10中类别
batch_size = 100#批次大小
n_batch = mnist.train.num_examples//batch_size#多少个批次
#定义两个占位符
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
#初始化权值和偏置值
weights = tf.Variable(tf.truncated_normal([lstm_size, n_classes], stddev=0.1))
biases = tf.Variable(tf.constant(0.1, shape=[n_classes]))
#定义RNN网络
def RNN(x,weights,biases):
inputs = tf.reshape(x,[-1,max_time,n_inputs])#50张图片(每个批次)
#定义LSTM基本单元
lstm_cell = tf.nn.rnn_cell.BasicLSTMCell (lstm_size)
# final_state[0]是cell state,final_state[1]是hidden_state
#'outputs' is a tensor of shape [batch_size, max_time, 256]
outputs, final_state = tf.nn.dynamic_rnn(lstm_cell, inputs, dtype=tf.float32)
results = tf.nn.softmax(tf.matmul(final_state[1], weights) + biases)
return results
#计算网络输出
prediction= RNN(x, weights, biases)
#损失函数
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))
#使用AdamOptimizer进行优化
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
#结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))#把correct_prediction变为float32类型
#初始化
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in range(10):
for batch in range(n_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys})
acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels})
print("Iter " + str(epoch) + ", Testing Accuracy= " + str(acc))
'''
在10词迭代之后,准确率达:0.9338
'''
参考:
1. https://blog.****.net/zhaojc1995/article/details/80572098