如何将最后一个输出y(t-1)作为输入馈送以在张量流RNN中生成y(t)?

时间:2016-02-02 05:22:55

标签: tensorflow recurrent-neural-network

我想在Tensorflow中设计单层RNN,以便最后输出(y(t-1))参与更新隐藏状态。

h(t) = tanh(W_{ih} * x(t) + W_{hh} * h(t) + **W_{oh}y(t - 1)**)
y(t) = W_{ho}*h(t)

如何输入最后一个输入y(t - 1)作为更新隐藏状态的输入?

3 个答案:

答案 0 :(得分:2)

y(t-1)是最后一个输入还是输出?在这两种情况下,它都不是TensorFlow RNN细胞抽象的直接拟合。如果您的RNN很简单,您可以自己编写循环,然后您就可以完全控制。我将使用的另一种方法是预处理您的RNN输入,例如,执行以下操作:

  

processed_input [t] = tf.concat(input [t],input [t-1])

然后使用processed_input调用RNN单元并在那里拆分。

答案 1 :(得分:0)

一种可能性是使用我在article中找到的tf.nn.raw_rnn。检查我对此related post的回答。

答案 2 :(得分:0)

我将您所说的称为“自回归RNN”。这是一个(不完整的)代码段,显示了如何使用tf.nn.raw_rnn创建一个代码段:

import tensorflow as tf

LSTM_SIZE = 128
BATCH_SIZE = 64
HORIZON = 10

lstm_cell = tf.nn.rnn_cell.LSTMCell(LSTM_SIZE, use_peepholes=True)


class RnnLoop:
    def __init__(self, initial_state, cell):
        self.initial_state = initial_state
        self.cell = cell

    def __call__(self, time, cell_output, cell_state, loop_state):
        emit_output = cell_output  # == None for time == 0
        if cell_output is None:  # time == 0
            initial_input = tf.fill([BATCH_SIZE, LSTM_SIZE], 0.0)
            next_input = initial_input
            next_cell_state = self.initial_state
        else:
            next_input = cell_output
            next_cell_state = cell_state

        elements_finished = (time >= HORIZON)
        next_loop_state = None
        return elements_finished, next_input, next_cell_state, emit_output, next_loop_state


rnn_loop = RnnLoop(initial_state=initial_state_tensor, cell=lstm_cell)
rnn_outputs_tensor_array, _, _ = tf.nn.raw_rnn(lstm_cell, rnn_loop)
rnn_outputs_tensor = rnn_outputs_tensor_array.stack()

这里,我们用一些向量initial_state_tensor初始化LSTM的内部状态,并将零数组作为输入输入t=0。之后,当前时间步的输出就是下一个时间步的输入。

相关问题