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

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


  • 代码示范

# -*- coding: utf-8 -*-
# Using Tensorboard
#----------------------------------
#
# We illustrate the various ways to use
# Tensorboard import os
import io
import time
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf # Initialize a graph session
sess = tf.Session() # Create a visualizer object
summary_writer = tf.train.SummaryWriter('tensorboard', tf.get_default_graph()) # Create tensorboard folder if not exists
if not os.path.exists('tensorboard'):
os.makedirs('tensorboard')
print('Running a slowed down linear regression. '
'Run the command: $tensorboard --logdir="tensorboard" '
' Then navigate to http://127.0.0.0:6006') # You can also specify a port option with --port 6006 # Wait a few seconds for user to run tensorboard commands
time.sleep(3) # Some parameters
batch_size = 50
generations = 100 # Create sample input data
x_data = np.arange(1000)/10.
true_slope = 2.
y_data = x_data * true_slope + np.random.normal(loc=0.0, scale=25, size=1000) 【构造好了ground true数据】 # Split into train/test
train_ix = np.random.choice(len(x_data), size=int(len(x_data)*0.9), replace=False)
test_ix = np.setdiff1d(np.arange(1000), train_ix)  # 提取出setdiff1d不同的部分(only index)
x_data_train, y_data_train = x_data[train_ix], y_data[train_ix]
x_data_test, y_data_test = x_data[test_ix ], y_data[test_ix ] # Declare placeholders 加载样本的容器
x_graph_input = tf.placeholder(tf.float32, [None])
y_graph_input = tf.placeholder(tf.float32, [None]) # Declare model variables
m = tf.Variable(tf.random_normal([1], dtype=tf.float32), name='Slope') # Declare model: Input layer + weight --> value of next layer
output = tf.mul(m, x_graph_input, name='Batch_Multiplication') # Declare loss function (L1)
residuals = output - y_graph_input  # 联想到了 "深度残差网络" 何凯明,减小均值
l2_loss = tf.reduce_mean(tf.abs(residuals), name="L2_Loss") # Declare optimization function
my_optim = tf.train.GradientDescentOptimizer(0.01)  # 通过这个solver缩小loss
train_step = my_optim.minimize(l2_loss) # Visualize a scalar
with tf.name_scope('Slope_Estimate'):
tf.scalar_summary('Slope_Estimate', tf.squeeze(m)) # Visualize a histogram (errors)
with tf.name_scope('Loss_and_Residuals'):
tf.histogram_summary('Histogram_Errors', l2_loss)
tf.histogram_summary('Histogram_Residuals', residuals) # Declare summary merging operation
summary_op = tf.merge_all_summaries()

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

#Create a function to save a protobuf bytes version of the graph
def gen_linear_plot(slope):
linear_prediction = x_data * slope
plt.plot(x_data, y_data, 'b.', label='data')
plt.plot(x_data, linear_prediction, 'r-', linewidth=3, label='predicted line')
plt.legend(loc='upper left')
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
return(buf) # Add image to tensorboard (plot the linear fit!)
slope = sess.run(m)
plot_buf = gen_linear_plot(slope[0])
# Convert PNG buffer to TF image
image = tf.image.decode_png(plot_buf.getvalue(), channels=4)
# Add the batch dimension
image = tf.expand_dims(image, 0)
# Add image summary
image_summary_op = tf.image_summary("Linear Plot", image)
image_summary = sess.run(image_summary_op)
log_writer.add_summary(image_summary, i)
log_writer.close()
  • 查看网络结构

  • 实时跟踪权重

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

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. 从零开始优雅的使用mongodb实例

    基本连接 一.创建express工程testmon express testmon 二.精简app.js var express = require("express"); var ...

  2. verilog语法实例学习(11)

    同步时序电路的一般形式 时序电路由组合逻辑以及一个或多个触发器实现.一般的架构如下图所示:W为输入,Z为输出,触发器中存储的状态为Q.在时钟信号的控制下,触发器通过加在其输入端的组合逻辑输入,使得电路 ...

  3. 混沌的艺术--- YChaos通过数学公式生成混沌图像

    艺术真得很难吗?也许如同编程一样容易.我写了一套软件,其功能是通过输入数学方程式,生成艺术图像.一提到数学有人可能会发怵,这里请不要担心,生成混沌的数学公式大都很是简单,基本上只用加.减.乘.除.余. ...

  4. Spark2.3(三十五)Spark Structured Streaming源代码剖析(从CSDN和Github中看到别人分析的源代码的文章值得收藏)

    从CSDN中读取到关于spark structured streaming源代码分析不错的几篇文章 spark源码分析--事件总线LiveListenerBus spark事件总线的核心是LiveLi ...

  5. 【T07】不要低估tcp的性能

    1.tcp在ip的基础上增加了校验和.可靠性和流量控制的功能,而udp只增加了校验和的功能,看起来udp应该会比tcp快很多, 但事实不是这样,有时候tcp比udp的性能还要好. 2.思考,在什么情况 ...

  6. MongoDB(1)--简单介绍以及安装

    前段时间接触了NoSql类型的数据库redis,当时是作为缓存server使用的.那么从这篇博客開始学习还有一个非常出名的NoSql数据库:MongoDb.只是眼下还没有在开发其中使用.一步一步来吧. ...

  7. js获取过滤条件中参数的快捷方式

    // window.location.href = "topupRecordController.do?exportExcel&" + encodeURI($(" ...

  8. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

  9. springboot1.5.4 log4j

    resources下面添加: log4j.properties: # log4j.rootCategory=INFO, stdout, file, errorfile log4j.category.c ...

  10. malloc()參数为0的情况

    以下的代码片段输出是什么?为什么? char *ptr; ))==NULL) puts("Got a null pointer"); else puts("Got a v ...