tf.keras.metrics.CosineSimilarity

Computes the cosine similarity between the labels and predictions.

Inherits From: MeanMetricWrapper, Mean, Metric

Formula:

loss = sum(l2_norm(y_true) * l2_norm(y_pred)) 

See: Cosine Similarity. This metric keeps the average cosine similarity between predictions and labels over a stream of data.

name (Optional) string name of the metric instance.
dtype (Optional) data type of the metric result.
axis (Optional) Defaults to -1. The dimension along which the cosine similarity is computed.

Example:

Example:

# l2_norm(y_true) = [[0., 1.], [1./1.414, 1./1.414]] # l2_norm(y_pred) = [[1., 0.], [1./1.414, 1./1.414]] # l2_norm(y_true) . l2_norm(y_pred) = [[0., 0.], [0.5, 0.5]] # result = mean(sum(l2_norm(y_true) . l2_norm(y_pred), axis=1)) # = ((0. + 0.) + (0.5 + 0.5)) / 2 m = keras.metrics.CosineSimilarity(axis=1) m.update_state([[0., 1.], [1., 1.]], [[1., 0.], [1., 1.]]) m.result() 0.49999997 m.reset_state() m.update_state([[0., 1.], [1., 1.]], [[1., 0.], [1., 1.]],  sample_weight=[0.3, 0.7]) m.result() 0.6999999

Usage with compile() API:

model.compile( optimizer='sgd', loss='mse', metrics=[keras.metrics.CosineSimilarity(axis=1)]) 

dtype

variables

Methods

add_variable

View source

add_weight

View source

from_config

View source

get_config

View source

Return the serializable config of the metric.

reset_state

View source

Reset all of the metric state variables.

This function is called between epochs/steps, when a metric is evaluated during training.

result

View source

Compute the current metric value.

Returns
A scalar tensor, or a dictionary of scalar tensors.

stateless_reset_state

View source

stateless_result

View source

stateless_update_state

View source

update_state

View source

Accumulate statistics for the metric.

__call__

View source

Call self as a function.