TensorFlow【极简】RNN

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/Yellow_python/article/details/86679020

简洁单层RNN

import tensorflow as tf
from tensorflow.contrib.rnn import LSTMCell
from tensorflow.examples.tutorials.mnist import input_data

"""加载样本集:手写数字"""
mnist = input_data.read_data_sets('data', one_hot=True)

"""网络结构"""
num_units = 50  # 隐层神经元数量
batch_size = 500  # 批量

# 输入层
X = tf.placeholder(tf.float32, [batch_size, 28, 28])  # 批量、高、宽
y = tf.placeholder(tf.float32, [batch_size, 10])  # 10分类(0~9)

# LSTM
lstm_cell = LSTMCell(num_units)

# 获取最后一个隐层
LSTMStateTuple = lstm_cell.zero_state(batch_size, tf.float32)  # c&h
for time_step in range(28):
    h, LSTMStateTuple = lstm_cell(X[:, time_step, :], LSTMStateTuple)

# 输出层
W = tf.Variable(tf.truncated_normal([num_units, 10], stddev=.1))
b = tf.Variable(tf.constant(.1, tf.float32, [10]), dtype=tf.float32)
o = tf.matmul(h, W) + b  # output

# softmax处理、交叉熵损失
loss = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=o)

# 优化
optimize = tf.train.AdamOptimizer().minimize(loss)

"""迭代训练"""
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(1001):
    batch = mnist.train.next_batch(batch_size)  # 分批
    inputs = batch[0].reshape([batch_size, 28, 28])
    labels = batch[1]
    feed_dict = {X: inputs, y: labels}
    sess.run(optimize, feed_dict)
    if i % 100 == 0:
        cross_entropy = tf.reduce_mean(loss).eval(feed_dict, sess)
        print('step %d; loss %g' % (i, cross_entropy))

详细双层RNN

import matplotlib.pyplot as mp, numpy as np, matplotlib.gridspec as mg
import tensorflow as tf
from tensorflow.contrib.rnn import MultiRNNCell, LSTMCell
from tensorflow.examples.tutorials.mnist import input_data
np.random.seed(0)

"""加载样本集:手写数字"""
mnist = input_data.read_data_sets('data', one_hot=True)
train, test = mnist.train, mnist.test

"""网络结构"""
num_units = 50  # 隐层神经元数量
batch_size = 1  # 批量

# 输入层
X = tf.placeholder(tf.float32, [batch_size, 28, 28])  # 批量、高、宽
y = tf.placeholder(tf.float32, [batch_size, 10])  # 10分类(0~9)

# 双层LSTM
lstm_cells = MultiRNNCell([LSTMCell(num_units),
                           LSTMCell(num_units)])

# 获取最后一个隐层
LSTMStateTuple = lstm_cells.zero_state(batch_size, tf.float32)
h_list = []
for time_step in range(28):
    h, LSTMStateTuple = lstm_cells(X[:, time_step, :], LSTMStateTuple)
    h_list.append(h)
h = h_list[-1]  # 获取最后一个隐藏

# 输出层
W = tf.Variable(tf.truncated_normal([num_units, 10], stddev=.1))
b = tf.Variable(tf.constant(.1, tf.float32, [10]), dtype=tf.float32)
y_fit = tf.nn.softmax(tf.matmul(h, W) + b)  # softmax
cross_entropy = -tf.reduce_mean(y * tf.log(y_fit))  # 交叉熵损失

# 优化
optimize = tf.train.AdamOptimizer().minimize(cross_entropy)

# 准确率
prediction = tf.equal(tf.argmax(y_fit, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32))

"""训练、检验"""
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(20001):
    batch = train.next_batch(batch_size)  # 分批
    inputs = batch[0].reshape([batch_size, 28, 28])
    labels = batch[1]
    sess.run(optimize, {X: inputs, y: labels})
    if i % 2000 == 0:
        batch_test = test.next_batch(batch_size)
        inputs_test = batch_test[0].reshape([batch_size, 28, 28])
        labels_test = batch_test[1]
        acc = sess.run(accuracy, {X: inputs_test, y: labels_test})
        print('step %d; acc %g' % (i, acc))

"""单个图像RNN每层结果"""
batch = test.next_batch(batch_size)
inputs = batch[0].reshape([batch_size, 28, 28])
labels = batch[1]
mp.figure(str(np.argmax(labels)))  # 数字对应标签
gs = mg.GridSpec(28, 2)  # 子图
for e, h_pred in enumerate(h_list):
    y_pred = tf.nn.softmax(tf.matmul(h_pred, W) + b)
    probs = y_pred.eval({X: inputs, y: labels}, sess)
    mp.subplot(gs[e, 1])
    mp.bar(range(10), probs[0], width=.1)
    mp.axis('off')
mp.subplot(gs[:, 0])
mp.imshow(inputs[0], cmap='gray')
mp.axis('off')
mp.show()

单个图像RNN每层结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Yellow_python/article/details/86679020