# Path: tensorflow_model_remediation/min_diff/losses/mmd_loss.py
# class MMDLoss(base_loss.MinDiffLoss):
#   def __init__(self,
#                kernel="gaussian",
#                predictions_transform=None,
#                name: Optional[str] = None,
#                enable_summary_histogram: Optional[bool] = True):
#   def _preprocess(self,
#                   membership: types.TensorType,
#                   predictions: types.TensorType,
#                   sample_weight: Optional[types.TensorType] = None):
#   def _calculate_mean(self, predictions_kernel: types.TensorType,
#                       normed_weights: types.TensorType,
#                       pos_mask: types.TensorType, neg_mask):
#   def call(self,
#            membership: types.TensorType,
#            predictions: types.TensorType,
#            sample_weight: Optional[types.TensorType] = None):
#   def from_config(cls, config):
# 
# Path: tensorflow_model_remediation/min_diff/losses/kernels/gaussian_kernel.py
# class GaussianKernel(base_kernel.MinDiffKernel):
#   def __init__(self, kernel_length: complex = 0.1, **kwargs):
#   def call(self, x: types.TensorType, y: types.TensorType) -> types.TensorType:
#   def get_config(self):
# 
# Path: tensorflow_model_remediation/min_diff/losses/kernels/laplacian_kernel.py
# _EPSILON = 1.0e-8
# class LaplacianKernel(base_kernel.MinDiffKernel):
#   def __init__(self, kernel_length: complex = 0.1, **kwargs):
#   def call(self, x: types.TensorType, y: types.TensorType) -> types.TensorType:
#   def get_config(self):


import tensorflow as tf
from tensorflow_model_remediation.min_diff.losses import mmd_loss as mmd_lib
from tensorflow_model_remediation.min_diff.losses.kernels import gaussian_kernel
from tensorflow_model_remediation.min_diff.losses.kernels import laplacian_kernel
      mmd_lib.MMDLoss(4)
    with self.assertRaisesRegex(
        TypeError,
        "predictions_kernel.*must be.*MinDiffKernel.*string.*lambda"):
      mmd_lib.MMDLoss(bad_kernel)

  def testSerialization(self):
    loss = mmd_lib.MMDLoss()
    serialized_loss = tf.keras.utils.serialize_keras_object(loss)
    deserialized_loss = tf.keras.utils.deserialize_keras_object(serialized_loss)

    self.assertIsInstance(deserialized_loss, mmd_lib.MMDLoss)
    self.assertIsNone(deserialized_loss.predictions_transform)
    self.assertIsInstance(deserialized_loss.predictions_kernel,
                          gaussian_kernel.GaussianKernel)
    self.assertEqual(deserialized_loss.name, loss.name)

  def testSerializationWithTransformAndKernel(self):
    predictions_fn = lambda x: x * 5.1  # Arbitrary operation.

    loss = mmd_lib.MMDLoss(
        predictions_transform=predictions_fn, kernel="laplacian")
    serialized_loss = tf.keras.utils.serialize_keras_object(loss)
    deserialized_loss = tf.keras.utils.deserialize_keras_object(serialized_loss)

    self.assertIsInstance(deserialized_loss, mmd_lib.MMDLoss)
    val = 7  # Arbitrary value.
    self.assertEqual(
        deserialized_loss.predictions_transform(val), predictions_fn(val))
    self.assertIsInstance(deserialized_loss.predictions_kernel,

# Path: tensorflow_model_remediation/min_diff/losses/mmd_loss.py
# class MMDLoss(base_loss.MinDiffLoss):
#   def __init__(self,
#                kernel="gaussian",
#                predictions_transform=None,
#                name: Optional[str] = None,
#                enable_summary_histogram: Optional[bool] = True):
#   def _preprocess(self,
#                   membership: types.TensorType,
#                   predictions: types.TensorType,
#                   sample_weight: Optional[types.TensorType] = None):
#   def _calculate_mean(self, predictions_kernel: types.TensorType,
#                       normed_weights: types.TensorType,
#                       pos_mask: types.TensorType, neg_mask):
#   def call(self,
#            membership: types.TensorType,
#            predictions: types.TensorType,
#            sample_weight: Optional[types.TensorType] = None):
#   def from_config(cls, config):
# 
# Path: tensorflow_model_remediation/min_diff/losses/kernels/gaussian_kernel.py
# class GaussianKernel(base_kernel.MinDiffKernel):
#   def __init__(self, kernel_length: complex = 0.1, **kwargs):
#   def call(self, x: types.TensorType, y: types.TensorType) -> types.TensorType:
#   def get_config(self):
# 
# Path: tensorflow_model_remediation/min_diff/losses/kernels/laplacian_kernel.py
# _EPSILON = 1.0e-8
# class LaplacianKernel(base_kernel.MinDiffKernel):
#   def __init__(self, kernel_length: complex = 0.1, **kwargs):
#   def call(self, x: types.TensorType, y: types.TensorType) -> types.TensorType:
#   def get_config(self):


# Path: tensorflow_model_remediation/min_diff/losses/mmd_loss_test.py
import tensorflow as tf
from tensorflow_model_remediation.min_diff.losses import mmd_loss as mmd_lib
from tensorflow_model_remediation.min_diff.losses.kernels import gaussian_kernel
from tensorflow_model_remediation.min_diff.losses.kernels import laplacian_kernel

      mmd_lib.MMDLoss(4)
    with self.assertRaisesRegex(
        TypeError,
        "predictions_kernel.*must be.*MinDiffKernel.*string.*lambda"):
      mmd_lib.MMDLoss(bad_kernel)

  def testSerialization(self):
    loss = mmd_lib.MMDLoss()
    serialized_loss = tf.keras.utils.serialize_keras_object(loss)
    deserialized_loss = tf.keras.utils.deserialize_keras_object(serialized_loss)

    self.assertIsInstance(deserialized_loss, mmd_lib.MMDLoss)
    self.assertIsNone(deserialized_loss.predictions_transform)
    self.assertIsInstance(deserialized_loss.predictions_kernel,
                          gaussian_kernel.GaussianKernel)
    self.assertEqual(deserialized_loss.name, loss.name)

  def testSerializationWithTransformAndKernel(self):
    predictions_fn = lambda x: x * 5.1  # Arbitrary operation.

    loss = mmd_lib.MMDLoss(
        predictions_transform=predictions_fn, kernel="laplacian")
    serialized_loss = tf.keras.utils.serialize_keras_object(loss)
    deserialized_loss = tf.keras.utils.deserialize_keras_object(serialized_loss)

    self.assertIsInstance(deserialized_loss, mmd_lib.MMDLoss)
    val = 7  # Arbitrary value.
    self.assertEqual(
        deserialized_loss.predictions_transform(val), predictions_fn(val))
    self.assertIsInstance(deserialized_loss.predictions_kernel,

                          laplacian_kernel.LaplacianKernel)