[Reserved] tensorflow single-layer static and dynamic comparative RNN

Original Address:

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

 

yanghedada

 

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

 

 

 

 

static_rnn 和 dynamic_rnn

 

1:     static_rnn

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_rnn

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: is the number of neurons in a Cell, forget_bias: Forget door how many remember, 1.0 on behalf of all remember)
 
 

tf.contrib.rnn.static_rnn:
Static rnn means that the number of samples in a time sequence (n_steps) to expand, creating (n_steps) sequences in the cell of FIG.
 
 

tf.nn.dynamic_rnn:
Dynamic rnn mean just create a sequence RNN sample, other sequence data will enter operation by the RNN cycle through the static generated RNN network, the time required for the generation process will be longer, there is a network share the memory will be more, export model will be even greater. With the model information of the sequence will intermediate state, facilitate debugging. Must be equal to the number of samples of the training sequence in use. RNN dynamically generated by the network, the take up less memory. The model will only be the last state, but also support in the use of different numbers of sequences.



 
 
 

the difference

1.tf.nn.dynamic_rnn and tf.contrib.rnn.static_rnn different input formats.
2.tf.nn.dynamic_rnn tf.contrib.rnn.static_rnn with different output formats.
3.tf.nn.dynamic_rnn and tf.contrib.rnn.static_rnn internal training methods.




 
 
 

Please carefully compare the following differences:

You can refer to: https://blog.csdn.net/mzpmzk/article/details/80573338

 
 
 
 
 
 
 
 
 

Dynamic 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}))

 

 

 

 

Static 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)
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}))

 

 

 

 

 

 

 

 

This code is derived:
Kevin self TensorFlow

 

# -*- 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
tf.placeholder = x ( " a float " , [None, n_steps, n_input]) 
Y = tf.placeholder ( " a float " , [None, n_classes])
 # Reset x to suit the required tf.contrib.rnn.static_rnn format 
# x1 = tf.unstack (the X-, n_steps, 1) 

# BasicLSTMCell (NUM_UNITS: is the number of neurons in a Cell, forget_bias: Remember forgotten how many doors, on behalf of all remember 1.0) 
# static (tf.contrib .rnn.static_rnn) means that the number of samples in a time sequence (n_steps) to expand, creating (n_steps) th cell sequence in FIG.; 
# dynamic (tf.nn.dynamic_rnn) is meant to create only a sequence of sample RNN, other sequence data will enter operation by the RNN cycle 
"" " 
static generated RNN network, the time required for the generation process will be longer, more memory will be occupied by the network, export model will be even greater 
. model information will be with the first intermediate state sequence, useful for debugging must be equal to the number of samples when using the training sequence by dynamic 
state generated RNN network, occupies less memory. models will have the last state, when using can support different Sequence number.
"" " 
# Lstm_cell = tf.contrib.rnn.BasicLSTMCell (n_hidden, forget_bias = 1.0) 
# Outputs, tf.contrib.rnn.static_rnn States = (lstm_cell, X1, DTYPE = tf.float32) 
" "" 
# 2 LSTMCell, an advanced version of the LSTM implemented (use_peepholes: default False, True represents enable peephole connection) 
cell_clip: whether cell state was conducted before the output of a given value truncation process 
initializer: specified initialization function 
num_proj: the model compressed output dimensions by Projection 
proj_clip : the num_proj proj_clip accordance with a given truncation 
"" " 
# lstm_cell = tf.contrib.rnn.LSTMCell (n_hidden, forget_bias = 1.0) 
# Outputs, tf.contrib.rnn.static_rnn States = (lstm_cell, X1, DTYPE = TF. float32) 

# . 3 GRU class definition 
# GRU = tf.contrib.rnn.GRUCell (n_hidden) 
#= tf.contrib.rnn.static_rnn Outputs (GRU, X1, DTYPE = tf.float32) 

# . 4 to create dynamic RNN, in this case the input is x, is dynamic [None, n_steps, n_input] the LIST 
# specific definition Reference 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 ])
 # take the last output, (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 # the 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}))

 

 

 

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

 

Guess you like

Origin www.cnblogs.com/devilmaycry812839668/p/11109045.html