# Load the TensorBoard notebook extension
%load_ext tensorboard
import os
import datetime
import tensorflow as tf
# 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
# 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
# Hyperparameters
training_epochs = 5 # Total number of training epochs
learning_rate = 0.001 # The learning rate
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 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])
Start TensorBoard with the line magic function. For command line, run the same command without "%"
%tensorboard --logdir=logs/fit