@创建于:2022.04.17
@修改于:2022.04.17
predict() 在tf.keras.Sequential 和 tf.keras.Model模块都有效;
predict_classes()、predict_proba()方法 在tf.keras.Sequential 模块下有效,在tf.keras.Model模块下无效。
predict()方法预测时,返回值是数值,表示样本属于每一个类别的概率。
predict_proba() 方法预测时,返回值是数值,表示样本属于每一个类别的概率。与predict输出结果一致。
predict_classes() 方法预测时,返回的是类别的索引,即该样本所属的类别标签。
predict_classes() 和 predict_proba()方法将逐渐被弃用了,不建议尝试了。虽然在tensorflow2.5.0中还存在,如果使用会报出如下警告。
UserWarning: `model.predict_proba()` is deprecated and will be removed after 2021-01-01. Please use `model.predict()` instead.
warnings.warn('`model.predict_proba()` is deprecated and '
predict_classes() 已被弃用并且将在 2021-01-01 之后删除,请改用下面做法。这样能够与tf.keras.Model模块保持一致,实现统一。
softmax
最后一层激活),使用np.argmax获得最大值索引。因为深度学习的多分类模型里面需要使用独热向量编码,或参考keras中to_categorical函数解析np.argmax(model.predict(x), axis=-1)
sigmoid
最后一层激活),使用下面代码获得分类(model.predict(x) > 0.5).astype("int32")
def predict(self,
x,
batch_size=None,
verbose=0,
steps=None,
callbacks=None,
max_queue_size=10,
workers=1,
use_multiprocessing=False):
"""Generates output predictions for the input samples.
Computation is done in batches. This method is designed for performance in
large scale inputs. For small amount of inputs that fit in one batch,
directly using `__call__` is recommended for faster execution, e.g.,
`model(x)`, or `model(x, training=False)` if you have layers such as
`tf.keras.layers.BatchNormalization` that behaves differently during
inference. Also, note the fact that test loss is not affected by
regularization layers like noise and dropout.
Args:
x: Input samples. 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 case the model has multiple inputs).
- A `tf.data` dataset.
- A generator or `keras.utils.Sequence` instance.
A more detailed description of unpacking behavior for iterator types
(Dataset, generator, Sequence) is given in the `Unpacking behavior
for iterator-like inputs` section of `Model.fit`.
batch_size: Integer or `None`.
Number of samples per batch.
If unspecified, `batch_size` will default to 32.
Do not specify the `batch_size` if your data is in the
form of dataset, generators, or `keras.utils.Sequence` instances
(since they generate batches).
verbose: Verbosity mode, 0 or 1.
steps: Total number of steps (batches of samples)
before declaring the prediction round finished.
Ignored with the default value of `None`. If x is a `tf.data`
dataset and `steps` is None, `predict` will
run until the input dataset is exhausted.
callbacks: List of `keras.callbacks.Callback` instances.
List of callbacks to apply during prediction.
See [callbacks](/api_docs/python/tf/keras/callbacks).
max_queue_size: Integer. Used for generator or `keras.utils.Sequence`
input only. Maximum size for the generator queue.
If unspecified, `max_queue_size` will default to 10.
workers: Integer. Used for generator or `keras.utils.Sequence` input
only. Maximum number of processes to spin up when using
process-based threading. If unspecified, `workers` will default
to 1.
use_multiprocessing: Boolean. Used for generator or
`keras.utils.Sequence` input only. If `True`, use process-based
threading. If unspecified, `use_multiprocessing` will default to
`False`. Note that because this implementation relies on
multiprocessing, you should not pass non-picklable arguments to
the generator as they can't be passed easily to children processes.
Keras中predict()方法和predict_classes()方法的区别
Keras非顺序模型没有model.predict_classes()方法如何获取测试数据分类的标签