详解TensorFlow的 tf.train.SummaryWriter 函数:保存 TensorBoard 可视化数据

yizhihongxing

TensorFlow的tf.train.SummaryWriter函数是可以用来将记录在TensorFlow张量中的数据输出到事件文件中。这些事件文件可以被TensorBoard可视化工具读取以便进行深入的分析。

1.函数作用

tf.train.SummaryWriter的作用是在训练过程中保存TensorBoard所需要的数据。

2.函数参数

tf.summary.FileWriter(logdir, graph=None,max_queue=10,flush_secs=120, 
                       filename_suffix=None)

其中,函数参数说明如下:

  • logdir - 输出文件的文件路径。
  • graph(默认为None) - 要写入的图,可通过tf.get_default_grap()函数获取。
  • max_queue- 最大的队列大小。默认取值为10。
  • flush_sec - 两次日志写入事件之间的最大秒值。默认为120秒。
  • filename_suffix - 日志文件名的后缀。

3.函数使用

下面示例代码展示了如何在代码运行过程中记录数据:

import tensorflow as tf
import numpy as np

# 构建图和数据
with tf.name_scope("input") :
    x = tf.placeholder(tf.float32, [None,1], name="x_input")
    y = tf.placeholder(tf.float32, [None,1], name="y_input")

with tf.name_scope("L1") :
    W1 = tf.Variable(tf.random_normal([1,10]), name="Weights")
    b1 = tf.Variable(tf.zeros([1,10]), name="bias")
    L1 = tf.matmul(x, W1) + b1
    #添加sigmoid激活函数
    L1 = tf.nn.sigmoid(L1)

with tf.name_scope("L2") :
    W2 = tf.Variable(tf.random_normal([10,1]), name="Weights")
    b2 = tf.Variable(tf.zeros([1,1]), name="bias")
    L2 = tf.matmul(L1,W2) + b2

loss = tf.reduce_mean(tf.square(y-L2))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

#初始化参数
init = tf.global_variables_initializer()

#记录数据操作
log_path="/tmp/tensorflow_logs/chapter2_logs"
writer = tf.summary.FileWriter(log_path, tf.get_default_graph())

#运行这个模型
with tf.Session() as sess:
    sess.run(init)   
    for i in range(1000):
        sess.run(train_step, feed_dict={x:np.linspace(-1,1,100)[:,np.newaxis],
                                        y:np.linspace(-1,1,100)[:,np.newaxis]})
        if i%10 ==0:
            _, l, result = sess.run([train_step, loss, L2], feed_dict={x:np.linspace(-1,1,100)[:,np.newaxis],
                                                                        y:np.linspace(-1,1,100)[:,np.newaxis]})
            print(l)
#关闭writer
writer.close()

运行上述代码,就可以在文件夹"/tmp/tensorflow_logs/chapter2_logs"中读到训练期间记录下来的数据。

我们还可以使用tf.summary来构建一个更为详细的示例:

import tensorflow as tf;
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("data/", one_hot=True)

def variable_summaries(var, name):
    """Attach a lot of summaries to a Tensor."""
    with tf.name_scope('summaries'):
        mean = tf.reduce_mean(var)
        tf.summary.scalar('mean/' + name, mean)
        with tf.name_scope('stddev'):
            stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
        tf.summary.scalar('stddev/' + name, stddev)
        tf.summary.scalar('max/' + name, tf.reduce_max(var))
        tf.summary.scalar('min/' + name, tf.reduce_min(var))
        tf.summary.histogram(name, var)

def nn_layer(input_tensor, input_dim, output_dim, layer_name, act=tf.nn.relu):  
    with tf.name_scope(layer_name):
        with tf.name_scope('weights'):
            weights = tf.Variable(tf.zeros([input_dim, output_dim]))
            variable_summaries(weights, layer_name + '/weights')
        with tf.name_scope('biases'):
            biases = tf.Variable(tf.zeros([output_dim]))
            variable_summaries(biases, layer_name + '/biases')
        with tf.name_scope('Wx_plus_b'):
            preactivate = tf.matmul(input_tensor, weights) + biases
            tf.summary.histogram(layer_name + '/pre_activations', preactivate)
        activations = act(preactivate, name='activation')
        tf.summary.histogram(layer_name + '/activations', activations)
        return activations

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')

#构建新的网络
hidden1 = nn_layer(x, 784, 500, 'layer1')

with tf.name_scope('softmax_linear'):
    with tf.name_scope('weights'):
        weights = tf.Variable(tf.zeros([500, 10]), name='weights')
        variable_summaries(weights, 'softmax_linear/weights')
    with tf.name_scope('biases'):
        biases = tf.Variable(tf.zeros([10]), name='biases')
        variable_summaries(biases, 'softmax_linear/biases')
    with tf.name_scope('Wx_plus_b'):
        logits = tf.matmul(hidden1, weights) + biases
        tf.summary.histogram('softmax_linear/pre_activations', logits)
    y = tf.nn.softmax(logits, name='softmax')

with tf.name_scope('cross_entropy'):
    diff = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y_)
    with tf.name_scope('total'):
        cross_entropy = tf.reduce_mean(diff)
    tf.summary.scalar('cross_entropy', cross_entropy)

with tf.name_scope('train'):
    train_step = tf.train.AdamOptimizer(0.002).minimize(cross_entropy)    

with tf.name_scope('accuracy'):
    with tf.name_scope('correct_prediction'):
        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)

merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter('/tmp/mnist_logs/train',sess.graph)
test_writer = tf.summary.FileWriter('/tmp/mnist_logs/test')

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(10000):
        if i % 10 == 0:  # 记录测试集的统计数据
            summary, acc = sess.run(
                fetches=[merged, accuracy],
                feed_dict={x: mnist.test.images, y_: mnist.test.labels})
            test_writer.add_summary(summary, i)
            print('Accuracy at step %s: %s' % (i, acc))
        else:  # 记录训练集的统计数据
            summary, _ = sess.run(
                fetches=[merged, train_step],
                feed_dict={x: mnist.train.images, y_: mnist.train.labels})
            train_writer.add_summary(summary, i)
train_writer.close()
test_writer.close()

这段代码会在卷积神经网络(CNN)训练中每一次迭代时输出结果。结果会发现一个网页上,我们最后要使用TensorBoard打开这个网页。在TensorBoard中,可以看到关于我们CNN的所有指标。

输出图像如下:

example_1.png

这里我们可以看到一个图表界面,显示了训练了多少?如果出现了任意奇怪的行为,会发现哪一个变量发生了什么。在这个特例中,图表展示了这个CNN的weights的变化。这是一个关键使用指标,在继续训练和测试之前,把我们的观察结果和假设做一个对比。

example_2.png

最后,我们发现指标结果还有很多分析可以做,但是这将会是我们另一个指标分析TensorBoard的教程。现在我们已经能够构建可供工程师和科学家使用的神经网络,并且了解了如何通过TensorBoard将指标导入仪器。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解TensorFlow的 tf.train.SummaryWriter 函数:保存 TensorBoard 可视化数据 - Python技术站

(0)
上一篇 2023年3月23日
下一篇 2023年3月30日

相关文章

  • 详解TensorFlow的 tf.image.resize_images 函数:改变图像的尺寸

    TensorFlow中tf.image.resize_images函数的作用与使用方法 1. 函数作用 tf.image.resize_images函数是TensorFlow的图像处理函数之一,主要用于对图像进行缩放处理。具体来说,该函数用于调整图像的大小(即缩放),可以根据输入的目标大小对图像进行放缩处理。此外,tf.image.resize_images…

    tensorflow-function 2023年4月4日
    00
  • 详解TensorFlow的 tf.get_collection 函数:获取指定名称的集合

    TensorFlow的tf.get_collection函数介绍 TensorFlow中的tf.get_collection用于根据集合名称获取相关的全部变量引用列表。 集合(collection)是TensorFlow中的一种管理与使用变量的方式,它类似于一个键值对,其中键表示变量的作用(比如保存模型的变量、计算损失函数的变量等),值则是保存相关变量的列表…

    tensorflow-function 2023年4月4日
    00
  • 详解TensorFlow的 tf.train.AdamOptimizer.minimize 函数:最小化损失函数

    TensorFlow的tf.train.AdamOptimizer.minimize函数 tf.train.AdamOptimizer.minimize是TensorFlow里面的一个优化器,它可以用来训练神经网络模型。 该函数的作用是通过对模型的损失函数进行优化,不断调整模型中的参数使得模型对训练数据的拟合能力更强,提高模型的泛化能力,从而提高模型的准确率…

    tensorflow-function 2023年4月4日
    00
  • 详解TensorFlow的 tf.nn.softmax 函数:softmax 激活函数

    什么是softmax函数 在机器学习的过程中,很多训练算法都是基于概率论的基础理论进行的。softmax函数是一种用于归一化多维向量的函数,通常作为神经网络的输出层的激活函数,可以将任意实数值向量转换为概率分布。 softmax函数的数学定义 对于给定的一个包含m个元素的向量 $\boldsymbol{z}=(z_1,z_2,…,z_m)$,softma…

    tensorflow-function 2023年3月23日
    00
  • 详解TensorFlow的 tf.losses.cosine_distance 函数:余弦距离损失函数

    TensorFlow的 tf.losses.cosine_distance 函数介绍 tf.losses.cosine_distance 是 TensorFlow 中用于计算余弦距离损失函数的函数。余弦距离是两个向量之间的夹角的余弦值,因此可用于计算向量之间的相似度。 函数签名如下: tf.losses.cosine_distance(labels, pre…

    tensorflow-function 2023年3月30日
    00
  • 详解TensorFlow的 tf.nn.rnn_cell.BasicRNNCell 函数:基本 RNN 单元

    TensorFlow中tf.nn.rnn_cell.BasicRNNCell函数的作用与使用方法 作用 tf.nn.rnn_cell.BasicRNNCell函数是根据来自前一时间步的输入和当前时间步的状态(输出)计算隐藏状态和输出的RNN基本单元。 使用方法 函数原型 tf.nn.rnn_cell.BasicRNNCell(num_units, activ…

    tensorflow-function 2023年3月23日
    00
  • 详解TensorFlow的 tf.Variable 函数:创建一个可训练的变量张量

    tf.Variable 是 TensorFlow 中创建和管理变量的主要方法。通过 tf.Variable 创建的变量是可训练的,并且可以在训练过程中更新它们的值。本文将介绍 tf.Variable 的作用、使用方法以及几个实例说明。 作用 在 TensorFlow 中,变量是一种特殊的张量,可用于存储模型中的可训练参数。与普通张量不同,变量存在于 Tens…

    tensorflow-function 2023年3月23日
    00
  • 详解TensorFlow的 tf.placeholder 函数:创建一个占位符张量

    在 TensorFlow 中,tf.placeholder() 函数的作用是声明一个占位符(placeholder),用于后面填充数据。就是我们在定义模型时,还不确定所需要的数据,就可以先通过占位符表示,最后再动态赋值。占位符本身不存储数值,其主要作用在于接收后面传递的数据,并协助程序构建计算图的形状。 语法格式 tf.placeholder(dtype, …

    tensorflow-function 2023年3月23日
    00
合作推广
合作推广
分享本页
返回顶部