← all repositories
omoindrot/tensorflow-triplet-loss

Teaching neural nets to tell twins apart with triplet loss

A clean TensorFlow implementation of online triplet mining that actually explains the math instead of hiding it.

1.1k stars Python ML FrameworksComputer Vision
tensorflow-triplet-loss
Velocity · 7d
+0.4
★ / day
Trend
steady
star history

What it does

This repo implements triplet loss training in TensorFlow, the technique FaceNet used to learn face embeddings where similar images cluster together and different ones push apart. It wraps the core logic into two callable functions—batch_all_triplet_loss and batch_hard_triplet_loss—and includes a full MNIST training pipeline with TensorBoard visualization.

The interesting bit

The “online” part matters: instead of pre-selecting triplets, the code mines hard and semi-hard triplets dynamically within each batch. The fraction_positive metric is a nice diagnostic touch—it lets you watch how many triplets are actually contributing to learning, which tells you whether your margin is too loose or your embeddings have already collapsed.

Key highlights

  • Two mining strategies: “batch all” (average over valid triplets) and “batch hard” (worst positive and negative per anchor)
  • fraction_positive returned for TensorBoard plotting—rare telemetry in a loss function
  • Full working pipeline: training script, JSON hyperparameter configs, and T-SNE embedding visualization
  • Adapted from Stanford CS230 course materials, so the structure is pedagogically sane
  • Includes pytest test suite for the loss functions

Caveats

  • Built for older TensorFlow (tf.contrib era); GPU install path references tensorflow-gpu which is deprecated
  • No mention of TF 2.x compatibility or Keras API integration

Verdict

Worth a look if you’re implementing metric learning from scratch and want readable reference code with the math explained. Skip if you need a maintained, production-ready library—this is teaching material that happens to run.

heatdrop uses Google Analytics to see which pages get read — nothing else. Your call. How we handle data.