Ich lese dieses tutorial auf, wie man ein schnelles neurales Netz mit TensorFlow bildet, und es funktioniert groß.Wie werden Variablen zu tf.trainable_variables hinzugefügt?
Aber ich wollte mehr darüber verstehen, wie es funktioniert hat.
Im Code definieren wir das neuronale Netz mit:
def neural_network_model(data):
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl0, n_nodes_hl1])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}
output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
'biases':tf.Variable(tf.random_normal([n_classes]))}
l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']), hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']), hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2,hidden_3_layer['weights']), hidden_3_layer['biases'])
l3 = tf.nn.relu(l3)
output = tf.matmul(l3,output_layer['weights']) + output_layer['biases']
return output
und schließlich
dann laufencost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
optimizer = tf.train.AdamOptimizer().minimize(cost)
ich, um herauszufinden, wollen, wie AdamOptimizer weiß, was von ihnen da keine ändern Matrizes zu werden in die Minimierungsfunktion übergeben.
So sah ich AdamOptimizer und fand heraus, dass minimize
einen optionalen Parameter hat:
var_list: Optional list or tuple of Variable objects to update to minimize loss. Defaults to the list of variables collected in the graph under the key GraphKeys.TRAINABLE_VARIABLES.
Also ich aufsah GraphKeys.TRAINABLE_VARIABLES und gefunden:
When passed trainable=True, the Variable() constructor automatically adds new variables to the graph collection GraphKeys.TRAINABLE_VARIABLES. This convenience function returns the contents of that collection.
Also ich eine Suche tat für der Begriff trainable
in meinem Code und nichts gefunden.
Wie also in der Welt weiß der AdamOptimizer, was er optimieren sollte?