class_docstr: TFTrainer is a simple but feature-complete training and eval loop for TensorFlow, optimized for 🤗 Transformers.
  
  Args:
      model ([`TFPreTrainedModel`]):
          The model to train, evaluate or use for predictions.
      args ([`TFTrainingArguments`]):
          The arguments to tweak training.
      train_dataset ([`~tf.data.Dataset`], *optional*):
          The dataset to use for training. The dataset should yield tuples of `(features, labels)` where `features`
          is a dict of input features and `labels` is the labels. If `labels` is a tensor, the loss is calculated by
          the model by calling `model(features, labels=labels)`. If `labels` is a dict, such as when using a
          QuestionAnswering head model with multiple targets, the loss is instead calculated by calling
          `model(features, **labels)`.
      eval_dataset ([`~tf.data.Dataset`], *optional*):
          The dataset to use for evaluation. The dataset should yield tuples of `(features, labels)` where `features`
          is a dict of input features and `labels` is the labels. If `labels` is a tensor, the loss is calculated by
          the model by calling `model(features, labels=labels)`. If `labels` is a dict, such as when using a
          QuestionAnswering head model with multiple targets, the loss is instead calculated by calling
          `model(features, **labels)`.
      compute_metrics (`Callable[[EvalPrediction], Dict]`, *optional*):
          The function that will be used to compute metrics at evaluation. Must take a [`EvalPrediction`] and return
          a dictionary string to metric values.
      tb_writer (`tf.summary.SummaryWriter`, *optional*):
          Object to write to TensorBoard.
      optimizers (`Tuple[tf.keras.optimizers.Optimizer, tf.keras.optimizers.schedules.LearningRateSchedule]`, *optional*):
          A tuple containing the optimizer and the scheduler to use. The optimizer default to an instance of
          [`tf.keras.optimizers.Adam`] if `args.weight_decay_rate` is 0 else an instance of [`AdamWeightDecay`]. The
          scheduler will default to an instance of [`tf.keras.optimizers.schedules.PolynomialDecay`] if
          `args.num_warmup_steps` is 0 else an instance of [`WarmUp`].
docstr: Train method to train the model.
docstr_tok: ["Train","method","to","train","the","model","."]
code: def train(self) -> None:
      """
      Train method to train the model.
      """
      train_ds = self.get_train_tfdataset()
  
      if self.args.debug:
          tf.summary.trace_on(graph=True, profiler=True)
  
      self.gradient_accumulator.reset()
  
      num_update_steps_per_epoch = (
          self.num_train_examples / self.total_train_batch_size
      )
  
      # In fact, ``self.args.dataloader_drop_last`` has no effect in `trainer_tf.py`, because
      # the dataset is repeated before being batched.
      # It has the effect only when TPU is used which requires explicit tensor shape in order to make
      # the gradient accumulation implementation work.
      approx = (
          math.floor if self.args.dataloader_drop_last else math.ceil
      )
      num_update_steps_per_epoch = approx(num_update_steps_per_epoch)
  
      # At least one update for each epoch.
      num_update_steps_per_epoch = max(num_update_steps_per_epoch, 1)
      self.steps_per_epoch = num_update_steps_per_epoch
  
      if self.args.max_steps > 0:
          t_total = self.args.max_steps
          epochs = (self.args.max_steps // self.steps_per_epoch) + int(
              self.args.max_steps % self.steps_per_epoch > 0
          )
      else:
          t_total = self.steps_per_epoch * self.args.num_train_epochs
          epochs = self.args.num_train_epochs
  
      # Since ``self.args.num_train_epochs`` can be `float`, we make ``epochs`` be a `float` always.
      epochs = float(epochs)
  
      with self.args.strategy.scope():
          self.create_optimizer_and_scheduler(
              num_training_steps=t_total
          )
          folder = os.path.join(
              self.args.output_dir, PREFIX_CHECKPOINT_DIR
          )
          ckpt = tf.train.Checkpoint(
              optimizer=self.optimizer, model=self.model
          )
          self.model.ckpt_manager = tf.train.CheckpointManager(
              ckpt, folder, max_to_keep=self.args.save_total_limit
          )
  
          iterations = self.optimizer.iterations
          epochs_trained = 0
          steps_trained_in_current_epoch = 0
          if self.model.ckpt_manager.latest_checkpoint:
              logger.info(
                  f"Checkpoint file {self.model.ckpt_manager.latest_checkpoint} found and restoring from checkpoint"
              )
              ckpt.restore(
                  self.model.ckpt_manager.latest_checkpoint
              ).expect_partial()
  
              self.global_step = iterations.numpy()
  
              epochs_trained = self.global_step // self.steps_per_epoch
              steps_trained_in_current_epoch = (
                  self.global_step % self.steps_per_epoch
              )
  
              logger.info(
                  "  Continuing training from checkpoint, will skip to saved global_step"
              )
              logger.info(
                  f"  Continuing training from epoch {epochs_trained}"
              )
              logger.info(
                  f"  Continuing training from global step {self.global_step}"
              )
              logger.info(
                  f"  Will skip the first {steps_trained_in_current_epoch} steps in the first epoch"
              )
  
          tf.summary.experimental.set_step(self.global_step)
  
          with self.tb_writer.as_default():
              tf.summary.text("args", self.args.to_json_string())
  
          self.tb_writer.flush()
  
          logger.info("***** Running training *****")
          logger.info(f"  Num examples = {self.num_train_examples}")
          # TODO: We might want to print a more precise ``epochs`` if self.args.max_steps > 0 ?
          logger.info(f"  Num Epochs = {epochs}")
          logger.info(
              f"  Instantaneous batch size per device = {self.args.per_device_train_batch_size}"
          )
          logger.info(
              f"  Total train batch size (w. parallel, distributed & accumulation) = {self.total_train_batch_size}"
          )
          logger.info(
              f"  Gradient Accumulation steps = {self.args.gradient_accumulation_steps}"
          )
          logger.info(f"  Steps per epoch = {self.steps_per_epoch}")
          logger.info(f"  Total optimization steps = {t_total}")
  
          self.train_loss = tf.keras.metrics.Sum()
          start_time = datetime.datetime.now()
  
          for epoch_iter in range(epochs_trained, int(epochs)):
              # Reset the past mems state at the beginning of each epoch if necessary.
              if self.args.past_index >= 0:
                  self._past = None
  
              for step, batch in enumerate(train_ds):
                  # Skip past any already trained steps if resuming training
                  if steps_trained_in_current_epoch > 0:
                      steps_trained_in_current_epoch -= 1
                      continue
  
                  self.distributed_training_steps(batch)
  
                  self.global_step = iterations.numpy()
                  self.epoch_logging = (
                      epoch_iter + (step + 1) / self.steps_per_epoch
                  )
  
                  training_loss = self.train_loss.result() / (step + 1)
  
                  if self.args.debug:
                      logs = {}
                      logs["loss"] = training_loss.numpy()
                      logs["epoch"] = self.epoch_logging
  
                      self.log(logs)
  
                  if self.global_step == 1 and self.args.debug:
                      with self.tb_writer.as_default():
                          tf.summary.trace_export(
                              name="training",
                              step=self.global_step,
                              profiler_outdir=self.args.logging_dir,
                          )
  
                  if (
                      self.args.eval_steps > 0
                      and self.args.evaluation_strategy
                      == IntervalStrategy.STEPS
                      and self.global_step % self.args.eval_steps == 0
                  ):
                      self.evaluate()
  
                  if (
                      self.args.logging_steps > 0
                      and self.global_step % self.args.logging_steps
                      == 0
                  ) or (
                      self.global_step == 1
                      and self.args.logging_first_step
                  ):
                      logs = {}
                      logs["loss"] = training_loss.numpy()
                      logs["learning_rate"] = self.lr_scheduler(
                          self.global_step
                      ).numpy()
                      logs["epoch"] = self.epoch_logging
  
                      self.log(logs)
  
                  if (
                      self.args.save_steps > 0
                      and self.global_step % self.args.save_steps == 0
                  ):
                      ckpt_save_path = self.model.ckpt_manager.save()
  
                      logger.info(
                          f"Saving checkpoint for step {self.global_step} at {ckpt_save_path}"
                      )
  
                  if (
                      self.args.max_steps > 0
                      and self.global_step >= t_total
                  ):
                      break
  
                  if self.global_step % self.steps_per_epoch == 0:
                      break
  
              self.train_loss.reset_states()
  
              if (
                  self.args.max_steps > 0
                  and self.global_step >= self.args.max_steps
              ):
                  break
  
          end_time = datetime.datetime.now()
  
          logger.info(f"Training took: {str(end_time - start_time)}")
  
      if self.args.past_index and hasattr(self, "_past"):
          # Clean the state at the end of training
          delattr(self, "_past")
  
imports: import datetime
  import math
  import os
  import warnings
  from typing import Callable, Dict, Optional, Tuple
  from .utils import ENV_VARS_TRUE_VALUES
  from .integrations import is_comet_available, is_wandb_available
  import numpy
  import tensorflow
  from tensorflow.python.distribute.values import PerReplica
  from .modeling_tf_utils import TFPreTrainedModel
  from .optimization_tf import GradientAccumulator, create_optimizer
  from .trainer_utils import PREFIX_CHECKPOINT_DIR, EvalPrediction, IntervalStrategy, PredictionOutput, enable_full_determinism, set_seed
  from .training_args_tf import TFTrainingArguments
  from .utils import logging
label_desc: Use image node_id 13 for calling the TFTrainer obj's underlying member method code with example usage: obj.train() without return types