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

@创建于: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、参考链接

Keras中predict()方法和predict_classes()方法的区别

Keras非顺序模型没有model.predict_classes()方法如何获取测试数据分类的标签

  • 7
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值