code: def train_and_save(
      model,
      train_data,
      val_data,
      train_path,
      save_intermediates=False,
      **train_kw,
  ):
      """Run training and save model."""
      # Set up logging for TensorBoard. To view, run:
      #   tensorboard --log_dir=<train_path>/tensorboard
      keras_callbacks = [
          tf.keras.callbacks.TensorBoard(
              log_dir=os.path.join(train_path, "tensorboard")
          )
      ]
      if save_intermediates:
          keras_callbacks.append(
              EpochSaverCallback(train_path, save_fn=model.save)
          )
      history = model.train(
          train_data.examples,
          validation_inputs=val_data.examples,
          keras_callbacks=keras_callbacks,
          **train_kw,
      )
  
      # Save training history too, since this is human-readable and more concise
      # than the TensorBoard log files.
      with open(
          os.path.join(train_path, "train.history.json"), "w"
      ) as fd:
          # Use LIT's custom JSON encoder to handle dicts containing NumPy data.
          fd.write(
              serialize.to_json(
                  history_to_dict(history), simple=True, indent=2
              )
          )
  
      model.save(train_path)
      logging.info(
          "Saved model files: \n  %s",
          "\n  ".join(os.listdir(train_path)),
      )
  
file_path: lit/lit_nlp/examples/tools/glue_trainer.py
imports: from collections.abc import Sequence
  import os
  from absl import app
  from absl import flags
  from absl import logging
  from lit_nlp.examples.datasets import classification
  from lit_nlp.examples.datasets import glue
  from lit_nlp.examples.models import glue_models
  from lit_nlp.lib import serialize
  import tensorflow
label_desc: Use image node_id 2 for calling a global function with example usage: train_and_save(model, train_data, val_data, train_path, save_intermediates) without return types