Run the execution function with the data from iterator. Given the dataset iterator and execution function, get the data from iterator and call it with the execution function to get the result (metric/loss). It will run for steps_per_epoch or until to the iterator is fully consumed. Args:
(model,
iterator,
execution_function,
dataset_size=None,
batch_size=None,
strategy=None,
steps_per_epoch=None,
num_samples=None,
mode=ModeKeys.TRAIN,
training_context=None,
total_epochs=None)
| 55 | |
| 56 | |
| 57 | def run_one_epoch(model, |
| 58 | iterator, |
| 59 | execution_function, |
| 60 | dataset_size=None, |
| 61 | batch_size=None, |
| 62 | strategy=None, |
| 63 | steps_per_epoch=None, |
| 64 | num_samples=None, |
| 65 | mode=ModeKeys.TRAIN, |
| 66 | training_context=None, |
| 67 | total_epochs=None): |
| 68 | """Run the execution function with the data from iterator. |
| 69 | |
| 70 | Given the dataset iterator and execution function, get the data from iterator |
| 71 | and call it with the execution function to get the result (metric/loss). |
| 72 | It will run for steps_per_epoch or until to the iterator is fully consumed. |
| 73 | |
| 74 | Args: |
| 75 | model: The keras model to run. |
| 76 | iterator: the dataset iterator to fetch the data. |
| 77 | execution_function: a tf.function that can be called with data. |
| 78 | dataset_size: the size of iterator, None when unknown. |
| 79 | batch_size: The size of the current batch. |
| 80 | strategy: the distribution strategy instance from the model. |
| 81 | steps_per_epoch: the number of steps to run for the epoch. |
| 82 | num_samples: the number of samples for the whole epoch if known. This can be |
| 83 | used to calculate the final partial batch, and scale the loss. |
| 84 | mode: the mode for the current epoch. |
| 85 | training_context: the context that contains callbacks and progress bar. |
| 86 | total_epochs: the total number of epochs that will be run. |
| 87 | Used when throw error when the iterator unexpectedly |
| 88 | reaches its end. |
| 89 | Returns: |
| 90 | The loss and metric value from the model. |
| 91 | """ |
| 92 | # Only use the sample to count if there is a partial batch at the end. |
| 93 | use_steps = num_samples is None |
| 94 | |
| 95 | if mode == ModeKeys.PREDICT: |
| 96 | aggregator = training_utils.OutputsAggregator( |
| 97 | use_steps=use_steps, |
| 98 | steps=steps_per_epoch, |
| 99 | num_samples=num_samples, |
| 100 | batch_size=batch_size) |
| 101 | else: |
| 102 | aggregator = training_utils.MetricsAggregator( |
| 103 | use_steps=use_steps, steps=steps_per_epoch, num_samples=num_samples) |
| 104 | callbacks = training_context.callbacks |
| 105 | progbar = training_context.progbar |
| 106 | |
| 107 | if callbacks.model.stop_training: |
| 108 | return |
| 109 | |
| 110 | target_steps = steps_per_epoch or np.inf |
| 111 | step = 0 |
| 112 | |
| 113 | while step < target_steps: |
| 114 | if use_steps: |
no test coverage detected