In [1]:
# Load the TensorBoard notebook extension
%load_ext tensorboard
In [2]:
import os
import datetime
import tensorflow as tf
In [3]:
# Load the digit dataset
mnist = tf.keras.datasets.mnist

# Spliting data into train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
In [4]:
# Creating a model
def create_model():
    
    model = tf.keras.models.Sequential()
    
    # Flatten - Flattens the input. Does not affect the batch size.
    model.add(tf.keras.layers.Flatten(input_shape=(28, 28)))
    # Dense - Fully conenected layer ( Each Input Neuron is connected to the output Neuron)
    model.add(tf.keras.layers.Dense(512, activation='relu'))
    # Dropout is a technique used to improve over-fit on neural networks
    # Basically during training some of neurons on a particular layer will be deactivated.
    model.add(tf.keras.layers.Dropout(0.2))
    model.add(tf.keras.layers.Dense(10, activation='softmax'))
    
    return model
In [5]:
# Hyperparameters
training_epochs = 5 # Total number of training epochs
learning_rate = 0.001 # The learning rate
In [6]:
model = create_model()

# Configure model for training
model.compile(
    optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'])
In [7]:
# In order not to overwrite some data, place the logs in a timestamped subdirectory
# This allows to easy select different training runs
log_dir="logs\\fit\\" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")

if not os.path.exists(log_dir):
    os.makedirs(log_dir)
    
# Specify the callback object
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)

# tf.keras.callback.TensorBoard ensures that logs are created and stored 
# We need to pass callback object to the fit method
# The way to do this is by passing the list of callback objects, which is in our case just one

model.fit(x=x_train, 
          y=y_train, 
          epochs=5, 
          validation_data=(x_test, y_test), 
          callbacks=[tensorboard_callback])
Train on 60000 samples, validate on 10000 samples
Epoch 1/5
   32/60000 [..............................] - ETA: 46:23 - loss: 2.2678 - accuracy: 0.1250WARNING:tensorflow:Method (on_train_batch_end) is slow compared to the batch update (0.506257). Check your callbacks.
60000/60000 [==============================] - 23s 386us/sample - loss: 0.2212 - accuracy: 0.9351 - val_loss: 0.1072 - val_accuracy: 0.9670
Epoch 2/5
60000/60000 [==============================] - 27s 450us/sample - loss: 0.0971 - accuracy: 0.9702 - val_loss: 0.0867 - val_accuracy: 0.9733
Epoch 3/5
60000/60000 [==============================] - 19s 321us/sample - loss: 0.0691 - accuracy: 0.9784 - val_loss: 0.0706 - val_accuracy: 0.9798
Epoch 4/5
60000/60000 [==============================] - 22s 363us/sample - loss: 0.0526 - accuracy: 0.9832 - val_loss: 0.0625 - val_accuracy: 0.9811
Epoch 5/5
60000/60000 [==============================] - 20s 337us/sample - loss: 0.0437 - accuracy: 0.9859 - val_loss: 0.0687 - val_accuracy: 0.9808
Out[7]:
<tensorflow.python.keras.callbacks.History at 0x27ca215ed68>

Start TensorBoard with the line magic function. For command line, run the same command without "%"

In [8]:
%tensorboard --logdir=logs/fit