Siamese Networkを使用して、2つの入力が同じかどうかを調べようとしています。シャムネットワークの概要を以下に示します。私はtensorflowを使用して、以下のネットワークを作成したTensorFlowを使用してSiameseネットワークを作成するときのValueError
A siamese network is a network consisting of two identical neural networks with tied weights (the weights of the two networks are the same). Given two inputs X_1 and X_2, X_1 is fed to the first network and X_2 to the second network. Then, the outputs from the two networks are combined and produce an answer to the question: are the two inputs similar or different?
は、私はエラーを取得しています。
graph = tf.Graph()
# Add nodes to the graph
with graph.as_default():
with tf.variable_scope('siamese_network') as scope:
labels = tf.placeholder(tf.int32, [None, None], name='labels')
keep_prob = tf.placeholder(tf.float32, name='question1_keep_prob')
question1_inputs = tf.placeholder(tf.int32, [None, None], name='question1_inputs')
question1_embedding = tf.get_variable(name='embedding', initializer=tf.random_uniform((n_words, embed_size), -1, 1))
question1_embed = tf.nn.embedding_lookup(question1_embedding, question1_inputs)
question1_lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size)
question1_drop = tf.contrib.rnn.DropoutWrapper(question1_lstm, output_keep_prob=keep_prob)
question1_multi_lstm = tf.contrib.rnn.MultiRNNCell([question1_drop] * lstm_layers)
initial_state = question1_multi_lstm.zero_state(batch_size, tf.float32)
question1_outputs, question1_final_state = tf.nn.dynamic_rnn(question1_multi_lstm, question1_embed, initial_state=initial_state, scope='question1_siamese')
question1_predictions = tf.contrib.layers.fully_connected(question1_outputs[:, -1], 1, activation_fn=tf.sigmoid)
scope.reuse_variables()
question2_inputs = tf.placeholder(tf.int32, [None, None], name='question2_inputs')
question2_embedding = tf.get_variable(name='embedding', initializer=tf.random_uniform((n_words, embed_size), -1, 1))
question2_embed = tf.nn.embedding_lookup(question2_embedding, question2_inputs)
question2_lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size)
question2_drop = tf.contrib.rnn.DropoutWrapper(question2_lstm, output_keep_prob=keep_prob)
question2_multi_lstm = tf.contrib.rnn.MultiRNNCell([question2_drop] * lstm_layers)
question2_outputs, question2_final_state = tf.nn.dynamic_rnn(question2_multi_lstm, question2_embed, initial_state=initial_state)
question2_predictions = tf.contrib.layers.fully_connected(question2_outputs[:, -1], 1, activation_fn=tf.sigmoid)
私は、次の行で、次のエラーを取得しています:
問題は、次の行にあった
ValueError: Variable siamese_network/rnn/multi_rnn_cell/cell_0/basic_lstm_cell/weights does not exist,
or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?
SOLUTION:ここ
question2_outputs, question2_final_state = tf.nn.dynamic_rnn(question2_multi_lstm, question2_embed, initial_state=initial_state)
は誤りです。
question1_outputs, question1_final_state = tf.nn.dynamic_rnn(question1_multi_lstm, question1_embed, initial_state=initial_state, scope='question1_siamese')
アトリビュートをscope
だけ削除するだけで正常に動作しました。あなたは、後で使用される変数がすでに宣言され、再利用されるべきであると言っているtensorflow
scope.reuse_variables()
を呼び出す
私は 'reuse_variables()'を使用したのは、Siameseネットワークが両方のネットワークで同じ重みを必要とするからです。 – Mithun
はい、 'question2_embedding = question1_embedding'を書いて、それだけで達成できます。 – user1735003