1.訓練的話一般一批一批訓練,即讓batch_size 個樣本同時訓練;
2.每個樣本又包含從該樣本往后的連續seq_len個樣本(如seq_len=15),seq_len也就是LSTM中cell的個數;
3.每個樣本又包含inpute_dim個維度的特征(如input_dim=7)
因此,輸入層的輸入數據通常先要reshape:
x= np.reshape(x, (batch_size , seq_len, input_dim))
(友情提示:每個cell共享參數!!!)
舉個例子:
from tensorflow.examples.tutorials.mnist import input_data import tensorflow as tf import numpy as np #在這里做數據加載,還是使用那個MNIST的數據,以one_hot的方式加載數據,記得目錄可以改成之前已經下載完成的目錄
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) ''' MNIST的數據是一個28*28的圖像,這里RNN測試,把他看成一行行的序列(28維度(28長的sequence)*28行) '''
# RNN學習時使用的參數
learning_rate = 0.001 training_iters = 100000 batch_size = 128 display_step = 10
# 神經網絡的參數
n_input = 28 # 輸入層的n
n_steps = 28 # 28長度
n_hidden = 128 # 隱含層的特征數
n_classes = 10 # 輸出的數量,因為是分類問題,0~9個數字,這里一共有10個
# 構建tensorflow的輸入X的placeholder
x = tf.placeholder("float", [None, n_steps, n_input]) # tensorflow里的LSTM需要兩倍於n_hidden的長度的狀態,一個state和一個cell # Tensorflow LSTM cell requires 2x n_hidden length (state & cell)
istate = tf.placeholder("float", [None, 2 * n_hidden]) # 輸出Y
y = tf.placeholder("float", [None, n_classes]) # 隨機初始化每一層的權值和偏置
weights = { 'hidden': tf.Variable(tf.random_normal([n_input, n_hidden])), # Hidden layer weights
'out': tf.Variable(tf.random_normal([n_hidden, n_classes])) } biases = { 'hidden': tf.Variable(tf.random_normal([n_hidden])), 'out': tf.Variable(tf.random_normal([n_classes])) } ''' 構建RNN '''
def RNN(_X, _istate, _weights, _biases): # 規整輸入的數據
_X = tf.transpose(_X, [1, 0, 2]) # permute n_steps and batch_size
_X = tf.reshape(_X, [-1, n_input]) # (n_steps*batch_size, n_input)
# 輸入層到隱含層,第一次是直接運算
_X = tf.matmul(_X, _weights['hidden']) + _biases['hidden'] # 之后使用LSTM
lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0) # 28長度的sequence,所以是需要分解位28次
_X = tf.split(0, n_steps, _X) # n_steps * (batch_size, n_hidden)
# 開始跑RNN那部分
outputs, states = tf.nn.rnn(lstm_cell, _X, initial_state=_istate) # 輸出層
return tf.matmul(outputs[-1], _weights['out']) + _biases['out'] pred = RNN(x, istate, weights, biases) # 定義損失和優化方法,其中算是為softmax交叉熵,優化方法為Adam
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y)) # Softmax loss
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) # Adam Optimizer
# 進行模型的評估,argmax是取出取值最大的那一個的標簽作為輸出
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) # 初始化
init = tf.initialize_all_variables() # 開始運行
with tf.Session() as sess: sess.run(init) step = 1
# 持續迭代
while step * batch_size < training_iters: # 隨機抽出這一次迭代訓練時用的數據
batch_xs, batch_ys = mnist.train.next_batch(batch_size) # 對數據進行處理,使得其符合輸入
batch_xs = batch_xs.reshape((batch_size, n_steps, n_input)) # 迭代
sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, istate: np.zeros((batch_size, 2 * n_hidden))}) # 在特定的迭代回合進行數據的輸出
if step % display_step == 0: # Calculate batch accuracy
acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys, istate: np.zeros((batch_size, 2 * n_hidden))}) # Calculate batch loss
loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, istate: np.zeros((batch_size, 2 * n_hidden))}) print "Iter " + str(step * batch_size) + ", Minibatch Loss= " + "{:.6f}".format(loss) + \ ", Training Accuracy= " + "{:.5f}".format(acc) step += 1
print "Optimization Finished!"
# 載入測試集進行測試
test_len = 256 test_data = mnist.test.images[:test_len].reshape((-1, n_steps, n_input)) test_label = mnist.test.labels[:test_len] print "Testing Accuracy:", sess.run(accuracy, feed_dict={x: test_data, y: test_label, istate: np.zeros((test_len, 2 * n_hidden))}