Sta*_*ess 4 python machine-learning neural-network deep-learning keras
在Philippe Remy 关于有状态LSTM 的博客文章中,他在底部说"你可能必须通过调用predict_on_batch()或test_on_batch()来手动进行验证/测试".
查看文档,predict_on_batch执行此操作:
predict_on_batch(self, x)
Returns predictions for a single batch of samples.
Arguments
x: Input samples, as a Numpy array.
Returns
Numpy array(s) of predictions.
Run Code Online (Sandbox Code Playgroud)
test_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
Run Code Online (Sandbox Code Playgroud)
我在训练循环中使用train_on_batch,基本上与博客文章一样.我如何知道是否使用predict_on_batch或test_on_batch进行交叉验证?
所以基本上:
predict_on_batch为您提供的数据作为参数提供一组预测.如果您训练网络识别猫和狗 - 一旦您将图像提供给predict_on_batch方法 - 如果给定图像中有猫或狗,您将获得概率.您还可以使用这些概率来评估模型.test_on_batch为您提供一个标量来衡量您的模型表现如何.给定图像和真实标签 - 它计算一堆损失和指标(在模型编译中定义),用于衡量模型如何适合数据.后缀on_batch来自模型一次执行所有计算的事实.有时这是不可行的,所以最好将数据分成小块并执行多次计算.keras为您提供自动执行此操作的功能.所以predict相当于predict_on_batch和evaluate- 到test_on_batch.
为了选择交叉验证期间使用的功能 - 您需要使用:
predict/ predict_on_batch当您需要实际预测时,不仅需要指标值.但是,在这种情况下,您仍需要计算指标值.test_on_batch/ evaluate当您只需要指标值时.所以 - 正如您所看到的 - 您可以在评估模型时实际组合这两个功能.如果您只是想要指标 - 尝试test_on_batch/ evaluate因为它可能会为您节省大量的计算.