Repository: tensorflow/addons

Issue:
`tfa.text.crf.crf_log_likelihood` function has a type error
**System information**
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
- TensorFlow version and how it was installed (source or binary):
- TensorFlow-Addons version and how it was installed (source or binary): master branch
- Python version:
- Is GPU used? (yes/no):

**Describe the bug**

https://github.com/tensorflow/addons/blob/53ef58f3aac9f3e1c36fca3bccaec73f67cd491c/tensorflow_addons/text/crf.py#L204-L248

Because `crf_log_likelihood` returns `log_likelihood` and `transition_params` as a tuple, return type of that function should be `Tuple[tf.Tensor, tf.Tensor]`



Discussion:

Patch:
diff --git a/tensorflow_addons/optimizers/novograd.py b/tensorflow_addons/optimizers/novograd.py
index 69fbf5f..159ec55 100644
--- a/tensorflow_addons/optimizers/novograd.py
+++ b/tensorflow_addons/optimizers/novograd.py
@@ -155,11 +155,11 @@ class NovoGrad(tf.keras.optimizers.Optimizer):
         coefficients = (apply_state or {}).get(
             (var_device, var_dtype)
         ) or self._fallback_apply_state(var_device, var_dtype)
-        weight_decay = self._get_hyper("weight_decay")
+        weight_decay = self._get_hyper("weight_decay", var_dtype)
         grad_averaging = self._get_hyper("grad_averaging")
 
         v = self.get_slot(var, "v")
-        g_2 = tf.reduce_sum(tf.square(tf.cast(grad, tf.float32)))
+        g_2 = tf.reduce_sum(tf.square(grad))
         v_t = tf.cond(
             tf.equal(self.iterations, 0),
             lambda: g_2,
@@ -198,11 +198,11 @@ class NovoGrad(tf.keras.optimizers.Optimizer):
         coefficients = (apply_state or {}).get(
             (var_device, var_dtype)
         ) or self._fallback_apply_state(var_device, var_dtype)
-        weight_decay = self._get_hyper("weight_decay")
+        weight_decay = self._get_hyper("weight_decay", var_dtype)
         grad_averaging = self._get_hyper("grad_averaging")
 
         v = self.get_slot(var, "v")
-        g_2 = tf.reduce_sum(tf.square(tf.cast(grad, tf.float32)))
+        g_2 = tf.reduce_sum(tf.square(grad))
         # v is just a scalar and does not need to involve sparse tensors.
         v_t = tf.cond(
             tf.equal(self.iterations, 0),
diff --git a/tensorflow_addons/text/crf.py b/tensorflow_addons/text/crf.py
index e096100..704baa0 100644
--- a/tensorflow_addons/text/crf.py
+++ b/tensorflow_addons/text/crf.py
@@ -19,7 +19,7 @@ import tensorflow as tf
 
 from tensorflow_addons.utils.types import TensorLike
 from typeguard import typechecked
-from typing import Optional
+from typing import Optional, Tuple
 
 # TODO: Wrap functions in @tf.function once
 # https://github.com/tensorflow/tensorflow/issues/29075 is resolved
@@ -206,7 +206,7 @@ def crf_log_likelihood(
     tag_indices: TensorLike,
     sequence_lengths: TensorLike,
     transition_params: Optional[TensorLike] = None,
-) -> tf.Tensor:
+) -> Tuple[tf.Tensor, tf.Tensor]:
     """Computes the log-likelihood of tag sequences in a CRF.
 
     Args:


Test patch:
diff --git a/tensorflow_addons/optimizers/tests/novograd_test.py b/tensorflow_addons/optimizers/tests/novograd_test.py
index 99cb259..facb2c5 100644
--- a/tensorflow_addons/optimizers/tests/novograd_test.py
+++ b/tensorflow_addons/optimizers/tests/novograd_test.py
@@ -21,12 +21,12 @@ import tensorflow as tf
 from tensorflow_addons.optimizers import NovoGrad
 
 
-def run_dense_sample(iterations, expected, optimizer):
-    var_0 = tf.Variable([1.0, 2.0], dtype=tf.dtypes.float32)
-    var_1 = tf.Variable([3.0, 4.0], dtype=tf.dtypes.float32)
+def run_dense_sample(iterations, expected, optimizer, dtype):
+    var_0 = tf.Variable([1.0, 2.0], dtype=dtype)
+    var_1 = tf.Variable([3.0, 4.0], dtype=dtype)
 
-    grad_0 = tf.constant([0.1, 0.2], dtype=tf.dtypes.float32)
-    grad_1 = tf.constant([0.3, 0.4], dtype=tf.dtypes.float32)
+    grad_0 = tf.constant([0.1, 0.2], dtype=dtype)
+    grad_1 = tf.constant([0.3, 0.4], dtype=dtype)
 
     grads_and_vars = list(zip([grad_0, grad_1], [var_0, var_1]))
 
@@ -38,38 +38,48 @@ def run_dense_sample(iterations, expected, optimizer):
 
 
 @pytest.mark.usefixtures("maybe_run_functions_eagerly")
-def test_dense_sample():
+@pytest.mark.parametrize("dtype", ["float32", "float64"])
+def test_dense_sample(dtype):
     run_dense_sample(
         iterations=1,
         expected=[[0.9552786425, 1.9105572849], [2.9400000012, 3.9200000016]],
         optimizer=NovoGrad(lr=0.1, epsilon=1e-8),
+        dtype=dtype,
     )
 
 
 @pytest.mark.usefixtures("maybe_run_functions_eagerly")
-def test_dense_sample_with_weight_decay():
+@pytest.mark.parametrize("dtype", ["float32", "float64"])
+def test_dense_sample_with_weight_decay(dtype):
     run_dense_sample(
         iterations=1,
         expected=[[0.945278642, 1.8905572849], [2.9100000012, 3.8800000016]],
         optimizer=NovoGrad(lr=0.1, weight_decay=0.1, epsilon=1e-8),
+        dtype=dtype,
     )
 
 
 @pytest.mark.usefixtures("maybe_run_functions_eagerly")
-def test_dense_sample_with_grad_averaging():
+@pytest.mark.parametrize("dtype", ["float32", "float64"])
+def test_dense_sample_with_grad_averaging(dtype):
     run_dense_sample(
         iterations=2,
         expected=[[0.9105572849, 1.8211145698], [2.8800000024, 3.8400000032]],
         optimizer=NovoGrad(lr=0.1, grad_averaging=True, epsilon=1e-8),
+        dtype=dtype,
     )
 
 
-def run_sparse_sample(iterations, expected, optimizer):
-    var_0 = tf.Variable([1.0, 2.0])
-    var_1 = tf.Variable([3.0, 4.0])
+def run_sparse_sample(iterations, expected, optimizer, dtype):
+    var_0 = tf.Variable([1.0, 2.0], dtype=dtype)
+    var_1 = tf.Variable([3.0, 4.0], dtype=dtype)
 
-    grad_0 = tf.IndexedSlices(tf.constant([0.1]), tf.constant([0]), tf.constant([2]))
-    grad_1 = tf.IndexedSlices(tf.constant([0.4]), tf.constant([1]), tf.constant([2]))
+    grad_0 = tf.IndexedSlices(
+        tf.constant([0.1], dtype=dtype), tf.constant([0]), tf.constant([2])
+    )
+    grad_1 = tf.IndexedSlices(
+        tf.constant([0.4], dtype=dtype), tf.constant([1]), tf.constant([2])
+    )
 
     grads_and_vars = list(zip([grad_0, grad_1], [var_0, var_1]))
 
@@ -81,29 +91,35 @@ def run_sparse_sample(iterations, expected, optimizer):
 
 
 @pytest.mark.usefixtures("maybe_run_functions_eagerly")
-def test_sparse_sample():
+@pytest.mark.parametrize("dtype", ["float32", "float64"])
+def test_sparse_sample(dtype):
     run_sparse_sample(
         iterations=2,
         expected=[[0.71, 2.0], [3.0, 3.71]],
         optimizer=NovoGrad(lr=0.1, epsilon=1e-8),
+        dtype=dtype,
     )
 
 
 @pytest.mark.usefixtures("maybe_run_functions_eagerly")
-def test_sparse_sample_with_weight_decay():
+@pytest.mark.parametrize("dtype", ["float32", "float64"])
+def test_sparse_sample_with_weight_decay(dtype):
     run_sparse_sample(
         iterations=2,
         expected=[[0.6821, 2.0], [3.0, 3.5954]],
         optimizer=NovoGrad(lr=0.1, weight_decay=0.1, epsilon=1e-8),
+        dtype=dtype,
     )
 
 
 @pytest.mark.usefixtures("maybe_run_functions_eagerly")
-def test_sparse_sample_with_grad_averaging():
+@pytest.mark.parametrize("dtype", ["float32", "float64"])
+def test_sparse_sample_with_grad_averaging(dtype):
     run_sparse_sample(
         iterations=2,
         expected=[[0.8, 2.0], [3.0, 3.8]],
         optimizer=NovoGrad(lr=0.1, grad_averaging=True, epsilon=1e-8),
+        dtype=dtype,
     )