Ich lerne TensorFlow und implementierte ein einfaches neuronales Netzwerk, wie in MNIST für Anfänger in TensorFlow-Dokumenten erklärt. Hier ist die link. Die Genauigkeit war wie erwartet 80-90%.Warum lernt dieses neuronale Netzwerk nichts?
Dann folgte der gleiche Artikel war MNIST für Experten mit ConvNet. Anstatt das zu implementieren, entschied ich mich, den Anfängerteil zu verbessern. Ich kenne Neuronale Netze und wie sie lernen und die Tatsache, dass tiefe Netzwerke besser funktionieren als flache Netzwerke. Ich habe das ursprüngliche Programm in MNIST für Beginner modifiziert, um ein neurales Netzwerk mit 2 versteckten Schichten von jeweils 16 Neuronen zu implementieren.
Es sieht ungefähr so aus:
Bild von Netzwerk
-Code für sie
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
x = tf.placeholder(tf.float32, [None, 784], 'images')
y = tf.placeholder(tf.float32, [None, 10], 'labels')
# We are going to make 2 hidden layer neurons with 16 neurons each
# All the weights in network
W0 = tf.Variable(dtype=tf.float32, name='InputLayerWeights', initial_value=tf.zeros([784, 16]))
W1 = tf.Variable(dtype=tf.float32, name='HiddenLayer1Weights', initial_value=tf.zeros([16, 16]))
W2 = tf.Variable(dtype=tf.float32, name='HiddenLayer2Weights', initial_value=tf.zeros([16, 10]))
# All the biases for the network
B0 = tf.Variable(dtype=tf.float32, name='HiddenLayer1Biases', initial_value=tf.zeros([16]))
B1 = tf.Variable(dtype=tf.float32, name='HiddenLayer2Biases', initial_value=tf.zeros([16]))
B2 = tf.Variable(dtype=tf.float32, name='OutputLayerBiases', initial_value=tf.zeros([10]))
def build_graph():
"""This functions wires up all the biases and weights of the network
and returns the last layer connections
:return: returns the activation in last layer of network/output layer without softmax
"""
A1 = tf.nn.relu(tf.matmul(x, W0) + B0)
A2 = tf.nn.relu(tf.matmul(A1, W1) + B1)
return tf.matmul(A2, W2) + B2
def print_accuracy(sx, sy, tf_session):
"""This function prints the accuracy of a model at the time of invocation
:return: None
"""
correct_prediction = tf.equal(tf.argmax(y), tf.argmax(tf.nn.softmax(build_graph())))
correct_prediction_float = tf.cast(correct_prediction, dtype=tf.float32)
accuracy = tf.reduce_mean(correct_prediction_float)
print(accuracy.eval(feed_dict={x: sx, y: sy}, session=tf_session))
y_predicted = build_graph()
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=y_predicted))
model = tf.train.GradientDescentOptimizer(0.03).minimize(cross_entropy)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for _ in range(1000):
batch_x, batch_y = mnist.train.next_batch(50)
if _ % 100 == 0:
print_accuracy(batch_x, batch_y, sess)
sess.run(model, feed_dict={x: batch_x, y: batch_y})
Der Output erwartet sollte besser sein als das, was mit erreicht werden könnte wenn nur eine einzige Schicht (angenommen, dass W0 die Form von [784,10] und B0 die Form von [10] hat)
def build_graph():
return tf.matmul(x,W0) + B0
Stattdessen sagt die Ausgabe, dass das Netzwerk überhaupt nicht trainierte. Die Genauigkeit überschritt in keiner Iteration 20%.
Output
Extrahierung MNIST_data/Zug-Bilder-IDX3-ubyte.gz
Extracting MNIST_data/Zug-Etiketten-idx1-ubyte.gz
Extracting MNIST_data/T10K-Bilder- IDX3-ubyte.gz
Extrahierung MNIST_data/T10K-Etiketten-idx1-ubyte.gz
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
0,1
Meine Frage
Was mit dem obigen Programm falsch ist, dass es überhaupt nicht verallgemeinern? Wie kann ich es verbessern, ohne faltungsneurale Netzwerke zu benutzen?
Ja, ich fand heraus, dass Fehler und fixiert, dass die Fehler von tf.argmax (y, 1): Es sollte hier ein vollständiges Code
sein. Lassen Sie mich Ihr Programm überprüfen und sehen, ob es funktioniert. Ich möchte auch fragen, warum die Initialisierung von wights auf Null gesetzt wird, falls keine versteckte Ebene funktioniert? Und das funktioniert auch nicht in Multilayern – coder3101