打開Python Shell,執行如下代碼:瀏覽器
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」,裏面輸入。dom
tensorboard --logdir=logs
執行上述bat文件,打開瀏覽器,輸入地址:http://localhost:6006,就能夠查看訓練過程當中的各類圖形。spa
重要提示:請不要用中文命名目錄,中文目錄中看不到任何圖形。這個問題困擾了我一週!!!scala