Tensorboard可視化(一)

  • 2019 年 10 月 6 日
  • 筆記

Tensorboard可視化(一)

1.搭建圖紙

input層開始

# 將xs和ys包含進來,形成一個大的圖層,圖層名字叫做inputs  with tf.name_scope('inputs'):      # 為xs指定名稱x_input      xs = tf.placeholder(tf.float32, [None, 1],name='x_input')      # 為ys指定名稱y_input      ys = tf.placeholder(tf.float32, [None, 1],name='y_input')  

layer層

# 定義添加神經層的函數  def add_layer(inputs,in_size,out_size,activation_function=None):      # 定義大框架名字為layer      with tf.name_scope('layes'):          # 框架裡面的小部件Weights定義,同時也可以在weights中指定名稱W(將會在Weights展開後顯示)          with tf.name_scope('weights'):              Weights=tf.Variable(tf.random_uniform([in_size,out_size]),name='W')          # 框架裡面的小部件biases定義          with tf.name_scope('biases'):              biases=tf.Variable(tf.zeros([1,out_size])+0.1)          # 框架裡面的小部件Wx_plus_b定義          with tf.name_scope('Wx_plus_b'):              Wx_plus_b=tf.matmul(inputs,Weights)+biases          '''          activation_function 的話,可以暫時忽略。因為當選擇          用 tensorflow 中的激勵函數(activation function)的時候,          tensorflow會默認添加名稱,這個可以在圖形呈現後對比兩個layer層進行查看          '''          if activation_function is None:              outputs=Wx_plus_b          else:              outputs=activation_function(Wx_plus_b)          return outputs  

定義兩層

# 定義隱藏層  l1=add_layer(xs,1,10,activation_function=tf.nn.relu)  # 定義輸出層  prediction=add_layer(l1,10,1,activation_function=None)  

繪製loss

# 計算預測值prediction與真實值的誤差,對兩者差的平方求和再取平均  with tf.name_scope('loss'):      loss=tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),                     reduction_indices=[1]))  

繪製train

# 機器學習提升準確率  with tf.name_scope('train'):      train_step=tf.train.GradientDescentOptimizer(0.1).minimize(loss) # 0.1表示學習效率  

收集框架並存儲至logs/目錄

sess=tf.Session()  writer=tf.summary.FileWriter("logs/",sess.graph)  

PyCharm Terminal直接進入項目根目錄,運行tensorboard --logdir=logs,複製相應的鏈接至Google瀏覽器即可!

2.可視化問題

如果可視化不出來,瀏覽器輸入localhost:你的dos下的埠號,進去了沒有圖片,請檢查運行命令,

tensorboard --logdir=logs

這個運行命令中logs沒有單引號!!!

3.學習文章

https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/4-1-tensorboard2/