tensorflow学习之dynamic_rnn使用详解

版权声明:微信公众号:数据挖掘与机器学习进阶之路。本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013230189/article/details/82814235

tf.nn.dynamic_rnn

使用指定的RNNCell单元创建一个循环神经网络,对输入执行完全动态展开。

tf.nn.dynamic_rnn(

    cell,
    inputs,
    sequence_length=None,
    initial_state=None,
    dtype=None,
    parallel_iterations=None,
    swap_memory=False,
    time_major=False,
   scope=None

)

参数说明:

  • cell:一个RNNCell实例
  • Inputs:RNN的输入。如果time_major==False(默认),张量的形状为:[batch_size,max_time,…]或这些元素的嵌套的tuple。如果time_major==True,张量的形状为:[max_time,batch_size,…]或者这些元素的嵌套的tuple。也可能是满足此属性的Tensors(可能是嵌套的)元组。每个时间步的单元输入的是张量或者是维度为[batch_size,…]的张量的tuple
  • Sequence_length:可选参数。一个大小为[batch_size]的int32/int64类型向量。表示每个输入样本长度,如时间步长。更多的考虑到性能而不是正确性。
  • Initial_state:可选参数。一个针对RNN的初始状态。
  • dtype:初始状态和预期输出的数据类型。
  • parallel_iteratioins:默认为32。并行运行的迭代次数。用于那些没有任何时间依赖性并且可以并行运行的操作。该参数是使用空间换时间。值>1则使用更多内存但占用更少时间。使用较小的值则使用较少内存但计算时间较长。
  • swap_memory:透明地交换前向推理中产生的张量,但对于后向传播,需要从GPU到CPU。这允许训练通常不适合单个GPU的RNN,具有非常小的性能损失。
  • time_major:指定输入和输出张量的形状格式。如果为True,张量必须形如[max_time,batch_size,depth].如果为False,张量必须形如:[batch_size,max_time,depth]。使用time_major=True会更有效,因为它避免了RNN计算开始和结束时的转置。但是,大多数Tensorflow数据都是batch_size为major的数据,因此,默认情况下,此函数以batch-major形式接受输入和输出。
  • Scope:用于创建子图的VariableScope,默认是”rnn”。

返回:

一个(outputs,state)对,其中:

  • outputs:如果time_major=False(默认),则是一个张量,形如:[batch_size,max_time,cell.output.size];如果time_major=True则形如[max_time,batch_size,cell.output_size]。

注意:如果cell.output_size是整数或TensorShape对象的元组,则输出将是具有与cell.output_size相同结构的元组,包含的张量的形状也同cell.output_size。

  • state:最终的状态。如果cell.state_size是一个整数,则形如[batch_size,cell.state_size]。如果是一个TensorShape,则形如[batch_size]+cell.state_size.如果是一个整数或者TensorShape的元组,则它是一个有着相应形状的元组。如果cell是LSTMCells.则state对于每个cell来说是一个包含一个LSTMStateTuple的元组。

 

代码实例1:

batch_size=10 #批处理大小

hidden_size=100 #隐藏层神经元

max_time=40 #最大时间步长

depth=400 #输入层神经元数量,如词向量维度

input_data=tf.Variable(tf.random_normal([max_time,batch_size,depth]))
# create a BasicRNNCell
rnn_cell = tf.nn.rnn_cell.BasicRNNCell(hidden_size)


# 'outputs' is a tensor of shape [batch_size, max_time, cell_state_size]

# defining initial state

initial_state = rnn_cell.zero_state(batch_size, dtype=tf.float32)

# 'state' is a tensor of shape [batch_size, cell_state_size]

outputs, state = tf.nn.dynamic_rnn(rnn_cell, input_data,

                                   initial_state=initial_state,

                                   dtype=tf.float32,time_major=True)               
print(outputs.shape) #(40, 10, 100)

print(state.shape) #(10, 100)

 

代码实例2:

batch_size=10 #批处理大小

hidden_size=100 #隐藏层神经元

max_time=40 #最大时间步长

depth=400 #输入层神经元数量,如词向量维度

input_data=tf.Variable(tf.random_normal([max_time,batch_size,depth]))

lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(hidden_size)

# 'outputs' is a tensor of shape [batch_size, max_time, cell_state_size]

# defining initial state

initial_state = lstm_cel.zero_state(batch_size, dtype=tf.float32)

# 'state' is a tensor of shape [batch_size, cell_state_size]

outputs, state = tf.nn.dynamic_rnn(lstm_cell, input_data,

                                   initial_state=initial_state,

                                   dtype=tf.float32,time_major=True)                

print(outputs.shape) #(40, 10, 100)

print(state.c) #Tensor("rnn_4/while/Exit_3:0", shape=(10, 100), dtype=float32)

print(state.h) #Tensor("rnn_4/while/Exit_4:0", shape=(10, 100), dtype=float32)

猜你喜欢

转载自blog.csdn.net/u013230189/article/details/82814235