4

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

the neural network i build

-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?

Antwort

5

Ihr Hauptfehler ist network symmetry, weil Sie alle Gewichte zu Nullen initialisiert haben.Daher werden die Gewichtungen nie aktualisiert. Ändern Sie es in kleine Zufallszahlen und es beginnt zu lernen. Es ist in Ordnung, Verzerrungen mit Nullen zu initialisieren.

Ein weiteres Problem ist rein technisch: print_accuracy Funktion erstellt neue Knoten in der Computergrafik, und da Sie es in der Schleife aufrufen, wird das Diagramm aufgebläht und schließlich wird der gesamte Speicher verbrauchen.

Sie können auch mit Hyper-Parametern spielen und das Netzwerk vergrößern, um seine Kapazität zu erhöhen.

Edit: Ich entdeckte auch einen Fehler in Ihrer Genauigkeitsberechnung.

import tensorflow as tf 
from tensorflow.examples.tutorials.mnist import input_data 

x = tf.placeholder(tf.float32, [None, 784], 'images') 
y = tf.placeholder(tf.float32, [None, 10], 'labels') 

W0 = tf.Variable(dtype=tf.float32, name='InputLayerWeights', initial_value=tf.truncated_normal([784, 16]) * 0.001) 
W1 = tf.Variable(dtype=tf.float32, name='HiddenLayer1Weights', initial_value=tf.truncated_normal([16, 16]) * 0.001) 
W2 = tf.Variable(dtype=tf.float32, name='HiddenLayer2Weights', initial_value=tf.truncated_normal([16, 10]) * 0.001) 

B0 = tf.Variable(dtype=tf.float32, name='HiddenLayer1Biases', initial_value=tf.ones([16])) 
B1 = tf.Variable(dtype=tf.float32, name='HiddenLayer2Biases', initial_value=tf.ones([16])) 
B2 = tf.Variable(dtype=tf.float32, name='OutputLayerBiases', initial_value=tf.ones([10])) 

A1 = tf.nn.relu(tf.matmul(x, W0) + B0) 
A2 = tf.nn.relu(tf.matmul(A1, W1) + B1) 
y_predicted = tf.matmul(A2, W2) + B2 
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_predicted, 1)) 
correct_prediction_float = tf.cast(correct_prediction, dtype=tf.float32) 
accuracy = tf.reduce_mean(correct_prediction_float) 
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=y_predicted)) 
optimizer = tf.train.AdamOptimizer(0.001).minimize(cross_entropy) 

mnist = input_data.read_data_sets('mnist', one_hot=True) 
with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    for i in range(20000): 
    batch_x, batch_y = mnist.train.next_batch(64) 
    _, cost_val, acc_val = sess.run([optimizer, cross_entropy, accuracy], feed_dict={x: batch_x, y: batch_y}) 
    if i % 100 == 0: 
     print('cost=%.3f accuracy=%.3f' % (cost_val, acc_val)) 
+0

Ja, ich fand heraus, dass Fehler und fixiert, dass die Fehler von tf.argmax (y, 1): Es sollte hier ein vollständiges Code

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_predicted, 1)) 

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

Verwandte Themen