Skip to content

FIX: compile triplet loss within keras model #298

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 4 commits into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions tensorflow_addons/losses/triplet.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ def triplet_semihard_loss(y_true, y_pred, margin=1.0):
margin: Float, margin term in the loss definition.
"""
labels, embeddings = y_true, y_pred
# Reshape [batch_size] label tensor to a [batch_size, 1] label tensor.
# Reshape label tensor to [batch_size, 1].
lshape = tf.shape(labels)
assert lshape.shape == 1
labels = tf.reshape(labels, [lshape[0], 1])

# Build pairwise squared distance matrix.
Expand Down
7 changes: 7 additions & 0 deletions tensorflow_addons/losses/triplet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ def test_unweighted(self):
loss = cce_obj(y_true, y_pred)
self.assertAlmostEqual(self.evaluate(loss), loss_np, 3)

def test_keras_model_compile(self):
model = tf.keras.models.Sequential([
tf.keras.layers.Input(shape=(784,)),
tf.keras.layers.Dense(10),
])
model.compile(loss="triplet_semihard_loss", optimizer="adam")


if __name__ == '__main__':
tf.test.main()