The following documents, use tensorflow started to learn the most basic example mnist, the most intuitive and easiest way to use shows tensorboard
Wherein the tensorboard comprising: scalar, image, histogram, and wherein the code space down to display.
tensorboard_test.py
#coding:. 8 UTF- Import tensorflow TF AS from tensorflow.examples.tutorials.mnist Import Input_Data Import Time Import OS Import PROJECTOR_visual "" " weights initialized initialized to a small positive number close to 0 " "" DEF weight_variable (Shape) : Initial = tf.truncated_normal (Shape, STDDEV = 0.1) return tf.Variable (Initial) DEF bias_variable (Shape): Initial tf.constant = (0.1, Shape = Shape) return tf.Variable (Initial) "" " convolution and pooling of the convolution steps of 1 (stride size), 0 margins (padding size) pooling max pooling do traditional 2x2 size with a simple template "" " DEF conv2d (X, W is): return TF. nn.conv2d (x, W, strides = [1, 1, 1, 1],padding = 'SAME') # tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None) # x(input) : [batch, in_height, in_width, in_channels] # W(filter) : [filter_height, filter_width, in_channels, out_channels] # strides : The stride of the sliding window for each dimension of input. # For the most common case of the same horizontal and vertices strides, strides = [1, stride, stride, 1] def max_pool_2x2(x): return tf.nn.max_pool(x, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = 'SAME') # tf.nn.max_pool(value, ksize, strides, padding, data_format='NHWC', name=None) # x(value) : [batch, height, width, channels] # Ksize (pool size): A List of INTS that has length> = 4. The window of The size for each of Dimension of Tensor The INPUT. # Strides (sliding the pool size): A list of ints that has length> = 4. The window of a stride of Sliding of The for each of Dimension The iNPUT Tensor. start = time.clock () # calculation start time mnist = input_data.read_data_sets ( "MNIST_data /" , one_hot = True) #MNIST data input os.system ( "python create_sprite.py ") " "" The first layer convolutional layer x_image (BATCH, 28, 28,. 1) -> h_pool1 (BATCH, 14, 14, 32) "" " X = tf.placeholder (tf.float32, [ none, 784]) x_image = -1, 28, 28,. 1]) # a dimension representing the number of the last channel tf.reshape (x, [, if compared rgb. 3 tf.summary.image ( 'input_image', x_image,10) W_conv1 = weight_variable([5, 5, 1, 32])10) b_conv1 = bias_variable([32]) tf.summary.histogram('W_conv1',W_conv1) tf.summary.histogram('b_conv1',b_conv1) conv1 = conv2d(x_image, W_conv1) + b_conv1 h_conv1 = tf.nn.relu(conv1) # x_image -> [batch, in_height, in_width, in_channels] # [batch, 28, 28, 1] # W_conv1 -> [filter_height, filter_width, in_channels, out_channels] # [5, 5, 1, 32] # output -> [batch, out_height, out_width, out_channels] # [batch, 28, 28, 32] h_pool1 = max_pool_2x2(h_conv1) # h_conv1 -> [batch, in_height, in_weight, in_channels] # [batch, 28, 28, 32] # output -> [batch, out_height, out_weight, out_channels] # [BATCH, 14, 14, 32] "" " The second layer convolutional layer ," "" h_pool1 (BATCH, 14, 14, 32) -> h_pool2 (BATCH,. 7,. 7, 64) "" " W_conv2 weight_variable = ([. 5,. 5, 32, 64]) b_conv2 bias_variable = ([64]) tf.summary .histogram ( 'W_conv2', W_conv2) tf.summary.histogram ( 'b_conv2', b_conv2) CONV2 = conv2d (h_pool1, W_conv2) + b_conv2 h_conv2 = tf.nn.relu (CONV2) # h_pool1 -> [BATCH, 14, 14, 32] # W_conv2 -> [. 5,. 5, 32, 64] # Output -> [BATCH, 14, 14, 64] h_pool2 = max_pool_2x2 (h_conv2) # h_conv2 -> [BATCH, 14, 14, 64] # output -> [BATCH,. 7,. 7, 64] "" " deconvolution layer, added to the output image reverse_weight1 = weight_variable ([5,5,32,64]) reverse_conv1 = tf.nn.conv2d_transpose(conv2,reverse_weight1,[50,14,14,32],strides=[1,1,1,1],padding="SAME") reverse_weight2 = weight_variable([5,5,1,32]) reverse_conv2 = tf.nn.conv2d_transpose(reverse_conv1,reverse_weight2,[50,28,28,1],strides=[1,2,2,1],padding="SAME") reverse_weight3 = weight_variable([5,5,1,32]) reverse_conv3 = tf.nn.conv2d_transpose(conv1,reverse_weight3,[50,28,28,1],strides=[1,1,1,1],padding="SAME") tf.summary.image("reverse_conv2",reverse_conv2,10) tf.summary.image("reverse_conv1",reverse_conv3,10) """ 第三层 全连接层 h_pool2(batch, 7, 7, 64) -> h_fc1(1, 1024) """ W_fc1 = weight_variable([7 * 7 * 64, 1024]) = bias_variable b_fc1 ([1024])feed_dict added control parameters keep_prob dropout ratio "" " tf.summary.histogram ( 'W_fc1', W_fc1) tf.summary.histogram ( 'b_fc1', b_fc1) 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) "" " Dropout h_fc1 -> h_fc1_drop, training is enabled, the test close " "" keep_prob = tf.placeholder ( "float") h_fc1_drop = tf.nn.dropout (h_fc1 , keep_prob) "" " the fourth layer Softmax output layer " "" W_fc2 weight_variable = ([1024, 10]) b_fc2 bias_variable = ([10]) y_conv = tf.nn.softmax (tf.matmul (h_fc1_drop, W_fc2) + b_fc2) "" " training and evaluation model ADAM optimizer do steepest descent gradient,feed_dict added dropout proportional control parameter keep_prob y_ = tf.placeholder ( "float", [None, 10]) cross_entropy = -tf.reduce_sum (y_ * tf.log ( y_conv)) # calculating cross entropy train_step = tf.train.AdamOptimizer (1e-4) .minimize (cross_entropy) # adam optimizer to use to learn to perform rate 0.0001 trimming correct_prediction = tf.equal (tf.argmax (y_conv, 1), tf.argmax (y_, 1)) # tag and determines the prediction matches the actual label accuracy = tf.reduce_mean (tf.cast (correct_prediction, "float") ) tf.summary.scalar ( 'cross_entropy', cross_entropy) tf.summary.scalar ( 'Accuracy', Accuracy) Merged = tf.summary.merge_all () sess = tf.Session () # boot models created writer = tf. summary.FileWriter ( "logs /", sess.graph) # sess.run (tf.initialize_all_variables ()) # old version sess.run (tf.global_variables_initializer ()) # initialize variables for i in range (PROJECTOR_visual.TRAINING_STEPS): # start training model 5000 circuit training batch = mnist.train.next_batch (50) #batch size to 50 IF I == 0% 100: train_accuracy = accuracy.eval (the session Sess =, feed_dict = {X: BATCH [0], Y_: BATCH [. 1], keep_prob: 1.0}) Print ( "% STEP D, G train_accuracy%"% (I, train_accuracy)) sess.run (train_step, feed_dict {X =: BATCH [0], Y_: BATCH [. 1], keep_prob: 0.5}) # neuron output probability remains unchanged as keep_prob 0.5 RS, sess.run _ = ([Merged, train_step], = {feed_dict X: BATCH [0], Y_: BATCH [. 1], keep_prob: 1.0}) writer.add_summary (RS, I) final_result = sess.run (h_fc1, feed_dict = {X: mnist.test.images}) Print ( "G% Accuracy Test"% accuracy.eval (= Sess the session, feed_dict = {X: mnist.test.images, Y_: mnist.test.labels, keep_prob: 1.0})) # neuron output remains unchanged keep_prob probability is 1, that is unchanged, has maintained output PROJECTOR_visual.visualisation (final_result)
PPROJECTOR_visual.py
tensorflow TF AS Import Import OS Import tqdm from tensorflow.contrib.tensorboard.plugins Import Projector TRAINING_STEPS = 1000 . LOG_DIR = 'logs' SPRITE_FILE = 'mnist_sprite.jpg' META_FIEL = "mnist_meta.tsv" TENSOR_NAME = "FINAL_LOGITS" # Visualizing the final output layers vector required log file DEF visualization (final_result): # using a new variable to hold the result of the final output layer vector, because the embedding is by Tensorflow variables completed, so PROJECTOR visualization are TensorFlow Variational wow. # So here needs a new definition of a variable to hold the value of output layer vector y_visual = tf.Variable (final_result, name = TENSOR_NAME) summary_writer tf.summary.FileWriter = (. LOG_DIR) # by project.ProjectorConfig classes to help generate a log file config = projector.ProjectorConfig () # A need to increase the visual bedding results embedding config.embeddings.add = () # Tensorflow specify the variable name corresponding to the result of embedding embedding.tensor_name = y_visual.name # Find the Specify WHERE you The Metadata # specified embedding result corresponding original Data information. For example, here is the categories specified real test each one corresponding to the image MNIST. In a word may be a word vector corresponding to the ID word. # This file is optional, if the vector is not specified then no label. = META_FIEL embedding.metadata_path # Find the Specify WHERE you The sprite (WE Will Create the this later) # specified sprite image. This is optional, if no sprite image, the visualization of results # Each point is a sleepy little spot, rather than a specific picture. = SPRITE_FILE embedding.sprite.image_path # when providing sprite images, you can specify a single picture by single_image_dim. # This will be used to intercept the correct picture from the original sprite image. embedding.sprite.single_image_dim.extend ([28, 28]) # Say that you want to Visualize The embeddings # The content PROJECTOR require written to the log file. projector.visualize_embeddings (summary_writer, config) log information generated # session initialization newly declared variable and require written to the file. = tf.InteractiveSession Sess () sess.run (tf.global_variables_initializer ()) Saver = tf.train.Saver () saver.save (Sess, the os.path.join (. LOG_DIR, "Model"), TRAINING_STEPS) summary_writer.close ()
The two next .py file in the same folder, and then run when the direct use cmd, execute python tensorboard_test.py, you will be able to start.
Then enter in your browser: http: // localhost: 8080 screen can be opened tensorboard