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’)

猜你喜欢

转载自blog.csdn.net/weixin_43387285/article/details/84105917