TensorBoard cheatsheet
step 1 构建模型
import tensorflow as tf
import matplotlib.pyplot as plt
import os
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images = train_images / 255.0
test_images = test_images / 255.0
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
step 2 设置log的根目录,以及每次模型保存的目录
root_logdir = './my_log'
def get_run_logdir():
import time
run_id = time.strftime('run_%Y_%m_%d-%H_%M_%S')
return os.path.join(root_logdir, run_id)
step 3 构建Callback
run_logdir = get_run_logdir()
tensorboard_cb = tf.keras.callbacks.TensorBoard(run_logdir)
step 4训练
history = model.fit(train_images, train_labels, epochs=10, validation_split=0.3, callbacks=[tensorboard_cb])
最后,在对应runid的目录启动tensorboard
tensorboard --logdir=./ --port 6006