现在我正试图在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,但我有点想为自己解决这个问题。
答案 0 :(得分: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])