TensorFlow----TensorBoard

以一個曲線擬合的小例子說明要使用TensorBoard,需要對程序添加那些額外的東西。程序:

  1. import tensorflow as tf  
  2. import numpy as np  
  3.   
  4. # Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3  
  5. x_data = np.random.rand(1000,1).astype(np.float32)  
  6. y_data = tf.sin(x_data)*tf.cos(x_data)+tf.random_uniform([1000,1], -0.10.1)  
  7.   
  8.   
  9. #graph  
  10. X = tf.placeholder(tf.float32,[None,1],name = 'X-input')  
  11. Y = tf.placeholder(tf.float32,[None,1],name = 'Y-input')  
  12.   
  13. W1 = tf.Variable(tf.random_uniform([1,5], -1.01.0),name = 'weight1')  
  14. W2 = tf.Variable(tf.random_uniform([5,2], -1.01.0),name = 'weight2')  
  15. W3 = tf.Variable(tf.random_uniform([2,1], -1.01.0),name = 'weight3')  
  16.   
  17. b1 = tf.Variable(tf.zeros([5]), name = 'bias1')  
  18. b2 = tf.Variable(tf.zeros([2]), name = 'bias2')  
  19. b3 = tf.Variable(tf.zeros([1]), name = 'bias3')  
  20.   
  21. with tf.name_scope('layer2') as scope:  
  22.     L2 = tf.sigmoid(tf.matmul(X,W1)+b1)  
  23.   
  24. with tf.name_scope('layer3') as scope:  
  25.     L3 = tf.sigmoid(tf.matmul(L2,W2)+b2)  
  26.   
  27. with tf.name_scope('layer4') as scope:  
  28.     hypothesis = tf.sigmoid(tf.matmul(L3,W3)+b3)  
  29.   
  30. with tf.name_scope('cost') as scope:  
  31.     cost = -tf.reduce_mean(Y*tf.log(hypothesis))  
  32.     cost_summery = tf.scalar_summary("cost",cost)  
  33.   
  34. with tf.name_scope('train') as scope:  
  35.     optimizer = tf.train.GradientDescentOptimizer(0.01)  
  36.     train = optimizer.minimize(cost)  
  37.   
  38. #the summery  
  39. w1_hist = tf.histogram_summary("weight1",W1)  
  40. w2_hist = tf.histogram_summary("weight2",W2)  
  41. b1_hist = tf.histogram_summary("bisa1",b1)  
  42. b2_hist = tf.histogram_summary("bisa2",b2)  
  43. y_hist = tf.histogram_summary("y",Y)  
  44.   
  45. init = tf.initialize_all_variables()  
  46.   
  47. #run  
  48. with tf.Session() as sess:  
  49.   
  50.     sess.run(init)  
  51.     #the workers who translate data to TensorBoard  
  52.     merged = tf.merge_all_summaries() #collect the tf.xxxxx_summary  
  53.     writer = tf.train.SummaryWriter('keep',sess.graph)   
  54.         # maybe many writers to show different curvs in the same figure  
  55.     for step in range(20000):  
  56.         summary, _ = sess.run([merged, train], feed_dict={X:x_data,Y:y_data.eval()})  
  57.         writer.add_summary(summary, step)  
  58.         if step%10 ==0:  
  59.             print('step %s' % (step))  

顯然,需要給程序的每一部分添加命名空間,名字分類越清楚,最後的圖越好看

  1. scalar_summary和histogram_summary  

分別記錄單個變量和一組變量

  1. tf.merge_all_summaries()  
類似個收集節點,執行這個節點就表示這個圖裏面的變量都要收集一遍等待傳遞給顯示文件。
  1. tf.train.SummaryWriter和add_summary  
與上面的函數是配合的,可以把剛纔收集的變量正式寫進去硬盤文件中(這個文件是網頁顯示Figure的數據),每次add添加進去數據

然後是啓動TensorBoard:

  1. tensorboard --logdir=/home/ xxxxxxxxx /keep  
就是告訴tensorboard從哪裏讀取數據,和writed的目錄一樣,打開網址:http://0.0.0.0:6006,就可以看到生成的圖表



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章