# 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
      tape.watch(variables)
      predictions = variables * 3  # arbitrary linear operation.
      loss_value = loss_fn(membership, predictions, sample_weights)

    gradients = tape.gradient(loss_value, variables)
    # Gradients should all be 0 for weights that are all 0.
    expected_gradients = [[0.0], [0.0], [0.0], [0.0], [0.0]]
    self.assertAllClose(expected_gradients, gradients)

  def testRaisesExpectedErrors(self):
    kernel = gaussian_kernel.GaussianKernel()
    mmd_lib.MMDLoss(kernel)
    bad_kernel = lambda x: kernel(x)  # pylint:disable=unnecessary-lambda
    with self.assertRaisesRegex(
        TypeError,
        "predictions_kernel.*must be.*MinDiffKernel.*string.*4.*int"):
      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,

# 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

      tape.watch(variables)
      predictions = variables * 3  # arbitrary linear operation.
      loss_value = loss_fn(membership, predictions, sample_weights)

    gradients = tape.gradient(loss_value, variables)
    # Gradients should all be 0 for weights that are all 0.
    expected_gradients = [[0.0], [0.0], [0.0], [0.0], [0.0]]
    self.assertAllClose(expected_gradients, gradients)

  def testRaisesExpectedErrors(self):
    kernel = gaussian_kernel.GaussianKernel()
    mmd_lib.MMDLoss(kernel)
    bad_kernel = lambda x: kernel(x)  # pylint:disable=unnecessary-lambda
    with self.assertRaisesRegex(
        TypeError,
        "predictions_kernel.*must be.*MinDiffKernel.*string.*4.*int"):
      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)