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



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模块保持一致,实现统一。

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


  def predict(self,
    """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.

        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.


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





亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球