【深度学习】实验08 TensorBoard案例

简介: 【深度学习】实验08 TensorBoard案例

TensorBoard可视化

import tensorflow as tf
# 定义命名空间
with tf.name_scope('input'):
  # fetch:就是同时运行多个op的意思
  # 定义名称,会在tensorboard中代替显示
    input1 = tf.constant(3.0,name='A')
    input2 = tf.constant(4.0,name='B')
    input3 = tf.constant(5.0,name='C')
with tf.name_scope('op'):
    #加法
    add = tf.add(input2,input3)
    #乘法
    mul = tf.multiply(input1,add)
with tf.Session() as ss:
    #默认在当前py目录下的logs文件夹,没有会自己创建
    result = ss.run([mul,add])
    wirter = tf.summary.FileWriter('logs/demo/',ss.graph)
    print(result)
[27.0, 9.0]

这段代码主要演示了如何使用TensorFlow和TensorBoard创建和可视化计算图。


TensorFlow是一个基于数据流图进行数值计算的开源软件库,具有快速的计算速度和灵活的构建方式,被广泛应用于机器学习、深度学习等领域。而TensorBoard是TensorFlow提供的一个可视化工具,可以帮助开发者更好地理解、调试和优化TensorFlow中的计算图。


在这段代码中,首先通过tf.constant方法创建了三个常量input1、input2和input3,分别赋值为3.0、4.0和5.0,并给这些常量取了一个别名,分别为“A”、“B”和“C”,这样在后续的TensorBoard中我们就可以清晰地看到它们之间的关系。


接着,使用tf.add和tf.multiply方法分别定义了加法和乘法操作,其中加法使用了input2和input3,乘法使用了input1和加法的结果。在这里也定义了两个命名空间input和op,分别代表输入和操作的过程。


然后,使用with tf.Session() as ss:创建一个会话,用ss.run方法来运行计算图,并将结果保存在result中。


最后,使用tf.summary.FileWriter方法将计算图写入到logs/demo/目录下,以便在TensorBoard中查看。运行python 文件名.py后,在命令行中输入tensorboard --logdir=logs/demo启动TensorBoard服务,打开浏览器,输入http://localhost:6006/即可访问TensorBoard的可视化界面。


在TensorBoard界面中,可以查看到计算图的可视化结构、常量的取值、操作的过程等信息,帮助开发者更好地理解、调试和优化TensorFlow的计算图。

TensorBoard案例

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import sys
import os
import tensorflow as tf
import warnings
warnings.filterwarnings("ignore")
from tensorflow.examples.tutorials.mnist import input_data
max_steps = 200  # 最大迭代次数 默认1000
learning_rate = 0.001   # 学习率
dropout = 0.9   # dropout时随机保留神经元的比例
data_dir = os.path.join('data', 'mnist')# 样本数据存储的路径
if not os.path.exists('log'):
    os.mkdir('log')
log_dir = 'log'   # 输出日志保存的路径
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)
sess = tf.InteractiveSession()
with tf.name_scope('input'):
    x = tf.placeholder(tf.float32, [None, 784], name='x-input')
    y_ = tf.placeholder(tf.float32, [None, 10], name='y-input')
#使用tf.summary.image保存图像信息,在tensorboard上还原出输入的特征数据对应的图片
with tf.name_scope('input_reshape'):
    image_shaped_input = tf.reshape(x, [-1, 28, 28, 1])
    tf.summary.image('input', image_shaped_input, 10)
def weight_variable(shape):
    """Create a weight variable with appropriate initialization."""
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)
def bias_variable(shape):
    """Create a bias variable with appropriate initialization."""
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)
def variable_summaries(var):
    """Attach a lot of summaries to a Tensor (for TensorBoard visualization)."""
    with tf.name_scope('summaries'):
      # 计算参数的均值,并使用tf.summary.scaler记录
        mean = tf.reduce_mean(var)
        tf.summary.scalar('mean', mean)
        # 计算参数的标准差
        with tf.name_scope('stddev'):
            stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
            # 使用tf.summary.scaler记录记录下标准差,最大值,最小值
            tf.summary.scalar('stddev', stddev)
            tf.summary.scalar('max', tf.reduce_max(var))
            tf.summary.scalar('min', tf.reduce_min(var))
            # 用直方图记录参数的分布
            tf.summary.histogram('histogram', var)
"""
构建神经网络层
创建第一层隐藏层
创建一个构建隐藏层的方法,输入的参数有:
input_tensor:特征数据
input_dim:输入数据的维度大小
output_dim:输出数据的维度大小(=隐层神经元个数)
layer_name:命名空间
act=tf.nn.relu:激活函数(默认是relu)
"""
def nn_layer(input_tensor, input_dim, output_dim, layer_name, act=tf.nn.relu):
    """Reusable code for making a simple neural net layer.
    It does a matrix multiply, bias add, and then uses relu to nonlinearize.
    It also sets up name scoping so that the resultant graph is easy to read,
    and adds a number of summary ops.
    """
    # 设置命名空间
    with tf.name_scope(layer_name):
        # 调用之前的方法初始化权重w,并且调用参数信息的记录方法,记录w的信息
        with tf.name_scope('weights'):
            weights = weight_variable([input_dim, output_dim]) #神经元数量
            variable_summaries(weights)
        # 调用之前的方法初始化权重b,并且调用参数信息的记录方法,记录b的信息
        with tf.name_scope('biases'):
            biases = bias_variable([output_dim])
            variable_summaries(biases)
        # 执行wx+b的线性计算,并且用直方图记录下来
        with tf.name_scope('linear_compute'):
            preactivate = tf.matmul(input_tensor, weights) + biases
            tf.summary.histogram('linear', preactivate)
        # 将线性输出经过激励函数,并将输出也用直方图记录下来
        activations = act(preactivate, name='activation')
        tf.summary.histogram('activations', activations)
        # 返回激励层的最终输出
        return activations
hidden1 = nn_layer(x, 784, 500, 'layer1')
"""
创建一个dropout层,,随机关闭掉hidden1的一些神经元,并记录keep_prob
"""
with tf.name_scope('dropout'):
    keep_prob = tf.placeholder(tf.float32)
    tf.summary.scalar('dropout_keep_probability', keep_prob)
    dropped = tf.nn.dropout(hidden1, keep_prob)
"""
创建一个输出层,输入的维度是上一层的输出:500,输出的维度是分类的类别种类:10,
激活函数设置为全等映射identity.(暂且先别使用softmax,会放在之后的损失函数中一起计算)
"""
y = nn_layer(dropped, 500, 10, 'layer2', act=tf.identity)
"""
创建损失函数
使用tf.nn.softmax_cross_entropy_with_logits来计算softmax并计算交叉熵损失,并且求均值作为最终的损失值。
"""
with tf.name_scope('loss'):
    # 计算交叉熵损失(每个样本都会有一个损失)
    diff = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)
    with tf.name_scope('total'):
        # 计算所有样本交叉熵损失的均值
        cross_entropy = tf.reduce_mean(diff)
tf.summary.scalar('loss', cross_entropy)
"""
训练,并计算准确率
使用AdamOptimizer优化器训练模型,最小化交叉熵损失
计算准确率,并用tf.summary.scalar记录准确率
"""
with tf.name_scope('train'):
    train_step = tf.train.AdamOptimizer(learning_rate).minimize(
        cross_entropy)
with tf.name_scope('accuracy'):
    with tf.name_scope('correct_prediction'):
        # 分别将预测和真实的标签中取出最大值的索引,弱相同则返回1(true),不同则返回0(false)
        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    with tf.name_scope('accuracy'):
        # 求均值即为准确率
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
tf.summary.scalar('accuracy', accuracy)
# summaries合并
merged = tf.summary.merge_all()
# 写到指定的磁盘路径中
#删除src路径下所有文件
def delete_file_folder(src):
    '''delete files and folders'''
    if os.path.isfile(src):
        try:
            os.remove(src)
        except:
            pass
    elif os.path.isdir(src):
        for item in os.listdir(src):
            itemsrc=os.path.join(src,item)
            delete_file_folder(itemsrc) 
        try:
            os.rmdir(src)
        except:
            pass
#删除之前生成的log
if os.path.exists(log_dir + '/train'):
    delete_file_folder(log_dir + '/train')
if os.path.exists(log_dir + '/test'):
    delete_file_folder(log_dir + '/test')
train_writer = tf.summary.FileWriter(log_dir + '/train', sess.graph)
test_writer = tf.summary.FileWriter(log_dir + '/test')
# 运行初始化所有变量
tf.global_variables_initializer().run()
#现在我们要获取之后要喂入的数据
def feed_dict(train):
    """Make a TensorFlow feed_dict: maps data onto Tensor placeholders."""
    if train:
        xs, ys = mnist.train.next_batch(100)
        k = dropout
    else:
        xs, ys = mnist.test.images, mnist.test.labels
        k = 1.0
    return {x: xs, y_: ys, keep_prob: k}
"""
开始训练模型。 每隔10步,就进行一次merge, 并打印一次测试数据集的准确率,
然后将测试数据集的各种summary信息写进日志中。 每隔100步,记录原信息 
其他每一步时都记录下训练集的summary信息并写到日志中。
"""
for i in range(max_steps):
    if i % 10 == 0:  # 记录测试集的summary与accuracy
        summary, acc = sess.run([merged, accuracy], feed_dict=feed_dict(False))
        test_writer.add_summary(summary, i)
        print('Accuracy at step %s: %s' % (i, acc))
    else:  # 记录训练集的summary
        if i % 100 == 99:  # Record execution stats
            run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
            run_metadata = tf.RunMetadata()
            summary, _ = sess.run([merged, train_step],
                              feed_dict=feed_dict(True),
                              options=run_options,
                              run_metadata=run_metadata)
            train_writer.add_run_metadata(run_metadata, 'step%03d' % i)
            train_writer.add_summary(summary, i)
            print('Adding run metadata for', i)
        else:  # Record a summary
            summary, _ = sess.run([merged, train_step], feed_dict=feed_dict(True))
            train_writer.add_summary(summary, i)
train_writer.close()
test_writer.close()
   WARNING:tensorflow:From <ipython-input-3-27b4be5f38e0>:25: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
   Instructions for updating:
   Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
   WARNING:tensorflow:From /home/nlp/anaconda3/lib/python3.7/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
   Instructions for updating:
   Please write your own downloading logic.
   WARNING:tensorflow:From /home/nlp/anaconda3/lib/python3.7/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
   Instructions for updating:
   Please use tf.data to implement this functionality.
   Extracting MNIST_data/train-images-idx3-ubyte.gz
   WARNING:tensorflow:From /home/nlp/anaconda3/lib/python3.7/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
   Instructions for updating:
   Please use tf.data to implement this functionality.
   Extracting MNIST_data/train-labels-idx1-ubyte.gz
   WARNING:tensorflow:From /home/nlp/anaconda3/lib/python3.7/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
   Instructions for updating:
   Please use tf.one_hot on tensors.
   Extracting MNIST_data/t10k-images-idx3-ubyte.gz
   Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
   WARNING:tensorflow:From /home/nlp/anaconda3/lib/python3.7/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
   Instructions for updating:
   Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
   WARNING:tensorflow:From <ipython-input-3-27b4be5f38e0>:109: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
   Instructions for updating:
   Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
   WARNING:tensorflow:From <ipython-input-3-27b4be5f38e0>:123: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
   Instructions for updating:
   Future major versions of TensorFlow will allow gradients to flow
   into the labels input on backprop by default.
   See `tf.nn.softmax_cross_entropy_with_logits_v2`.
   Accuracy at step 0: 0.0639
   Accuracy at step 10: 0.7139
   Accuracy at step 20: 0.8271
   Accuracy at step 30: 0.8647
   Accuracy at step 40: 0.8818
   Accuracy at step 50: 0.8932
   Accuracy at step 60: 0.8984
   Accuracy at step 70: 0.8986
   Accuracy at step 80: 0.9062
   Accuracy at step 90: 0.9128
   Adding run metadata for 99
   Accuracy at step 100: 0.9134
   Accuracy at step 110: 0.9212
   Accuracy at step 120: 0.9156
   Accuracy at step 130: 0.9226
   Accuracy at step 140: 0.9251
   Accuracy at step 150: 0.9238
   Accuracy at step 160: 0.9259
   Accuracy at step 170: 0.9265
   Accuracy at step 180: 0.9291
   Accuracy at step 190: 0.932
   Adding run metadata for 199    

这段代码主要演示了如何使用Tensorflow和TensorBoard创建和可视化卷积神经网络(CNN)。


CNN是一种深度学习结构,是神经网络中的一种,可以应用于图像识别、语音识别等领域。在这段代码中,我们将使用CNN完成MNIST手写数字识别任务,输入为28×28像素的手写数字图像,输出为0-9其中一种数字的概率。


首先,通过tf.placeholder方法创建了两个placeholder变量x和y_,分别表示网络的输入和输出。在输入数据的处理上,为了将输入数据(28×28个像素点)可视化,使用了tf.summary.image记录了图像信息,用reshape方法将输入特征数据进行重构,确保输入的图像是28×28×1的大小,并用tf.summary.image将其记录下来。


其次,在神经网络的构建方面,我们创建了两个隐藏层和一个输出层。其中,每一个隐藏层都包含一个线性计算层和一个ReLU激活函数层,并用tf.summary.histogram方法记录下每一层的相关参数,以便在TensorBoard中查看各个层的变化。


然后,我们在第一个隐藏层后加入了dropout层,随机关闭掉一定比例的神经元,以避免过拟合。在输出层中,使用tf.nn.softmax cross_entropy_with_logits计算交叉熵损失,并用tf.summary.scalar方法记录损失信息。我们使用tf.train.AdamOptimizer训练模型,并使用tf.reduce_mean(tf.cast(correct_prediction, tf.float32))计算准确率,并用tf.summary.scalar记录准确率信息。


最后,我们定义了merged变量,将所有需要记录下来的信息汇总在一起,并通过tf.summary.merge_all()的方法全部合并,最后通过tf.summary.FileWriter方法将所有的信息写入到日志文件中。在训练过程中,每隔10步就记录下测试集的准确率和相关信息,并记录到日志中;每隔100步记录下训练集的原信息,并记录到日志中;其他步数记录训练集的summary以及写入到日志中。最终,通过train_writer.close()和test_writer.close()关闭日志文件。


整个代码中,命名空间的使用规范,各个参数的记录方式清晰明了,使得我们在TensorBoard中能够清晰地了解每一层的参数变化、loss的变化、准确率的变化等。因此,TensorBoard能够很好地帮助开发者进行模型的调试、分析和优化。

目录
相关文章
|
15天前
|
机器学习/深度学习 并行计算 算法
R语言深度学习不同模型对比分析案例
R语言深度学习不同模型对比分析案例
28 0
|
3月前
|
机器学习/深度学习
分享3个深度学习练手的小案例
分享3个深度学习练手的小案例
22 0
|
7月前
|
机器学习/深度学习 人工智能 自然语言处理
【深度学习】实验18 自然语言处理
【深度学习】实验18 自然语言处理
38 0
|
7月前
|
机器学习/深度学习 自然语言处理
【深度学习】实验17 使用GAN生成手写数字样本
【深度学习】实验17 使用GAN生成手写数字样本
73 0
|
7月前
|
机器学习/深度学习 算法 PyTorch
【深度学习】实验16 使用CNN完成MNIST手写体识别(PyTorch)
【深度学习】实验16 使用CNN完成MNIST手写体识别(PyTorch)
79 0
|
7月前
|
机器学习/深度学习 自然语言处理 算法
【深度学习】实验15 使用CNN完成MNIST手写体识别(Keras)
【深度学习】实验15 使用CNN完成MNIST手写体识别(Keras)
58 0
|
7月前
|
机器学习/深度学习 算法 TensorFlow
【深度学习】实验14 使用CNN完成MNIST手写体识别(TensorFlow)
【深度学习】实验14 使用CNN完成MNIST手写体识别(TensorFlow)
68 0
|
7月前
|
机器学习/深度学习 自然语言处理 PyTorch
【深度学习】实验12 使用PyTorch训练模型
【深度学习】实验12 使用PyTorch训练模型
81 0
|
7月前
|
机器学习/深度学习
【深度学习】实验13 使用Dropout抑制过拟合 2
【深度学习】实验13 使用Dropout抑制过拟合
24 0
|
7月前
|
机器学习/深度学习
【深度学习】实验13 使用Dropout抑制过拟合 1
【深度学习】实验13 使用Dropout抑制过拟合
30 0