Search code examples
pythontensorflowmachine-learningkerastensorboard

Keras - plot values to tensorboard


I have the following code (using Keras):

self.tensorboard = TensorBoard(log_dir='./logs', histogram_freq=0,
                                   write_graph=False, write_images=True)

input_ = Input(shape=self.s_dim, name='input')
hidden = Dense(self.n_hidden, activation='relu')(input_)
out = Dense(3, activation='softmax')(hidden)

model = Model(inputs=input_, outputs=out, name="br-model")
model.compile(loss='mean_squared_error', optimizer=SGD(lr=0.005), metrics=['accuracy'])

# Some stuff in-between
model.fit(batch, target, epochs=2, verbose=0, callbacks=[self.tensorboard])

for k in batch:
    exploitability.append(np.max(model.predict(batch[k]))

It plot's the loss and the accuracy to tensorboard.

But i want to plot the np.average(exploitabilty) as well to tensorboard - how does it work? Is there any possibility to pass it as a metric or something similar?


Solution

  • You can add custom metrics to your model when you compile it, e.g.:

    def custom_metric(y_true, y_pred):
        max = K.max(y_pred)
        return max
    
    model.compile(loss='mean_squared_error', optimizer=SGD(lr=0.005), 
                  metrics=['accuracy', custom_metric])