首页 文章

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

提问于
浏览
8

我想在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

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

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

    然后使用processed_input调用RNN单元并在那里进行拆分 .

  • 0

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

  • 2

    我会打电话给你所描述的"autoregressive RNN" . 这是一个(不完整的)代码段,显示了如何使用_2545225创建一个:

    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 处将零数组作为输入 . 之后,当前时间步的输出是下一个时间步的输入 .

相关问题