Runs a single gradient update on a single batch of data. Arguments: x: Input data. It could be: - A Numpy array (or array-like), or a list of arrays (in case the model has multiple inputs). - A TensorFlow tensor, or a list of tensors (in c
(self,
x,
y=None,
sample_weight=None,
class_weight=None,
reset_metrics=True)
| 918 | distributed_training_utils._reset_metrics(self) # pylint: disable=protected-access |
| 919 | |
| 920 | def train_on_batch(self, |
| 921 | x, |
| 922 | y=None, |
| 923 | sample_weight=None, |
| 924 | class_weight=None, |
| 925 | reset_metrics=True): |
| 926 | """Runs a single gradient update on a single batch of data. |
| 927 | |
| 928 | Arguments: |
| 929 | x: Input data. It could be: |
| 930 | - A Numpy array (or array-like), or a list of arrays |
| 931 | (in case the model has multiple inputs). |
| 932 | - A TensorFlow tensor, or a list of tensors |
| 933 | (in case the model has multiple inputs). |
| 934 | - A dict mapping input names to the corresponding array/tensors, |
| 935 | if the model has named inputs. |
| 936 | - A `tf.data` dataset. |
| 937 | y: Target data. Like the input data `x`, it could be either Numpy |
| 938 | array(s) or TensorFlow tensor(s). It should be consistent with `x` |
| 939 | (you cannot have Numpy inputs and tensor targets, or inversely). If |
| 940 | `x` is a dataset, `y` should not be specified |
| 941 | (since targets will be obtained from the iterator). |
| 942 | sample_weight: Optional array of the same length as x, containing |
| 943 | weights to apply to the model's loss for each sample. In the case of |
| 944 | temporal data, you can pass a 2D array with shape (samples, |
| 945 | sequence_length), to apply a different weight to every timestep of |
| 946 | every sample. In this case you should make sure to specify |
| 947 | sample_weight_mode="temporal" in compile(). This argument is not |
| 948 | supported when `x` is a dataset. |
| 949 | class_weight: Optional dictionary mapping class indices (integers) to a |
| 950 | weight (float) to apply to the model's loss for the samples from this |
| 951 | class during training. This can be useful to tell the model to "pay |
| 952 | more attention" to samples from an under-represented class. |
| 953 | reset_metrics: If `True`, the metrics returned will be only for this |
| 954 | batch. If `False`, the metrics will be statefully accumulated across |
| 955 | batches. |
| 956 | |
| 957 | Returns: |
| 958 | Scalar training loss |
| 959 | (if the model has a single output and no metrics) |
| 960 | or list of scalars (if the model has multiple outputs |
| 961 | and/or metrics). The attribute `model.metrics_names` will give you |
| 962 | the display labels for the scalar outputs. |
| 963 | |
| 964 | Raises: |
| 965 | ValueError: In case of invalid user-provided arguments. |
| 966 | """ |
| 967 | self._assert_compile_was_called() |
| 968 | self._check_call_args('train_on_batch') |
| 969 | if self._experimental_run_tf_function: |
| 970 | outputs = training_v2_utils.train_on_batch( |
| 971 | self, x, y=y, sample_weight=sample_weight, |
| 972 | class_weight=class_weight, reset_metrics=reset_metrics) |
| 973 | outputs = (outputs['total_loss'] + outputs['output_losses'] + |
| 974 | outputs['metrics']) |
| 975 | outputs = [ |
| 976 | training_v2_utils._non_none_constant_value(v) for v in outputs] # pylint: disable=protected-access |
| 977 | if len(outputs) == 1: |