#@title Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Explanation: Copyright 2019 The TensorFlow Authors.
End of explanation
"""


try:
  # %tensorflow_version 仅存在于 Colab。
  %tensorflow_version 2.x
except Exception:
  pass

!pip install -U tensorboard >piplog 2>&1

import tensorflow as tf
import datetime

"""
Explanation: 开始使用 TensorBoard.dev
<table class="tfo-notebook-buttons" align="left">
  <td>
    <a target="_blank" href="https://tensorflow.google.cn/tensorboard/tbdev_getting_started">
    <img src="https://tensorflow.google.cn/images/tf_logo_32px.png" />
    在 tensorFlow.google.cn 上查看</a>
  </td>
  <td>
    <a target="_blank" href="https://colab.research.google.com/github/tensorflow/docs-l10n/blob/master/site/zh-cn/tensorboard/tbdev_getting_started.ipynb">
    <img src="https://tensorflow.google.cn/images/colab_logo_32px.png" />
    在 Google Colab 中运行</a>
  </td>
  <td>
    <a target="_blank" href="https://github.com/tensorflow/docs-l10n/blob/master/site/zh-cn/tensorboard/tbdev_getting_started.ipynb">
    <img src="https://tensorflow.google.cn/images/GitHub-Mark-32px.png" />
    在 GitHub 上查看源代码</a>
  </td>
  <td>
    <a href="https://storage.googleapis.com/tensorflow_docs/docs-l10n/site/zh-cn/tensorboard/tbdev_getting_started.ipynb"><img src="https://tensorflow.google.cn/images/download_logo_32px.png" />下载 notebook</a>
  </td>
</table>

TensorBoard.dev 提供了云端的 TensorBoard 体验，可让您上传并与所有人共享 ML 实验结果。
该笔记本训练了一个简单的模型，并演示了如何将日志上传到 TensorBoard.dev。
配置并导入
End of explanation
"""


mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = 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')
  ])

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

log_dir="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)

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

"""
Explanation: 训练一个简单的模型并创建 TensorBoard 日志
End of explanation
"""


!tensorboard dev upload --logdir ./logs

"""
Explanation: 上传到 TensorBoard.dev
上传 TensorBoard 日志将提供一个可以与任何人共享的链接。 请注意，上传的 TensorBoard 是公开的。 不要上传敏感数据。
上传程序将一直运行直到停止，以便在进行训练时从目录中读取新数据。
End of explanation
"""


!tensorboard dev list

"""
Explanation: 请注意，您上传的每个实例都有一个唯一的实例 ID。 即使您使用相同的目录重新上传，您也会获得新的实例 ID。 可以使用 list 命令查看您上传的实例列表。
End of explanation
"""


# 您必须将 YOUR_EXPERIMENT_ID 替换为前一个输出的值
# tensorboard `list` 命令或 `upload` 命令。 例如
# `tensorboard dev delete --experiment_id pQpJNh00RG2Lf1zOe9BrQA`

## !tensorboard dev delete --experiment_id YOUR_EXPERIMENT_ID_HERE

"""
Explanation: 要删除已上传的实例，可以使用 delete 命令并指定适当的实例。
End of explanation
"""


