MCPcopy Create free account
hub / github.com/tensorflow/models / __init__

Method __init__

official/core/base_trainer.py:141–240  ·  view source on GitHub ↗

Initialize common trainer for TensorFlow models. Args: config: An `ExperimentConfig` instance specifying experiment config. task: A base_task.Task instance. model: The model instance, e.g. a tf_keras.Model instance. optimizer: tf.optimizers.Optimizer instance. trai

(
      self,
      config: ExperimentConfig,
      task: base_task.Task,
      model: tf_keras.Model,
      optimizer: tf.optimizers.Optimizer,
      train: bool = True,
      evaluate: bool = True,
      train_dataset: Optional[Union[tf.data.Dataset,
                                    tf.distribute.DistributedDataset]] = None,
      validation_dataset: Optional[Union[
          tf.data.Dataset, tf.distribute.DistributedDataset]] = None,
      checkpoint_exporter=None)

Source from the content-addressed store, hash-verified

139
140 # pylint: disable=super-init-not-called
141 def __init__(
142 self,
143 config: ExperimentConfig,
144 task: base_task.Task,
145 model: tf_keras.Model,
146 optimizer: tf.optimizers.Optimizer,
147 train: bool = True,
148 evaluate: bool = True,
149 train_dataset: Optional[Union[tf.data.Dataset,
150 tf.distribute.DistributedDataset]] = None,
151 validation_dataset: Optional[Union[
152 tf.data.Dataset, tf.distribute.DistributedDataset]] = None,
153 checkpoint_exporter=None):
154 """Initialize common trainer for TensorFlow models.
155
156 Args:
157 config: An `ExperimentConfig` instance specifying experiment config.
158 task: A base_task.Task instance.
159 model: The model instance, e.g. a tf_keras.Model instance.
160 optimizer: tf.optimizers.Optimizer instance.
161 train: bool, whether or not this trainer will be used for training.
162 default to True.
163 evaluate: bool, whether or not this trainer will be used for evaluation.
164 default to True.
165 train_dataset: a dataset object created for training. With tf.distribute,
166 it needs to be a `DistributedDataset`.
167 validation_dataset: a dataset object created for evaluation. With
168 tf.distribute, it needs to be a `DistributedDataset`. The evaluator will
169 create a dataset iterator for each eval round, so the dataset does not
170 need to repeat.
171 checkpoint_exporter: an object that has the `maybe_export_checkpoint`
172 interface.
173 """
174 # Gets the current distribution strategy. If not inside any strategy scope,
175 # it gets a single-replica no-op strategy.
176 self._strategy = tf.distribute.get_strategy()
177 self._validate_params(
178 config,
179 check_train_data=train_dataset is None,
180 check_validation_data=validation_dataset is None)
181 self._config = config
182 self._task = task
183 self._model = model
184 self._optimizer = optimizer
185 self._checkpoint_exporter = checkpoint_exporter
186 self._recovery = None
187 # Runtime options are only applied to train_step.
188 # We use default for eval_step.
189 self._runtime_options = get_runtime_options(config)
190
191 # Creates a shadow copy of the weights to store weights moving average.
192 if isinstance(self._optimizer, optimization.ExponentialMovingAverage
193 ) and not self._optimizer.has_shadow_copy:
194 self._optimizer.shadow_copy(self._model)
195
196 # global_step increases by 1 after each training iteration.
197 # We should have global_step.numpy() == self.optimizer.iterations.numpy()
198 # when there is only 1 optimizer.

Callers

nothing calls this directly

Calls 6

_validate_paramsMethod · 0.95
get_runtime_optionsFunction · 0.85
shadow_copyMethod · 0.80
init_asyncMethod · 0.80
distribute_datasetMethod · 0.80
build_metricsMethod · 0.45

Tested by

no test coverage detected