分享

tensorboard的使用

 beginnow1 2019-12-13

作用:

在复杂的问题中,网络往往都是很复杂的,为了方便调试参数以及调整网络结构,我们需要将计算图可视化出来,以便能够更好的进行下一步的决策。tensorboard是可视化tensorflow模型的训练过程的工具,在安装tensorflow的同时就已经安装好了tensorboard。tensorboard能够有效地展示tensorflow在运行过程中的计算图、各种指标随着时间的变化趋势以及训练中使用到的数据信息

工作流程:

tensorboard是通过一些操作将数据记录到文件中,然后再读取文件来完成作图的,关键的几个步骤如下:

(1)summary:在定义计算图的时候,在适当的位置加上一些summary操作

(2)merge:在训练的时候可能加了多个summary操作,需要使用tf.summary.merge_all将这些summary操作合成一个操作,由它来产生所有的summary数据

(3)run:在没有运行的时候这些操作是不会执行任何东西的,仅仅是定义了一下,在运行(开始训练)的时候,我们需要通过tf.summary.FileWrite()指定一个目录告诉程序把产生的文件放到哪,然后在运行的时候使用 add_summary() 来将某一步的 summary 数据记录到文件中

运行:

当训练完成后,在命令行使用 tensorboard --logdir=path/to/log-directory 来启动 TensorBoard,按照提示在浏览器打开页面,注意把 path/to/log-directory 替换成你上面指定的目录。

Tensorboard可以记录与展示以下数据形式:

(1)标量Scalars   存储和显示诸如学习率和损失等单个值的变化趋势

  1. 图片Images   对于输入是图像的模型,显示某一步输入给模型的图像

(3)音频Audio   显示可播放的音频

(4)计算图Graph   显示代码中定义的计算图,也可以显示包括每个节点的计算时间、内存使用等情况

(5)数据分布Distribution   显示模型参数随迭代次数的变化情况

(6)直方图Histograms   显示模型参数随迭代次数的变化情况

(7)嵌入向量Embeddings  在3D或者2D图中展示高维数据

(8)文本Text   显示保存的一小段文字

 

例子1:

具体的代码如下所示:

  1. #coding:utf-8
  2. import tensorflow as tf
  3. ##定义一个简单的计算图,实现两个常量相加的操作
  4. with tf.name_scope('graph') as scope:
  5. a = tf.constant(1,name='a')
  6. b = tf.constant(2,name='b')
  7. add = tf.add(a,b,name='add')
  8. sess = tf.Session()
  9. ##生成一个写日志的writer,并将当前的tensorflow计算图写入日志
  10. writer = tf.summary.FileWriter('F:/logs',sess.graph)
  11. init = tf.global_variables_initializer()
  12. sess.run(init)
  13. '''
  14. tf.name_scope函数是作用域名,上述代码中在graph中有3个op:a、b、add,用tf函数内部的name参数命名,这些会在tensorboard中显示出来
  15. '''

在pycharm中的terminal中输入:

tensorboard --logdir=F:\logs

如下所示:

在上图中的红色框中在谷歌浏览器中输入相对应的网址,则显示的结果如下图所示:

注意:

写日志的位置中不要出现中文的地址,否则很有可能报错,本人刚开始时用的中文地址,结果一直没有显示出来,稍微注意一下下

 

例子2:

这个例子来自:

https://www.cnblogs.com/fydeblog/p/7429344.html

具体的代码如下所示:

  1. import tensorflow as tf
  2. import numpy as np
  3. ## prepare the original data
  4. with tf.name_scope('data'):
  5. x_data = np.random.rand(100).astype(np.float32)
  6. y_data = 0.3*x_data+0.1
  7. ##creat parameters
  8. with tf.name_scope('parameters'):
  9. weight = tf.Variable(tf.random_uniform([1],-1.0,1.0))
  10. bias = tf.Variable(tf.zeros([1]))
  11. ##get y_prediction
  12. with tf.name_scope('y_prediction'):
  13. y_prediction = weight*x_data+bias
  14. ##compute the loss
  15. with tf.name_scope('loss'):
  16. loss = tf.reduce_mean(tf.square(y_data-y_prediction))
  17. ##creat optimizer
  18. optimizer = tf.train.GradientDescentOptimizer(0.5)
  19. #creat train ,minimize the loss
  20. with tf.name_scope('train'):
  21. train = optimizer.minimize(loss)
  22. #creat init
  23. with tf.name_scope('init'):
  24. init = tf.global_variables_initializer()
  25. ##creat a Session
  26. sess = tf.Session()
  27. ##initialize
  28. writer = tf.summary.FileWriter('F:/logs', sess.graph)
  29. sess.run(init)
  30. ## Loop
  31. for step in range(200):
  32. sess.run(train)
  33. if step %10==0 :
  34. print (step ,'weight:',sess.run(weight),'bias:',sess.run(bias))

先初始化参数,算出预测,计算损失,然后训练,更新相应的参数

 

例子3:

针对例子2进行了一些修改,修改之后的代码如下所示:
修改的部分在代码中相应的做了注释

  1. import tensorflow as tf
  2. import numpy as np
  3. ## prepare the original data
  4. with tf.name_scope('data'):
  5. x_data = np.random.rand(100).astype(np.float32)
  6. y_data = 0.3*x_data+0.1
  7. ##creat parameters
  8. with tf.name_scope('parameters'):
  9. weight = tf.Variable(tf.random_uniform([1],-1.0,1.0))
  10. tf.summary.histogram('weights',weight) ##新增加的代码
  11. bias = tf.Variable(tf.zeros([1]))
  12. tf.summary.histogram('bias',bias) ##新增加的代码
  13. ##get y_prediction
  14. with tf.name_scope('y_prediction'):
  15. y_prediction = weight*x_data+bias
  16. ##compute the loss
  17. with tf.name_scope('loss'):
  18. loss = tf.reduce_mean(tf.square(y_data-y_prediction))
  19. tf.summary.scalar('loss',loss) ##新增加的代码
  20. ##creat optimizer
  21. optimizer = tf.train.GradientDescentOptimizer(0.5)
  22. #creat train ,minimize the loss
  23. with tf.name_scope('train'):
  24. train = optimizer.minimize(loss)
  25. #creat init
  26. with tf.name_scope('init'):
  27. init = tf.global_variables_initializer()
  28. ##creat a Session
  29. sess = tf.Session()
  30. ##merged
  31. merged = tf.summary.merge_all() ##新增加的代码
  32. ##initialize
  33. writer = tf.summary.FileWriter('F:/logs', sess.graph)
  34. sess.run(init)
  35. ## Loop
  36. for step in range(200):
  37. sess.run(train)
  38. result = sess.run(merged)
  39. writer.add_summary(result,step) ##新增加的代码 将打印代码进行了去除操作

scalar中的loss训练图如下所示:

distribution中的weight和bias的训练图如下所示:

histogram中的weight和bias的训练图如下所示:

我们可以根据训练图,对模型的学习情况进行评估,比如我们看损失训练图,可以看到现在是一条慢慢变小的曲线,最后的值趋近于0(在现实情况,往往都有误差,趋近于0反而是过拟合),这符合本意,就是要最小化loss,如果loss的曲线最后没有平滑趋近一个数,则说明训练的力度还不够,还要加大次数,如果loss还很大,说明学习算法不太理想,需改变当前的算法,去实现更小的loss,另外两幅与loss类似,最后都是要趋近一个数的,没有趋近和上下浮动都是有问题的。

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多