MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / test_on_batch

Function test_on_batch

tensorflow/python/keras/engine/training_v2_utils.py:273–333  ·  view source on GitHub ↗

Test the model on a single batch of samples. Arguments: model: The model to test. 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 (

(model, x, y=None, sample_weight=None, reset_metrics=True)

Source from the content-addressed store, hash-verified

271
272
273def test_on_batch(model, x, y=None, sample_weight=None, reset_metrics=True):
274 """Test the model on a single batch of samples.
275
276 Arguments:
277 model: The model to test.
278 x: Input data. It could be:
279 - A Numpy array (or array-like), or a list of arrays
280 (in case the model has multiple inputs).
281 - A TensorFlow tensor, or a list of tensors
282 (in case the model has multiple inputs).
283 - A dict mapping input names to the corresponding array/tensors,
284 if the model has named inputs.
285 - A `tf.data` dataset.
286 y: Target data. Like the input data `x`,
287 it could be either Numpy array(s) or TensorFlow tensor(s).
288 It should be consistent with `x` (you cannot have Numpy inputs and
289 tensor targets, or inversely). If `x` is a dataset,
290 `y` should not be specified
291 (since targets will be obtained from the iterator).
292 sample_weight: Optional array of the same length as x, containing
293 weights to apply to the model's loss for each sample.
294 In the case of temporal data, you can pass a 2D array
295 with shape (samples, sequence_length),
296 to apply a different weight to every timestep of every sample.
297 In this case you should make sure to specify
298 sample_weight_mode="temporal" in compile(). This argument is not
299 supported when `x` is a dataset.
300 reset_metrics: If `True`, the metrics returned will be only for this
301 batch. If `False`, the metrics will be statefully accumulated across
302 batches.
303
304 Returns:
305 Scalar test loss (if the model has a single output and no metrics)
306 or list of scalars (if the model has multiple outputs
307 and/or metrics). The attribute `model.metrics_names` will give you
308 the display labels for the scalar outputs.
309
310 Raises:
311 ValueError: In case of invalid user-provided arguments.
312 """
313 model._assert_compile_was_called()
314
315 # TODO(scottzhu): Standardization should happen in the data handlers,
316 ## not on a per batch basis in the *_on_batch methods
317 # Validate and standardize user data.
318 x, y, sample_weights = model._standardize_user_data(
319 x, y, sample_weight=sample_weight, extract_tensors_from_dataset=True)
320
321 batch_size = array_ops.shape(nest.flatten(x, expand_composites=True)[0])[0]
322 outputs = training_eager.test_on_batch(
323 model,
324 x,
325 y,
326 sample_weights=sample_weights,
327 output_loss_metrics=model._output_loss_metrics)
328
329 if reset_metrics:
330 model.reset_metrics()

Callers

nothing calls this directly

Calls 7

test_on_batchMethod · 0.80
reset_metricsMethod · 0.80
shapeMethod · 0.45
flattenMethod · 0.45
castMethod · 0.45

Tested by

no test coverage detected