2017-10-30 8 views
0

ここで私のコードはテンソルフローです。私はBi-LSTMを定義しました。特定のタスクでグラフをループする必要があります。スコープ変数でreuse = Trueを設定していますが、コードの下に記載されているエラーが生成されます。TensorFlow再利用可変スコープ

for run in range(0, 2): 


    with tf.variable_scope("LSTM", reuse= True) as scope: 

    def LSTM(input_data): 



     LSTM_cell_fw= tf.contrib.rnn.BasicLSTMCell(num_units= hidden_size) 
     LSTM_cell_bw= tf.contrib.rnn.BasicLSTMCell(num_units= hidden_size)   
     output, states = tf.nn.bidirectional_dynamic_rnn(LSTM_cell_fw, LSTM_cell_bw, inputs= input_data, dtype=tf.float32) 
     output_1= output[0] 
     output_2= output[1] 
     output_1= output_1[-1, -1, :] 
     output_1= tf.reshape(output_1, shape= (1, hidden_size)) 
     output_2= output_2[-1, -1, :] 
     output_2= tf.reshape(output_2, shape= (1, hidden_size)) 
     fin_output= tf.concat((output_1, output_2), axis=1) 

     return fin_output 

とエラーになります:ValueErrorを:可変bidirectional_rnn/FW/basic_lstm_cell /カーネルはすでに禁止さ、存在しています。 VarScopeでreuse = Trueを設定することを意味しましたか?もともとで定義:

ファイル "alpha-rep.py"、ライン65は、LSTM 出力で、= tf.nn.bidirectional_dynamic_rnn(LSTM_cell_fw、LSTM_cell_bw、入力= INPUT_DATA、DTYPE = tf.float32) ファイル」を述べて変数を再利用する アウト= LSTM(INPUT_DATA)

答えて

1

で、「alpha-rep.pyライン77、あなた最初は、あなたがそれを再利用することができる唯一のことの後、それを定義する必要があります。

変数を定義するための関数を定義:

def LSTM(input_data): 
    LSTM_cell_fw= tf.contrib.rnn.BasicLSTMCell(num_units= hidden_size) 
    LSTM_cell_bw= tf.contrib.rnn.BasicLSTMCell(num_units= hidden_size)   
    output, states = tf.nn.bidirectional_dynamic_rnn(LSTM_cell_fw, LSTM_cell_bw, inputs= input_data, dtype=tf.float32) 
    output_1= output[0] 
    output_2= output[1] 
    output_1= output_1[-1, -1, :] 
    output_1= tf.reshape(output_1, shape= (1, hidden_size)) 
    output_2= output_2[-1, -1, :] 
    output_2= tf.reshape(output_2, shape= (1, hidden_size)) 
    return tf.concat((output_1, output_2), axis=1) 

その後、変数を定義し、必要な範囲の下にそれを置くために最初の時間のためにそれを呼び出す:

with tf.variable_scope("LSTM", reuse=False) as scope: 
    first = LSTM(your_input_here) 

今、あなたは他を定義することができます既に定義されている変数を再利用して、同じスコープのレイヤーを作成することができます。

with tf.variable_scope("LSTM", reuse=True) as scope: 
    second = LSTM(your_other_input_here) 
+0

ありがとうございました。たとえば、グラフが動作している最初の時間(run = 0)を意味します。再利用はfalseになります。実行したときに0を返します。 – Maz

+0

それは完璧なおかげで働いた – Maz

+0

ようこそ! – nessuno

関連する問題