{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "TsHV-7cpVkyK" }, "source": [ "##### Copyright 2019 The TensorFlow Authors.\n", "##### Modified and provided for the binder project by Bartosz Telenczuk, 2020" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "atWM-s8yVnfX" }, "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "9GtR_cTTkf9G" }, "source": [ "Import TensorFlow, datetime, and os:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "mVtYvbbIWRkV" }, "outputs": [], "source": [ "import tensorflow as tf\n", "import datetime, os" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Cu1fbH-S3oAX" }, "source": [ "## TensorBoard in notebooks" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "XfCa27_8kov6" }, "source": [ "Download the [FashionMNIST](https://github.com/zalandoresearch/fashion-mnist) dataset and scale it:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 153 }, "colab_type": "code", "id": "z8b82G7YksOS", "outputId": "69f33eef-0951-4426-d77b-f208bac3d843" }, "outputs": [], "source": [ "fashion_mnist = tf.keras.datasets.fashion_mnist\n", "\n", "(x_train, y_train),(x_test, y_test) = fashion_mnist.load_data()\n", "x_train, x_test = x_train / 255.0, x_test / 255.0" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "lBk1BqAZKEKd" }, "source": [ "Create a very simple model:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "OS7qGYiMKGQl" }, "outputs": [], "source": [ "def create_model():\n", " return tf.keras.models.Sequential([\n", " tf.keras.layers.Flatten(input_shape=(28, 28)),\n", " tf.keras.layers.Dense(512, activation='relu'),\n", " tf.keras.layers.Dropout(0.2),\n", " tf.keras.layers.Dense(10, activation='softmax')\n", " ])" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "RNaPPs5ZKNOV" }, "source": [ "Train the model using Keras and the TensorBoard callback:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 204 }, "colab_type": "code", "id": "lpUO9HqUKP6z", "outputId": "40acf9dc-919d-4dca-8f83-fd11ac53f45c" }, "outputs": [], "source": [ "def train_model():\n", " \n", " model = create_model()\n", " model.compile(optimizer='adam',\n", " loss='sparse_categorical_crossentropy',\n", " metrics=['accuracy'])\n", "\n", " logdir = os.path.join(\"logs\", datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\"))\n", " tensorboard_callback = tf.keras.callbacks.TensorBoard(\n", " logdir, histogram_freq=1, profile_batch=0)\n", "\n", " model.fit(x=x_train, \n", " y=y_train,\n", " epochs=5, \n", " validation_data=(x_test, y_test), \n", " callbacks=[tensorboard_callback])\n", "\n", "train_model()" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "aQq3UHgmLBpC" }, "source": [ "You can now go to the [tensorboard interface](../proxy/6006/) to watch dashboards such as scalars, graphs, histograms, and others." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "tensorboard_in_notebooks.ipynb", "provenance": [], "toc_visible": true, "version": "0.3.2" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }