Visualization of Tensorflow

Table of Contents

第七章 Tensorboard可视化图及训练

       TensorboardTensorboard可以用来可视化图和训练曲线,可以帮助识别图的错误及瓶颈。下面以最小批量梯度下降算法为例探究Tensorboard的使用。

7.1 过程记录

  1. 导入包

       所用包与之前类似,额外导入datatime包,方便使用系统时间命名日志文件,具体代码如下:

 import tensorflow as tf
 import numpy as np
 from sklearn.datasets import fetch_california_housing
 from sklearn.preprocessing import StandardScaler
 from datetime import datetime
  1. 数据处理

       将数据下载,整理并做预处理。

housing = fetch_california_housing()
m,n = housing.data.shape # 获取数据的行列数
housing_data_plus_bias = np.c_[np.ones((m,1)),housing.data] # 为数据添加偏差项, 即添加y=ax+b中的b,其中b为1

# 数据预处理,归一化
scaler = StandardScaler().fit(housing_data_plus_bias)
scaled_housing_data_plus_bias = scaler.transform(housing_data_plus_bias)
  1. 设置日志文件

       定义日志文件目录,每次运行时指定不同的日志文件,否则Tensorboard会自动将同目录下同名文件合并,导致可视化结果混乱。所以最方便的命名方法是利用本地时间戳进行命名。

now = datetime.utcnow().strftime("%Y%m%d%H%M%S")
root_logdir = "tf_logs"
logdir = "{}/run-{}/".format(root_logdir,now)
# # 以下代码放置在构造期最后  
# mse_summary = tf.summary.scalar('MSE',mse)  
# file_writer = tf.summary.FileWriter(logdir, tf.get_default_graph())
  1. 创建计算图(构造期)

       将图的定义与训练状态(汇总)写入Tensorboard需要读取的日志文件中。

X = tf.placeholder(tf.float32, shape=(None, n+1), name="X")
y = tf.placeholder(tf.float32, shape=(None, 1), name="y")
n_epochs = 1000
batch_size = 100
n_batches= int(np.ceil(m/batch_size))
global_learning_rate = 0.01
XT = tf.transpose(X)
theta = tf.Variable(tf.random_uniform([n+1,1],-1.0,1.0),name="theta")     # * 参数 seed=42
y_pred =  tf.matmul(X, theta, name="prediction")                          # 预测值
error = y_pred-y                                                          # 误差
mse = tf.reduce_mean(tf.square(error), name="mse")                        # 均方误差(成本函数)
## 定义优化器(梯度下降)
optimizer = tf.train.GradientDescentOptimizer(learning_rate =   global_learning_rate)
training_op = optimizer.minimize(mse) 
## 定义汇总
mse_summary = tf.summary.scalar('MSE',mse)                                # 创建了用来求MSE值的节点,并将其写入称为汇总(summary)的二进制日志字符串中  
file_writer = tf.summary.FileWriter(logdir, tf.get_default_graph())       # 创建一个将汇总写入日志目录的file_writer
  1. 创建会话(执行期)

       在训练时定期对mse_summary节点求值,将汇总信息输出,并利用file_writer将其写入事件文件(日志文件)。

init = tf.global_variables_initializer()                                  # 添加初始化节点
def fetch_batch(epoch, batch_index, batch_size):
# 随机分组
   np.random.seed(epoch * n_batches + batch_index) 
   indices = np.random.randint(m, size=batch_size)
   X_batch = scaled_housing_data_plus_bias[indices] 
   y_batch = housing.target.reshape(-1, 1)[indices] 
 return X_batch, y_batch  

with tf.Session() as sess:
   sess.run(init)
   for epoch in range(n_epochs):
       for batch_index in range(n_batches):
           X_batch, y_batch = fetch_batch(epoch, batch_index, batch_size)
          # 将均方误差mse写入日志文件
          if batch_index % 10 == 0:
              summary_str = mse_summary.eval(feed_dict={X:X_batch, y:y_batch})
              step = epoch * n_batches + batch_index
              file_writer.add_summary(summary_str, step)
             
sess.run(training_op, feed_dict={X:X_batch, y:y_batch})
best_theta = theta.eval()
print("The best theta is", best_theta)
file_writer.close()
  1. 运行结果

       运行会话后输出最终参数值并在目录./tf_logs/run-20190410073421下(目录名即系统时间戳)生成文件events.out.tfevents.1561521973.BaoDadeMacBook-Pro.local

  1. 启动Tensorboard服务器

       可以通过多种方式启动Tensorboard服务,可以通过virtualenv新建一个虚拟环境并启动Tensorboard,也可以在当前目录下直接启动Tensorboard,个人感觉两者并无太大差别,只要保证启动Tensorboard服务的目录一样就好。

  • 在当前目录启动Tensorboard
    终端进入当前目录;
    启动Tensorboard,终端输入: tensorboard --logdir=./tf_logs/(file_writer序列化数据的存储路径);
    启动完毕后:浏览器输入localhost:6006

  • virtualenv环境中启动Tensorboard
    根据个人喜好进入一个目录并在此目录下创建一个virtualenv环境并激活它;
    在虚拟环境下启动Tensorboard:

    tensorboard --logdir=tf_logs/
    starting TensorBoard on port 6006
    (You can navigate to http://0.0.0.0:6006)
    
  1. 查看训练状态

       由终端提示TensorBoard 1.13.1 at ...local:6006可知Tensorboard服务已启动并在6006端口监听,在浏览器中输入loclahost:6006可查看Tensorboard的监听界面如下:
[外链图片转存失败(img-bwpdS370-1562123625172)(Pictures/Tensorboard_1.png)]

图9-1.TensordBoard监听界面
  1. 使用Tensorboard

       再次运行之前的代码,将产生新的日志文件。关于Tensorboard的使用可参见图9-2和图9-3。
[外链图片转存失败(img-X0MLoz0j-1562123625174)(Pictures/Tensorboard_Event.png)]

图9-2.训练过程

在这里插入图片描述
图9-3.图结构

7.2 参考

[1] Geron.机器学习实战

As an AI language model, I can provide information on Python 3.9 and TensorFlow 2. Here is a brief overview: Python 3.9: Python 3.9 is the latest version of the Python programming language. It was released in October 2020 and comes with several new features and improvements. Some of the notable changes include: - Improved performance: Python 3.9 comes with several performance improvements, such as faster startup times and reduced memory usage. - New syntax features: Python 3.9 introduces new syntax features such as flexible function and variable annotations, as well as improved support for dictionaries. - Improved type hinting: Type hinting has been improved in Python 3.9, making it easier to write and maintain large codebases. - Other improvements: Python 3.9 also comes with several other improvements, such as improved string methods, new modules, and better error messages. TensorFlow 2: TensorFlow is an open-source machine learning framework developed by Google. TensorFlow 2 is the latest version of the framework, released in 2019. Some of the notable changes in TensorFlow 2 include: - Eager execution: TensorFlow 2 comes with eager execution enabled by default, making it easier to debug and develop models. - Keras integration: TensorFlow 2 has tighter integration with Keras, a popular high-level neural network API. This makes it easier to build and train deep learning models. - Improved performance: TensorFlow 2 comes with several performance improvements, such as better GPU support and improved data loading. - Other improvements: TensorFlow 2 also comes with several other improvements, such as improved support for distributed training, improved support for mobile and embedded devices, and improved visualization tools. In summary, Python 3.9 and TensorFlow 2 are both powerful tools for building machine learning models. Python 3.9 provides a range of new features and improvements, while TensorFlow 2 comes with improved performance and tighter integration with Keras. Together, they provide a powerful platform for building and training machine learning models.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值