tensorflow
euclidean distance
machine learning
distance metrics
deep learning

Mean Euclidean distance in Tensorflow

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Mean Euclidean distance in TensorFlow is a common metric for embedding quality, regression targets, and trajectory evaluation. The calculation is simple, but shape choices and axis handling determine whether the result matches your intended definition. A correct implementation computes per-sample distances first, then averages them.

Euclidean Distance Formula in Tensor Terms

For predictions and targets shaped as batch x features, per-sample Euclidean distance is:

  • subtract vectors
  • square elements
  • sum across feature axis
  • take square root

Then mean over batch axis.

Basic TensorFlow Implementation

python
1import tensorflow as tf
2
3
4def mean_euclidean_distance(y_true, y_pred):
5    diff = y_pred - y_true
6    squared = tf.square(diff)
7    summed = tf.reduce_sum(squared, axis=-1)
8    distances = tf.sqrt(summed + 1e-12)
9    return tf.reduce_mean(distances)
10
11
12y_true = tf.constant([[1.0, 2.0], [3.0, 4.0]])
13y_pred = tf.constant([[2.0, 2.0], [2.0, 5.0]])
14print(mean_euclidean_distance(y_true, y_pred).numpy())

Small epsilon prevents numerical issues near zero.

Use as a Keras Metric

Wrap function so it can be passed into compile.

python
1model.compile(
2    optimizer="adam",
3    loss="mse",
4    metrics=[mean_euclidean_distance],
5)

This reports mean Euclidean distance per training and validation step.

Distance Per Sequence or Image

For higher-rank tensors, choose axes intentionally.

Example for batch x time x features:

python
1def sequence_med(y_true, y_pred):
2    diff = y_pred - y_true
3    per_timestep = tf.sqrt(tf.reduce_sum(tf.square(diff), axis=-1) + 1e-12)
4    return tf.reduce_mean(per_timestep)

This averages across both batch and time dimensions.

Compare With MSE

Mean squared error and mean Euclidean distance are related but not interchangeable:

  • MSE squares residuals and averages directly
  • Euclidean distance takes square root per sample then averages

Euclidean distance is often easier to interpret in the original feature scale.

Weighted Mean Euclidean Distance

If samples have different importance, apply weights.

python
1def weighted_med(y_true, y_pred, weights):
2    diff = y_pred - y_true
3    d = tf.sqrt(tf.reduce_sum(tf.square(diff), axis=-1) + 1e-12)
4    weights = tf.cast(weights, d.dtype)
5    return tf.reduce_sum(d * weights) / tf.reduce_sum(weights)

Use this for imbalanced datasets or confidence-weighted scoring.

Performance Considerations

For very large tensors, metric computation can become noticeable. Keep metric simple, avoid unnecessary casts, and profile only if step time increases materially. In many pipelines, input processing remains the dominant cost.

Validation Example

Quick consistency check with NumPy style calculation:

python
1import numpy as np
2
3yt = np.array([[0., 0.], [1., 1.]])
4yp = np.array([[3., 4.], [1., 2.]])
5manual = np.mean(np.sqrt(np.sum((yp - yt) ** 2, axis=1)))
6print(manual)

Matching TensorFlow output confirms axis logic.

Choosing the Right Axis Semantics

For sequence and image tasks, document exactly which dimensions are reduced when computing distance. Teams often disagree about whether to average over time first or across batch first, which leads to inconsistent metrics between training and evaluation scripts. Explicit axis semantics in code comments and tests prevent mismatched monitoring dashboards and confusing model comparisons.

Monitoring Consistency

If metric is used in production monitoring, keep one shared implementation imported by both training and serving evaluation code. Shared metric code eliminates divergence between offline experiments and online quality checks.

Common Pitfalls

  • Reducing across wrong axis and getting scalar too early
  • Forgetting square root, which changes metric meaning
  • Mixing integer tensors and floating point operations
  • Ignoring shape compatibility between predictions and targets
  • Comparing MED values directly with MSE thresholds

Correct axis design is the most important part of metric correctness.

Summary

  • Compute per-sample Euclidean distances, then average.
  • Choose reduction axes based on tensor shape and task semantics.
  • Use epsilon for numerical stability near zero.
  • Integrate as custom Keras metric when needed.
  • Validate implementation with small manual test cases.

Course illustration
Course illustration

All Rights Reserved.