2016-06-13 4 views
1

지금은 tensorflow에서 여러 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]) 
관련 문제