谷歌 colab 中用于 tensorflow-1.x 的 Tensorboard

Posted

技术标签:

【中文标题】谷歌 colab 中用于 tensorflow-1.x 的 Tensorboard【英文标题】:Tensorboard in google colab for tensorflow-1.x 【发布时间】:2020-11-10 17:09:13 【问题描述】:

在使用 tensorflow-1.x 时,有什么方法可以在 google collab 中运行 tensorboard?如果没有,如何在 tensorflow-1.x 中使用 tensorboard?

如果能发布任何工作示例,我将不胜感激。

【问题讨论】:

here 中的答案可能对您有所帮助。 【参考方案1】:

是的,这是可能的。这是在 Google Colab 中使用 Tensorboard 可视化直方图的完整工作代码。

%tensorflow_version 1.x
%load_ext tensorboard

import tensorflow as tf
print(tf.__version__)
import datetime, os

fashion_mnist = tf.keras.datasets.fashion_mnist

(x_train, y_train),(x_test, y_test) = fashion_mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

def create_model():
  return tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation='softmax')
  ])

def train_model():

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

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

  model.fit(x=x_train, 
            y=y_train, 
            epochs=5, 
            validation_data=(x_test, y_test), 
            callbacks=[tensorboard_callback])

train_model()

%tensorboard --logdir logs

输出:

TensorFlow 1.x selected.
1.15.2
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 1s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
8192/5148 [===============================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 0s 0us/step
WARNING:tensorflow:From /tensorflow-1.15.2/python3.6/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
Train on 60000 samples, validate on 10000 samples
Epoch 1/5
60000/60000 [==============================] - 15s 250us/sample - loss: 0.4987 - acc: 0.8206 - val_loss: 0.4289 - val_acc: 0.8476
Epoch 2/5
60000/60000 [==============================] - 15s 253us/sample - loss: 0.3847 - acc: 0.8592 - val_loss: 0.3928 - val_acc: 0.8600
Epoch 3/5
60000/60000 [==============================] - 15s 246us/sample - loss: 0.3463 - acc: 0.8730 - val_loss: 0.3713 - val_acc: 0.8660
Epoch 4/5
60000/60000 [==============================] - 15s 246us/sample - loss: 0.3292 - acc: 0.8786 - val_loss: 0.3523 - val_acc: 0.8697
Epoch 5/5
60000/60000 [==============================] - 15s 249us/sample - loss: 0.3100 - acc: 0.8848 - val_loss: 0.3455 - val_acc: 0.8757

【讨论】:

我复制粘贴并在 google collab 中运行您的代码,但不知何故我没有看到 tensorboard 本身 --- 它只是在训练后没有出现。 Howhewer,如果我运行“%tensorboard --logdir logs”,我会看到一条消息:“在端口 6006 (pid 139) 上重用 TensorBoard,0:11:31 前开始。 (使用 '!kill 139' 杀死它。)'。这意味着张量板正在运行,但只是没有出现。你对我应该做些什么来解决这个问题有什么建议吗? @user48115,简单的技巧,请在 Colab 的下一个单元格中执行 %tensorboard --logdir logs。不要将此部分包含在执行的第一个单元格中。请让我知道,您仍然面临问题吗?

以上是关于谷歌 colab 中用于 tensorflow-1.x 的 Tensorboard的主要内容,如果未能解决你的问题,请参考以下文章

如何在谷歌colab中使用TPU

如何在 colab 中使用 R 运行时更改 tensorflow 版本?

TensorFlow 1 中的 TensorBoard 使用 Google Colab

将本地文件导入谷歌 colab

谷歌colab中的包

如何使用 colab 在谷歌驱动器上保存 np.array?