class_docstr: Tests for neural_structured_learning.keras.layers.
class_docstr_tok: ["Tests","for","neural_structured_learning.keras.layers","."]
docstr: Fit and evaluate models with various distance configurations.
docstr_tok: ["Fit","and","evaluate","models","with","various","distance","configurations","."]
code: def testModelFitAndEvaluate(self, model_fn, distance_type):
      """Fit and evaluate models with various distance configurations."""
      # Set up graph-regularized model.
      distance_config = configs.DistanceConfig(
          distance_type=distance_type,
          transform_fn=configs.TransformType.SOFTMAX,
          sum_over_axis=-1,
      )
      model = model_fn(distance_config)
      model.compile(
          optimizer=tf.keras.optimizers.SGD(),
          loss=tf.keras.losses.SparseCategoricalCrossentropy(
              from_logits=True
          ),
          metrics=[
              tf.keras.metrics.SparseCategoricalAccuracy(),
              tf.keras.metrics.SparseCategoricalCrossentropy(
                  from_logits=True
              ),
          ],
      )
      # Fit and evaluate the model on dummy data that has 8 examples.
      features = {
          "features": np.random.normal(size=(8, 4)),
          "neighbors": np.random.normal(size=(8, 2, 4)),
          "neighbor_weights": np.random.uniform(size=(8, 2, 1)),
      }
      labels = np.random.randint(0, 3, size=8)
      train_history = model.fit(
          features, labels, batch_size=2, epochs=16
      ).history
      evaluation_results = dict(
          zip(
              model.metrics_names,
              model.evaluate(features, labels, batch_size=4),
          )
      )
      # Assert that losses and metrics were evaluated.
      self.assertAllGreater(train_history["graph_loss"], 0.0)
      self.assertGreater(evaluation_results["graph_loss"], 0.0)
      self.assertAllClose(
          train_history["loss"],
          np.add(
              train_history["graph_loss"],
              train_history["sparse_categorical_crossentropy"],
          ),
          _ERR_TOL,
      )
      self.assertNear(
          evaluation_results["loss"],
          evaluation_results["graph_loss"]
          + evaluation_results["sparse_categorical_crossentropy"],
          _ERR_TOL,
      )
  
file_path: neural-structured-learning/neural_structured_learning/keras/layers/layers_test.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import itertools
  from absl.testing import parameterized
  from neural_structured_learning import configs
  from neural_structured_learning.keras import layers
  import numpy
  import tensorflow
label_desc: Use image node_id 1 for calling the LayersTest obj's underlying member method code with example usage: obj.testModelFitAndEvaluate(model_fn, distance_type) without return types