Neural Network Workshop – Lab 6 Tensorboard and Training

Configure Tensorboard

Labels for the layers

  1. Create a set variable named embedding_layer_names and fill it with the names of all the model layers

Reference Material

https://www.programiz.com/python-programming/methods/built-in/set

https://www.programiz.com/python-programming/list-comprehension

https://keras.io/models/about-keras-models/

Hint 1

Create a set like this:

embedding_layer_names = set(???)

[collapse]
Hint 2

This is an iterable for layer names:

layer.name for layer in model.layers

[collapse]
Full Solution
embedding_layer_names = set(layer.name
                            for layer in model.layers)

[collapse]

[collapse]
Instantiate Tensorboard Callback

  1. Import Tensorboard from keras.callbacks
  2. import the time standard python library
  3. Import strftime from time library
  4. Instantiate Tensorboard in a variable named tensorboard with the following options:
    • log_dir = basepath+logs+current date time (see hint 3 don’t spend a lot of time on this)
    • write_graph=True
    • write_grads=True
    • write_images=True
    • embeddings_freq=10
    • embeddings_metadata=None
    • embeddings_layer_names=embedding_layer_names

Reference Material

https://keras.io/callbacks/#tensorboardc

Hint 1

Imports:

import time
from time import strftime
from keras.callbacks import TensorBoard

[collapse]
Hint 2

Instantiate like this:

tensorboard = TensorBoard(???)

[collapse]
Hint 3

The code to generate log_dir is:

log_dir=os.path.join (os.path.join (basepath, "logs"), format(strftime("%Y %d %m %H %M %S", time.localtime())))

[collapse]
Full Solution

Set outpath to the folder out in the current folder:

import time
from time import strftime
from keras.callbacks import TensorBoard

tensorboard = TensorBoard(log_dir=os.path.join (os.path.join (basepath, "logs"), format(strftime("%Y %d %m %H %M %S", time.localtime()))), write_graph=True, write_grads=True, write_images=True, embeddings_metadata=None, embeddings_layer_names=embedding_layer_names)

[collapse]

[collapse]

 

Train The Model

Fit the Model

  1. Fit the model with the following parameters:
    • the inputs (X)
    • the outpus (y)
    • validation_split=.20
    • batch_size=64
    • epochs =25
    • callbacks = tensorboard
  2. Print the model summary in the output window

Reference Material

https://keras.io/models/sequential/

Hint 1

call:

model.fit(???)

[collapse]
Hint 2

The entire model.fit command:

model.fit(X, y, validation_split = .20, batch_size = 64, epochs = 25, callbacks = [tensorboard])

[collapse]
Hint 3

Print the summary of a model:

print (model.summary())

[collapse]
Full Solution
#Fit the ANN to the training set
model.fit(X, y, validation_split = .20, batch_size = 64, epochs = 25, callbacks = [tensorboard])

# summary to console
print (model.summary())

[collapse]
  1. Open an Anconda Command Prompt (Windows) or Terminal Window (macOS)
  2. Navigate to the same folder your sales.py file is in
  3. Type the command tensorboard –logdir=logs –host localhost
  4. Navigate to the address in a web browser as instructed in the prompt/terminal
  5. Explore Tensorboard as the model is training, you will see updates in real time shortly after each epoch completes

[collapse]

 

Lab Complete!

 

Extra Credit – Understand Tensorboard

Interpret the Scalar Graphs
Interpret the Graphs tab