Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
ml-mastery-zh
提交
5e7b3045
M
ml-mastery-zh
项目概览
OpenDocCN
/
ml-mastery-zh
8 个月 前同步成功
通知
2
Star
556
Fork
158
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
ml-mastery-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
5e7b3045
编写于
10月 22, 2019
作者:
ElamDavies
提交者:
GitHub
10月 22, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #35 from ElmaDavies/master
格式校验,翻译校正
上级
7d0e6ab6
2523071b
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
367 addition
and
368 deletion
+367
-368
docs/dl-keras/how-to-make-classification-and-regression-predictions-for-deep-learning-models-in-keras.md
...gression-predictions-for-deep-learning-models-in-keras.md
+367
-368
未找到文件。
docs/dl-keras/how-to-make-classification-and-regression-predictions-for-deep-learning-models-in-keras.md
浏览文件 @
5e7b3045
...
...
@@ -2,7 +2,7 @@
> 原文: [https://machinelearningmastery.com/how-to-make-classification-and-regression-predictions-for-deep-learning-models-in-keras/](https://machinelearningmastery.com/how-to-make-classification-and-regression-predictions-for-deep-learning-models-in-keras/)
一旦您在 Keras 中选择并
适合最终的深度学习模型,您就可以使用它来对新数据实例进行预测。
一旦您在 Keras 中选择并
拟合了最终的深度学习模型,您就可以使用它来对新数据实例进行预测。
初学者对如何做到这一点有一些困惑。我经常看到以下问题:
...
...
@@ -13,19 +13,18 @@
完成本教程后,您将了解:
*
如何最终确定模型以便为预测做好准备。
*
如何对 Keras 中的分类问题进行类
和概率预测。
*
如何对 Keras 中的分类问题进行类
别概率预测。
*
如何在 Keras 中进行回归预测。
让我们开始吧
。
让我们开始吧
!
![
How to Make Classification and Regression Predictions for Deep Learning Models in Keras
](
img/f0f15aa8316dd7ee6c7b1361b8727f8a.png
)
如何对 Keras 中的深度学习模型进行分类和回归预测
[
mstk east
](
https://www.flickr.com/photos/120248737@N03/16306796118/
)
的照片,保留一些权利。
照片由
[
mstk east
](
https://www.flickr.com/photos/120248737@N03/16306796118/
)
提供,并保留所属权利。
## 教程概述
本教程分为 3 个部分
;他们是:
本教程分为 3 个部分
,分别是:
1.
完成模型
2.
分类预测
...
...
@@ -35,36 +34,36 @@
在进行预测之前,必须训练最终模型。
您可能使用 k 折交叉验证或训练/测试分割数据来训练
模型。这样做是为了让您估算模型中样本数据的技能,例如:新数据。
您可能使用 k 折交叉验证或训练/测试分割数据来训练
许多模型,这样做是为了让能够利用模型能估计样本外的数据,例如一些新数据。
这些模型已达到目的,现在可以丢弃。
如果这些模型已达到目的,则可以丢弃。
您现在必须在所有可用数据上训练最终模型
。您可以在此处了解有关如何训练最终模型的更多信息:
您现在必须在所有可用数据上训练最终模型
,您可以在此处了解有关如何训练最终模型的更多信息:
*
[
如何训练最终机器学习模型
](
https://machinelearningmastery.com/train-final-machine-learning-model/
)
## 2.分类预测
分类问题是模型学习输入要素和作为标签的输出要素之间的映射的问题,例如“_
垃圾邮件 _”和“_ 不垃圾邮件 _”。
分类问题是模型学习输入要素和作为标签的输出要素之间的映射的问题,例如“_
垃圾邮件_”和“_非垃圾邮件_”。
下面是 Keras 中针对简单的两类(二元)分类问题开发的最终神经网络模型的示例。
如果
在 Keras 中开发神经网络模型对您来说是新的,请参阅帖子:
如果
您不熟悉在 Keras 中开发神经网络模型,请参阅帖子:
*
[
用 Keras 逐步开发 Python 中的第一个神经网络
](
https://machinelearningmastery.com/tutorial-first-neural-network-python-keras/
)
```
py
#
example of training a final classification model
#
训练最终模型的示例
from
keras.models
import
Sequential
from
keras.layers
import
Dense
from
sklearn.datasets.samples_generator
import
make_blobs
from
sklearn.preprocessing
import
MinMaxScaler
#
generate 2d classification dataset
#
生成2维分类数据集
X
,
y
=
make_blobs
(
n_samples
=
100
,
centers
=
2
,
n_features
=
2
,
random_state
=
1
)
scalar
=
MinMaxScaler
()
scalar
.
fit
(
X
)
X
=
scalar
.
transform
(
X
)
#
define and fit the final model
#
定义并拟合最终模型
model
=
Sequential
()
model
.
add
(
Dense
(
4
,
input_dim
=
2
,
activation
=
'relu'
))
model
.
add
(
Dense
(
4
,
activation
=
'relu'
))
...
...
@@ -73,53 +72,53 @@ model.compile(loss='binary_crossentropy', optimizer='adam')
model
.
fit
(
X
,
y
,
epochs
=
200
,
verbose
=
0
)
```
完成后,您可能希望将模型保存到文件,例如通过 Keras API
。保存后,您可以随时加载模型并使用它进行预测。有关此示例,请参阅帖子:
完成后,您可能希望将模型保存到文件,例如通过 Keras API
保存后,您可以随时加载模型并使用它进行预测,有关此示例,请参阅帖子:
*
[
保存并加载您的 Keras 深度学习模型
](
https://machinelearningmastery.com/save-load-keras-deep-learning-models/
)
为简单起见,我们将跳过本教程中的示例。
我们可能希望使用最终模型进行两种类型的分类预测
;它们是阶级预测和概率预测。
我们可能希望使用最终模型进行两种类型的分类预测
,它们是类别预测和概率预测。
###
阶级预测
###
类别预测
给出最终模型和一个或多个数据实例的类
预测,预测数据实例的类。
给出最终模型和一个或多个数据实例的类
别预测,预测数据实例的类别。
我们
不知道新数据的结果类。这就是我们首先需要模型的原因。
我们
并不知道新数据的分类结果,这就是我们首先需要定义模型的原因。
我们可以使用
_predict_classes()_ 函数在 Keras 中使用我们最终的分类模型来预测新数据实例的类。请注意,此功能仅适用于 _Sequential_ 型号,而不适用于使用功能 API 开发的型号。
我们可以使用
`predict_classes()`
函数在 Keras 中使用我们最终的分类模型来预测新数据实例的类别。请注意,此函数仅适用于
`Sequential`
模型,而不适用于使用函数 API 开发的其他模型。
例如,我们在名为
_Xnew_ 的数组中有一个或多个数据实例。这可以传递给我们模型上的 _predict_classes()_ 函数,以便预测数组中每个实例的类值。
例如,我们在名为
`Xnew`
的数组中有一个或多个数据实例,可以传递给我们模型上的
`predict_classes()`
函数,以便预测数组中每个实例的类别。
```
py
Xnew
=
[[...],
[...]]
ynew
=
model
.
predict_classes
(
Xnew
)
```
让我们通过一个例子来具体
化:
让我们通过一个例子来具体
演示这个过程:
```
py
#
example making new class predictions for a classification problem
#
对分类问题做出类别预测的示例
from
keras.models
import
Sequential
from
keras.layers
import
Dense
from
sklearn.datasets.samples_generator
import
make_blobs
from
sklearn.preprocessing
import
MinMaxScaler
#
generate 2d classification dataset
#
生成2维的分类数据集
X
,
y
=
make_blobs
(
n_samples
=
100
,
centers
=
2
,
n_features
=
2
,
random_state
=
1
)
scalar
=
MinMaxScaler
()
scalar
.
fit
(
X
)
X
=
scalar
.
transform
(
X
)
#
define and fit the final model
#
定义并拟合最终模型
model
=
Sequential
()
model
.
add
(
Dense
(
4
,
input_dim
=
2
,
activation
=
'relu'
))
model
.
add
(
Dense
(
4
,
activation
=
'relu'
))
model
.
add
(
Dense
(
1
,
activation
=
'sigmoid'
))
model
.
compile
(
loss
=
'binary_crossentropy'
,
optimizer
=
'adam'
)
model
.
fit
(
X
,
y
,
epochs
=
500
,
verbose
=
0
)
#
new instances where we do not know the answer
#
不知分类结果的新的实例
Xnew
,
_
=
make_blobs
(
n_samples
=
3
,
centers
=
2
,
n_features
=
2
,
random_state
=
1
)
Xnew
=
scalar
.
transform
(
Xnew
)
#
make a prediction
#
做出预测
ynew
=
model
.
predict_classes
(
Xnew
)
# show the inputs and predicted outputs
for
i
in
range
(
len
(
Xnew
)):
...
...
@@ -134,32 +133,32 @@ X=[0.29097707 0.12978982], Predicted=[1]
X
=
[
0.78082614
0.75391697
],
Predicted
=
[
0
]
```
如果您只有一个新的数据实例,则可以将其作为包含在数组中的实例提供给
_predict_classes()_ 函数;例如:
如果您只有一个新的数据实例,则可以将其作为包含在数组中的实例提供给
`predict_classes()`
函数,例如:
```
py
#
example making new class prediction for a classification problem
#
对分类问题做出类别预测的示例
from
keras.models
import
Sequential
from
keras.layers
import
Dense
from
sklearn.datasets.samples_generator
import
make_blobs
from
sklearn.preprocessing
import
MinMaxScaler
from
numpy
import
array
#
generate 2d classification dataset
#
生成2维分类数据集
X
,
y
=
make_blobs
(
n_samples
=
100
,
centers
=
2
,
n_features
=
2
,
random_state
=
1
)
scalar
=
MinMaxScaler
()
scalar
.
fit
(
X
)
X
=
scalar
.
transform
(
X
)
#
define and fit the final model
#
定义并拟合最终模型
model
=
Sequential
()
model
.
add
(
Dense
(
4
,
input_dim
=
2
,
activation
=
'relu'
))
model
.
add
(
Dense
(
4
,
activation
=
'relu'
))
model
.
add
(
Dense
(
1
,
activation
=
'sigmoid'
))
model
.
compile
(
loss
=
'binary_crossentropy'
,
optimizer
=
'adam'
)
model
.
fit
(
X
,
y
,
epochs
=
500
,
verbose
=
0
)
#
new instance where we do not know the answer
#
不知结果的新的实例
Xnew
=
array
([[
0.89337759
,
0.65864154
]])
# make a prediction
ynew
=
model
.
predict_classes
(
Xnew
)
#
show the inputs and predicted outputs
#
输入和输出的预测结果
print
(
"X=%s, Predicted=%s"
%
(
Xnew
[
0
],
ynew
[
0
]))
```
...
...
@@ -169,55 +168,55 @@ print("X=%s, Predicted=%s" % (Xnew[0], ynew[0]))
X
=
[
0.89337759
0.65864154
],
Predicted
=
[
0
]
```
### 关于类
标签的注释
### 关于类
别标签的注意事项
请注意,在准备数据时,您将把域中的类值(例如字符串)映射到整数值。您可能使用过 _
[
LabelEncoder
](
http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html#sklearn.preprocessing.LabelEncoder
)
_ 。
此 _LabelEncoder_ 可用于通过
_inverse_transform()_ 函数将整数转换回字符串值。
此 _LabelEncoder_ 可用于通过
`inverse_transform()`
函数将整数转换回字符串值。
因此,您可能希望在拟合最终模型时保存(pickle)用于编码
_y_ 值的 _LabelEncoder_ 。
因此,您可能希望在拟合最终模型时保存(pickle)用于编码
`y`
值的 _LabelEncoder_ 。
### 概率预测
您可能希望进行的另一种类型的预测是数据实例属于每个类
的概率。
您可能希望进行的另一种类型的预测是数据实例属于每个类
别的概率。
这被称为概率预测,其中,给定新实例,模型将
每个结果类的概率返回为 0 和 1 之间的值。
这被称为概率预测,其中,给定新实例,模型将
为每个结果类别的概率返回为 0 和 1 之间的值。
您可以通过调用
_predict_proba()_ 函数在 Keras 中进行这些类型的预测;例如:
您可以通过调用
`predict_proba()`
函数在 Keras 中进行这些类型的预测,如下所示:
```
py
Xnew
=
[[...],
[...]]
ynew
=
model
.
predict_proba
(
Xnew
)
```
在
两类(二进制)分类问题的情况下,通常在输出层中使用 S 形激活函数。预测概率被视为观察属于类 1 的可能性,或被反转(1-概率)以给出类 0 的概率。
在
二元(二进制)分类问题的情况下,通常在输出层中使用
`sigmoid`
激活函数,预测概率被视为属于类 1 的观测值的概率,或被反转(1-概率)以给出类 0 的概率。
在多类分类问题的情况下,通常在输出层上使用
softmax 激活函数,并且将每个类的观察的可能性作为向量返回。
在多类分类问题的情况下,通常在输出层上使用
`softmax`
激活函数,并且将每个类的概率观测值作为向量返回。
以下示例对数据实例的 _Xnew_ 数组中的每个示例进行概率预测。
```
py
#
example making new probability predictions for a classification problem
#
为分类问题做出概率预测的示例
from
keras.models
import
Sequential
from
keras.layers
import
Dense
from
sklearn.datasets.samples_generator
import
make_blobs
from
sklearn.preprocessing
import
MinMaxScaler
#
generate 2d classification dataset
#
生成2维分类数据集
X
,
y
=
make_blobs
(
n_samples
=
100
,
centers
=
2
,
n_features
=
2
,
random_state
=
1
)
scalar
=
MinMaxScaler
()
scalar
.
fit
(
X
)
X
=
scalar
.
transform
(
X
)
#
define and fit the final model
#
定义并拟合最终模型
model
=
Sequential
()
model
.
add
(
Dense
(
4
,
input_dim
=
2
,
activation
=
'relu'
))
model
.
add
(
Dense
(
4
,
activation
=
'relu'
))
model
.
add
(
Dense
(
1
,
activation
=
'sigmoid'
))
model
.
compile
(
loss
=
'binary_crossentropy'
,
optimizer
=
'adam'
)
model
.
fit
(
X
,
y
,
epochs
=
500
,
verbose
=
0
)
#
new instances where we do not know the answer
#
不知分类结果的新的实例
Xnew
,
_
=
make_blobs
(
n_samples
=
3
,
centers
=
2
,
n_features
=
2
,
random_state
=
1
)
Xnew
=
scalar
.
transform
(
Xnew
)
#
make a prediction
#
做出预测
ynew
=
model
.
predict_proba
(
Xnew
)
# show the inputs and predicted outputs
for
i
in
range
(
len
(
Xnew
)):
...
...
@@ -232,7 +231,7 @@ X=[0.29097707 0.12978982], Predicted=[0.82020265]
X
=
[
0.78082614
0.75391697
],
Predicted
=
[
0.00693122
]
```
如果您想向用户提供专
家解释的概率,这在您的应用程序中会有所帮助。
如果您想向用户提供专
业的概率解释,这的应用程序中会对您有所帮助。
## 3.回归预测
...
...
@@ -241,19 +240,19 @@ X=[0.78082614 0.75391697], Predicted=[0.00693122]
下面是用于回归的最终 Keras 模型的示例。
```
py
#
example of training a final regression model
#
训练最终回归模型的示例
from
keras.models
import
Sequential
from
keras.layers
import
Dense
from
sklearn.datasets
import
make_regression
from
sklearn.preprocessing
import
MinMaxScaler
#
generate regression dataset
#
生成回归数据集
X
,
y
=
make_regression
(
n_samples
=
100
,
n_features
=
2
,
noise
=
0.1
,
random_state
=
1
)
scalarX
,
scalarY
=
MinMaxScaler
(),
MinMaxScaler
()
scalarX
.
fit
(
X
)
scalarY
.
fit
(
y
.
reshape
(
100
,
1
))
X
=
scalarX
.
transform
(
X
)
y
=
scalarY
.
transform
(
y
.
reshape
(
100
,
1
))
#
define and fit the final model
#
定义并拟合最终模型
model
=
Sequential
()
model
.
add
(
Dense
(
4
,
input_dim
=
2
,
activation
=
'relu'
))
model
.
add
(
Dense
(
4
,
activation
=
'relu'
))
...
...
@@ -262,38 +261,38 @@ model.compile(loss='mse', optimizer='adam')
model
.
fit
(
X
,
y
,
epochs
=
1000
,
verbose
=
0
)
```
我们可以通过在最终模型上调用
_predict()_ 函数来使用最终的回归模型预测数量。
我们可以通过在最终模型上调用
`predict()`
函数来使用最终的回归模型预测数量。
_predict()_
函数采用一个或多个数据实例的数组。
`predict()`
函数获取一个或多个数据实例的数组。
下面的示例演示了如何对具有未知预期结果的多个数据实例进行回归预测。
```
py
#
example of making predictions for a regression problem
#
为回归问题做出最终预测的示例
from
keras.models
import
Sequential
from
keras.layers
import
Dense
from
sklearn.datasets
import
make_regression
from
sklearn.preprocessing
import
MinMaxScaler
#
generate regression dataset
#
生成回归数据集
X
,
y
=
make_regression
(
n_samples
=
100
,
n_features
=
2
,
noise
=
0.1
,
random_state
=
1
)
scalarX
,
scalarY
=
MinMaxScaler
(),
MinMaxScaler
()
scalarX
.
fit
(
X
)
scalarY
.
fit
(
y
.
reshape
(
100
,
1
))
X
=
scalarX
.
transform
(
X
)
y
=
scalarY
.
transform
(
y
.
reshape
(
100
,
1
))
#
define and fit the final model
#
定义并拟合最终模型
model
=
Sequential
()
model
.
add
(
Dense
(
4
,
input_dim
=
2
,
activation
=
'relu'
))
model
.
add
(
Dense
(
4
,
activation
=
'relu'
))
model
.
add
(
Dense
(
1
,
activation
=
'linear'
))
model
.
compile
(
loss
=
'mse'
,
optimizer
=
'adam'
)
model
.
fit
(
X
,
y
,
epochs
=
1000
,
verbose
=
0
)
#
new instances where we do not know the answer
#
不知结果的新的数据实例
Xnew
,
a
=
make_regression
(
n_samples
=
3
,
n_features
=
2
,
noise
=
0.1
,
random_state
=
1
)
Xnew
=
scalarX
.
transform
(
Xnew
)
#
make a prediction
#
做出预测
ynew
=
model
.
predict
(
Xnew
)
#
show the inputs and predicted outputs
#
打印输入值和预测的输出
for
i
in
range
(
len
(
Xnew
)):
print
(
"X=%s, Predicted=%s"
%
(
Xnew
[
i
],
ynew
[
i
]))
```
...
...
@@ -306,36 +305,36 @@ X=[0.39445118 0.79390858], Predicted=[0.7475489]
X
=
[
0.02884127
0.6208843
],
Predicted
=
[
0.43370453
]
```
可以使用相同的函数来对单个数据实例进行预测,只要它适当地包装在
周围的列表或数组中即可。
可以使用相同的函数来对单个数据实例进行预测,只要它适当地包装在
列表或者数组的环境中即可。
例如:
```
py
#
example of making predictions for a regression problem
#
做出回归预测的实例
from
keras.models
import
Sequential
from
keras.layers
import
Dense
from
sklearn.datasets
import
make_regression
from
sklearn.preprocessing
import
MinMaxScaler
from
numpy
import
array
#
generate regression dataset
#
生成回归数据集
X
,
y
=
make_regression
(
n_samples
=
100
,
n_features
=
2
,
noise
=
0.1
,
random_state
=
1
)
scalarX
,
scalarY
=
MinMaxScaler
(),
MinMaxScaler
()
scalarX
.
fit
(
X
)
scalarY
.
fit
(
y
.
reshape
(
100
,
1
))
X
=
scalarX
.
transform
(
X
)
y
=
scalarY
.
transform
(
y
.
reshape
(
100
,
1
))
#
define and fit the final model
#
定义并拟合最终模型
model
=
Sequential
()
model
.
add
(
Dense
(
4
,
input_dim
=
2
,
activation
=
'relu'
))
model
.
add
(
Dense
(
4
,
activation
=
'relu'
))
model
.
add
(
Dense
(
1
,
activation
=
'linear'
))
model
.
compile
(
loss
=
'mse'
,
optimizer
=
'adam'
)
model
.
fit
(
X
,
y
,
epochs
=
1000
,
verbose
=
0
)
#
new instance where we do not know the answer
#
不知结果的新的实例
Xnew
=
array
([[
0.29466096
,
0.30317302
]])
#
make a prediction
#
做出预测
ynew
=
model
.
predict
(
Xnew
)
#
show the inputs and predicted outputs
#
打印输入和预测的输出
print
(
"X=%s, Predicted=%s"
%
(
Xnew
[
0
],
ynew
[
0
]))
```
...
...
@@ -362,7 +361,7 @@ X=[0.29466096 0.30317302], Predicted=[0.17333156]
具体来说,你学到了:
*
如何最终确定模型以便为预测做好准备。
*
如何对 Keras 中的分类问题进行类
和概率预测。
*
如何对 Keras 中的分类问题进行类
别和概率预测。
*
如何在 Keras 中进行回归预测。
你有任何问题吗?
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录