如何在单个层中使用多个RNN单元?

时间:2018-04-16 12:31:40

标签: tensorflow

我想在我的模型中使用多个GRU单元格。但不是堆叠格式! 也就是说,例如,有两种类型的输入,即单词序列和POS功能。我想将它们分别送入2个独立的GRU单元,然后使用输出继续工作。当我自定义序列模型时,我必须在每个时间步骤获得2个输出。 现在我的代码是:

def create_cell(self, Scope, cell_size):
    with tf.variable_scope(Scope, reuse = None):
        if not self.forward_only and self.dropout_keep_prob < 1.0:
            single_cell = GRUCell(cell_size)
            single_cell = DropoutWrapper(single_cell, input_keep_prob = dropout_keep_prob, output_keep_prob = dropout_keep_prob)
        else:
            single_cell = GRUCell(cell_size)
    return single_cell

这是GRU细胞的发生器。

for type in types:
    grus[type] = self.create_cell(type, size)
    states[type] = grus[type].zero_state(1, tf.float32)

我使用dict为不同类型的输入存储不同的GRU。

for step in range(self.max_sequence_length):
    for i in range(len(types)):
        type = types[i]
        curgru = grus[type]
        out, state = curgru(input[:,step,:], states[type])
        states[type] = state

然而,第一次执行循环(我的意思是i == 0)它的工作原理。然后,当i == 1时,out, state = curgru(input[:,step,:], states[type])行会引发错误: gru_cell/gates/kernel already exists, disallowed. Did you mean to set reuse=True in VarScope? 但我不想在不同类型的输入之间重用GRU单元。那么我该怎么做才能解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

我修好了= =。 GRUCell的函数调用()有一个参数'scope',我给它调整了与curgru相同的变量范围。