Tensorflow visualization MNIST handwritten digital training

[Description]

  When we learn a programming language, often the first program is to print "Hello World", then for artificial intelligence learning platform, his "Hello World" applet is MNIST handwritten digital trained. MNIST is a handwritten numeral data set, official website Yann LeCun - who apos Website . Data set contains a total of 60,000 sets of training data lines ( mnist.train) and 10,000 rows of test data sets ( mnist.test), each number is the size of 28 * 28 pixels. By using Tensorflow artificial intelligence platform, we can learn how artificial intelligence learning platform for learning through the data.

【data preparation】

  Download mnist data sets, and mnist_10k_sprite.png pictures, respectively, on MNIST_data folders and projector / data folder.

 

[Code]

Copy the code

tensorflow TF AS Import 
from tensorflow.examples.tutorials.mnist Import Input_Data 
from tensorflow.contrib.tensorboard.plugins Import Projector 

# Loading data set 
MNIST = input_data.read_data_sets ( "MNIST_data /", = True one_hot) 
# number of runs 
max_steps = 1001 
picture number # 
IMAGE_NUM = 3000 
# path file 
DIR = "E: / Github / TensorFlow / Trunk / the Test /" 

# define session 
sess = tf.Session () 

# load the image 
embedding = tf.Variable (tf.stack (mnist. test.images [: IMAGE_NUM]), trainable = False, name = 'embedding that') 

# parameter Summary 
DEF variable_summaries (var): 
    with tf.name_scope ( 'Summaries'): 
        Mean = tf.reduce_mean (var) 
        tf.summary. scalar ( 'mean',mean) # average value
        tf.name_scope with ( 'STDDEV'): 
            STDDEV = tf.sqrt (tf.reduce_mean (tf.square (var - Mean))) 
        tf.summary.scalar ( 'STDDEV', STDDEV) # standard deviation 
        tf.summary.scalar ( 'max', tf.reduce_max (var )) # maximum 
        tf.summary.scalar ( 'min', tf.reduce_min ( var)) # minimum 
        tf.summary.histogram ( 'histogram', var) # histograms 

# namespace 
with tf.name_scope ( 'INPUT'): 
    # none herein denotes a dimension may be any length 
    x = tf.placeholder (tf.float32, [none , 784], name = 'x-input' ) 
    # correct labels 
    Y = tf.placeholder (tf.float32, [None, 10], name = 'INPUT-Y') 

# display picture 
with tf.name_scope ( 'input_reshape'): 
    image_shaped_input tf.reshape = (X, [-1, 28, 28, 1]) 
    tf.summary.image ( 'INPUT',image_shaped_input, 10)

with tf.name_scope('layer'):
    #创建一个简单神经网络
    with tf.name_scope('weights'):
        W = tf.Variable(tf.zeros([784,10]),name='W')
        variable_summaries(W)
    with tf.name_scope('biases'):
        b = tf.Variable(tf.zeros([10]),name='b')
        variable_summaries(b)
    with tf.name_scope('wx_plus_b'):
        wx_plus_b = tf.matmul(x,W) + b
    with tf.name_scope('softmax'):    
        prediction = tf.nn.softmax(wx_plus_b)

with tf.name_scope('loss'):
    #交叉熵代价函数
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
    tf.summary.scalar ( 'Loss', Loss) 
with tf.name_scope ( 'Train'): 
    # using a gradient descent method 
    train_step = tf.train.GradientDescentOptimizer (0.5) .minimize (Loss) 

#初始化变量
sess.run (tf.global_variables_initializer ( )) 

with tf.name_scope ( 'Accuracy'): 
    with tf.name_scope ( 'correct_prediction'): 
        # store the result in a Boolean list 
        correct_prediction = tf.equal (tf.argmax (y, 1), tf.argmax ( prediction, 1)) # argmax return to the one-dimensional position of the maximum value of the tensor located 
    with tf.name_scope ( 'accuracy'): 
        # accuracy required 
        accuracy = tf.reduce_mean (tf.cast (correct_prediction, tf.float32)) # the correct_prediction becomes float32 type 
        tf.summary.scalar ( 'Accuracy', Accuracy) 

# metadata files generated 
IF tf.gfile.Exists (the DIR + 'Projector / Projector / metadata.tsv'): 
    TF .gfile.DeleteRecursively (the DIR + 'Projector / Projector / metadata.tsv') 
with Open (the DIR + 'Projector / Projector / metadata.tsv', 'W') as f:
    labels = sess.run(tf.argmax(mnist.test.labels[:],1))
    for i in range(image_num):   
        f.write(str(labels[i]) + '\n')        
        
#合并所有的summary
merged = tf.summary.merge_all()   


projector_writer = tf.summary.FileWriter(DIR + 'projector/projector',sess.graph)
saver = tf.train.Saver()
config = projector.ProjectorConfig()
embed = config.embeddings.add()
embed.tensor_name = embedding.name
embed.metadata_path = DIR + 'projector/projector/metadata.tsv'
embed.sprite.image_path = DIR + 'projector/data/mnist_10k_sprite.png'
embed.sprite.single_image_dim.extend([28,28])
projector.visualize_embeddings(projector_writer,config)

for i in range(max_steps):
    #每个批次100个样本
    batch_xs,batch_ys = mnist.train.next_batch(100)
    run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
    run_metadata = tf.RunMetadata()
    sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys},options=run_options,run_metadata=run_metadata)
    summary = sess.run(merged,feed_dict={x:batch_xs,y:batch_ys},options=run_options,run_metadata=run_metadata)
    projector_writer.add_run_metadata(run_metadata, 'step%03d' % i)
    projector_writer.add_summary(summary, i)
    
    if i%100 == 0:
        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print ("Iter " + str(i) + ", Testing Accuracy= " + str(acc))

saver.save(sess, DIR + 'projector/projector/a_model.ckpt', global_step=max_steps)
projector_writer.close()
sess.close()

Copy the code

 【run】

  Directly run the code

[Visual interface]

  1, the cmd command line input tensorboard --logdir = progector folder path;

  2. Open your browser to http: // localhost: 6006 Path to view visualizations.

 

Source acquisition mode, the total number of public attention RaoRao1994, wonderful view to the stage - all articles, you can get the download link resources

Get more resources, please pay attention to the public the total number RaoRao1994

Published 37 original articles · won praise 13 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_26078953/article/details/91366919