一、前述
TensorBoard是tensorFlow中的可視化界面,可以清楚的看到數據的流向以及各種參數的變化,本文基於一個案例講解TensorBoard的用法。
二、代碼
設計一個MLP多層神經網絡來訓練數據
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data max_steps = 1000#最大迭代次數 learning_rate = 0.001#學習率 dropout = 0.9# 保留的數據 data_dir = './MNIST_data_bak' log_dir = './logs/mnist_with_summaries' mnist = input_data.read_data_sets(data_dir, one_hot=True)#把y這一列變成one_hot編碼 sess = tf.InteractiveSession() with tf.name_scope('input'):#with塊中名字才是最重要的 一個塊 x = tf.placeholder(tf.float32, [None, 784], name='x-input') y_ = tf.placeholder(tf.float32, [None, 10], name='y-input') with tf.name_scope('input_reshape'): # 784維度變形為圖片保持到節點 image_shaped_input = tf.reshape(x, [-1, 28, 28, 1])#-1代維度表不管有多少個 1代表1個通道 28*28個 tf.summary.image('input', image_shaped_input, 10)#當做一個圖片存起來 # 定義神經網絡的初始化方法 def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1)#截斷的正態分布 這里可以用he_initinelize return tf.Variable(initial)#創建一個變量 def bias_variable(shape):#截距 initial = tf.constant(0.1, shape=shape) return tf.Variable(initial) # 以下代碼是關於畫圖的 # 定義Variable變量的數據匯總函數,我們計算出變量的mean、stddev、max、min # 對這些標量數據使用tf.summary.scalar進行記錄和匯總 # 使用tf.summary.histogram直接記錄變量var的直方圖數據 def variable_summaries(var): with tf.name_scope('summaries'): mean = tf.reduce_mean(var) tf.summary.scalar('mean', mean) with tf.name_scope('stddev'): stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean))) tf.summary.scalar('stddev', stddev) tf.summary.scalar('max', tf.reduce_max(var)) tf.summary.scalar('min', tf.reduce_min(var)) tf.summary.histogram('histogram', var) # 設計一個MLP多層神經網絡來訓練數據 # 在每一層中都對模型數據進行匯總 def nn_layer(input_tensor, input_dim, output_dim, layer_name, act=tf.nn.relu):#定義一個隱藏層 input_dim上一層 output_dim本層輸出 with tf.name_scope(layer_name): with tf.name_scope('weights'): weights = weight_variable([input_dim, output_dim])#shape傳進來是上一層輸入,本層輸出 如果是MLP,就是全連接可以知道參數個數 variable_summaries(weights)#把權重的各個指標(方差,平均值)進行總結 with tf.name_scope('biases'): biases = bias_variable([output_dim]) variable_summaries(biases) with tf.name_scope('Wx_plus_b'): preactivate = tf.matmul(input_tensor, weights) + biases#帶到激活函數之前的公式 tf.summary.histogram('pre_activations', preactivate) activations = act(preactivate, name='activation')#運用激活函數 函數里面傳函數 高階函數 tf.summary.histogram('activations', activations) return activations # 我們使用剛剛定義的函數創建一層神經網絡,輸入維度是圖片的尺寸784=28*28 # 輸出的維度是隱藏節點數500,再創建一個Dropout層,並使用tf.summary.scalar記錄keep_prob hidden1 = nn_layer(x, 784, 500, 'layer1')#建立第一層 隱藏層 with tf.name_scope('dropout'): keep_prob = tf.placeholder(tf.float32) tf.summary.scalar('dropout_keep_probability', keep_prob) dropped = tf.nn.dropout(hidden1, keep_prob)#應用drop_out函數 保留下來的數據 # 然后使用nn_layer定義神經網絡輸出層,其輸入維度為上一層隱含節點數500,輸出維度為類別數10 # 同時激活函數為全等映射identity,暫時不使用softmax y = nn_layer(dropped, 500, 10, 'layer2', act=tf.identity)#建立第二層 輸出層 # 使用tf.nn.softmax_cross_entropy_with_logits()對前面的輸出層的結果進行Softmax # 處理並計算交叉熵損失cross_entropy,計算平均的損失,使用tf.summary.scalar進行統計匯總 with tf.name_scope('cross_entropy'): diff = tf.nn.softmax_cross_entropy_with_logits(logits=y, labels=y_)#輸出層給的結果logits=y #每一行的y是有10個數預測10個值 然后利用這10個值做歸一化 然后具備一個概率的含義 第二步計算交叉熵 with tf.name_scope('total'): cross_entropy = tf.reduce_mean(diff)#平均損失 tf.summary.scalar('cross_entropy', cross_entropy) # 下面使用Adam優化器對損失進行優化,同時統計預測正確的樣本數並計算正確率accuracy,匯總 with tf.name_scope('train'): train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)#AdamOptimizer比SGD更好一些,下降速度更快,更容易計算局部最優解 ,當數據量大的時候不如SGD #learning_rate雖然是固定的,后面會自適應,根據上一次的結果 所以大數據量的話,不如定義好策略,這樣省時間 with tf.name_scope('accuracy'): with tf.name_scope('correct_prediction'): correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))#預測值最大的索引 和真實值的索引 with tf.name_scope('accuracy'): accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))#true 1 false 0 reduce_mean 是一個比例得到的結果 tf.summary.scalar('accuracy', accuracy) # 因為我們之前定義了太多的tf.summary匯總操作,逐一執行這些操作太麻煩, # 使用tf.summary.merge_all()直接獲取所有匯總操作,以便后面執行 merged = tf.summary.merge_all() # 定義兩個tf.summary.FileWriter文件記錄器再不同的子目錄,分別用來存儲訓練和測試的日志數據 train_writer = tf.summary.FileWriter(log_dir + '/train', sess.graph) test_writer = tf.summary.FileWriter(log_dir + '/test') # 同時,將Session計算圖sess.graph加入訓練過程,這樣再TensorBoard的GRAPHS窗口中就能展示 # 整個計算圖的可視化效果,最后初始化全部變量 tf.global_variables_initializer().run() # 定義feed_dict函數,如果是訓練,需要設置dropout,如果是測試,keep_prob設置為1 def feed_dict(train): if train:#如果是訓練的話需要Droupout 測試的時候不要Droupout xs, ys = mnist.train.next_batch(100)#每一次拿一批次數據去訓練 k = dropout else: xs, ys = mnist.test.images, mnist.test.labels#真正測試的話全部測試,不是拿一批次的數據了 k = 1.0 return {x: xs, y_: ys, keep_prob: k} # 執行訓練、測試、日志記錄操作 # 創建模型的保存器 saver = tf.train.Saver() for i in range(max_steps):#max_steps迭代次數 if i % 10 == 0:#每執行10次的時候匯總一次信息 然后計算測試集的一次准確率 因為傳的是Flase summary, acc = sess.run([merged, accuracy], feed_dict=feed_dict(False)) test_writer.add_summary(summary, i)#然后寫出 print('Accuracy at step %s: %s' % (i, acc)) else: if i % 100 == 99:#如果到100次 run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata()#保存的是元數據信息 summary, _ = sess.run([merged, train_step], feed_dict=feed_dict(True))# summary寫的是跑完之后的數據 train_writer.add_run_metadata(run_metadata, 'step%03d' % i) train_writer.add_summary(summary, 1)#寫文件 saver.save(sess, log_dir + 'model.ckpt', i) print('Adding run metadata for', i) else:#不是10次,也不是100次 ,說明其他批次,則訓練數據 summary, _ = sess.run([merged, train_step], feed_dict=feed_dict(True))#訓練 train_writer.add_summary(summary, i) train_writer.close() test_writer.close()
三、TensorBoard的使用
1、找到代碼中tf.summary.FileWriter文件記錄器存儲訓練和測試的日志數據的目錄
在Terninal中輸入tensorboard --logdir="D:\Project\AI_Project\tensforflow_study\logs"
其中Logdir正式上面代碼中保存的路徑,這里注意是絕對路徑,並且使用雙引號引起來。
2、在瀏覽器中輸入localhost:6006即可
Graph模塊是整個數據流向圖
SCALARS可以看到損失函數逐漸減小,w參數也在逐漸減小
剩下的功能自己挖掘吧!!!