tensorflow.keras.models.Sequential——predict()、predict_classes()、predict_proba()方法的区别

人工智能23

@创建于:2022.04.17
@修改于:2022.04.17

predict() 在tf.keras.Sequential 和 tf.keras.Model模块都有效;
predict_classes()、predict_proba()方法 在tf.keras.Sequential 模块下有效,在tf.keras.Model模块下无效。

1、方法介绍

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 '

2、建议使用predict(),进而改写

predict_classes() 已被弃用并且将在 2021-01-01 之后删除,请改用下面做法。这样能够与tf.keras.Model模块保持一致,实现统一。

np.argmax(model.predict(x), axis=-1)
  • 如果你的模型进行二分类(例如,如果它使用 sigmoid 最后一层激活),使用下面代码获得分类
(model.predict(x) > 0.5).astype("int32")

3、predict()参数介绍

  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.

4、参考链接

Original: https://blog.csdn.net/chenhepg/article/details/124237827
Author: 条件漫步
Title: tensorflow.keras.models.Sequential——predict()、predict_classes()、predict_proba()方法的区别

相关文章
empty怎么发音_empty怎么读 人工智能

empty怎么发音_empty怎么读

用中文说下啊,但不是翻译哈,我急需有人来解答吗 empty [英][?empti][美][mpti] 怎样 adj.空的,事半功倍是什么意思空虚的,空洞的; 空闲的,无效的,徒劳的; 无聊的,愚蠢的;...
RNN详解 人工智能

RNN详解

RNN(循环神经网络)详解 为什么要引入RNN? ​ 我们可以把一个普通的神经网络当成一个能够你和任意函数的黑盒,只要训练的数据足够多,给定特定的x,我们就可得到希望的y。结构如下 ​ 该模型可以用于...
Pytorch Unet 复现 人工智能

Pytorch Unet 复现

pytorch-unet 来源:https://github.com/milesial/Pytorch-UNet 前两天搞了一下图像分割,用了下unet。之前没怎么用过。复现了一下18年的une py...