Runs a single gradient update on a single batch of data. Arguments: model: The model to train. 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 tens
(
model,
x,
y=None,
sample_weight=None,
class_weight=None,
reset_metrics=True)
| 195 | |
| 196 | |
| 197 | def train_on_batch( |
| 198 | model, |
| 199 | x, |
| 200 | y=None, |
| 201 | sample_weight=None, |
| 202 | class_weight=None, |
| 203 | reset_metrics=True): |
| 204 | """Runs a single gradient update on a single batch of data. |
| 205 | |
| 206 | Arguments: |
| 207 | model: The model to train. |
| 208 | x: Input data. It could be: |
| 209 | - A Numpy array (or array-like), or a list of arrays |
| 210 | (in case the model has multiple inputs). |
| 211 | - A TensorFlow tensor, or a list of tensors |
| 212 | (in case the model has multiple inputs). |
| 213 | - A dict mapping input names to the corresponding array/tensors, |
| 214 | if the model has named inputs. |
| 215 | - A `tf.data` dataset. |
| 216 | y: Target data. Like the input data `x`, it could be either Numpy |
| 217 | array(s) or TensorFlow tensor(s). It should be consistent with `x` |
| 218 | (you cannot have Numpy inputs and tensor targets, or inversely). If |
| 219 | `x` is a dataset `y` should not be specified |
| 220 | (since targets will be obtained from the iterator). |
| 221 | sample_weight: Optional array of the same length as x, containing |
| 222 | weights to apply to the model's loss for each sample. In the case of |
| 223 | temporal data, you can pass a 2D array with shape (samples, |
| 224 | sequence_length), to apply a different weight to every timestep of |
| 225 | every sample. In this case you should make sure to specify |
| 226 | sample_weight_mode="temporal" in compile(). This argument is not |
| 227 | supported when `x` is a dataset. |
| 228 | class_weight: Optional dictionary mapping class indices (integers) to a |
| 229 | weight (float) to apply to the model's loss for the samples from this |
| 230 | class during training. This can be useful to tell the model to "pay |
| 231 | more attention" to samples from an under-represented class. |
| 232 | reset_metrics: If `True`, the metrics returned will be only for this |
| 233 | batch. If `False`, the metrics will be statefully accumulated across |
| 234 | batches. |
| 235 | |
| 236 | Returns: |
| 237 | Scalar training loss |
| 238 | (if the model has a single output and no metrics) |
| 239 | or list of scalars (if the model has multiple outputs |
| 240 | and/or metrics). The attribute `model.metrics_names` will give you |
| 241 | the display labels for the scalar outputs. |
| 242 | |
| 243 | Raises: |
| 244 | ValueError: In case of invalid user-provided arguments. |
| 245 | """ |
| 246 | model._assert_compile_was_called() |
| 247 | |
| 248 | # TODO(scottzhu): Standardization should happen in the data handlers, |
| 249 | ## not on a per batch basis in the *_on_batch methods |
| 250 | # Validate and standardize user data. |
| 251 | x, y, sample_weights = model._standardize_user_data( |
| 252 | x, y, sample_weight=sample_weight, class_weight=class_weight, |
| 253 | extract_tensors_from_dataset=True) |
| 254 | batch_size = array_ops.shape(nest.flatten(x, expand_composites=True)[0])[0] |
nothing calls this directly
no test coverage detected