tensorboard 查看訓練模型

https://www.cnblogs.com/tengge/p/6376073.html

 

import tensorflow as tf
import numpy as np

#輸入數據
x_data = np.linspace(-1,1,300)[:, np.newaxis]
noise = np.random.normal(0,0.05, x_data.shape)
y_data = np.square(x_data)-0.5+noise

#輸入層
with tf.name_scope('input_layer'): #輸入層。將這兩個變量放到input_layer作用域下,tensorboard會把他們放在一個圖形裏面
    xs = tf.placeholder(tf.float32, [None, 1], name = 'x_input') # xs起名x_input,會在圖形上顯示
    ys = tf.placeholder(tf.float32, [None, 1], name = 'y_input') # ys起名y_input,會在圖形上顯示

#隱層
with tf.name_scope('hidden_layer'): #隱層。將隱層權重、偏置、淨輸入放在一起
    with tf.name_scope('weight'): #權重
        W1 = tf.Variable(tf.random_normal([1,10]))
        tf.summary.histogram('hidden_layer/weight', W1)
    with tf.name_scope('bias'): #偏置
        b1 = tf.Variable(tf.zeros([1,10])+0.1)
        tf.summary.histogram('hidden_layer/bias', b1)
    with tf.name_scope('Wx_plus_b'): #淨輸入
        Wx_plus_b1 = tf.matmul(xs,W1) + b1
        tf.summary.histogram('hidden_layer/Wx_plus_b',Wx_plus_b1)
output1 = tf.nn.relu(Wx_plus_b1)

#輸出層
with tf.name_scope('output_layer'): #輸出層。將輸出層權重、偏置、淨輸入放在一起
    with tf.name_scope('weight'): #權重
        W2 = tf.Variable(tf.random_normal([10,1]))
        tf.summary.histogram('output_layer/weight', W2)
    with tf.name_scope('bias'): #偏置
        b2 = tf.Variable(tf.zeros([1,1])+0.1)
        tf.summary.histogram('output_layer/bias', b2)
    with tf.name_scope('Wx_plus_b'): #淨輸入
        Wx_plus_b2 = tf.matmul(output1,W2) + b2
        tf.summary.histogram('output_layer/Wx_plus_b',Wx_plus_b2)
output2 = Wx_plus_b2

#損失
with tf.name_scope('loss'): #損失
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-output2),reduction_indices=[1]))
    tf.summary.scalar('loss',loss)
with tf.name_scope('train'): #訓練過程
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

#初始化
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
merged = tf.summary.merge_all() #將圖形、訓練過程等數據合併在一起
writer = tf.summary.FileWriter('logs',sess.graph) #將訓練日誌寫入到logs文件夾下

#訓練
for i in range(1000):
    sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
    if(i%50==0): #每50次寫一次日誌
        result = sess.run(merged,feed_dict={xs:x_data,ys:y_data}) #計算需要寫入的日誌數據
        writer.add_summary(result,i) #將日誌數據寫入文件

 執行上述代碼,會在“當前路徑/logs”目錄下生成一個events.out.tfevents.{time}.{machine-name}的文件。在當前目錄新建“查看訓練過程.bat”,裏面輸入。

tensorboard --logdir=logs

cd到logs平行目錄下,執行tensorboard --logdir=logs命令,或者直接雙擊“查看訓練過程.bat”文件都可出現

執行上述bat文件,打開瀏覽器,輸入地址:http://localhost:6006,就可以查看訓練過程中的各種圖形。

 

 

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