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

Method train_step

official/core/base_task.py:223–280  ·  view source on GitHub ↗

Does forward and backward. With distribution strategies, this method runs on devices. Args: inputs: a dictionary of input tensors. model: the model, forward pass definition. optimizer: the optimizer for this training step. metrics: a nested structure of metrics obje

(self,
                 inputs,
                 model: tf_keras.Model,
                 optimizer: tf_keras.optimizers.Optimizer,
                 metrics=None)

Source from the content-addressed store, hash-verified

221 compiled_metrics.update_state(labels, model_outputs)
222
223 def train_step(self,
224 inputs,
225 model: tf_keras.Model,
226 optimizer: tf_keras.optimizers.Optimizer,
227 metrics=None):
228 """Does forward and backward.
229
230 With distribution strategies, this method runs on devices.
231
232 Args:
233 inputs: a dictionary of input tensors.
234 model: the model, forward pass definition.
235 optimizer: the optimizer for this training step.
236 metrics: a nested structure of metrics objects.
237
238 Returns:
239 A dictionary of logs.
240 """
241 if isinstance(inputs, tuple) and len(inputs) == 2:
242 features, labels = inputs
243 else:
244 features, labels = inputs, inputs
245 with tf.GradientTape() as tape:
246 outputs = model(features, training=True)
247 # Computes per-replica loss.
248 if model.compiled_loss:
249 loss = model.compiled_loss(
250 labels, outputs, regularization_losses=model.losses)
251 loss += self.build_losses(
252 labels=labels, model_outputs=outputs, aux_losses=None)
253 else:
254 loss = self.build_losses(
255 labels=labels, model_outputs=outputs, aux_losses=model.losses)
256 # Scales loss as the default gradients allreduce performs sum inside the
257 # optimizer.
258 scaled_loss = loss / tf.distribute.get_strategy().num_replicas_in_sync
259
260 # For mixed precision, when a LossScaleOptimizer is used, the loss is
261 # scaled to avoid numeric underflow.
262 if isinstance(optimizer,
263 tf_keras.mixed_precision.LossScaleOptimizer):
264 scaled_loss = optimizer.get_scaled_loss(scaled_loss)
265
266 tvars = model.trainable_variables
267 grads = tape.gradient(scaled_loss, tvars)
268
269 if isinstance(optimizer,
270 tf_keras.mixed_precision.LossScaleOptimizer):
271 grads = optimizer.get_unscaled_gradients(grads)
272 optimizer.apply_gradients(list(zip(grads, tvars)))
273 logs = {self.loss: loss}
274 if metrics:
275 self.process_metrics(metrics, labels, outputs)
276 if model.compiled_metrics:
277 self.process_compiled_metrics(model.compiled_metrics, labels, outputs)
278 logs.update({m.name: m.result() for m in metrics or []})
279 logs.update({m.name: m.result() for m in model.metrics})
280 return logs

Callers

nothing calls this directly

Calls 6

build_lossesMethod · 0.95
process_metricsMethod · 0.95
updateMethod · 0.80
apply_gradientsMethod · 0.45
resultMethod · 0.45

Tested by

no test coverage detected