tensorflow中的RNN与LSTM函数异同点分析

时间:2021-05-29 19:06:44
import tensorflow as tf
import numpy as np

# X = np.random.randn(2, 5, 2)
X = np.array([[[0.1,0.2],[0.3,0.4],[0.5,0.6],[0.7,0.8],[0.9,0.1]],[[0.1,0.9],[0.8,0.7],[0.6,0.5],[0.4,0.3],[0.2,0.1]]])

#################################################################################
################ 测试RNN ################

xRNN = tf.unstack(X,axis=1) # 转为成list input [batch_size, input_size]

cellRNN = tf.contrib.rnn.BasicRNNCell(4)

outputsRNN, last_statesRNN = tf.contrib.rnn.static_rnn(
cell=cellRNN,
dtype=tf.float64,
sequence_length=None,
inputs=xRNN)
resultRNN = tf.contrib.learn.run_n(
{"outputs": outputsRNN, "last_states": last_statesRNN},
n=1,
feed_dict=None)
print 'RNN的输入情况:'
print len(resultRNN)
print resultRNN[0]
print



#################################################################################
################ 测试LSTM ################
xLSTM = X # input [batch_size,max_time, input_size]
# 第二个example长度为3
xLSTM[1,3:] = 0
xLSTM_lengths = [5, 3]

cellLSTM = tf.contrib.rnn.BasicLSTMCell(num_units=4, state_is_tuple=True)
outputsLSTM, last_statesLSTM = tf.nn.dynamic_rnn(
cell=cellLSTM,
dtype=tf.float64,
sequence_length=xLSTM_lengths,
inputs=xLSTM)

resultLSTM = tf.contrib.learn.run_n(
{"outputs": outputsLSTM, "last_states": last_statesLSTM},
n=1,
feed_dict=None)
print 'LSTM的输入情况:'
print len(resultLSTM)
print resultLSTM[0]
print


不同点:

1,是否可以处理变长数据的情况

2,是否可以添加遗忘值

3,输入的维度不一样


RNN

很明显,这个输入是一个list,len(list)=步长 list[0].shape=[batch,input]
这个输出和输入一样是个list,len(list)=步长,list[0].shape=[batch,hidden]
所以output=outputs[-1] 就取到了最后一步的ouput



LSTM

输入是    [[batch,步长,input]
输出是   [batch,n_steps,n_hidden]
需要tf.transpose(outputs, [1, 0, 2]),这样就可以取到最后一步的output

 outputs = tf.unstack(tf.transpose(outputs, [1, 0, 2])) # 将tensor解开成list
 results = tf.matmul(outputs[-1],weights['out']) + biases['out']