|
| 1 | +# Copyright 2021 The Orbit Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""A trainer object that can train models with a single output.""" |
| 16 | + |
| 17 | +import orbit |
| 18 | +import tensorflow as tf |
| 19 | + |
| 20 | + |
| 21 | +class SingleTaskTrainer(orbit.StandardTrainer): |
| 22 | + """Trains a single-output model on a given dataset. |
| 23 | +
|
| 24 | + This trainer will handle running a model with one output on a single |
| 25 | + dataset. It will apply the provided loss function to the model's output |
| 26 | + to calculate gradients and will apply them via the provided optimizer. It will |
| 27 | + also supply the output of that model to one or more `tf.keras.metrics.Metric` |
| 28 | + objects. |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__(self, |
| 32 | + train_dataset, |
| 33 | + label_key, |
| 34 | + model, |
| 35 | + loss_fn, |
| 36 | + optimizer, |
| 37 | + metrics=None, |
| 38 | + trainer_options=None): |
| 39 | + """Initializes a `SingleTaskTrainer` instance. |
| 40 | +
|
| 41 | + If the `SingleTaskTrainer` should run its model under a distribution |
| 42 | + strategy, it should be created within that strategy's scope. |
| 43 | +
|
| 44 | + This trainer will also calculate metrics during training. The loss metric |
| 45 | + is calculated by default, but other metrics can be passed to the `metrics` |
| 46 | + arg. |
| 47 | +
|
| 48 | + Arguments: |
| 49 | + train_dataset: A `tf.data.Dataset` or `DistributedDataset` that contains a |
| 50 | + string-keyed dict of `Tensor`s. |
| 51 | + label_key: The key corresponding to the label value in feature |
| 52 | + dictionaries dequeued from `train_dataset`. This key will be removed |
| 53 | + from the dictionary before it is passed to the model. |
| 54 | + model: A `tf.Module` or Keras `Model` object to evaluate. It must accept a |
| 55 | + `training` kwarg. |
| 56 | + loss_fn: A per-element loss function of the form (target, output). The |
| 57 | + output of this loss function will be reduced via `tf.reduce_mean` to |
| 58 | + create the final loss. We recommend using the functions in the |
| 59 | + `tf.keras.losses` package or `tf.keras.losses.Loss` objects with |
| 60 | + `reduction=tf.keras.losses.reduction.NONE`. |
| 61 | + optimizer: A `tf.keras.optimizers.Optimizer` instance. |
| 62 | + metrics: A single `tf.keras.metrics.Metric` object, or a list of |
| 63 | + `tf.keras.metrics.Metric` objects. |
| 64 | + trainer_options: An optional `orbit.utils.StandardTrainerOptions` object. |
| 65 | + """ |
| 66 | + self.label_key = label_key |
| 67 | + self.model = model |
| 68 | + self.loss_fn = loss_fn |
| 69 | + self.optimizer = optimizer |
| 70 | + |
| 71 | + # Capture the strategy from the containing scope. |
| 72 | + self.strategy = tf.distribute.get_strategy() |
| 73 | + |
| 74 | + # We always want to report training loss. |
| 75 | + self.train_loss = tf.keras.metrics.Mean('training_loss', dtype=tf.float32) |
| 76 | + |
| 77 | + # We need self.metrics to be an iterable later, so we handle that here. |
| 78 | + if metrics is None: |
| 79 | + self.metrics = [] |
| 80 | + elif isinstance(metrics, list): |
| 81 | + self.metrics = metrics |
| 82 | + else: |
| 83 | + self.metrics = [metrics] |
| 84 | + |
| 85 | + super(SingleTaskTrainer, self).__init__( |
| 86 | + train_dataset=train_dataset, options=trainer_options) |
| 87 | + |
| 88 | + def train_loop_begin(self): |
| 89 | + """Actions to take once, at the beginning of each train loop.""" |
| 90 | + self.train_loss.reset_states() |
| 91 | + for metric in self.metrics: |
| 92 | + metric.reset_states() |
| 93 | + |
| 94 | + def train_step(self, iterator): |
| 95 | + """A train step. Called multiple times per train loop by the superclass.""" |
| 96 | + |
| 97 | + def train_fn(inputs): |
| 98 | + with tf.GradientTape() as tape: |
| 99 | + # Extract the target value and delete it from the input dict, so that |
| 100 | + # the model never sees it. |
| 101 | + target = inputs.pop(self.label_key) |
| 102 | + |
| 103 | + # Get the outputs of the model. |
| 104 | + output = self.model(inputs, training=True) |
| 105 | + |
| 106 | + # Get the average per-batch loss and scale it down by the number of |
| 107 | + # replicas. This ensures that we don't end up multiplying our loss by |
| 108 | + # the number of workers - gradients are summed, not averaged, across |
| 109 | + # replicas during the apply_gradients call. |
| 110 | + loss = tf.reduce_mean(self.loss_fn(target, output)) |
| 111 | + scaled_loss = loss / self.strategy.num_replicas_in_sync |
| 112 | + |
| 113 | + # Get the gradients by applying the loss to the model's trainable |
| 114 | + # variables. |
| 115 | + gradients = tape.gradient(scaled_loss, self.model.trainable_variables) |
| 116 | + |
| 117 | + # Apply the gradients via the optimizer. |
| 118 | + self.optimizer.apply_gradients( |
| 119 | + list(zip(gradients, self.model.trainable_variables))) |
| 120 | + |
| 121 | + # Update metrics. |
| 122 | + self.train_loss.update_state(loss) |
| 123 | + for metric in self.metrics: |
| 124 | + metric.update_state(target, output) |
| 125 | + |
| 126 | + # This is needed to handle distributed computation. |
| 127 | + self.strategy.run(train_fn, args=(next(iterator),)) |
| 128 | + |
| 129 | + def train_loop_end(self): |
| 130 | + """Actions to take once after a training loop.""" |
| 131 | + with self.strategy.scope(): |
| 132 | + # Export the metrics. |
| 133 | + metrics = {metric.name: metric.result() for metric in self.metrics} |
| 134 | + metrics[self.train_loss.name] = self.train_loss.result() |
| 135 | + |
| 136 | + return metrics |
0 commit comments