TensorFlow 循环神经网络RNN

本章讲解循环神经网络的TensorFlow开发模式。其他TensorFlow学习请参考:TensorFlow 学习目录

目录

一、TensorFlow中的cell类

二、通过cell构建RNN

<1> 静态RNN构建

<2> 动态RNN构建

<3> 双向RNN构建

三、RNN在MNIST数据集上的实现

1、单层静态GRU

2、单层动态GRU

3、单层静态LSTM

4、单层静态双向RNN

5、单层动态双向RNN

6、多层静态LSTM

7、多层动态LSTM

8、多层混合静态LSTM+GRU

9、多层双向RNN

10、多层动态双向RNN


一、TensorFlow中的cell类

如何理解cell?是熟练掌握RNN的基础。都知道MLP,多层感知器,除了输入层和输出层以外,其不同的隐藏层由不同个数的神经单元组成。每一个神经元就是一个权重加上一个偏置,RNN也可以用这样的神经元来构建网络,但是为了增加网络的记忆性,不断地对神经元进行复杂化,以此出现了GRU、LSTM,也就是说将原先的单一神经元变成了一个系列操作,并将这一系列操作运算封装起来,将其看成是一个复杂的神经元,也就是我们所说的cell,具体内容还是需要去学习一下。

有了Cell我们就可以像MLP那样构建RNN循环神经网络了,就完全把他当成一个神经元来理解。比如我们定义一个隐藏层的时候,需要定义隐藏层神经元个数,那么上述函数的定义就相当于是定义了一层隐藏层。

二、通过cell构建RNN

<1> 静态RNN构建

  • cell:生成好的cell类对象
  • inputs:一定是list
  • initial_state:初始化cell状态
  • dtype:期望输出和初始化state的类型
  • sequenc_length:每一个输入的序列长度
  • scope:命名空间
  • 返回值:有两个一个是outputs一个是state,其outputs是一个list,输入多少个时序,list中就有多少时序的输出

<2> 动态RNN构建

  • inputs:输入数据,一定是Tensor,[batch_size, max_time, ...]
  • time_major:默认值为False,input的shape为[batch_size, max_time, ...],如果将其改为True,input的shape为[max_time, batch_size, ...]
  • 返回值:一个是结果cell,outputs是[batch_size, max_time, ...]的Tensor。
  • 注意:在默认time_major的时候,输出的第一维度不是时间,所以要transpose一下,具体操作在代码中有体现

<3> 双向RNN构建

唯一的不同点是cell的输入有两个,一个是正向的cells,一个是反向的cells

三、RNN在MNIST数据集上的实现

其中有关MNIST加载使用问题,请参考TensorFlow 手写数字识别

1、单层静态GRU

import tensorflow as tf
import numpy as np
import get_Dataset

x_train, y_train, x_test, y_test = get_Dataset.get_Dataset(name='mnist')

n_input = 28
n_steps = 28
n_hidden = 100
n_classes = 10
batch_size = 32

inputs = tf.placeholder(dtype=tf.float32, shape=[None, n_steps*n_input])
labels = tf.placeholder(dtype=tf.float32, shape=[None, n_classes])
test_labels = tf.placeholder(dtype=tf.int64, shape=[None])

inputs_reshape = tf.reshape(inputs, [-1, n_steps, n_input])
inputs_ = tf.unstack(inputs_reshape, n_steps, 1) # return a list

# neural network architicture
gru = tf.contrib.rnn.GRUCell(n_hidden)
outputs = tf.contrib.rnn.static_rnn(gru, inputs_, dtype=tf.float32)
logits = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)

# build_loss
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

# evaluate
correct_pred = tf.equal(tf.argmax(logits, 1), test_labels)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(30):
        for batch in range(int(x_train.shape[0]/batch_size)):
            batchx = x_train[batch*batch_size: (batch+1)*batch_size]
            batchy = y_train[batch*batch_size: (batch+1)*batch_size]
            feed_dict = {
                inputs: batchx,
                labels: batchy
            }
            _ = sess.run(optimizer, feed_dict=feed_dict)
        acc = sess.run(accuracy, feed_dict={inputs: x_test, test_labels: y_test})
        print ("Epoch ", epoch+1, "acc=", acc)

输出

Epoch  1 acc= 0.9637
Epoch  2 acc= 0.9769
Epoch  3 acc= 0.9809
Epoch  4 acc= 0.9815
Epoch  5 acc= 0.9828
Epoch  6 acc= 0.9814
Epoch  7 acc= 0.9861
Epoch  8 acc= 0.9858
Epoch  9 acc= 0.9836
Epoch  10 acc= 0.9882
Epoch  11 acc= 0.9843
Epoch  12 acc= 0.987
Epoch  13 acc= 0.9872
Epoch  14 acc= 0.9877
Epoch  15 acc= 0.9885
Epoch  16 acc= 0.9894
Epoch  17 acc= 0.9881
Epoch  18 acc= 0.9868
Epoch  19 acc= 0.9864
Epoch  20 acc= 0.9891
Epoch  21 acc= 0.9867
Epoch  22 acc= 0.9873
Epoch  23 acc= 0.9873
Epoch  24 acc= 0.9885
Epoch  25 acc= 0.9894
Epoch  26 acc= 0.9898
Epoch  27 acc= 0.987
Epoch  28 acc= 0.9891
Epoch  29 acc= 0.9887
Epoch  30 acc= 0.9885

2、单层动态GRU

import tensorflow as tf
import numpy as np
import get_Dataset

x_train, y_train, x_test, y_test = get_Dataset.get_Dataset(name='mnist')

n_input = 28
n_steps = 28
n_hidden = 100
n_classes = 10
batch_size = 32

inputs = tf.placeholder(dtype=tf.float32, shape=[None, n_steps*n_input])
labels = tf.placeholder(dtype=tf.float32, shape=[None, n_classes])
test_labels = tf.placeholder(dtype=tf.int64, shape=[None])

inputs_reshape = tf.reshape(inputs, [-1, n_steps, n_input])

# neural network architicture
gru = tf.contrib.rnn.GRUCell(n_hidden)
outputs, _ = tf.nn.dynamic_rnn(gru, inputs_reshape, dtype=tf.float32)
outputs = tf.transpose(outputs, [1, 0, 2])
logits = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)

# build_loss
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

# evaluate
correct_pred = tf.equal(tf.argmax(logits, 1), test_labels)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(25):
        for batch in range(int(x_train.shape[0]/batch_size)):
            batchx = x_train[batch*batch_size: (batch+1)*batch_size]
            batchy = y_train[batch*batch_size: (batch+1)*batch_size]
            feed_dict = {
                inputs: batchx,
                labels: batchy
            }
            _ = sess.run(optimizer, feed_dict=feed_dict)
        acc = sess.run(accuracy, feed_dict={inputs: x_test, test_labels: y_test})
        print ("Epoch ", epoch+1, "acc=", acc)

输出

Epoch  1 acc= 0.9651
Epoch  2 acc= 0.9696
Epoch  3 acc= 0.9727
Epoch  4 acc= 0.9831
Epoch  5 acc= 0.9855
Epoch  6 acc= 0.9819
Epoch  7 acc= 0.9855
Epoch  8 acc= 0.9854
Epoch  9 acc= 0.9857
Epoch  10 acc= 0.9874
Epoch  11 acc= 0.9859
Epoch  12 acc= 0.986
Epoch  13 acc= 0.9858
Epoch  14 acc= 0.9883
Epoch  15 acc= 0.9876
Epoch  16 acc= 0.9863
Epoch  17 acc= 0.9892
Epoch  18 acc= 0.9895
Epoch  19 acc= 0.9886
Epoch  20 acc= 0.9868
Epoch  21 acc= 0.988
Epoch  22 acc= 0.9845
Epoch  23 acc= 0.9879
Epoch  24 acc= 0.9825
Epoch  25 acc= 0.9877

3、单层静态LSTM

import tensorflow as tf
import numpy as np
import get_Dataset

x_train, y_train, x_test, y_test = get_Dataset.get_Dataset(name='mnist')

n_input = 28
n_steps = 28
n_hidden = 100
n_classes = 10
batch_size = 32

inputs = tf.placeholder(dtype=tf.float32, shape=[None, n_steps*n_input])
labels = tf.placeholder(dtype=tf.float32, shape=[None, n_classes])
test_labels = tf.placeholder(dtype=tf.int64, shape=[None])

inputs_reshape = tf.reshape(inputs, [-1, n_steps, n_input])
inputs_ = tf.unstack(inputs_reshape, n_steps, 1) # return a list

# neural network architicture
lstm_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0) # forget_bias 添加到forget门的偏置
outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, inputs_, dtype=tf.float32)
logits = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)

# build_loss
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

# evaluate
correct_pred = tf.equal(tf.argmax(logits, 1), test_labels)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(30):
        for batch in range(int(x_train.shape[0]/batch_size)):
            batchx = x_train[batch*batch_size: (batch+1)*batch_size]
            batchy = y_train[batch*batch_size: (batch+1)*batch_size]
            feed_dict = {
                inputs: batchx,
                labels: batchy
            }
            _ = sess.run(optimizer, feed_dict=feed_dict)
        acc = sess.run(accuracy, feed_dict={inputs: x_test, test_labels: y_test})
        print ("Epoch ", epoch+1, "acc=", acc)

输出

Epoch  1 acc= 0.9396
Epoch  2 acc= 0.9623
Epoch  3 acc= 0.9637
Epoch  4 acc= 0.9769
Epoch  5 acc= 0.9808
Epoch  6 acc= 0.9832
Epoch  7 acc= 0.9825
Epoch  8 acc= 0.986
Epoch  9 acc= 0.9848
Epoch  10 acc= 0.9836
Epoch  11 acc= 0.9872
Epoch  12 acc= 0.9845
Epoch  13 acc= 0.9872
Epoch  14 acc= 0.9879
Epoch  15 acc= 0.9886
Epoch  16 acc= 0.9855
Epoch  17 acc= 0.9889
Epoch  18 acc= 0.9878
Epoch  19 acc= 0.9862
Epoch  20 acc= 0.9843
Epoch  21 acc= 0.9883
Epoch  22 acc= 0.9867
Epoch  23 acc= 0.9877
Epoch  24 acc= 0.9888
Epoch  25 acc= 0.9814
Epoch  26 acc= 0.9891
Epoch  27 acc= 0.9878
Epoch  28 acc= 0.9887
Epoch  29 acc= 0.9892
Epoch  30 acc= 0.9896

4、单层静态双向RNN

import tensorflow as tf
import numpy as np
import get_Dataset

x_train, y_train, x_test, y_test = get_Dataset.get_Dataset(name='mnist')

n_input = 28
n_steps = 28
n_hidden = 50
n_classes = 10
batch_size = 32

inputs = tf.placeholder(dtype=tf.float32, shape=[None, n_steps*n_input])
labels = tf.placeholder(dtype=tf.float32, shape=[None, n_classes])
test_labels = tf.placeholder(dtype=tf.int64, shape=[None])

inputs_reshape = tf.reshape(inputs, [-1, n_steps, n_input])
inputs_ = tf.unstack(inputs_reshape, n_steps, 1) # return a list

# neural network architicture
lstm_fw_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
lstm_bw_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)

outputs, _, _ = tf.contrib.rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, inputs_, dtype=tf.float32)
logits = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)

# build_loss
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

# evaluate
correct_pred = tf.equal(tf.argmax(logits, 1), test_labels)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(10):
        for batch in range(int(x_train.shape[0]/batch_size)):
            batchx = x_train[batch*batch_size: (batch+1)*batch_size]
            batchy = y_train[batch*batch_size: (batch+1)*batch_size]
            feed_dict = {
                inputs: batchx,
                labels: batchy
            }
            _ = sess.run(optimizer, feed_dict=feed_dict)
        acc = sess.run(accuracy, feed_dict={inputs: x_test, test_labels: y_test})
        print ("Epoch ", epoch+1, "acc=", acc)

输出

Epoch  1 acc= 0.9279
Epoch  2 acc= 0.9477
Epoch  3 acc= 0.9543
Epoch  4 acc= 0.9663
Epoch  5 acc= 0.9725
Epoch  6 acc= 0.9765
Epoch  7 acc= 0.9765
Epoch  8 acc= 0.9743
Epoch  9 acc= 0.9808
Epoch  10 acc= 0.9794

5、单层动态双向RNN

import tensorflow as tf
import numpy as np
import get_Dataset

x_train, y_train, x_test, y_test = get_Dataset.get_Dataset(name='mnist')

n_input = 28
n_steps = 28
n_hidden = 50
n_classes = 10
batch_size = 32

inputs = tf.placeholder(dtype=tf.float32, shape=[None, n_steps*n_input])
labels = tf.placeholder(dtype=tf.float32, shape=[None, n_classes])
test_labels = tf.placeholder(dtype=tf.int64, shape=[None])

inputs_reshape = tf.reshape(inputs, [-1, n_steps, n_input])
inputs_ = tf.unstack(inputs_reshape, n_steps, 1) # return a list

# neural network architicture
lstm_fw_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
lstm_bw_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)

outputs, states = tf.nn.bidirectional_dynamic_rnn(lstm_fw_cell, lstm_bw_cell, inputs_reshape, dtype=tf.float32)
outputs = tf.concat(outputs, 2)
outputs = tf.transpose(outputs, [1, 0, 2])
logits = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)

# build_loss
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

# evaluate
correct_pred = tf.equal(tf.argmax(logits, 1), test_labels)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(10):
        for batch in range(int(x_train.shape[0]/batch_size)):
            batchx = x_train[batch*batch_size: (batch+1)*batch_size]
            batchy = y_train[batch*batch_size: (batch+1)*batch_size]
            feed_dict = {
                inputs: batchx,
                labels: batchy
            }
            _ = sess.run(optimizer, feed_dict=feed_dict)
        acc = sess.run(accuracy, feed_dict={inputs: x_test, test_labels: y_test})
        print ("Epoch ", epoch+1, "acc=", acc)

输出

Epoch  1 acc= 0.9289
Epoch  2 acc= 0.9477
Epoch  3 acc= 0.9539
Epoch  4 acc= 0.9623
Epoch  5 acc= 0.9725
Epoch  6 acc= 0.969
Epoch  7 acc= 0.9764
Epoch  8 acc= 0.9801
Epoch  9 acc= 0.9793
Epoch  10 acc= 0.9784

6、多层静态LSTM

import tensorflow as tf
import numpy as np
import get_Dataset

x_train, y_train, x_test, y_test = get_Dataset.get_Dataset(name='mnist')

n_input = 28
n_steps = 28
n_hidden = 50
n_classes = 10
batch_size = 32

inputs = tf.placeholder(dtype=tf.float32, shape=[None, n_steps*n_input])
labels = tf.placeholder(dtype=tf.float32, shape=[None, n_classes])
test_labels = tf.placeholder(dtype=tf.int64, shape=[None])

inputs_reshape = tf.reshape(inputs, [-1, n_steps, n_input])
inputs_ = tf.unstack(inputs_reshape, n_steps, 1) # return a list

# neural network architicture
stacked_rnn = []
for i in range(3):
    stacked_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
mcell = tf.contrib.rnn.MultiRNNCell(stacked_rnn)

outputs, states = tf.contrib.rnn.static_rnn(mcell, inputs_, dtype=tf.float32)
logits = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)


# build_loss
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

# evaluate
correct_pred = tf.equal(tf.argmax(logits, 1), test_labels)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(30):
        for batch in range(int(x_train.shape[0]/batch_size)):
            batchx = x_train[batch*batch_size: (batch+1)*batch_size]
            batchy = y_train[batch*batch_size: (batch+1)*batch_size]
            feed_dict = {
                inputs: batchx,
                labels: batchy
            }
            _ = sess.run(optimizer, feed_dict=feed_dict)
        acc = sess.run(accuracy, feed_dict={inputs: x_test, test_labels: y_test})
        print ("Epoch ", epoch+1, "acc=", acc)

输出

Epoch  1 acc= 0.9467
Epoch  2 acc= 0.97
Epoch  3 acc= 0.9723
Epoch  4 acc= 0.9785
Epoch  5 acc= 0.9824
Epoch  6 acc= 0.9855
Epoch  7 acc= 0.9808
Epoch  8 acc= 0.9841
Epoch  9 acc= 0.9863
Epoch  10 acc= 0.9871
Epoch  11 acc= 0.9836
Epoch  12 acc= 0.9874
Epoch  13 acc= 0.9876
Epoch  14 acc= 0.9881
Epoch  15 acc= 0.9881
Epoch  16 acc= 0.9865
Epoch  17 acc= 0.9855
Epoch  18 acc= 0.9885
Epoch  19 acc= 0.9881
Epoch  20 acc= 0.9883
Epoch  21 acc= 0.9847
Epoch  22 acc= 0.9889
Epoch  23 acc= 0.9883
Epoch  24 acc= 0.9852
Epoch  25 acc= 0.9891
Epoch  26 acc= 0.9866
Epoch  27 acc= 0.9899
Epoch  28 acc= 0.9877
Epoch  29 acc= 0.9899
Epoch  30 acc= 0.9857

7、多层动态LSTM

import tensorflow as tf
import numpy as np
import get_Dataset

x_train, y_train, x_test, y_test = get_Dataset.get_Dataset(name='mnist')

n_input = 28
n_steps = 28
n_hidden = 50
n_classes = 10
batch_size = 32

inputs = tf.placeholder(dtype=tf.float32, shape=[None, n_steps*n_input])
labels = tf.placeholder(dtype=tf.float32, shape=[None, n_classes])
test_labels = tf.placeholder(dtype=tf.int64, shape=[None])

inputs_reshape = tf.reshape(inputs, [-1, n_steps, n_input])
inputs_ = tf.unstack(inputs_reshape, n_steps, 1) # return a list

# neural network architicture
lstm1 = tf.contrib.rnn.LSTMCell(n_hidden)
lstm2 = tf.contrib.rnn.LSTMCell(n_hidden*2)
mcell = tf.contrib.rnn.MultiRNNCell([lstm2, lstm1])

outputs, states = tf.nn.dynamic_rnn(mcell, inputs_reshape, dtype=tf.float32)
outputs = tf.transpose(outputs, [1, 0, 2])
logits = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)

# build_loss
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

# evaluate
correct_pred = tf.equal(tf.argmax(logits, 1), test_labels)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(20):
        for batch in range(int(x_train.shape[0]/batch_size)):
            batchx = x_train[batch*batch_size: (batch+1)*batch_size]
            batchy = y_train[batch*batch_size: (batch+1)*batch_size]
            feed_dict = {
                inputs: batchx,
                labels: batchy
            }
            _ = sess.run(optimizer, feed_dict=feed_dict)
        acc = sess.run(accuracy, feed_dict={inputs: x_test, test_labels: y_test})
        print ("Epoch ", epoch+1, "acc=", acc)

输出

Epoch  1 acc= 0.9621
Epoch  2 acc= 0.9589
Epoch  3 acc= 0.9782
Epoch  4 acc= 0.9825
Epoch  5 acc= 0.9845
Epoch  6 acc= 0.9874
Epoch  7 acc= 0.9873
Epoch  8 acc= 0.9866
Epoch  9 acc= 0.9872
Epoch  10 acc= 0.987
Epoch  11 acc= 0.9895
Epoch  12 acc= 0.989
Epoch  13 acc= 0.9892
Epoch  14 acc= 0.9886
Epoch  15 acc= 0.9904
Epoch  16 acc= 0.9902
Epoch  17 acc= 0.9894
Epoch  18 acc= 0.9881
Epoch  19 acc= 0.9889
Epoch  20 acc= 0.9869

8、多层混合静态LSTM+GRU

import tensorflow as tf
import numpy as np
import get_Dataset

x_train, y_train, x_test, y_test = get_Dataset.get_Dataset(name='mnist')

n_input = 28
n_steps = 28
n_hidden = 50
n_classes = 10
batch_size = 32

inputs = tf.placeholder(dtype=tf.float32, shape=[None, n_steps*n_input])
labels = tf.placeholder(dtype=tf.float32, shape=[None, n_classes])
test_labels = tf.placeholder(dtype=tf.int64, shape=[None])

inputs_reshape = tf.reshape(inputs, [-1, n_steps, n_input])
inputs_ = tf.unstack(inputs_reshape, n_steps, 1) # return a list

# neural network architicture
gru = tf.contrib.rnn.GRUCell(n_hidden*2)
lstm = tf.contrib.rnn.LSTMCell(n_hidden)
mcell = tf.contrib.rnn.MultiRNNCell([lstm, gru])

outputs, states = tf.contrib.rnn.static_rnn(mcell, inputs_, dtype=tf.float32)
logits = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)

# build_loss
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

# evaluate
correct_pred = tf.equal(tf.argmax(logits, 1), test_labels)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(30):
        for batch in range(int(x_train.shape[0]/batch_size)):
            batchx = x_train[batch*batch_size: (batch+1)*batch_size]
            batchy = y_train[batch*batch_size: (batch+1)*batch_size]
            feed_dict = {
                inputs: batchx,
                labels: batchy
            }
            _ = sess.run(optimizer, feed_dict=feed_dict)
        acc = sess.run(accuracy, feed_dict={inputs: x_test, test_labels: y_test})
        print ("Epoch ", epoch+1, "acc=", acc)

输出

Epoch  1 acc= 0.9562
Epoch  2 acc= 0.974
Epoch  3 acc= 0.9817
Epoch  4 acc= 0.9848
Epoch  5 acc= 0.985
Epoch  6 acc= 0.9876
Epoch  7 acc= 0.9875
Epoch  8 acc= 0.9875
Epoch  9 acc= 0.9864
Epoch  10 acc= 0.986
Epoch  11 acc= 0.9877
Epoch  12 acc= 0.9896
Epoch  13 acc= 0.9892
Epoch  14 acc= 0.9891
Epoch  15 acc= 0.9861
Epoch  16 acc= 0.9866
Epoch  17 acc= 0.9875
Epoch  18 acc= 0.9875
Epoch  19 acc= 0.9872
Epoch  20 acc= 0.9878
Epoch  21 acc= 0.9885
Epoch  22 acc= 0.9889
Epoch  23 acc= 0.9883
Epoch  24 acc= 0.9883
Epoch  25 acc= 0.9888
Epoch  26 acc= 0.9844
Epoch  27 acc= 0.9892
Epoch  28 acc= 0.9884
Epoch  29 acc= 0.9895
Epoch  30 acc= 0.9872

9、多层双向RNN

import tensorflow as tf
import numpy as np
import get_Dataset

x_train, y_train, x_test, y_test = get_Dataset.get_Dataset(name='mnist')

n_input = 28
n_steps = 28
n_hidden = 50
n_classes = 10
batch_size = 32

inputs = tf.placeholder(dtype=tf.float32, shape=[None, n_steps*n_input])
labels = tf.placeholder(dtype=tf.float32, shape=[None, n_classes])
test_labels = tf.placeholder(dtype=tf.int64, shape=[None])

inputs_reshape = tf.reshape(inputs, [-1, n_steps, n_input])
inputs_ = tf.unstack(inputs_reshape, n_steps, 1) # return a list

# neural network architicture
# way 1: list多层RNN
# stacked_fw_rnn = []
# stacked_bw_rnn = []
# for i in range(2):
#     stacked_fw_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
#     stacked_bw_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
#
# outputs, _, _ = tf.contrib.rnn.stack_bidirectional_rnn(stacked_fw_rnn, stacked_bw_rnn, inputs_, dtype=tf.float32)

# way 2: multi双向RNN
stacked_fw_rnn = []
stacked_bw_rnn = []
for i in range(2):
    stacked_fw_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
    stacked_bw_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))

mcell_fw = tf.contrib.rnn.MultiRNNCell(stacked_fw_rnn)
mcell_bw = tf.contrib.rnn.MultiRNNCell(stacked_bw_rnn)
outputs, _, _ = tf.contrib.rnn.stack_bidirectional_rnn([mcell_fw], [mcell_bw], inputs_, dtype=tf.float32)

logits = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)



# build_loss
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

# evaluate
correct_pred = tf.equal(tf.argmax(logits, 1), test_labels)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(10):
        for batch in range(int(x_train.shape[0]/batch_size)):
            batchx = x_train[batch*batch_size: (batch+1)*batch_size]
            batchy = y_train[batch*batch_size: (batch+1)*batch_size]
            feed_dict = {
                inputs: batchx,
                labels: batchy
            }
            _ = sess.run(optimizer, feed_dict=feed_dict)
        acc = sess.run(accuracy, feed_dict={inputs: x_test, test_labels: y_test})
        print ("Epoch ", epoch+1, "acc=", acc)

输出

输出1:
Epoch  1 acc= 0.9471
Epoch  2 acc= 0.9619
Epoch  3 acc= 0.9756
Epoch  4 acc= 0.9766
Epoch  5 acc= 0.9768
Epoch  6 acc= 0.9837
Epoch  7 acc= 0.9808
Epoch  8 acc= 0.9851
Epoch  9 acc= 0.9816
Epoch  10 acc= 0.9841
输出2:
Epoch  1 acc= 0.9455
Epoch  2 acc= 0.9697
Epoch  3 acc= 0.9654
Epoch  4 acc= 0.9769
Epoch  5 acc= 0.9805
Epoch  6 acc= 0.9818
Epoch  7 acc= 0.9851
Epoch  8 acc= 0.9874
Epoch  9 acc= 0.9859
Epoch  10 acc= 0.9866

10、多层动态双向RNN

import tensorflow as tf
import numpy as np
import get_Dataset

x_train, y_train, x_test, y_test = get_Dataset.get_Dataset(name='mnist')

n_input = 28
n_steps = 28
n_hidden = 50
n_classes = 10
batch_size = 32

inputs = tf.placeholder(dtype=tf.float32, shape=[None, n_steps*n_input])
labels = tf.placeholder(dtype=tf.float32, shape=[None, n_classes])
test_labels = tf.placeholder(dtype=tf.int64, shape=[None])

inputs_reshape = tf.reshape(inputs, [-1, n_steps, n_input])
inputs_ = tf.unstack(inputs_reshape, n_steps, 1) # return a list

# neural network architicture
stacked_fw_rnn = []
stacked_bw_rnn = []
for i in range(2):
    stacked_fw_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))
    stacked_bw_rnn.append(tf.contrib.rnn.LSTMCell(n_hidden))

mcell_fw = tf.contrib.rnn.MultiRNNCell(stacked_fw_rnn)
mcell_bw = tf.contrib.rnn.MultiRNNCell(stacked_bw_rnn)
outputs, _, _ = tf.contrib.rnn.stack_bidirectional_dynamic_rnn([mcell_fw], [mcell_bw], inputs_reshape, dtype=tf.float32)
outputs = tf.transpose(outputs, [1, 0, 2])
logits = tf.contrib.layers.fully_connected(outputs[-1], n_classes, activation_fn=None)

# build_loss
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

# evaluate
correct_pred = tf.equal(tf.argmax(logits, 1), test_labels)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(10):
        for batch in range(int(x_train.shape[0]/batch_size)):
            batchx = x_train[batch*batch_size: (batch+1)*batch_size]
            batchy = y_train[batch*batch_size: (batch+1)*batch_size]
            feed_dict = {
                inputs: batchx,
                labels: batchy
            }
            _ = sess.run(optimizer, feed_dict=feed_dict)
        acc = sess.run(accuracy, feed_dict={inputs: x_test, test_labels: y_test})
        print ("Epoch ", epoch+1, "acc=", acc)

输出

Epoch  1 acc= 0.9406
Epoch  2 acc= 0.9627
Epoch  3 acc= 0.9675
Epoch  4 acc= 0.9706
Epoch  5 acc= 0.9792
Epoch  6 acc= 0.9823
Epoch  7 acc= 0.984
Epoch  8 acc= 0.9866
Epoch  9 acc= 0.9853
Epoch  10 acc= 0.9844
发布了331 篇原创文章 · 获赞 135 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/Triple_WDF/article/details/103299114