nbro
nbro

Reputation: 15857

Is it possible to have a metric that returns an array (or tensor) rather than a number?

I have a neural network with an output NxM, where N is the batch size and M are the number of outputs where the network needs to make a prediction. I would like to compute a metric for each of the M outputs of the network, i.e. across all instances of the batch but separately for each of the M outputs, so that there would be M values of this metric. I tried to create a custom metric as follows.

def my_metric(y_true, y_pred):
    return [3.1, 5.2] # a list of dummy values

and then pass this metric to the list of metrics of the compile method of the model, then Keras outputs a number that is the average of 3.1 and 5.2 (in this case, (3.1 + 5.2)/2 = 4.15) rather than printing the actual list. So, is there a way of returning and printing a list (or numpy array) as the metric? Of course, in my specific case, I will not return the dummy list in the example above, but my custom metric is more complex.

Upvotes: 2

Views: 1217

Answers (1)

Daniel Möller
Daniel Möller

Reputation: 86610

Make one metric per M.

Working code for one output:

from keras.layers import Dense, Input
from keras.models import Model
import keras.backend as K
import numpy as np

inputs = Input((5,))
outputs = Dense(3)(inputs)
model = Model(inputs, outputs)

def metricWrapper(m):
    def meanMetric(true, pred):
        return pred[:, m]
    meanMetric.__name__ = 'meanMetric_' + str(m)
    return meanMetric
metrics = [metricWrapper(m) for m in range(3)]

model.compile(loss='mse', metrics=metrics, optimizer='adam')
model.fit(np.random.rand(10,5), np.zeros((10,3)))

Upvotes: 3

Related Questions