Search code examples
pythontensorflowkerastensorboard

Access loss metric from keras model


I am trying to access the training loss of my model which I have built, as I call a training loop on it. I have built a model as follows:

    def create_model(self):
        
        model = Sequential()
        model.add(Dense(172, input_shape=(1, 172), activation = 'relu'))
        model.add(Dense(172, activation = 'relu'))
        model.add(Dense(86, activation = 'relu'))
        model.add(Dense(43, activation = 'relu'))
        model.add(Dense(21, activation = "relu"))
        model.add(Flatten())
        model.add(Dense(7, activation="linear"))
        model.compile(loss="mse", optimizer=Adam(lr=0.001), metrics=['accuracy'])
        return model

And my fit is as follows:

self.model.fit(np.array(X), np.array(y), batch_size=MINIBATCH_SIZE, verbose=0, shuffle=False) 

The model is fit on a batch, and is part of a larger class (DQN Agent).

As I run a fit every timestep, and I have multiple timesteps per episode, how can I have a variable I can access in the training loop which gives me the loss of the network, so I can monitor it more closely? (with an end goal of adding it to tensorboard)

I can try to either access this every step or every episode (300 steps), whatever works easier.


Solution

  • You can either change your verbose to 1 (shows continuously changing loss and progress within epoch) or change it to 2 (shows loss after each epoch). You can also change your fit line to:

    history = self.model.fit(np.array(X), np.array(y), batch_size=MINIBATCH_SIZE,verbose=0, shuffle=False)
    

    Then after n_epochs, you can extract the loss in an array with the following call:

    losses = history.history['loss']
    

    You can iterate through training in a loop, to extract the losses however frequently you would like, and can either plot them, or save them to look at them separately.