2017-08-13 10 views
1

イメージ分類を行うRNNモデルを構築しています。私はパイプラインを使ってデータを入力しました。入力パイプラインとRNNを実現する多くの例がありませんので、しかし、それはテンソルフローRNNの実装

ValueError: Variable rnn/rnn/basic_rnn_cell/weights already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at: 

を返す私は、私はこれを修正するために何ができるのだろうか。プレースホルダを使用するとうまくいくことがわかりますが、私のデータは既にテンソルの形になっています。プレースホルダにテンソルを与えることができない限り、私はパイプラインを使うだけです。

def RNN(inputs): 
with tf.variable_scope('cells', reuse=True): 
    basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=batch_size) 

with tf.variable_scope('rnn'): 
    outputs, states = tf.nn.dynamic_rnn(basic_cell, inputs, dtype=tf.float32) 

fc_drop = tf.nn.dropout(states, keep_prob) 

logits = tf.contrib.layers.fully_connected(fc_drop, batch_size, activation_fn=None) 

return logits 

#Training 
with tf.name_scope("cost_function") as scope: 
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=train_label_batch, logits=RNN(train_batch))) 
    train_step = tf.train.MomentumOptimizer(learning_rate, 0.9).minimize(cost) 


#Accuracy 
with tf.name_scope("accuracy") as scope: 
    correct_prediction = tf.equal(tf.argmax(RNN(test_image), 1), tf.argmax(test_image_label, 0)) 
    accuracy = tf.cast(correct_prediction, tf.float32) 

答えて

2

reuseオプションを正しく使用する必要があります。それに続く変更はそれを解決するでしょう。予測のためには、既に存在する変数をグラフに使用する必要があります。

def RNN(inputs, reuse): 
    with tf.variable_scope('cells', reuse=reuse): 
     basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=batch_size, reuse=reuse) 

    ... 

... 
#Training 
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=train_label_batch, logits=RNN(train_batch, reuse=None))) 

#Accuracy 
... 
    correct_prediction = tf.equal(tf.argmax(RNN(test_image, reuse=True), 1), tf.argmax(test_image_label, 0)) 
+0

私を助けてくれてありがとうを、私はちょうど別のエラーを得たこと basic_cell = tf.contrib.rnn.BasicRNNCell(NUM_UNITS = BATCH_SIZE、リユース=再利用) はTypeError:__init __()予期しないキーワード引数を得ました「再利用」 これに対処する方法は分かりますか? APIをチェックしたところ、実際には再利用thoという引数が必要です。 – ALeex

+0

テンソルフローのバージョンを1.2.0 –

+0

にアップグレードすることをお勧めします。どうもありがとうございます。再利用メカニズムの仕組みを簡単に説明できますか? – ALeex

関連する問題