Tensorboard简介

Tensorboard是一款为了更方便TensorFlow程序的理解、调试与优化发布的可视化工具。你可以用Tensorboard来展现你的Tensorflow图像,绘制图像生成的定量指标图以及附加数据。

Tensorboard通过读取TensorFlow的事件文件来运行。TensorFlow的事件文件又包括了你会在TensorFlow运行中涉及到的主要数据。

Tensorboard随着TensorFlow的安装一并被安装好了,不用单独去安装。

1、通过tf.keras回调函数使用Tensorboard

在Keras的fit()里面是用callbacks保存下训练过程的参数,后面可以可视化查看。

# -*- coding: UTF-8 -*-
"""
Author: LGD
FileName: test_tensorboard
DateTime: 2020/12/13 21:35
SoftWare: PyCharm
"""
import tensorflow as tf
import datetime

# 数据集预处理阶段
(train_image, train_labels), (test_image, test_labels) = tf.keras.datasets.mnist.load_data()

train_image = tf.expand_dims(train_image, -1)
test_image = tf.expand_dims(test_image, -1)

train_image = tf.cast(train_image/255, tf.float32)
test_image = tf.cast(test_image/255, tf.float32)

train_labels = tf.cast(train_labels, tf.int64)
test_labels = tf.cast(test_labels, tf.int64)

train_dataset = tf.data.Dataset.from_tensor_slices((train_image, train_labels))
test_dataset = tf.data.Dataset.from_tensor_slices((test_image, test_labels))

print(train_dataset, test_dataset)

# 对数据集乱序、重复、划分批次, 使用model.fit方法时,需要repeat;使用自定义循环时,不用调用repeat或者参数为1
train_dataset = train_dataset.repeat().shuffle(60000).batch(128)
test_dataset = test_dataset.repeat().batch(128)

# 模型建立阶段
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(None, None, 1)),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.GlobalMaxPooling2D(),
tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['acc']
)

# Tensorboard可视化部分
import os
log_dir = os.path.join('logs', datetime.datetime.now().strftime('%Y%m%d-%H%M%S'))
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir, histogram_freq=1)

model.fit(
train_dataset,
epochs=5,
steps_per_epoch=60000//128,
validation_data=test_dataset,
validation_steps=10000//128,
callbacks=[tensorboard_callback]
)

2、tensorboard界面

在jupyter notebook里面可以直接查看

%load_ex tensorboard

%tensorboard --logdir logs # logs表示保存的目录

在浏览器中进行查看

到logs所在目录,使用cmd进入dos窗口下。输入下面命令,会显示出一条链接,复制链接到浏览器即可查看。

Tensorboard可视化_python

Tensorboard可视化界面:

Tensorboard可视化_深度学习_02

3、在tf.keras回调函数中记录自定义变量

重新调整回归模型并记录自定义学习率。在训练过程中特别是快接近极值点的时候使学习率变小,以至于不错过极值点,使训练收敛。

做法思路如下:使用创建文件编写器tf.summary.create_file_writer()。定义自定义学习率功能。这将传递给Keras learningRateSchedule回调。在学习率功能内,用于tf.summary.scalar()记录自定义学习率。将LearningRateSchedule回调传递给Model.fit()。通常要记录自定义标量,都要使用tf.summary.scalar()文件编写器。文件编写器负责将此运行的数据写入指定的目录,并在使用时隐式使用tf.summary.scalar()。

# -*- coding: UTF-8 -*-
"""
Author: LGD
FileName: test_tensorboard
DateTime: 2020/12/13 21:35
SoftWare: PyCharm
"""
import tensorflow as tf
import datetime

# 数据集预处理阶段
(train_image, train_labels), (test_image, test_labels) = tf.keras.datasets.mnist.load_data()

train_image = tf.expand_dims(train_image, -1)
test_image = tf.expand_dims(test_image, -1)

train_image = tf.cast(train_image / 255, tf.float32)
test_image = tf.cast(test_image / 255, tf.float32)

train_labels = tf.cast(train_labels, tf.int64)
test_labels = tf.cast(test_labels, tf.int64)

train_dataset = tf.data.Dataset.from_tensor_slices((train_image, train_labels))
test_dataset = tf.data.Dataset.from_tensor_slices((test_image, test_labels))

print(train_dataset, test_dataset)

# 对数据集乱序、重复、划分批次, 使用model.fit方法时,需要repeat;使用自定义循环时,不用调用repeat或者参数为1
train_dataset = train_dataset.repeat().shuffle(60000).batch(128)
test_dataset = test_dataset.repeat().batch(128)

# 模型建立阶段
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(None, None, 1)),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.GlobalMaxPooling2D(),
tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['acc']
)

# Tensorboard可视化部分
import os

log_dir = os.path.join('logs', datetime.datetime.now().strftime('%Y%m%d-%H%M%S'))
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir, histogram_freq=1)

# 创建文件编写器
file_writer = tf.summary.create_file_writer(log_dir + '/lr')
file_writer.set_as_default()


# 创建学习率变化函数
def lr_sche(epoch):
learning_rate = 0.2
if epoch > 5:
learning_rate = 0.02
if epoch > 10:
learning_rate = 0.01
if epoch > 20:
learning_rate = 0.005

tf.summary.scalar('learning_rate', data=learning_rate, step=epoch)
return learning_rate


# 建立学习率回调函数
lr_callback = tf.keras.callbacks.LearningRateScheduler(lr_sche)


model.fit(
train_dataset,
epochs=25,
steps_per_epoch=60000 // 128,
validation_data=test_dataset,
validation_steps=10000 // 128,
callbacks=[tensorboard_callback, lr_callback]
)

4、在自定义循环中使用Tensorboard

还是以MNIST数据集为例,使用自定义网络。

# -*- coding: UTF-8 -*-
"""
Author: LGD
FileName: tensorboard
DateTime: 2020/12/14 16:18
SoftWare: PyCharm
"""
import tensorflow as tf
import datetime

print(tf.__version__)
# 数据集预处理阶段
(train_image, train_labels), (test_image, test_labels) = tf.keras.datasets.mnist.load_data()

train_image = tf.expand_dims(train_image, -1)
test_image = tf.expand_dims(test_image, -1)

train_image = tf.cast(train_image / 255, tf.float32)
test_image = tf.cast(test_image / 255, tf.float32)

train_labels = tf.cast(train_labels, tf.int64)
test_labels = tf.cast(test_labels, tf.int64)

train_dataset = tf.data.Dataset.from_tensor_slices((train_image, train_labels))
test_dataset = tf.data.Dataset.from_tensor_slices((test_image, test_labels))

print(train_dataset, test_dataset)

# 对数据集乱序、重复、划分批次, 使用model.fit方法时,需要repeat;使用自定义循环时,不用调用repeat或者参数为1
train_dataset = train_dataset.repeat().shuffle(60000).batch(128)
test_dataset = test_dataset.repeat().batch(128)

# 模型建立阶段
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(None, None, 1)),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.GlobalMaxPooling2D(),
tf.keras.layers.Dense(10, activation='softmax')
])


# 自定义模型部分
optimizer = tf.keras.optimizers.Adam()
loss_func = tf.keras.losses.SparseCategoricalCrossentropy()


def loss(model, x, y):
y_ = model(x)
return loss_func(y, y_)


train_loss = tf.keras.metrics.Mean('train_loss')
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy('train_accuracy')

test_loss = tf.keras.metrics.Mean('test_loss')
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy('test_accuracy')


def train_step(model, images, labels):
with tf.GradientTape as t:
pred = model(images)
loss_step = loss_func(labels, pred)

grads = t.gradient(loss_step, model.trainable_variables)
optimizer.apply_gradients(zip(grads, model.trainable_variables))
train_loss(loss_step)
train_accuracy(labels, pred)


def test_step(model, images, labels):
pred = model(images)
loss_step = loss_func(labels, pred)
test_loss(loss_step)
test_accuracy(labels, pred)


current_time = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
train_log_dir = 'logs/gradient_tape' + current_time + 'train'
test_log_dir = 'logs/gradient_tape' + current_time + 'test'

train_writer = tf.summary.create_file_writer(train_log_dir)
test_writer = tf.summary.create_file_writer(test_log_dir)


def train():
for epoch in range(10):
for (batch, (images, labels)) in enumerate(train_dataset):
train_step(model, images, labels)

with train_writer.as_default():
tf.summary.scalar('loss', train_loss.result(), step=epoch) # 收集标量值
tf.summary.scalar('acc', train_accuracy.result(), step=epoch)

for (batch, (images, labels)) in enumerate(test_dataset):
test_step(model, images, labels)
print('*', end='')

with test_writer.as_default():
tf.summary.scalar('loss', test_loss.result(), step=epoch) # 收集标量值
tf.summary.scalar('acc', test_accuracy.result(), step=epoch)

# 输出格式
template = 'Epoch {}, Loss: {:.3f}, Accuracy: {:.4f}, Test Loss: {:.3f}, Test Accuracy: {:.4f}'
print(template.format(
epoch + 1,
train_loss.result(),
train_accuracy.result()*100,
test_loss.result(),
test_accuracy.result()*100
))

train_loss.reset_states()
train_accuracy.reset_states()
test_loss.reset_states()
test_accuracy.reset_states()


train()