参考
https://blog.csdn.net/yangfengling1023/article/details/82854923
使用案例1(写入日志)
#coding:utf-8
# 我这里安装的是tensorflow2,如果按照tensorflow1的用法使用,需要先声明以下两句
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
##定义一个简单的计算图,实现两个常量相加的操作
with tf.name_scope('graph') as scope:
a = tf.constant(1,name='a')
b = tf.constant(2,name='b')
add = tf.add(a,b,name='add')
sess = tf.Session()
##生成一个写日志的writer,并将当前的tensorflow计算图写入日志
writer = tf.summary.FileWriter('/Users/xiaokang/Desktop/问题解决/自己/log/',sess.graph)
init = tf.global_variables_initializer()
sess.run(init)
'''
tf.name_scope函数是作用域名,上述代码中在graph中有3个op:a、b、add,用tf函数内部的name参数命名,这些会在tensorboard中显示出来
'''
WARNING:tensorflow:From /Users/xiaokang/Downloads/n/lib/python3.8/site-packages/tensorflow/python/compat/v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
'\ntf.name_scope函数是作用域名,上述代码中在graph中有3个op:a、b、add,用tf函数内部的name参数命名,这些会在tensorboard中显示出来\n'
import os
os.getcwd()
# 在jupyter中,这个tensorboard即可以使用绝对路径,也可以使用相对路径
# 当想关闭这个local host时,直接点击jupyter 工具栏的正方形按钮,停止cell的运行即可
'/Users/xiaokang/Desktop/问题解决/自己'
!tensorboard --logdir=/Users/xiaokang/Desktop/问题解决/自己/log/
Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
TensorBoard 2.4.0 at http://localhost:6006/ (Press CTRL+C to quit)
^C
!tensorboard --logdir=./log
Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
TensorBoard 2.4.0 at http://localhost:6006/ (Press CTRL+C to quit)
^C
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jDkNi8RT-1619444084482)(attachment:image.png)]
案例2
import numpy as np
## prepare the original data
with tf.name_scope('data'):
x_data = np.random.rand(100).astype(np.float32)
y_data = 0.3*x_data+0.1
##creat parameters
with tf.name_scope('parameters'):
weight = tf.Variable(tf.random_uniform([1],-1.0,1.0))
bias = tf.Variable(tf.zeros([1]))
##get y_prediction
with tf.name_scope('y_prediction'):
y_prediction = weight*x_data+bias
##compute the loss
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.square(y_data-y_prediction))
##creat optimizer
optimizer = tf.train.GradientDescentOptimizer(0.5)
#creat train ,minimize the loss
with tf.name_scope('train'):
train = optimizer.minimize(loss)
#creat init
with tf.name_scope('init'):
init = tf.global_variables_initializer()
##creat a Session
sess = tf.Session()
##initialize
writer = tf.summary.FileWriter('./log1/', sess.graph)
sess.run(init)
## Loop
for step in range(200):
sess.run(train)
if step %10==0 :
print (step ,'weight:',sess.run(weight),'bias:',sess.run(bias))
0 weight: [0.35282803] bias: [0.09636413]
10 weight: [0.32174712] bias: [0.08892063]
20 weight: [0.31089404] bias: [0.09444987]
30 weight: [0.30545732] bias: [0.09721969]
40 weight: [0.3027338] bias: [0.09860723]
50 weight: [0.3013695] bias: [0.0993023]
60 weight: [0.30068606] bias: [0.09965048]
70 weight: [0.3003437] bias: [0.09982491]
80 weight: [0.30017215] bias: [0.09991229]
90 weight: [0.30008623] bias: [0.09995607]
100 weight: [0.3000432] bias: [0.099978]
110 weight: [0.30002162] bias: [0.09998899]
120 weight: [0.30001086] bias: [0.09999447]
130 weight: [0.30000547] bias: [0.09999722]
140 weight: [0.30000272] bias: [0.09999862]
150 weight: [0.30000138] bias: [0.09999931]
160 weight: [0.3000007] bias: [0.09999964]
170 weight: [0.30000037] bias: [0.09999982]
180 weight: [0.30000016] bias: [0.09999992]
190 weight: [0.30000016] bias: [0.09999993]
!tensorboard --logdir=./log1
Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
TensorBoard 2.4.0 at http://localhost:6006/ (Press CTRL+C to quit)
^C
例3
## prepare the original data
with tf.name_scope('data'):
x_data = np.random.rand(100).astype(np.float32)
y_data = 0.3*x_data+0.1
##creat parameters
with tf.name_scope('parameters'):
weight = tf.Variable(tf.random_uniform([1],-1.0,1.0))
tf.summary.histogram('weights',weight) ##新增加的代码
bias = tf.Variable(tf.zeros([1]))
tf.summary.histogram('bias',bias) ##新增加的代码
##get y_prediction
with tf.name_scope('y_prediction'):
y_prediction = weight*x_data+bias
##compute the loss
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.square(y_data-y_prediction))
tf.summary.scalar('loss',loss) ##新增加的代码
##creat optimizer
optimizer = tf.train.GradientDescentOptimizer(0.5)
#creat train ,minimize the loss
with tf.name_scope('train'):
train = optimizer.minimize(loss)
#creat init
with tf.name_scope('init'):
init = tf.global_variables_initializer()
##creat a Session
sess = tf.Session()
##merged
merged = tf.summary.merge_all() ##新增加的代码
##initialize
writer = tf.summary.FileWriter('./log2/', sess.graph)
sess.run(init)
## Loop
for step in range(200):
sess.run(train)
result = sess.run(merged)
writer.add_summary(result,step) ##新增加的代码 将打印代码进行了去除操作
!tensorboard --logdir=./log2
Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
TensorBoard 2.4.0 at http://localhost:6006/ (Press CTRL+C to quit)
^C
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-R8MKHqy6-1619444084483)(attachment:image.png)]