了解tensorflow basicLSTMCell参数输入

时间:2017-10-30 09:11:36

标签: machine-learning tensorflow rnn

我在理解张量流BasicLSTMCell num_units输入参数时遇到了一些麻烦。

我已经看过其他帖子,但我没有关注,所以希望一个简单的例子会有所帮助。

所以说我们下面有以下LTSM RNN模型。如何确定单元格所需的单位数?是否有可能为LTSM RNN建立这样的结构?

   Input Vec       1st Hidden Layer     2nd Hidden Layer     Output
   20 x 1          20 x 1               5 x 1                3 x 1

1 个答案:

答案 0 :(得分:2)

关注,我已经使用动态rnn(https://www.tensorflow.org/api_docs/python/tf/nn/dynamic_rnn

为您的模型提供了示例代码
N_INPUT = 20
N_TIME_STEPS = #Define here
N_HIDDEN_UNITS1 = 20
N_HIDDEN_UNITS2 = 5
N_OUTPUT =3

input = tf.placeholder(tf.float32, [None, N_TIME_STEPS, N_INPUT], name="input")

lstm_layers = [tf.contrib.rnn.BasicLSTMCell(N_HIDDEN_UNITS1, forget_bias=1.0),tf.contrib.rnn.BasicLSTMCell(N_HIDDEN_UNITS2, forget_bias=1.0),tf.contrib.rnn.BasicLSTMCell(N_OUTPUT, forget_bias=1.0)]
lstm_layers = tf.contrib.rnn.MultiRNNCell(lstm_layers)

outputs, _ = tf.nn.dynamic_rnn(lstm_layers, input, dtype=tf.float32)

模型的输入(输入)应为 [BATCH_SIZE,N_TIME_STEPS,N_INPUT] 和输出(代码中的输出) RNN的形状为 [BATCH_SIZE,N_TIME_STEPS,N_OUTPUT]

希望这有帮助。