2016-06-13 8 views
1

今、私はテンソルフローで複数のGRU反復レイヤーを互いに連鎖しようとしています。次のエラーが表示されます。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を提供することを承知していますが、私は一種の自分のためにそれを把握したかったです。

答えて

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]) 
関連する問題