タイトルが示唆しているように、tensorboardの内部のグラフを見ると、variable_scope内に作成した変数が2回再作成されます。何故ですか?私は何を間違えているのですか?あなたは「NN」範囲は任意の入力に接続されていない私の隠された層で二回作成されたことがわかりますが、Adagradオプティマイザに影響を与えることができvariable_scopeの下にオプティマイザ変数を導入すると、2回再作成されるのはなぜですか?
:
def weights_biases(weights_shape, biases_shape):
weights = tf.get_variable("weights", weights_shape, initializer = tf.random_normal_initializer())
biases = tf.get_variable("biases", biases_shape, initializer = tf.random_normal_initializer())
return weights, biases
def hl_relu(input_tensor, weights_shape, biases_shape):
weights, biases = weights_biases(weights_shape, biases_shape)
regression = tf.matmul(input_tensor, weights) + biases
return tf.nn.relu(regression)
def neural_network_model(x):
# W = tf.Variable(
# tf.truncated_normal([vocab_size, embedding_size], stddev=1/math.sqrt(vocab_size)),
# name="W")
# embedded = tf.nn.embedding_lookup(W, x)
# embedding_aggregated = tf.reduce_sum(embedded, [1])
with tf.variable_scope("hidden_layer_1"):
relu1 = hl_relu(x, [max_words_len, n_nodes_hl1], [n_nodes_hl1])
with tf.variable_scope("hidden_layer_2"):
relu2 = hl_relu(relu1, [n_nodes_hl1,n_nodes_hl2], [n_nodes_hl2])
with tf.variable_scope("hidden_layer_3"):
relu3 = hl_relu(relu2, [n_nodes_hl2,n_nodes_hl3], [n_nodes_hl3])
with tf.variable_scope("output_layer"):
weights, biases = weights_biases([n_nodes_hl3, n_classes], [n_classes])
output_regression = tf.matmul(relu3, weights) + biases
return output_regression
def train_neural_network(test_x, test_y):
with tf.device("/cpu:0"):
custom_runner = CustomRunner()
x_batch, y_batch = custom_runner.get_inputs()
with tf.variable_scope("test"):
testX = tf.constant(test_x, name="testX")
testX = tf.cast(testX, tf.float32)
testY = tf.constant(test_y, name="testY")
testY = tf.cast(testY, tf.float32)
with tf.variable_scope("nn") as scope:
global_step = tf.Variable(0, trainable=False, name='global_step')
logits = neural_network_model(x_batch)
scope.reuse_variables()
test_logits = neural_network_model(testX)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, y_batch), name="cost")
tf.scalar_summary('cost', cost)
optimizer = tf.train.AdagradOptimizer(0.01).minimize(cost, global_step = global_step)
は、次の異常を生成しますそれらの初期化されたランダムな重みを常に提供することによって。私はこれもトレーニングを遅くすると思う。
私のコードで何が間違っていますか?
残念ながら彼らはそうです。これらの行は文脈から外れているように見えますが、私の謝罪です。しかし、とにかく、たとえ私がこの2つのラインを発言したとしても、あなたはこの問題について言及しています。私はなぜこれが起こるかについて完全に失われています....それは私がスコープを使用するときだけです。スコープがなければ、それは起こりません。 QueueRunnerフィードを使用したい場合、スコープを使用する必要があるという問題があります。これは、上記の2つの行を持つ理由です。 – schone
また、adamとadadeltaは別の変数セットを作成することに注意してください。作成されたモデルファイルを見ると、それらのメソッドを使用している間にモデルファイルのサイズが2倍になっています。彼らはそれが彼らの方法が働く方法であるので、これを行う必要があります。 – Steven
私はそれを完全に購入します。スコープを使用しないときは、上のスクリーンショットで参照した余分な変数は表示されません。私は余分なものにupdate_のプレフィックスが付いているのを見るだけです。それらは上記のものではありません。 – schone