Tensorflow-LSTM RNN 例子

一些 RNN相關資料
  這次使用 RNN 來進行分類的訓練 (Classification). 會繼續使用到手寫數字 MNIST 數據集. 讓 RNN 從每張圖片的第一行像素讀到最後一行, 然後再進行分類判斷.
  定義 RNN 主體結構在這個這個 RNN 總共有 3 個組成部分 ( input_layer, cell, output_layer). 大概就是這些,demo 看下面:

#coding:utf-8
import tensorflow as tf 
from tensorflow.contrib import rnn
import input_data

mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

# Training Parameters
learning_rate = 0.001
training_steps = 1000
batch_size = 128
display_step = 100

num_input = 28 # MNIST data input (img shape: 28*28)
timesteps = 28 # time steps
num_hidden = 128 # hidden layer num of features
num_classes = 10 # MNIST total classes (0-9 digits)

# tf Draph input
X = tf.placeholder(tf.float32,[None,timesteps,num_input])
Y = tf.placeholder(tf.float32,[None,num_classes])

# Define weights
weights = {
    # (28,128)
    'in': tf.Variable(tf.random_normal([num_input,num_hidden])),

    #(128,10)
    'out': tf.Variable(tf.random_normal([num_hidden,num_classes]))
}

biases = {
    #(128,)
    'in':tf.Variable(tf.constant(0.1,shape = [num_hidden,])),

    #(10,)
    'out':tf.Variable(tf.constant(0.1,shape = [num_classes,]))
}

# Define RNN

def RNN(X, weights, biases):
    #### hidden layer for input to  cell #####

    #改變輸入的shape X ==> (128 batch * 28 steps, 28 inputs)
    X = tf.reshape(X,[-1,num_input])
    X_in = tf.matmul(X,weights['in']+biases['in'])
    # X_in ==> (128 batch, 28 steps, 128 hidden)
    X_in = tf.reshape(X_in,[-1,timesteps,num_hidden])


    ######################cell##########################
    cell = rnn.BasicLSTMCell(num_hidden)
    #一個lstm cell 有兩部分 c_state, h_state
    init_state = cell.zero_state(batch_size, dtype=tf.float32)
    outputs, final_state = tf.nn.dynamic_rnn(cell, X_in, initial_state=init_state, time_major=False)

    #### hindden layer for output as final results ####
    # # way 1
    # outputs = tf.unstack(tf.transpose(outputs, [1,0,2]))
    # result = tf.matmul(outputs[-1], weights['out']) + biases['out']    #調用最後一個 outputs
    #or
    # way 2
    result = tf.matmul(final_state[1], weights['out']) + biases['out'] #直接調用final_state 中的 h_state (final_state[1]) 來進行運算
    return result  #shape = (128, 10)


# Define loss and optimizer
pred = RNN(X, weights, biases)
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op =  optimizer.minimize(loss_op)


# Evaluate model (with test logits, for dropout to be disabled)
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

#start
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for step in range(1, training_steps+1):
        batch_x, batch_y = mnist.train.next_batch(batch_size)
        # Reshape data to get 28 seq of 28 elements
        batch_x = batch_x.reshape((batch_size, timesteps, num_input))
        # Run optimization op (backprop)
        sess.run(train_op, feed_dict={X: batch_x, Y: batch_y})
        if step % display_step == 0 or step == 1:
            loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x,Y: batch_y})
            print("Step " + str(step) + ", 小批量損失 = " + \
                  "{:.4f}".format(loss) + ", 訓練準確度 = " + \
                  "{:.3f}".format(acc))
    print("優化完成")
    # Calculate accuracy for 128 mnist test images
    test_len = 128
    test_data = mnist.test.images[:test_len].reshape((-1, timesteps, num_input))
    test_label = mnist.test.labels[:test_len]
    print("測試精度爲 :", sess.run(accuracy, feed_dict={X: test_data, Y: test_label}))

運行結果看下圖建議在GPU上運行 ,cpu太耗時了:
這裏寫圖片描述

發佈了90 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章