1

I'm new to Keras and I intend to store the output of my network for every epoch. To that end, I want to use Tensorbaord to observe the output layer in its environment.

class OutputObserver(Callback):
""""
callback to observe the output of the network
"""

    def on_train_begin(self, logs={}):
        self.epoch = []
        self.out_log = []

    def on_epoch_end(self, epoch, logs={}):
        self.epoch.append(epoch) 
        self.out_log.append(self.model.get_layer('Dense03/Output').output)

This will store the outputs tensors into a list. The problem is that I can do neither 1. convert this to a Numpy array so that can be read a CSV, ... file, 2. write a summary using Tensorflow (as Keras does not have this ability) and then analyze the output in Tensorboard.

I would be pleased to hear your opinions on storing and visualizing the output layer in every epoch of training.

Sincerely, Saeed.

Saeed Khorram
  • 71
  • 1
  • 7
  • 1
    Maybe [this answer](https://stackoverflow.com/a/45321332/1531463) (the part after **Edit**) can help. It visualizes the output layer with TensorBoard by feeding the validation data through the model in `on_epoch_end`. – Yu-Yang Oct 05 '17 at 15:10
  • @Yu-Yang Thanks a lot. I found the answer helpful. – Saeed Khorram Oct 07 '17 at 08:37

1 Answers1

5

To save the output layer for every epoch you need to pass the training/validation data to the callback object. The callback I used is as follow,

class OutputObserver(Callback):
""""
callback to observe the output of the network
"""

def __init__(self, xy):
    self.out_log = []
    self.xy = xy

def on_epoch_end(self, epoch, logs={}):
    self.out_log.append(self.model.predict(self.xy.x_train, batch_size=p.bath_size))

which xy.x_train is the training data.

now, the out_log array is a numpy.ndarray of shape (epoch_number, data_number, prediction_length):

type(prediction_logs[0])
Out[62]: numpy.ndarray
Saeed Khorram
  • 71
  • 1
  • 7
  • For more details see the official Keras documentation on callbacks, https://keras.io/callbacks/ – mrk Nov 26 '19 at 17:02