【轉載】 tensorflow的單層靜態與動態RNN比較

原文地址:git

https://www.jianshu.com/p/1b1ea45fab47網絡

 

yanghedadasession

 

-----------------------------------------------------------------------------------函數

 

 

 

 

static_rnn和dynamic_rnn

 

1:     static_rnnlua

x = tf.placeholder("float", [None, n_steps, n_input])
x1 = tf.unstack(x, n_steps, 1)
lstm_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x1, dtype=tf.float32)
pred = tf.contrib.layers.fully_connected(outputs[-1],n_classes,activation_fn = None)

 

 

 

2:     dynamic_rnnspa

x = tf.placeholder("float", [None, n_steps, n_input])
lstm_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
outputs,_  = tf.nn.dynamic_rnn(lstm_cell ,x,dtype=tf.float32)
outputs = tf.transpose(outputs, [1, 0, 2])
pred = tf.contrib.layers.fully_connected(outputs[-1],n_classes,activation_fn = None)

 

 

 

BasicLSTMCell:
(num_units: 是指一個Cell中神經元的個數,forget_bias:忘記門記住多少,1.0表明所有記住)
 
 

tf.contrib.rnn.static_rnn:
靜態 rnn的意思就是按照樣本時間序列個數(n_steps)展開,在圖中建立(n_steps)個序列的cell
 
 

tf.nn.dynamic_rnn:
動態rnn的意思是隻建立樣本中的一個序列RNN,其餘序列數據會經過循環進入該RNN運算。  經過靜態static_rnn生成的RNN網絡,生成過程所需的時間會更長,網絡所佔有的內存會更多,導出的模型會更大。static_rnn模型中會帶有第個序列中間態的信息,利於調試。static_rnn在使用時必須與訓練的樣本序列個數相同。 dynamic_rnn經過動態生成的RNN網絡,所佔用內存較少。 dynamic_rnn模型中只會有最後的狀態,在使用時還能支持不一樣的序列個數。



 
 
 

區別

1.tf.nn.dynamic_rnn與tf.contrib.rnn.static_rnn輸入格式不一樣。
2.tf.nn.dynamic_rnn與tf.contrib.rnn.static_rnn輸出格式不一樣。
3.tf.nn.dynamic_rnn與tf.contrib.rnn.static_rnn內部訓練方式。.net




 
 
 

請仔細對比如下區別:

能夠參考:https://blog.csdn.net/mzpmzk/article/details/80573338調試

 
 
 
 
 
 
 
 
 

動態rnn

import tensorflow as tf
# 導入 MINST 數據集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("c:/user/administrator/data/", one_hot=True)
n_input = 28 # MNIST data 輸入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10  # MNIST 列別 (0-9 ,一共10類)
batch_size = 128
tf.reset_default_graph()

# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
lstm_cell = tf.contrib.rnn.LSTMCell(n_hidden, forget_bias=1.0)
outputs,_  = tf.nn.dynamic_rnn(lstm_cell,x,dtype=tf.float32)
outputs = tf.transpose(outputs, [1, 0, 2])
#取最後一條輸出信息,(outputs[-1])
pred = tf.contrib.layers.fully_connected(outputs[-1],n_classes,activation_fn = None)

learning_rate = 0.001
training_iters = 100000

display_step = 10

# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# Evaluate model
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

# 啓動session
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    step = 1
    # Keep training until reach max iterations
    while step * batch_size < training_iters:
        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, n_steps, n_input))
        # Run optimization op (backprop)
        sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
        if step % display_step == 0:
            # 計算批次數據的準確率
            acc = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y})
            # Calculate batch loss
            loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})
            print ("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
                  "{:.6f}".format(loss) + ", Training Accuracy= " + \
                  "{:.5f}".format(acc))
        step += 1
    print (" Finished!")

    # 計算準確率 for 128 mnist test images
    test_len = 128
    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}))

 

 

 

 

靜態RNNcode

import tensorflow as tf
# 導入 MINST 數據集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("c:/user/administrator/data/", one_hot=True)
n_input = 28 # MNIST data 輸入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10  # MNIST 列別 (0-9 ,一共10類)
batch_size = 128
tf.reset_default_graph()

# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
lstm_cell = tf.contrib.rnn.LSTMCell(n_hidden, forget_bias=1.0)
x1 = tf.unstack(x, n_steps, 1)
lstm_cell = tf.contrib.rnn.LSTMCell(n_hidden, forget_bias=1.0)
outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x1, dtype=tf.float32)
#取最後一條輸出信息,(outputs[-1])
pred = tf.contrib.layers.fully_connected(outputs[-1],n_classes,activation_fn = None)

learning_rate = 0.001
training_iters = 100000

display_step = 10

# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# Evaluate model
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

# 啓動session
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    step = 1
    # Keep training until reach max iterations
    while step * batch_size < training_iters:
        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, n_steps, n_input))
        # Run optimization op (backprop)
        sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
        if step % display_step == 0:
            # 計算批次數據的準確率
            acc = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y})
            # Calculate batch loss
            loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})
            print ("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
                  "{:.6f}".format(loss) + ", Training Accuracy= " + \
                  "{:.5f}".format(acc))
        step += 1
    print (" Finished!")

    # 計算準確率 for 128 mnist test images
    test_len = 128
    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}))

 

 

 

 

 

 

 

 

本代碼源自:
凱文自學TensorFloworm

 

# -*- coding: utf-8 -*-


import tensorflow as tf
# 導入 MINST 數據集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("c:/user/administrator/data/", one_hot=True)
n_input = 28 # MNIST data 輸入 (img shape: 28*28)
n_steps = 28 # timesteps
n_hidden = 128 # hidden layer num of features
n_classes = 10  # MNIST 列別 (0-9 ,一共10類)
batch_size = 128
tf.reset_default_graph()

# tf Graph input
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
#重置x以適合tf.contrib.rnn.static_rnn所要求的格式
#x1 = tf.unstack(x, n_steps, 1)

#BasicLSTMCell(num_units: 是指一個Cell中神經元的個數,forget_bias:忘記門記住多少,1.0表明所有記住)
#靜態 (tf.contrib.rnn.static_rnn)的意思就是按照樣本時間序列個數(n_steps)展開,在圖中建立(n_steps)個序列的cell;
#動態(tf.nn.dynamic_rnn)的意思是隻建立樣本中的一個序列RNN,其餘序列數據會經過循環進入該RNN運算
"""
經過靜態生成的RNN網絡,生成過程所需的時間會更長,網絡所佔有的內存會更多,導出的模型會更大
。模型中會帶有第個序列中間態的信息,利於調試。在使用時必須與訓練的樣本序列個數相同。經過動
態生成的RNN網絡,所佔用內存較少。模型中只會有最後的狀態,在使用時還能支持不一樣的序列個數。
"""
#lstm_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
#outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x1, dtype=tf.float32)
"""
#2 LSTMCell,LSTM實現的一個高級版本(use_peepholes:默認False,True表示啓用peephole鏈接)
cell_clip:是否在輸出前對cell狀態按照給定值進行截斷處理
initializer:指定初始化函數
num_proj:經過projection進行模型壓縮的輸出維度
proj_clip:將num_proj按照給定的proj_clip截斷
"""
#lstm_cell = tf.contrib.rnn.LSTMCell(n_hidden, forget_bias=1.0)
#outputs, states = tf.contrib.rnn.static_rnn(lstm_cell, x1, dtype=tf.float32)

#3 gru類定義
#gru = tf.contrib.rnn.GRUCell(n_hidden)
#outputs = tf.contrib.rnn.static_rnn(gru, x1, dtype=tf.float32)

#4 建立動態RNN,此時的輸入是x,是動態的[None, n_steps, n_input]LIST
#具體定義參考https://blog.csdn.net/mzpmzk/article/details/80573338
gru = tf.contrib.rnn.GRUCell(n_hidden)
outputs,_  = tf.nn.dynamic_rnn(gru,x,dtype=tf.float32)
outputs = tf.transpose(outputs, [1, 0, 2])
#取最後一條輸出信息,(outputs[-1])
pred = tf.contrib.layers.fully_connected(outputs[-1],n_classes,activation_fn = None)



learning_rate = 0.001
training_iters = 100000

display_step = 10

# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# Evaluate model
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

# 啓動session
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    step = 1
    # Keep training until reach max iterations
    while step * batch_size < training_iters:
        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, n_steps, n_input))
        # Run optimization op (backprop)
        sess.run(optimizer, feed_dict={x: batch_x, y: batch_y})
        if step % display_step == 0:
            # 計算批次數據的準確率
            acc = sess.run(accuracy, feed_dict={x: batch_x, y: batch_y})
            # Calculate batch loss
            loss = sess.run(cost, feed_dict={x: batch_x, y: batch_y})
            print ("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
                  "{:.6f}".format(loss) + ", Training Accuracy= " + \
                  "{:.5f}".format(acc))
        step += 1
    print (" Finished!")

    # 計算準確率 for 128 mnist test images
    test_len = 128
    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}))

 

 

 

-----------------------------------------------------------------------------------

相關文章
相關標籤/搜索