Python學習日記14tensorboard視覺化顯示
x^2的程式碼加入寫log和summary
然後在anaconda prompt中用
d:
cd codefolder
activate tensorflow
進入log檔案的上級目錄
使用命令
tensorboard --log logs
在chrome瀏覽器位址列輸入prompt給出的地址
aaa:6006
遺留問題:
1還需要仔細閱讀官方文件和教程
https://tensorflow.google.cn/guide/summaries_and_tensorboard
http://www.tensorfly.cn/tfdoc/how_tos/summaries_and_tensorboard.html
2anaconda每次執行都建立新圖,在tensorboard時會出現很多圖(刪除log檔案也無用)。在程式最開始呼叫tf.reset_default_graph(),可以保證只畫出當前的圖。
附程式碼:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
#input matplotlib in conmmand line
tf.reset_default_graph()
def add_layer(input_holder,in_size,out_size,activation_function=None):
with tf.name_scope(‘layer’):
with tf.name_scope(‘weights’):
Variable_Weight = tf.Variable(
tf.random_normal([in_size,out_size]),name=‘w’)
with tf.name_scope(‘biases’):
Variable_biases = tf.Variable(
tf.zeros([1,out_size])+0.1,name=‘b’)
with tf.name_scope(‘Wx_plus_b’):
Wx_plus_b = tf.matmul(input_holder,Variable_Weight)+Variable_biases
if activation_function is None:
output_holder = Wx_plus_b
else:
output_holder = activation_function(Wx_plus_b)
return output_holder
#create sample data
sample_X = np.linspace(-2,2,200)[:, np.newaxis]#100numbers from -1 to 1
noise=np.random.normal(0,0.3,sample_X.shape).astype(np.float32)
sample_Y = np.square(sample_X)-0.5+noise
#draw image with dot(‘ro’)
#fig=plt.figure() 老版本,新版本不用了
#samplefig = fig.add_subplot(1,1,1)
plt.scatter(sample_X,sample_Y)
plt.show()
plt.ion()
#plt.plot(sample_X,sample_Y,‘ro’,label=‘sample data’)
#plt.legend()
#place holder
with tf.name_scope(‘inputs’):
holder_X = tf.placeholder(tf.float32,[None,1],name=‘x_in’)#,
holder_Y = tf.placeholder(tf.float32,[None,1],name=‘y_in’)#
#add layer,define session
layer1 = add_layer(holder_X,1,10,activation_function=tf.nn.relu)
prediction = add_layer(layer1,10,1,activation_function = None)
############### 1 ###################
tf.summary.histogram(‘prediction’,prediction)
tf.summary.histogram(‘in_X’,holder_X)
with tf.name_scope(‘loss’):
loss = tf.reduce_mean(
tf.reduce_sum(tf.square(holder_Y-prediction),
reduction_indices=[1]))#
################ 2 #################
tf.summary.scalar(‘loss_function’,loss)
tf.summary.histogram(‘loss’,loss)
with tf.name_scope(‘train’):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
#init
init = tf.global_variables_initializer()
#run
with tf.Session() as sess:
sess.run(init)
################# 3 ####################
merged_summary_op=tf.summary.merge_all()
#################### 4 ###################
#writhe the log file
writer =tf.summary.FileWriter(‘logs’,sess.graph
plotdata ={“batchsize”:[],“loss”:[]}
for i in range(200):
sess.run(train_step,feed_dict={holder_X:sample_X,holder_Y:sample_Y})
temp_loss = sess.run(loss,
feed_dict={holder_X:sample_X,holder_Y:sample_Y})
plotdata[“batchsize”].append(i)
plotdata[“loss”].append(temp_loss)
################## 5 #############
summary_str = sess.run(merged_summary_op,feed_dict={holder_X:sample_X,holder_Y:sample_Y});
writer.add_summary(summary_str,i);
if i%50==0:
#print(i,’ loss=’,temp_loss )
# to visualize the result and improvement
prediction_value = sess.run(prediction,feed_dict={holder_X:sample_X})#,holder_Y:sample_Y})
plt.cla()
plt.scatter(sample_X,sample_Y)
plt.plot(sample_X,prediction_value,‘r-’,lw=5)
plt.pause(0.1)
print(‘finished’)