Regard y_true and y_pred as single sample or a batch of samples in Keras.Metric?

Viewed 173

When I want to build a custom metric which can used with model.compile, I got confused about how to understand y_true and y_pred in the callable function. Here is an example given in the Keras documnets.

def my_metric_fn(y_true, y_pred):
    squared_difference = tf.square(y_true - y_pred)
    return tf.reduce_mean(squared_difference, axis=-1)  # Note the `axis=-1`

model.compile(optimizer='adam', loss='mean_squared_error', metrics=[my_metric_fn])

From the codes, I think y_true and y_pred are batch of samples. But it also says that "Much like loss functions, any callable with signature metric_fn(y_true, y_pred) that returns an array of losses (one of sample in the input batch) can be passed to compile() as a metric".

Does that mean the output return in the callable is the loss for one single sample? Should we regard y_true and y_pred as single sample or batch of samples in metric/loss function?

1 Answers

They’re batches of samples, yes.

Your function should accept two arrays, then return one of the same length. So to answer your question, regard them as batches of samples, not single samples.

Related