http://qiita.com/northriver/items/17e936343110d392cce8 Ich habe das Tutorial für Anfänger oben ausprobiert, also gehe zu Deep MNIST für Experten
https://www.tensorflow.org/versions/master/tutorials/mnist/pros/index.html
Lassen Sie uns zuerst einmal lernen, genau wie für Anfänger
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
W = tf.Variable(tf.zeros([784, 10]))
B = tf.Variable(tf.zeros([10]))
#Die Initialisierung scheint etwas verkürzt zu sein
sess.run(tf.initialize_all_variables())
y = tf.nn.softmax(tf.matmul(x,W) + b)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
for i in range(1000):
batch = mnist.train.next_batch(50)
train_step.run(feed_dict={x: batch[0], y_: batch[1]})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
0.9085 Bisher die erste Bewertung
Laut MNIST-Lernen ist die Genauigkeit von 91% überraschend niedrig. Verwenden Sie ein mehrschichtiges Faltungsnetzwerk, um die Genauigkeit um bis zu 99% zu erhöhen
Da ich ein Anfänger bin, kann ich nicht verstehen, ob es eine Funktion gibt, die ich gemacht habe, also werde ich zuerst alles wiederherstellen. Erstellen Sie zunächst einen Diagrammknoten Teil 1
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
w1=tf.Variable(tf.truncated_normal([5,5,1,32],stddev=0.1))
b1=tf.Variable(0.1,[32])
x_image = tf.reshape(x,[-1,28,28,1])
#Faltschicht, Poolschicht 1
h_conv1=tf.nn.relu(tf.nn.conv2d(x_image, w1, strides=[1, 1, 1, 1], padding='SAME')+b1)
h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
Was sind die Faltschicht und die Poolschicht? Es sind diese Schichten, die den Unterschied zwischen neuronalen Netzen und tiefem Lernen ausmachen.
Die Faltungsschicht ist "wo das Eingabebild gefiltert und eine Merkmalsextraktion durchgeführt wird". Die Pooling-Schicht "verbessert die Robustheit gegen winzige Verschiebungen", oder? Verbesserte Robustheit?
Dies liegt vermutlich daran, dass beispielsweise die Streikzone erweitert wird, sodass selbst wenn die Zahl "7" in die Bildmitte geschrieben oder leicht nach links oder rechts verschoben ist, sie auf die gleiche Weise als "7" beurteilt werden kann. Es fühlt sich gut an
Die Bedeutung von [5, 5, 1, 32] ist übrigens [Breite, Höhe, Eingabe, Filter], und es scheint, dass Filter mit einer Größe von 5 x 5 auf jedes Bild angewendet werden. x_image = tf.reshape (x, [-1,28,28,1]) Es scheint, dass das, was mit der 28x28-Matrix verarbeitet wurde, in die ursprüngliche Vektorform zurückgeführt wird
Machen Sie die zweite Schicht
w2=tf.Variable(tf.truncated_normal([5,5,32,64],stddev=0.1))
b2=tf.Variable(0.1,[64])
h_conv2 =tf.nn.relu(tf.nn.conv2d(h_pool1, w2, strides=[1, 1, 1, 1], padding='SAME')+b2)
h_pool2 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
Erstellen Sie eine vollständig verbundene Ebene Zu diesem Zeitpunkt ist die Bildgröße auf 7x7 gesunken. Fügen Sie dazu eine vollständig verbundene Schicht mit 1024 neuronalen Elementen hinzu
W_fc1=tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024],stddev=0.1))
b_fc1=tf.Variable(0.1,[1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
Er möchte der Beantwortung näher kommen, während er die Funktionen so weit wie möglich verlässt, und er scheint diese verborgene Ebene einmal zu erstellen, um ein Überlernen zu vermeiden, das sich nur an die Trainingsdaten anpasst ... Ich weiß es nicht genau.
Dropout-Einstellungen ... Ich bin nicht sicher, aber es scheint notwendig
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
Ebene lesen Dies ist das gleiche wie für Anfänger
W_fc2=tf.Variable(tf.truncated_normal([1024, 10],stddev=0.1))
b_fc2=tf.Variable(0.1,[10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
Lernen und bewerten Sie das Modell
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.initialize_all_variables())
Lauf
Lauf
for i in range(20000):
batch = mnist.train.next_batch(50)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print("test accuracy %g"%accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
step 0, training accuracy 0.12 step 100, training accuracy 0.8 step 200, training accuracy 0.88 step 300, training accuracy 0.84 step 400, training accuracy 0.96 ・ ・ ・ step 19900, training accuracy 1 test accuracy 0.9914
Ich werde es 20.000 Mal machen, also wird es einige Zeit dauern. Es scheint mehrere Stunden zu dauern Genauigkeit 99%
Integrierte ver.py
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
w1=tf.Variable(tf.truncated_normal([5,5,1,32],stddev=0.1))
b1=tf.Variable(0.1,[32])
x_image = tf.reshape(x,[-1,28,28,1])
h_conv1=tf.nn.relu(tf.nn.conv2d(x_image, w1, strides=[1, 1, 1, 1], padding='SAME')+b1)
h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
w2=tf.Variable(tf.truncated_normal([5,5,32,64],stddev=0.1))
b2=tf.Variable(0.1,[64])
h_conv2 =tf.nn.relu(tf.nn.conv2d(h_pool1, w2, strides=[1, 1, 1, 1], padding='SAME')+b2)
h_pool2 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
W_fc1=tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024],stddev=0.1))
b_fc1=tf.Variable(0.1,[1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2=tf.Variable(tf.truncated_normal([1024, 10],stddev=0.1))
b_fc2=tf.Variable(0.1,[10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.initialize_all_variables())
for i in range(10000):
batch = mnist.train.next_batch(50)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print("test accuracy %g"%accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
Recommended Posts