Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions examples/vision/metric_learning_tf_similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import numpy as np

import tensorflow as tf
from tensorflow import keras
import keras

import tensorflow_similarity as tfsim

Expand Down Expand Up @@ -216,13 +216,14 @@
val_steps = 50

# init similarity loss
loss = tfsim.losses.MultiSimilarityLoss()
loss = tfsim.losses.MultiSimilarityLoss(reduction='sum_over_batch_size')

# compiling and training
model.compile(
optimizer=keras.optimizers.Adam(learning_rate),
loss=loss,
steps_per_execution=10,
run_eagerly=True,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Enabling run_eagerly=True can significantly degrade training performance as it disables graph optimizations. While it can be useful for debugging, it's generally recommended to make the model and training loop compatible with graph execution (i.e., by setting run_eagerly=False or removing the argument). Was this added to work around a specific incompatibility with Keras 3's graph mode? If so, it would be better to address the underlying issue to ensure the example runs efficiently.

)
history = model.fit(
train_ds, epochs=epochs, validation_data=val_ds, validation_steps=val_steps
Expand Down Expand Up @@ -321,7 +322,7 @@
for idx in np.argsort(y_display):
tfsim.visualization.viz_neigbors_imgs(
x_display[idx],
y_display[idx],
y_display[idx].numpy(),
nns[idx],
class_mapping=class_mapping,
fig_size=(16, 2),
Expand Down Expand Up @@ -394,7 +395,7 @@
"""

idx_no_match = np.where(np.array(matches) == 10)
no_match_queries = x_confusion[idx_no_match]
no_match_queries = keras.ops.take(x_confusion, keras.ops.cast(idx_no_match[0], dtype="int32"), axis=0)
if len(no_match_queries):
plt.imshow(no_match_queries[0])
else:
Expand Down