Keras:test_on_batch和predict_on_batch之间的区别

时间:2018-01-21 22:55:59

标签: python machine-learning neural-network keras deep-learning

在Philippe Remy的blog post有状态LSTM中,他说“你可能必须通过调用predict_on_batch()或test_on_batch()来手动进行验证/测试”。

查看the documentation,predict_on_batch执行此操作:

test_on_batch(self, x, y, sample_weight=None)

Test the model on a single batch of samples.

Arguments

    x: Numpy array of test data, or list of Numpy arrays if the model has multiple inputs. If all inputs in the model are named, you can also pass a dictionary mapping input names to Numpy arrays.
    y: Numpy array of target data, or list of Numpy arrays if the model has multiple outputs. If all outputs in the model are named, you can also pass a dictionary mapping output names to Numpy arrays.
    sample_weight: Optional array of the same length as x, containing weights to apply to the model's loss for each sample. In the case of temporal data, you can pass a 2D array with shape (samples, sequence_length), to apply a different weight to every timestep of every sample. In this case you should make sure to specify sample_weight_mode="temporal" in compile().

Returns

Scalar test loss (if the model has a single output and no metrics) or list of scalars (if the model has multiple outputs and/or metrics). The attribute model.metrics_names will give you the display labels for the scalar outputs

test_on_batch执行此操作:

stringr

我在训练循环中使用train_on_batch,基本上与博客文章一样。我如何知道是否使用predict_on_batch或test_on_batch进行交叉验证?

1 个答案:

答案 0 :(得分:5)

基本上是这样的:

  • predict_on_batch为您提供的参数数据提供了一组预测。如果您训练网络识别猫和狗 - 一旦您将图像提供给predict_on_batch方法 - 如果给定图像中有猫或狗,您将获得概率。您也可以使用这些概率来评估您的模型。
  • test_on_batch为您提供了一个衡量您的模型表现如何的标量。给定图像和真实标签 - 它计算了一系列损失和指标(在模型编译中定义),用于衡量模型如何适合数据。

后缀on_batch来自模型一次执行所有计算的事实。有时它不可行,因此以小块划分数据并执行多次计算会更好。 keras为您提供自动执行此操作的功能。因此predict相当于predict_on_batchevaluate - 相当于test_on_batch

为了选择交叉验证期间使用的功能,您需要使用:

    当您需要实际预测时,
  • predict / predict_on_batch,而不仅仅是指标值。但是,在这种情况下,您仍需要计算指标值。
  • 当您只需要指标值时,
  • test_on_batch / evaluate

所以 - 正如您所看到的 - 您可以在评估模型时实际组合这两个功能。如果您只是想要指标 - 请尝试test_on_batch / evaluate,因为它可能会为您节省大量的计算。