Repository: tensorflow/addons

Issue:
tf.keras.load_model throws error when trained using the tfa.losses.TripletSemiHardLoss
**System information**
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 16.04
- TensorFlow version and how it was installed (source or binary): 2.0 -source
- TensorFlow-Addons version and how it was installed (source or binary): 0.6.0; 
- Python version: 3.6
- Is GPU used? (yes/no):yes

I get the following error when running the following command. I get this error when training the network with tfa.losses.TripletSemiHardLoss but do not get this error when training with tf.keras.losses.CategoricalCrossentropy().

Command:
loaded_model=tf.keras.models.load_model(filepath)

Error:
TypeError    
Traceback (most recent call last)
<ipython-input-33-dd68e3d40410> in <module>()
----> 1 loaded_model=tf.keras.models.load_model(filepath)

6 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/losses.py in from_config(cls, config)
    138         A `Loss` instance.
    139     """
--> 140     return cls(**config)
    141 
    142   def get_config(self):

TypeError: __init__() got an unexpected keyword argument 'reduction'





Discussion:

Resolving patch:
diff --git a/tensorflow_addons/losses/lifted.py b/tensorflow_addons/losses/lifted.py
--- a/tensorflow_addons/losses/lifted.py
+++ b/tensorflow_addons/losses/lifted.py
@@ -116,7 +116,7 @@ class LiftedStructLoss(tf.keras.losses.Loss):
       name: Optional name for the op.
     """
 
-    def __init__(self, margin=1.0, name=None):
+    def __init__(self, margin=1.0, name=None, **kwargs):
         super(LiftedStructLoss, self).__init__(
             name=name, reduction=tf.keras.losses.Reduction.NONE)
         self.margin = margin
diff --git a/tensorflow_addons/losses/triplet.py b/tensorflow_addons/losses/triplet.py
--- a/tensorflow_addons/losses/triplet.py
+++ b/tensorflow_addons/losses/triplet.py
@@ -150,7 +150,7 @@ class TripletSemiHardLoss(tf.keras.losses.Loss):
       name: Optional name for the op.
     """
 
-    def __init__(self, margin=1.0, name=None):
+    def __init__(self, margin=1.0, name=None, **kwargs):
         super(TripletSemiHardLoss, self).__init__(
             name=name, reduction=tf.keras.losses.Reduction.NONE)
         self.margin = margin


Test patch:
diff --git a/tensorflow_addons/losses/lifted_test.py b/tensorflow_addons/losses/lifted_test.py
--- a/tensorflow_addons/losses/lifted_test.py
+++ b/tensorflow_addons/losses/lifted_test.py
@@ -113,6 +113,10 @@ def test_keras_model_compile(self):
         ])
         model.compile(loss="Addons>lifted_struct_loss", optimizer="adam")
 
+    def test_serialization(self):
+        loss = lifted.LiftedStructLoss()
+        new_loss = tf.keras.losses.deserialize(tf.keras.losses.serialize(loss))
+
 
 if __name__ == '__main__':
     tf.test.main()
diff --git a/tensorflow_addons/losses/triplet_test.py b/tensorflow_addons/losses/triplet_test.py
--- a/tensorflow_addons/losses/triplet_test.py
+++ b/tensorflow_addons/losses/triplet_test.py
@@ -114,6 +114,10 @@ def test_keras_model_compile(self):
         ])
         model.compile(loss="Addons>triplet_semihard_loss", optimizer="adam")
 
+    def test_serialization(self):
+        loss = triplet.TripletSemiHardLoss()
+        new_loss = tf.keras.losses.deserialize(tf.keras.losses.serialize(loss))
+
 
 if __name__ == '__main__':
     tf.test.main()