Colab 中的 Tensorboard:当前数据集没有活动的仪表板

Tensorboard in Colab: No dashboards are active for the current data set

我正在尝试在 Google Colab 中显示 Tensorboard。我导入张量板:%load_ext tensorboard,然后创建一个log_dir,并按如下方式拟合:

log_dir = '/gdrive/My Drive/project/' + "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)

history = model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size,
    callbacks=[tensorboard_callback])

但是当我用%tensorboard --logdir logs/fit调用它时它不显示。相反,它会抛出以下消息:

No dashboards are active for the current data set.

有解决办法吗?问题出在我传入的固定路径log_dir?

请尝试以下代码

log_dir = '/gdrive/My Drive/project/' + "logs/fit/"
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)

history = model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size,
    callbacks=[tensorboard_callback])

    %load_ext tensorboard
    %tensorboard --logdir /gdrive/My Drive/project/logs/fit/

也许您在某种程度上弄乱了路径。如果您使用的是 tensorflow 2.0+ 版本,请尝试使用此解决方案

## setup 
# Load the TensorBoard notebook extension.
%load_ext tensorboard

导入必要的包

from datetime import datetime
from packaging import version

import tensorflow as tf
from tensorflow import keras

import numpy as np

print("TensorFlow version: ", tf.__version__)
assert version.parse(tf.__version__).release[0] >= 2, \
"This notebook requires TensorFlow 2.0 or above."

您需要在 model.fit() 的回调参数中提供 tensorboard_callbacks 它会出现这样的内容 --

# define path to save log files
logdir = "logs/fit/" + datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir, histogram_frequency=1, write_graph=True)

# define & compile your model; here i am moving forward with assumption that you've already defined and compiled your model
model = keras.models.Sequential([
    keras.layers.Dense(16, input_dim=1),
    keras.layers.Dense(1),
    ])

model.compile(
    loss='mse', # keras.losses.mean_squared_error
    optimizer=keras.optimizers.SGD(lr=0.2),
    )

# watch closely the argument passed in 'callbacks'
model.fit(x=x_train, 
      y=y_train, 
      epochs=10, 
      validation_data=(x_test, y_test), 
      callbacks=[tensorboard_callback]))

这会将您的日志文件保存在您的 google colab 笔记本中分配的内存中。

查看 TensorBoard 结果 --

%tensorboard --logdir logs/fit/

结果应该是这样的---

更多资源

  • https://www.tensorflow.org/tensorboard/scalars_and_keras