[Tensorflow]之TensorBoard 入门

Tensorboard 是Tensorflow自带的一个可视化工具。

功能

  • Scalars:展示训练过程中记录的标量
  • Image: 展示训练过程中记录的图像
  • Audio: 展示训练过程中记录的音频
  • Graphs: 展示训练中构建的计算图
  • Distributions: 压缩的数据分布图
  • Histogram: 展示训练过程中记录的数据的分布图
  • Embeddings: 展示记录的Tensor
  • Text: 这个不是很清楚。

原理

TensorBoard通过读取Tensorflow的事件文件,通过读取保存到指定目录(通过参数--logdir指定)的序列化后的Summary数据。运行过程中记录结构化的数据,通过一个本地服务器,监听6006端口,分析记录数据,并绘制。
通常看到的代码里tf.summary.scalar,tf.summary.histogram,tf.summary.image等这些是收集训练过程中你想记录的参数,参数一般是(tag,value). 而tf.summary.FileWriter 是将数据写道本地磁盘,以便之后读取。因为tensorflow中的数据都需要run以下才能使用,所以summary也需要run,通常将多个summary 整合到一起(summ = tf.summary.merge_all()),方便一次出结果。

实例

import os
import random
import numpy as np
import tensorflow as tf

ROOT = "/tmp/test_log/"
LABELS = os.path.join(os.getcwd(),"labels_100.tsv")
SPRITES = os.path.join(os.getcwd(),"sprite_1024.png")

mnist = tf.contrib.learn.datasets.mnist.read_data_sets(train_dir=ROOT+"data",one_hot = True)
'''  
def conv_layer(input,size_in,size_out,name="conv"):
   with tf.name_scope(name):
       filters = tf.Variable(tf.truncated_normal([5,5,size_in,size_out],stddev=0.1),name="filters")
       b = tf.Variable(tf.constant(0.1,shape=[size_out]),name="b")
       conv = tf.nn.conv2d(input,filters,strides=[1,1,1,1],padding="SAME")
       act = tf.nn.relu(conv+b)
       tf.summary.histogram("weights",filters)
       tf.summary.histogram("biases",b)
       tf.summary.histogram("activation",act)
       return tf.nn.max_pool(act,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")

def fc_layer(input,size_in,size_out,name="fc"):
   with tf.name_scope(name):
       w = tf.Variable(tf.truncated_normal([size_in,size_out],stddev=0.1),name="W")
       b = tf.Variable(tf.constant(0.1,shape=[size_out]),name="B")
       act = tf.matmul(input,w)+b
       tf.summary.histogram("weights",w)
       tf.summary.histogram("biases",b)
       tf.summary.histogram("activations",act)
       return act

def mnist_model(learning_rate,use_two_fc,use_two_conv,hparam):
   tf.reset_default_graph()

   sess = tf.Session()
   x = tf.placeholder(tf.float32,shape=[None,784],name="x")
   x_image = tf.reshape(x,[-1,28,28,1])
   tf.summary.image('input',x_image,3)
   y = tf.placeholder(tf.float32,shape=[None,10],name="lables")
   
   if use_two_conv:
       conv1 = conv_layer(x_image,1,32,"conv1")
       conv_out = conv_layer(conv1,32,64,"conv2")
   else:
       conv1 = conv_layer(x_image,1,64,"conv1")
       conv_out = tf.nn.max_pool(conv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
   flatten = tf.reshape(conv_out,[-1,7*7*64])
   
   if use_two_fc:
       fc1 = fc_layer(flatten,7*7*64,1024,"fc1")
       relu = tf.nn.relu(fc1)
       embedding_input = relu
       tf.summary.histogram("fc1/relu",relu)
       embedding_size = 1024
       logits = fc_layer(fc1,1024,10,"fc2")
   else:
       embedding_input = flatten
       embedding_size = 7*7*64
       logits = fc_layer(flatten,embedding_size,10,"fc")
   
   with tf.name_scope("xent"):
       xent = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
                               logits=logits,labels=y),name="xent")
       tf.summary.scalar("xent",xent)
   with tf.name_scope("optimizer"):
       train_step = tf.train.AdamOptimizer(learning_rate).minimize(xent)
   
   with tf.name_scope("accuary"):
       correct_prediction = tf.equal(tf.argmax(logits,1),tf.argmax(y,1))
       accuary = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
       tf.summary.scalar('accuary',accuary)
   summ = tf.summary.merge_all()
   
   embedding = tf.Variable(tf.zeros([1024,embedding_size]),name="test_embedding")
   assignment = embedding.assign(embedding_input)
   saver = tf.train.Saver()
   
   sess.run(tf.global_variables_initializer())
   writer = tf.summary.FileWriter(ROOT+hparam)
   writer.add_graph(sess.graph)

   config = tf.contrib.tensorboard.plugins.projector.ProjectorConfig()
   embedding_config = config.embeddings.add()
   embedding_config.tensor_name = embedding.name
   embedding_config.sprite.image_path = SPRITES
   embedding_config.metadata_path = LABELS

   embedding_config.sprite.single_image_dim.extend([28,28])
   tf.contrib.tensorboard.plugins.projector.visualize_embeddings(writer,config)

   for i in range(2001):
       batch = mnist.train.next_batch(100)
       if i%5 ==0:
           [train_accuary,s] = sess.run([accuary,summ],feed_dict = {x:batch[0],y:batch[1]})
           writer.add_summary(s,i)
       if i%500 == 0:
           sess.run(assignment,feed_dict={x:mnist.test.images[:1024],y:mnist.test.labels[:1024]})
           saver.save(sess,os.path.join(ROOT,"model.ckpt"),i)
       sess.run(train_step,feed_dict = {x:batch[0],y:batch[1]})

def make_hparam_string(learning_rate,use_two_fc,use_two_conv):
   conv_param = "conv=2" if use_two_conv else "conv1"
   fc_param = "fc=2" if use_two_fc else "fc=1"
   return "lr_%.0E,%s,%s"%(learning_rate,conv_param,fc_param)


def main():
   
   for learning_rate in [1E-2,1E-3]:
       for use_two_fc in [True]:
           for use_two_conv in [False,True]:
               hparam = make_hparam_string(learning_rate,use_two_fc,use_two_conv)
               print("Starting run for %s"%hparam)
               mnist_model(learning_rate,use_two_fc,use_two_conv,hparam)
   
   print("Done!\n Run `tensorboard --logdir=%s` to see the results."%ROOT)

'''
if __name__ == '__main__':
   #main()


   tf.reset_default_graph()
   sess = tf.Session()
   writer = tf.summary.FileWriter(ROOT+"new")
   summary = tf.Summary(value=[
       tf.Summary.Value(tag="summary_tag",simple_value = 0),
       tf.Summary.Value(tag="summary_tag2",simple_value=1)])
   writer.add_summary(summary,1) # 这些summary都是


   embedding = tf.Variable(tf.truncated_normal([100,128],stddev=0.1),name='embedding')

   config = tf.contrib.tensorboard.plugins.projector.ProjectorConfig()

   embedding_config = config.embeddings.add()
   embedding_config.tensor_name = embedding.name
   embedding_config.sprite.image_path = SPRITES # 这个只是用图片代表标签,没有的化,直接用文字代替
   embedding_config.metadata_path = LABELS # 这是变量对应的标签 
   embedding_config.sprite.single_image_dim.extend([28, 28]) #显示图片标签的大小 
   # 这整个结构体都是和tensorboard 页面里的选项相对应的 

   saver = tf.train.Saver()
   sess.run(tf.global_variables_initializer())

   saver.save(sess, os.path.join(ROOT, "model.ckpt"))
   #sess.run(embedding)

   tf.contrib.tensorboard.plugins.projector.visualize_embeddings(writer,config) #这是显示embedding的设置,必须要设置这个函数



   writer.close()
   print("Done!")  

你可能感兴趣的:([Tensorflow]之TensorBoard 入门)