Neural network Tensorflow,如何链接GRU层

Neural network Tensorflow,如何链接GRU层,neural-network,tensorflow,Neural Network,Tensorflow,现在我正在尝试在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", lin

现在我正在尝试在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])

我知道tensorflow提供了一个多线程来实现这一点,但我有点想自己解决它。

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

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])
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])