【问题标题】:Tensorflow, how to chain GRU layersTensorflow,如何链接 GRU 层
【发布时间】:2016-06-13 14:18:25
【问题描述】:

现在我正在尝试在 tensorflow 中将多个 GRU 循环层相互链接。我收到以下错误。

ValueError: Variable GRUCell/Gates/Linear/Matrix already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

  File "/home/chase/workspace/SentenceEncoder/sent_enc.py", line 42, in <module>
    output, states[i] = grus[i](output, states[i])

这是我的代码。

x = tf.placeholder(tf.float32, (batch_size, time_steps, vlen), 'x')
y_exp = tf.placeholder(tf.float32, (batch_size, time_steps, vlen), 'y_exp')

with tf.name_scope('encoder'):
    gru_sizes = (128, 256, 512)
    grus = [tf.nn.rnn_cell.GRUCell(sz) for sz in gru_sizes]
    states = [tf.zeros((batch_size, g.state_size)) for g in grus]
    for t in range(time_steps):
        output = tf.reshape(x[:, t, :], (batch_size, vlen))
        for i in range(len(grus)):
            output, states[i] = grus[i](output, states[i])

我知道 tensorflow 提供了 MultiRNNCell 来执行此操作,但我有点想自己弄清楚。

【问题讨论】:

    标签: neural-network tensorflow


    【解决方案1】:

    我设法修复它。我需要为每一层添加不同的变量范围。我还需要在第一个时间步之后重用变量。

    x = tf.placeholder(tf.float32, (batch_size, time_steps, vlen), 'x')
    y_exp = tf.placeholder(tf.float32, (batch_size, time_steps, vlen), 'y_exp')
    
    with tf.name_scope('encoder'):
        gru_sizes = (128, 256, 512)
        grus = [tf.nn.rnn_cell.GRUCell(sz) for sz in gru_sizes]
        states = [tf.zeros((batch_size, g.state_size)) for g in grus]
        for t in range(time_steps):
            output = tf.reshape(x[:, t, :], (batch_size, vlen))
            for i in range(len(grus)):
                with tf.variable_scope('gru_' + str(i), reuse = t > 0):
                    output, states[i] = grus[i](output, states[i])
    

    【讨论】:

      猜你喜欢
      • 2021-09-17
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      相关资源
      最近更新 更多