1. 使用 with tf.name_scope('layer') 加標籤
def add_layer(inputs, in_size, out_size, activation_function=none):2. pycharm terminal 中進入project目錄with tf.name_scope(
'layer'):
with tf.name_scope(
'weights'):
weight = tf.variable(tf.random_normal([in_size, out_size]), name='
w') #
初始權重隨機
with tf.name_scope('
biases'):
biases = tf.variable(tf.zeros([1, out_size]) + 0.1, name='
b') #
biases推薦不為0,所以需要加上0.1
with tf.name_scope('
wx_plus_b'):
wx_plus_b = tf.add(tf.matmul(inputs, weight), biases) #
啟用前if activation_function is
none:
outputs =wx_plus_b
else
: outputs =activation_function(wx_plus_b)
return outputs
輸入 tensorboard --logdir=logs
將得到的** http://desktop-v7i30oq:6006 輸入瀏覽器,即可得到
3. 檢視weight、biases、loss
tf.summary.histogram(layer_name+'/weights
', weight)
tf.summary.histogram(layer_name + '
/biases
', biases)
tf.summary.scalar(
'loss
', loss)
merged =tf.summary.merge_all() # 打包
result = sess.run(merged, feed_dict=)
writer.add_summary(result, i) # 每i步畫乙個點
4. 參考**
importtensorflow as tf
import
numpy as np
import
matplotlib.pyplot as plt
def add_layer(inputs, in_size, out_size, n_layer, activation_function=none):
layer_name = '
layer%s
' %n_layer
with tf.name_scope(
'layer_name'):
with tf.name_scope(
'weights'):
weight = tf.variable(tf.random_normal([in_size, out_size]), name='
w') #
初始權重隨機
tf.summary.histogram(layer_name+'
/weights
', weight)
with tf.name_scope(
'biases'):
biases = tf.variable(tf.zeros([1, out_size]) + 0.1, name='
b') #
biases推薦不為0,所以需要加上0.1
tf.summary.histogram(layer_name + '
/biases
', biases)
with tf.name_scope(
'wx_plus_b'):
wx_plus_b = tf.add(tf.matmul(inputs, weight), biases) #
啟用前if activation_function is
none:
outputs =wx_plus_b
else
: outputs =activation_function(wx_plus_b)
tf.summary.histogram(layer_name + '
/outputs
', outputs)
return
outputs
#資料準備
x_data = np.linspace(-1, 1, 300)[:, np.newaxis] #
生成[-1,1]之間的300個數,組成300行的乙個陣列
noise = np.random.normal(0, 0.05, x_data.shape) #
mean = 0;std = 0.05; 格式:x_data
y_data = np.square(x_data) - 0.5 + noise #
y = x^2 - 0.5
with tf.name_scope('
inputs'):
xs = tf.placeholder(tf.float32, [none, 1], name='
x_input
') #
none表示sample數量任意
ys = tf.placeholder(tf.float32, [none, 1], name='
y_input')
#搭建神經網路
#由於輸入一維,輸出一維,所以我們定義的神經網路為輸入層乙個神經元,輸出層乙個神經元,中間隱藏層10個神經元
l1 = add_layer(xs, 1, 10, n_layer=1, activation_function=tf.nn.relu) #
隱藏層prediction = add_layer(l1, 10, 1, n_layer=2, activation_function=none) #
輸出層#
計算損失函式
with tf.name_scope('
loss'):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1])) #
距離平方求和求平均,reduction_indices表示資料處理的維度
tf.summary.scalar('
loss
', loss)#訓練
with tf.name_scope('
train'):
train_step = tf.train.gradientdescentoptimizer(0.1).minimize(loss) #
learning rate = 0.1
#初始化
init = tf.initialize_all_variables() #
初始化所有變數
sess =tf.session()
merged =tf.summary.merge_all()
writer = tf.summary.filewriter("
logs/
", sess.graph)
sess.run(init)
#視覺化輸出
fig =plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.scatter(x_data, y_data)
plt.ion()
#保證連續輸出
for i in range(1000):
sess.run(train_step, feed_dict=)
if i % 50 == 0: #
每50個資料輸出一次
try: #
為了避免第一次remove時報錯
ax.lines.remove(lines[0])
except
exception:
pass
prediction_value = sess.run(prediction, feed_dict=)
lines = ax.plot(x_data, prediction_value, '
r-', lw=5)
plt.pause(0.1) #
暫停0.1秒
result = sess.run(merged, feed_dict=)
writer.add_summary(result, i)
#每i步畫乙個點
TensorBoard模型視覺化
tensorboard是乙個基於瀏覽器的互動式工具,可以讓我們看到學習過程,並探索我們訓練好的模型。要執行tensorboard,首先到命令終端 開始 anaconda anaconda prompt,輸入activate tensorflow 然後,告訴tensorboard記錄的相關摘要 ten...
Tensorboard視覺化流程
在session會話中,加入視覺化 記住你的路徑!with tf.session as sess 你的內容 模型視覺化輸出 writer tf.summary.filewriter lenet ln1 graph tf.get default graph writer.close 然後輸入cmd,開...
深度學習 TensorBoard視覺化
1 概述 tensorboard是tensorflow的視覺化工具 通過tensorflow程式執行過程中輸出的日誌檔案視覺化tensorflow程式的執行狀態 tensorflow和tensorboard程式跑在不同的程序中 清除default graph和不斷增加的節點 tf.reset def...