@创建于: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()方法的区别
相关文章

前向传播代码解析——你真的明白了吗?
我们知道预训练模型通常包括两部分:def _ init _ (self,last_conv_stride=2): 和def forward(self,x):两部分,前者主要用来继承nn.Module模...

主流深度学习框架对比(TensorFlow、Keras、MXNet、PyTorch)
目录 主流深度学习框架对比(TensorFlow、Keras、MXNet、PyTorch) 一、 简介 二、 流行度 Original: https://blog.csdn.net/qq_389982...

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

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

【OpenCV 例程300篇】01. 图像的读取(cv2.imread)
专栏地址:『youcans 的 OpenCV 例程300篇 - 总目录』 01. 图像的读取(cv2.imread) 02. 图像的保存(cv2.imwrite) 03. 图像的显示(cv2.imsh...

python3.6 import matplotlib pyparsing 包版本获取报错
python3.6 import matplotlib报错信息如下: File "D:\python3.6\Lib\site-packages\pyparsing_ _init__.py", line...

URL fetch failure on https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz: None
TensorFlow调用Keras数据集出现错误 问题描述 解决方法 问题描述 Keras框架为我们提供了一些常用的内置数据集。比如,图像识别领域的 手写识别MNIST数据集、 文本分类领域的电影影评...

机器学习模型太慢?来看看英特尔(R) 扩展加速 ⛵
💡 作者:韩信子@ShowMeAI 📘 机器学习实战系列:https://www.showmeai.tech/tutorials/41 📘 本文地址:https://www.showmeai.tech...

TF2-Tips:自定义model.fit
官方示例 keras官方代码给的例子很详细:Customizing what happens in fit() 基础 class CustomModel(keras.Model): def train...

matlab函数变速不变调_变速不变调小结
变速不变调,即TSM(Time scale modification)。 最近在做变调一块的工作,常见的变调算法有时域,频域,参量法。时域较易实现,多采用变速不变调+重采样实现变调不变速,详细原理可以...

freeswitch智能语音开发之ASR
ASR(Automatic Speech Recognition)自动语音识别技术是一种将人的语音转换为文本的技术。 一、freeswitch如何使用asr freeswitch提供两个app功能de...

《数字语音处理》- 实验4. 基于MATLAB与VQ的特定人孤立词语音识别研究(附代码)
声明 本文仅在CSDN发布,未经允许请勿转载或引用! 正版链接: https://blog.csdn.net/meenr/article/details/117629850 MATLAB基于VQ的特定...

语音论文阅读:U2
题目: Unified Streaming and Non-streaming Two-pass End-to-end Model for Speech Recognition 摘要 提出一种双路方法...

Keras-gpu版本安装教程(亲测有效)
本教程是在annoconda下安装Keras-gpu版本 步骤1:先根据英伟达的显卡的版本下载cuda 步骤2:然后通过cuda版本来下载对应 cudnn版本,接下来需要配置一下环境变量 步骤3:配置...

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