Python 3.x tensorflow-1.x的google colab中的Tensorboard

Python 3.x tensorflow-1.x的google colab中的Tensorboard,python-3.x,tensorflow,google-colaboratory,tensorboard,Python 3.x,Tensorflow,Google Colaboratory,Tensorboard,在使用tensorflow-1.x时,有没有办法在google collab中运行tensorboard?如果没有,如何在tensorflow-1.x中使用tensorboard 我希望能发布一个有用的例子。是的,这是可能的。下面是在GoogleColab中使用Tensorboard可视化直方图的完整工作代码 %tensorflow_version 1.x %load_ext tensorboard import tensorflow as tf print(tf.__version__) i

在使用tensorflow-1.x时,有没有办法在google collab中运行tensorboard?如果没有,如何在tensorflow-1.x中使用tensorboard


我希望能发布一个有用的例子。

是的,这是可能的。下面是在GoogleColab中使用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本身——它只是在培训后没有显示出来。但是,如果我运行“%tensorboard--logdir logs”,我会看到一条消息:“在端口6006(pid 139)上重用tensorboard,从0:11:31开始。(使用“!kill 139”将其杀死。)。这意味着张力板正在运行,但没有出现。你对我应该如何解决这个问题有什么建议吗?@user48115,简单的技巧,请在Colab的下一个单元格中执行
%tensorboard--logdir logs
。不要将此部分包含在执行的第一个单元格中。请让我知道,你们仍然面临这个问题吗?