2017-07-06 6 views
1

Dies ist der Code von senddex tutorials: Wie werden die Daten vom MNIST-Set in den Platzhalter x übertragen.MNIST: x ist nur ein Platzhalter, Wie gehen Daten von MNIST in den Platzhalter x?

Bitte helfen Sie mir, wenn ich bedenke, ich bin nur ein Anfänger in Tensorflow, wenn es etwas mit dem Platzhalter zu tun hat, dann bitte erklären.

Vielen Dank im Voraus!

""" 
os.environ removes the warning 
""" 
import os 
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 
""" 
tensorflow starts below 
""" 

import tensorflow as tf 

from tensorflow.examples.tutorials.mnist import input_data 
mnist = input_data.read_data_sets("/tmp/data/",one_hot=True) 

# 10 classes , 0-9 

""" 
nodes for the hidden layers 
""" 
n_nodes_hl1 = 500 
n_nodes_hl2 = 500 
n_nodes_hl3 = 500 

n_classes = 10 # 0-9 

batch_size = 100 

""" 
placeholders 
""" 

x = tf.placeholder('float',[None,784]) # 784 is 28*28 ,i.e., the size of mnist images 
y = tf.placeholder('float') 


# y is the label of data 

def neural_network_model(data): 


    # biases are added so that the some neurons get fired even when input_data is 0 

    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([784,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]))} 


    # (input_data * weights) + biases 

    l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']) , hidden_1_layer['biases']) 
    l1 = tf.nn.relu(l1) # activation func 

    l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']) , hidden_2_layer['biases']) 
    l2 = tf.nn.relu(l2) # activation func 

    l3 = tf.add(tf.matmul(l2,hidden_3_layer['weights']) , hidden_3_layer['biases']) 
    l3 = tf.nn.relu(l3) # activation func 

    output = tf.matmul(l3,output_layer['weights']) + output_layer['biases'] 

    return output 

# we now have modeled a neural network 

def train_neural_network(x): 
    prediction = neural_network_model(x) 
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y)) 
    # softmax_cross_entropy_with_logits ==> for changing weights 
    # we wanna minimize the difference 

    # AdamOptimizer optionally has a learning_reate : 0.0001 
    optimizer = tf.train.AdamOptimizer().minimize(cost) 

    hm_epochs = 5 # cycles of feed forward + back 

    with tf.Session() as sess: 
     sess.run(tf.global_variables_initializer()) # replace it with global_variable_initializer 

     for epoch in range(hm_epochs): 
      epoch_loss = 0 
      for _ in range(int(mnist.train.num_examples/batch_size)): 
       epoch_x,epoch_y = mnist.train.next_batch(batch_size) 
       _,c = sess.run([optimizer, cost], feed_dict = {x: epoch_x, y: epoch_y}) 
       epoch_loss += c 
      print('Epoch',epoch,'completed out of',hm_epochs,' loss:',epoch_loss) 


     correct = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1)) 
     accuracy = tf.reduce_mean(tf.cast(correct,'float')) # cast changes the data type of a tensor 
     print('Accuracy: ',accuracy.eval({x:mnist.test.images,y:mnist.test.labels})) 


if __name__ == "__main__": 
    train_neural_network(x) 

Antwort

0

Um zu sehen, wo die MNIST Daten in die tf.placeholder() Tensoren übertragen x und y, auf diesen Linien-Fokus:

for _ in range(int(mnist.train.num_examples/batch_size)): 
    epoch_x, epoch_y = mnist.train.next_batch(batch_size) 
    _, c = sess.run([optimizer, cost], feed_dict = {x: epoch_x, y: epoch_y}) 

Die Arrays epoch_x und epoch_y sind ein Paar von (etwas verwirrender genannt) NumPy Arrays, die einen Stapel von batch_size Bildern und Etiketten enthalten, aus dem MNIST Training Datensatz. Sie enthalten in jeder Iteration der Schleife for einen anderen Stapel.

Das feed_dict Argument sess.run() sagt TensorFlow y den Wert von epoch_x für Platzhalter x und den Wert von epoch_y für Platzhalter zu ersetzen. Somit wird TensorFlow diese Werte verwenden, um einen Schritt des Optimierungsalgorithmus auszuführen (in diesem Fall Adam).

Beachten Sie, dass die MNIST Daten auch auf dieser Linie verwendet wird:

print('Accuracy: ', accuracy.eval({x: mnist.test.images, y: mnist.test.labels})) 

... außer hier wird das Programm mit dem gesamten Test Daten stellen Sie die Genauigkeit des Modells zu bewerten.