我有一个三维张量的形状[batch, None, dim],其中第二个维度,即时间步长,是未知的.我dynamic_rnn用来处理这样的输入,如下面的代码片段所示:
import numpy as np
import tensorflow as tf
batch = 2
dim = 3
hidden = 4
lengths = tf.placeholder(dtype=tf.int32, shape=[batch])
inputs = tf.placeholder(dtype=tf.float32, shape=[batch, None, dim])
cell = tf.nn.rnn_cell.GRUCell(hidden)
cell_state = cell.zero_state(batch, tf.float32)
output, _ = tf.nn.dynamic_rnn(cell, inputs, lengths, initial_state=cell_state)
Run Code Online (Sandbox Code Playgroud)
实际上,运行这个剪切了一些实际数字,我有一些合理的结果:
inputs_ = np.asarray([[[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]],
[[6, 6, 6], [7, 7, 7], [8, 8, 8], [9, 9, 9]]],
dtype=np.int32)
lengths_ = np.asarray([3, 1], …Run Code Online (Sandbox Code Playgroud) python neural-network deep-learning tensorflow recurrent-neural-network