TensorBoard可视化实例(MNIST)

TensorBoard可视化

利用TensorBoard对MNIST数据集在训练和测试时进行数据监听,将loss与预测值以图的形式显示出来。
首先在cmd进入你的tensorflow环境,通过目录进入你要监听的.py文件的主目录下,我的.py文件实在logs目录下,然后输入下面的代码开启TensorBoard,然后再网页输入下面的网址进入TensorBoard页面。运行py文件就可以进行数据的监听了。
在这里插入图片描述
监听画面如下:
在这里插入图片描述
在这里插入图片描述

调用plo_to_image接口,将数据集中的图像转换成png类型。

def plo_to_image(figure):
    """Converts the matplotlib plot specified by 'figure' to a PNG image and
    return it. The supplied figure is closed and inaccessible after this call."""
    #Save the plot to a PNG in memory.
    buf = io.BytesIO()
    plt.savefig(buf, format='png')
    #Closing the figure prevents it from being displayed directly inside the notebook
    plt.close(figure)
    buf.seek(0)
    image = tf.image.decode_png(buf.getvalue(), channels=4)
    image = tf.expand_dims(image, 0)
    return image

调用image_grid接口,将数据集中的图片以25张图片合并在一张图片上。

def image_grid(images):
    """返回5 * 5的MNIST图像网格作为matplotlib图."""
    #创建一个包含图的图形.
    figure =plt.figure(figsize=(10, 10))
    for i in range(25):
        #开始下一个子图
        plt.subplot(5, 5, i+1, title='name')
        plt.xticks([])
        plt.yticks([])
        plt.grid(False)
        plt.imshow(images[i], cmap=plt.cm.binary)
    return figure

创建监听对象路径

#创建监听对象路径
current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
log_dir = 'logs/' + current_time
summary_writer  = tf.summary.create_file_writer(log_dir)

将数据和图片传入TensorBoard中进行监听

        with summary_writer.as_default():
            #一张一张的显示
            tf.summary.scalar('test-acc', float(total_correct/total), step=step)
            tf.summary.image("test-onebyone-images:", test_images, max_outputs=25, step=step)

            #将25张图片合成一张显示
            test_images = tf.reshape(test_images,[-1, 28, 28])
            figure = image_grid(test_images)
            tf.summary.image('test-images:',plo_to_image(figure), step=step)

完整代码

# -*- coding: utf-8 -*-
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets,layers,optimizers,Sequential,metrics
from  matplotlib import pyplot as plt
import io
import datetime


def preprocess(x,y):#数据预处理
    x = tf.cast(x, dtype=tf.float32)/255.
    y = tf.cast(y, dtype=tf.int32)
    return x,y

def plo_to_image(figure):
    """Converts the matplotlib plot specified by 'figure' to a PNG image and
    return it. The supplied figure is closed and inaccessible after this call."""
    #Save the plot to a PNG in memory.
    buf = io.BytesIO()
    plt.savefig(buf, format='png')
    #Closing the figure prevents it from being displayed directly inside
    #the notebook
    plt.close(figure)
    buf.seek(0)
    image = tf.image.decode_png(buf.getvalue(), channels=4)
    image = tf.expand_dims(image, 0)
    return image

def image_grid(images):
    """Return a 5*5 grid of the MNIST images as a matplotlib figure."""
    #Create a figure to contain the plot.
    figure =plt.figure(figsize=(10, 10))
    for i in range(25):
        #Start next subplot
        plt.subplot(5, 5, i+1, title='name')
        plt.xticks([])
        plt.yticks([])
        plt.grid(False)
        plt.imshow(images[i], cmap=plt.cm.binary)
    return figure


#加载数据集
(x, y), (x_test, y_test) = datasets.mnist.load_data()
print('datasets:', x.shape, y.shape)#(60000, 28, 28) (60000,)

batchsz = 128
db = tf.data.Dataset.from_tensor_slices((x,y))
db = db.map(preprocess).shuffle(60000).batch(batchsz).repeat(10)  #调用预处理函数,对每一个x,y进行处理,打散数据,截断

db_test = tf.data.Dataset.from_tensor_slices((x_test,y_test))
db_test = db_test.map(preprocess).batch(batchsz, drop_remainder=True)



#Squential是个容器,将创建的list传给Sequential,它就是一个网络层
network = Sequential([
        layers.Dense(256, activation = tf.nn.relu), #[b,784]==>[b,256]  全连接层,线性激活函数
        layers.Dense(128, activation = tf.nn.relu), #[b,256]==>[b,128]
        layers.Dense(64, activation = tf.nn.relu),  #[b,128]==>[b, 64]
        layers.Dense(32, activation = tf.nn.relu),  #[b, 64]==>[b, 32]
        layers.Dense(10)                            #[b, 32]==>[b, 10]
        ])

network.build(input_shape=[None, 28*28])#喂一个输入,构建一个权值
network.summary()#调试,可以将网络结构打印出来


#优化器 : w = w - lr*grad
optimizer = optimizers.Adam(lr = 0.01)

#创建监听对象路径
current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
log_dir = 'logs/' + current_time
summary_writer  = tf.summary.create_file_writer(log_dir)

#get x from (x, y)
smaple_img = next(iter(db))[0]

#get first image instance
smaple_img = smaple_img[0]
smaple_img = tf.reshape(smaple_img, [1, 28, 28, 1])
with summary_writer.as_default():
    tf.summary.image("Training sample:", smaple_img, step=0)


for step,(x,y) in enumerate(db):

    #求梯度
    with tf.GradientTape() as tape:
        # x:[b,28,28] ==> [b,784]
        # y:[b]
        x = tf.reshape(x, [-1, 28 * 28])
        #[b,784] ==> [b,10]
        out = network(x)#调用网络层,得到没有归一化的输出值logits
        y_onehot = tf.one_hot(y, depth = 10)  #将y转成one_hot

        loss = tf.losses.categorical_crossentropy(y_onehot, out, from_logits=True)   #交叉熵损失函数
        loss = tf.reduce_mean(loss)

    grads = tape.gradient(loss, network.trainable_variables)  #model.trainable_variables返回【w,b】参数列表
    optimizer.apply_gradients(zip(grads, network.trainable_variables))  #原地更新参数
            
            
    if step%100 == 0:
        print(step,'loss:',float(loss))
        with summary_writer.as_default():
            tf.summary.scalar('train-loss', float(loss), step=step)

    #evaluate
    if step % 500 ==0:
        total, total_correct = 0, 0

        for _,(x,y) in enumerate(db_test):
            # x:[b,28,28] ==> [b,784]
            # y:[b]
            x = tf.reshape(x, [-1, 28 * 28])

            # [b,784] ==> [b,10]
            out = network(x)  # 调用网络层,使用当前状态的【w,b】,得出测试图片的输出值
            #[b,10] ==> [b]  pred: int64 ==> int32
            pred = tf.argmax(out, axis=1)  #获取prob的最大得分的索引位置【0-9】
            pred = tf.cast(pred, dtype=tf.int32)

            #pred:[b]
            #y:[b] 存储的时数字的值【0-9】
            correct = tf.equal(pred,y)   #比较pred与y,相等返回True,不等返回False
            # bool tensor => int tensor => numpy
            correct = tf.reduce_sum(tf.cast(correct, dtype=tf.int32))  # 将bool型转成int,求出b张图片中正确的个数

            total_correct += int(correct)  # 求出测试数据集中总的正确个数
            total += x.shape[0]    #计算测试集的图片个数

        print(step, 'Test Acc:', total_correct/total)

        #print(x.shape)
        test_images = x[:25]
        test_images = tf.reshape(test_images,[-1, 28, 28, 1])
        with summary_writer.as_default():
            #一张一张的显示
            tf.summary.scalar('test-acc', float(total_correct/total), step=step)
            tf.summary.image("test-onebyone-images:", test_images, max_outputs=25, step=step)

            #将25张图片合成一张显示
            test_images = tf.reshape(test_images,[-1, 28, 28])
            figure = image_grid(test_images)
            tf.summary.image('test-images:',plo_to_image(figure), step=step)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
TensorBoard是TensorFlow的一个可视化工具,可以帮助我们更好地理解和调试TensorFlow模型。 下面是一个简单的TensorBoard可视化实例: 1.导入必要的库和数据集 ```python import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) ``` 2.定义一个简单的三层神经网络模型 ```python 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") with tf.name_scope("layer"): with tf.name_scope("weights"): W = tf.Variable(tf.zeros([784, 10]), name="W") with tf.name_scope("biases"): b = tf.Variable(tf.zeros([10]), name="b") with tf.name_scope("softmax"): y_pred = tf.nn.softmax(tf.matmul(x, W) + b) ``` 3.定义交叉熵损失函数和优化器 ```python with tf.name_scope("loss"): cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(y_pred), reduction_indices=[1])) tf.summary.scalar("loss", cross_entropy) with tf.name_scope("train"): train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) ``` 4.定义准确率计算 ```python with tf.name_scope("accuracy"): correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) tf.summary.scalar("accuracy", accuracy) ``` 5.合并所有的summary ```python merged_summary = tf.summary.merge_all() ``` 6.定义Session和FileWriter来保存summary ```python with tf.Session() as sess: writer = tf.summary.FileWriter("logs/", sess.graph) sess.run(tf.global_variables_initializer()) for i in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) _, summary = sess.run([train_step, merged_summary], feed_dict={x: batch_xs, y: batch_ys}) writer.add_summary(summary, i) print(sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels})) ``` 7.在终端中运行以下命令以启动TensorBoard ```bash tensorboard --logdir=logs/ ``` 8.在浏览器中打开http://localhost:6006/,即可看到TensorBoard可视化结果。 这个例子中,我们使用TensorBoard可视化模型的损失函数和准确率的变化,以及模型结构的图形化展示。通过TensorBoard可视化,我们可以更加直观地了解模型的训练过程和性能表现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值