Ref: https://www.tensorflow.org/get_started/summaries_and_tensorboard

可视化对于Training的重要性,不言而喻。


  • 代码示范

  1. # -*- coding: utf-8 -*-
  2. # Using Tensorboard
  3. #----------------------------------
  4. #
  5. # We illustrate the various ways to use
  6. # Tensorboard
  7.  
  8. import os
  9. import io
  10. import time
  11. import numpy as np
  12. import matplotlib.pyplot as plt
  13. import tensorflow as tf
  14.  
  15. # Initialize a graph session
  16. sess = tf.Session()
  17.  
  18. # Create a visualizer object
  19. summary_writer = tf.train.SummaryWriter('tensorboard', tf.get_default_graph())
  20.  
  21. # Create tensorboard folder if not exists
  22. if not os.path.exists('tensorboard'):
  23. os.makedirs('tensorboard')
  24. print('Running a slowed down linear regression. '
  25. 'Run the command: $tensorboard --logdir="tensorboard" '
  26. ' Then navigate to http://127.0.0.0:6006')
  27.  
  28. # You can also specify a port option with --port 6006
  29.  
  30. # Wait a few seconds for user to run tensorboard commands
  31. time.sleep(3)
  32.  
  33. # Some parameters
  34. batch_size = 50
  35. generations = 100
  36.  
  37. # Create sample input data
  38. x_data = np.arange(1000)/10.
  39. true_slope = 2.
  40. y_data = x_data * true_slope + np.random.normal(loc=0.0, scale=25, size=1000)
  41.  
  42. 【构造好了ground true数据】
  43.  
  44. # Split into train/test
  45. train_ix = np.random.choice(len(x_data), size=int(len(x_data)*0.9), replace=False)
  46. test_ix = np.setdiff1d(np.arange(1000), train_ix)  # 提取出setdiff1d不同的部分(only index)
  47. x_data_train, y_data_train = x_data[train_ix], y_data[train_ix]
  48. x_data_test, y_data_test = x_data[test_ix ], y_data[test_ix ]
  49.  
  50. # Declare placeholders 加载样本的容器
  51. x_graph_input = tf.placeholder(tf.float32, [None])
  52. y_graph_input = tf.placeholder(tf.float32, [None])
  53.  
  54. # Declare model variables
  55. m = tf.Variable(tf.random_normal([1], dtype=tf.float32), name='Slope')
  56.  
  57. # Declare model: Input layer + weight --> value of next layer
  58. output = tf.mul(m, x_graph_input, name='Batch_Multiplication')
  59. # Declare loss function (L1)
  60. residuals = output - y_graph_input  # 联想到了 "深度残差网络" 何凯明,减小均值
  61. l2_loss = tf.reduce_mean(tf.abs(residuals), name="L2_Loss")
  62.  
  63. # Declare optimization function
  64. my_optim = tf.train.GradientDescentOptimizer(0.01)  # 通过这个solver缩小loss
  65. train_step = my_optim.minimize(l2_loss)
  66.  
  67. # Visualize a scalar
  68. with tf.name_scope('Slope_Estimate'):
  69. tf.scalar_summary('Slope_Estimate', tf.squeeze(m))
  70.  
  71. # Visualize a histogram (errors)
  72. with tf.name_scope('Loss_and_Residuals'):
  73. tf.histogram_summary('Histogram_Errors', l2_loss)
  74. tf.histogram_summary('Histogram_Residuals', residuals)
  75.  
  76. # Declare summary merging operation
  77. summary_op = tf.merge_all_summaries()

  78. op操作各种各样,所以需要有个汇总的op操作】
  79. # Initialize Variables
  80. init = tf.initialize_all_variables()
  81. sess.run(init)
  82.  
  83. for i in range(generations):
  84. batch_indices = np.random.choice(len(x_data_train), size=batch_size)
  85. x_batch = x_data_train[batch_indices]
  86. y_batch = y_data_train[batch_indices]
  87. _, train_loss, summary = sess.run([train_step, l2_loss, summary_op],
  88. feed_dict={x_graph_input: x_batch, y_graph_input: y_batch})
  89.  
  90. test_loss, test_resids = sess.run([l2_loss, residuals], feed_dict={x_graph_input: x_data_test, y_graph_input: y_data_test})
  91.  
  92. if (i+1)%10==0:
  93. print('Generation {} of {}. Train Loss: {:.3}, Test Loss: {:.3}.'.format(i+1, generations, train_loss, test_loss))
  94.  
  95. log_writer = tf.train.SummaryWriter('tensorboard')
  96. log_writer.add_summary(summary, i)
  97. time.sleep(0.5)

  98. #Create a function to save a protobuf bytes version of the graph
  99. def gen_linear_plot(slope):
  100. linear_prediction = x_data * slope
  101. plt.plot(x_data, y_data, 'b.', label='data')
  102. plt.plot(x_data, linear_prediction, 'r-', linewidth=3, label='predicted line')
  103. plt.legend(loc='upper left')
  104. buf = io.BytesIO()
  105. plt.savefig(buf, format='png')
  106. buf.seek(0)
  107. return(buf)
  108.  
  109. # Add image to tensorboard (plot the linear fit!)
  110. slope = sess.run(m)
  111. plot_buf = gen_linear_plot(slope[0])
  112. # Convert PNG buffer to TF image
  113. image = tf.image.decode_png(plot_buf.getvalue(), channels=4)
  114. # Add the batch dimension
  115. image = tf.expand_dims(image, 0)
  116. # Add image summary
  117. image_summary_op = tf.image_summary("Linear Plot", image)
  118. image_summary = sess.run(image_summary_op)
  119. log_writer.add_summary(image_summary, i)
  120. log_writer.close()
  • 查看网络结构

  • 实时跟踪权重

Ref: http://www.jianshu.com/p/52e773d47338

  1. tensorboard --logdir results --reload_interval 5
    【默认的 reload_interval 120秒,以避免在计算机上面太快统计,但是在我们的情况下,我们可以安全地加速一点】

06:Tensorflow的可视化工具Tensorboard的初步使用

小姑娘整理的不错,之后二次整理一下。

[TensorBoard] *Cookbook - Tensorboard的更多相关文章

  1. [TensorBoard] Train and Test accuracy simultaneous tracking

    训练时的实时状态跟踪的重要性 不言而喻. [Tensorboard] Cookbook - Tensorboard  讲解调节更新频率 直接上代码展示: import numpy as np impo ...

  2. 本人AI知识体系导航 - AI menu

    Relevant Readable Links Name Interesting topic Comment Edwin Chen 非参贝叶斯   徐亦达老板 Dirichlet Process 学习 ...

  3. tensorflow笔记(三)之 tensorboard的使用

    tensorflow笔记(三)之 tensorboard的使用 版权声明:本文为博主原创文章,转载请指明转载地址 http://www.cnblogs.com/fydeblog/p/7429344.h ...

  4. 机器学习笔记4-Tensorflow线性模型示例及TensorBoard的使用

    前言 在上一篇中,我简单介绍了一下Tensorflow以及在本机及阿里云的PAI平台上跑通第一个示例的步骤.在本篇中我将稍微讲解一下几个基本概念以及Tensorflow的基础语法. 本文代码都是基于A ...

  5. 【keras】用tensorboard监视CNN每一层的输出

    from keras.models import Sequential from keras.layers import Dense, Dropout from keras.layers import ...

  6. windows tensorboard http://0.0.0.0:6006 无法访问 解决方法 - using chrome and localhost as ip

    启动命令: tensorboard --logdir="tensorboard" 启动后显示 Starting TensorBoard b'47' at http://0.0.0. ...

  7. TensorFlow基础笔记(9) Tensorboard可视化显示以及查看pb meta模型文件的方法

    参考: http://blog.csdn.net/l18930738887/article/details/55000008 http://www.jianshu.com/p/19bb60b52dad ...

  8. tensorboard实现tensorflow可视化

    1.工程目录 2.data.input_data.py的导入 在tensorflow更新之后可以进行直接的input_data的导入 # from tensorflow.examples.tutori ...

  9. windows平台tensorboard的配置及使用

    由于官网和其他教程里面都是以Linux为平台演示tensorboard使用的,而在Windows上与Linux上会有一些差别,因此我将学习的过程记录下来与大家分享(基于tensorflow1.2.1版 ...

随机推荐

  1. unity无限循环报错的定位

    晚上遇到了,碰到了程序一运行就卡住的尴尬问题,然后百度下,看了看,Get到了一个新的skill. 1. 打开对应的VS程序,选择“调试/Attach Unity Debuger”菜单来调试代码. 2. ...

  2. JNI编程实现(Linux)

    JNI是Java Native Interface的缩写,是Java平台的本地调用,从Java1.1就成为了Java标准的一部分,它允许Java代码和其它语言的代码进行互相调用,只要调用约定支持即可, ...

  3. Ubuntu GNOME 13.04将关闭窗口的按钮放在最右边

    转载请注明:转自http://blog.csdn.net/u010811449/article/details/9426187 先上图: 首先打开dconf系统配置编译器. 找到 rog -> ...

  4. Java全栈程序员之01:做个Linux下的程序猿

    Windows10正在成为史上口碑最差的Windows系统 (图侵删) 我曾经花了数次1小时去寻找解决方案去关闭自动更新,包括停掉服务.修改注册表等等.但是都没有成功. 微软自身是知道这个问题的,但就 ...

  5. 递归与迭代的联系以及优缺点(以c++为例)

    1.递归的定义: 程序直接或间接的调用自身的方法. 递归算法的特点:(1) 递归就是在过程或函数里调用自身.(2) 在使用递归策略时,必须有一个明确的递归结束条件,称为递归出口.(3) 递归算法解题通 ...

  6. 几种php加速器比较

    一.PHP加速器介绍 PHP加速器是一个为了提高PHP执行效率,从而缓存起PHP的操作码,这样PHP后面执行就不用解析转换了,可以直接调用PHP操作码,这样速度上就提高了不少. Apache中使用mo ...

  7. ubuntu 登陆信息打印 -- motd

    新需求需要改变 Ubuntu 启动时的登录信息打印,根据搜索到的资料,找到了这里: luo[~]ssh luo@192.168.100.233 Press ^@ (C-Space) to enter ...

  8. 详解CentOS设置163的yum源的过程

    转自启动CentOS系统,打开火狐浏览器,如下图所示: 2 登录“mirrors.163.com”,如下图所示: 3 点击centos后面的“centos使用帮助”,如下图所示: 4 可以看到设置和使 ...

  9. 【微信上传素材接口--永久性】微信永久性上传、获取返回的medie_id 和url

    上传图片到微信服务器获得media_id和url (永久性) 其他接口类:https://www.cnblogs.com/gjw-hsf/p/7375261.html 转载地址:https://blo ...

  10. javascript回调函数笔记

    来源于:https://github.com/useaname/blog-study 在Javascript中,函数是第一类对象.意味函数可以像对象一样按照第一类被管理使用.回调函数是从一个叫函数式编 ...