此页面由 Cloud Translation API 翻译。
Switch to English

在笔记本电脑使用TensorBoard

查看上TensorFlow.org 在谷歌Colab运行 GitHub上查看源代码

TensorBoard可以直接体验的笔记本电脑,如内使用ColabJupyter 。这对于共享成果,整合TensorBoard到现有的工作流程,并采用TensorBoard无需在本地安装任何帮助。

建立

通过安装TF 2.0和加载TensorBoard笔记本扩展开始:

对于Jupyter用户:如果您已经安装Jupyter和TensorBoard到相同的virtualenv,那么你应该去的好。如果您使用的是更复杂的设置,如全球Jupyter安装和内核的不同康达/ virtualenv中的环境中,则必须确保tensorboard二进制是在你的PATH的Jupyter笔记本上下文中。要做到这一点的方法之一是修改kernel_spec预先考虑环境的bin目录PATH如下描述

如果你运行的是一个码头工人的形象使用TensorFlow的夜间Jupyter笔记本电脑服务器 ,有必要揭露不仅是笔记本电脑的端口,但TensorBoard的端口。

因此,运行使用下面的命令的容器:

 docker run -it -p 8888:8888 -p 6006:6006 \
tensorflow/tensorflow:nightly-py3-jupyter 
 

其中-p 6006是TensorBoard的默认端口。这将分配一个端口,您可以运行一个TensorBoard实例。有并发的情况下,需要分配更多的端口。

 # Load the TensorBoard notebook extension
%load_ext tensorboard
 

进口TensorFlow,datetime和操作系统:

 import tensorflow as tf
import datetime, os
 

TensorBoard在笔记本电脑

下载FashionMNIST数据集中和规模:

 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
 
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 [==============================] - 0s 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

创建一个非常简单的模型:

 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')
  ])
 

列车采用Keras和TensorBoard回调模型:

 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()
 
Train on 60000 samples, validate on 10000 samples
Epoch 1/5
60000/60000 [==============================] - 11s 182us/sample - loss: 0.4976 - accuracy: 0.8204 - val_loss: 0.4143 - val_accuracy: 0.8538
Epoch 2/5
60000/60000 [==============================] - 10s 174us/sample - loss: 0.3845 - accuracy: 0.8588 - val_loss: 0.3855 - val_accuracy: 0.8626
Epoch 3/5
60000/60000 [==============================] - 10s 175us/sample - loss: 0.3513 - accuracy: 0.8705 - val_loss: 0.3740 - val_accuracy: 0.8607
Epoch 4/5
60000/60000 [==============================] - 11s 177us/sample - loss: 0.3287 - accuracy: 0.8793 - val_loss: 0.3596 - val_accuracy: 0.8719
Epoch 5/5
60000/60000 [==============================] - 11s 178us/sample - loss: 0.3153 - accuracy: 0.8825 - val_loss: 0.3360 - val_accuracy: 0.8782

使用笔记本电脑内启动TensorBoard 魔法

 %tensorboard --logdir logs
 

现在,您可以查看仪表盘,如标量,图表,柱状图,等等。一些仪表盘目前还无法在Colab(如配置文件的插件)。

所述%tensorboard魔法具有完全相同的格式与TensorBoard命令行调用,而是用%在它前面-sign。

您也可以训练,监视它正在进行启动前TensorBoard:

 %tensorboard --logdir logs
 

相同TensorBoard后端通过发出同一命令重复使用。如果选择一个不同的日志目录,TensorBoard的新实例将被打开。端口自动管理。

开始训练的新模式和观看TensorBoard更新每30秒自动或右上角的按钮刷新:

 train_model()
 
Train on 60000 samples, validate on 10000 samples
Epoch 1/5
60000/60000 [==============================] - 11s 184us/sample - loss: 0.4968 - accuracy: 0.8223 - val_loss: 0.4216 - val_accuracy: 0.8481
Epoch 2/5
60000/60000 [==============================] - 11s 176us/sample - loss: 0.3847 - accuracy: 0.8587 - val_loss: 0.4056 - val_accuracy: 0.8545
Epoch 3/5
60000/60000 [==============================] - 11s 176us/sample - loss: 0.3495 - accuracy: 0.8727 - val_loss: 0.3600 - val_accuracy: 0.8700
Epoch 4/5
60000/60000 [==============================] - 11s 179us/sample - loss: 0.3282 - accuracy: 0.8795 - val_loss: 0.3636 - val_accuracy: 0.8694
Epoch 5/5
60000/60000 [==============================] - 11s 176us/sample - loss: 0.3115 - accuracy: 0.8839 - val_loss: 0.3438 - val_accuracy: 0.8764

您可以使用tensorboard.notebook的多一点控制的API:

 from tensorboard import notebook
notebook.list() # View open TensorBoard instances
 
Known TensorBoard instances:

  - port 6006: logdir logs (started 0:00:54 ago; pid 265)

 # Control TensorBoard display. If no port is provided, 
# the most recently launched TensorBoard is used
notebook.display(port=6006, height=1000)