Path: blob/master/site/zh-cn/guide/migrate/canned_estimators.ipynb
25118 views
Copyright 2021 The TensorFlow Authors.
迁移示例:预设 Estimator
预设(或预制)Estimator 在 TensorFlow 1 中一直被用作一种快速简单的方式来针对各种典型用例训练模型。TensorFlow 2 通过 Keras 模型为其中一些方式提供了直接的近似替代。对于那些没有内置 TensorFlow 2 替代的预设 Estimator,您仍然能够相当轻松地构建自己的替代。
本指南将通过几个直接等效项和自定义替代示例来演示如何使用 Keras 将 TensorFlow 1 的 tf.estimator
派生模型迁移到 TensorFlow 2。
即,本指南包含下列迁移过程的示例:
从 TensorFlow 1 中
tf.estimator
的LinearEstimator
、Classifier
或Regressor
到 TensorFlow 2 中的 Kerastf.compat.v1.keras.models.LinearModel
从 TensorFlow 1 中
tf.estimator
的DNNEstimator
、Classifier
或Regressor
到 TensorFlow 2 中的自定义 Keras DNN ModelKeras从 TensorFlow 1 中
tf.estimator
的DNNLinearCombinedEstimator
、Classifier
或Regressor
到 TensorFlow 2 中的tf.compat.v1.keras.models.WideDeepModel
从 TensorFlow 1 中
tf.estimator
的BoostedTreesEstimator
、Classifier
或Regressor
到 TensorFlow 2 中的tfdf.keras.GradientBoostedTreesModel
in
模型训练的一个常见前身是特征预处理,可以使用 tf.feature_column
为 TensorFlow 1 Estimator 模型完成此过程。有关 TensorFlow 2 中特征预处理的更多信息,请参阅有关从特征列迁移到 Keras 预处理层 API 的本指南。
安装
从几个必要的 TensorFlow 导入开始:
从标准 Titanic 数据集中准备一些简单的数据进行演示:
然后,创建一个方法来实例化一个简单的样本优化器,以便与我们的各种 TensorFlow 1 Estimator 和 TensorFlow 2 Keras 模型一起使用。
示例 1:从 LinearEstimator 迁移
TensorFlow 1:使用 LinearEstimator
在 TensorFlow 1 中,可以使用 tf.estimator.LinearEstimator
为回归和分类问题创建基线线性模型。
TensorFlow 2:使用 Keras LinearModel
在 TensorFlow 2 中,可以创建 Keras tf.compat.v1.keras.models.LinearModel
的实例,它是 tf.estimator.LinearEstimator
的替代。tf.compat.v1.keras
路径用于表示预制模型的存在目的是兼容性。
示例 2:从 DNNEstimator 迁移
TensorFlow 1:使用 DNNEstimator
在 TensorFlow 1 中,可以使用 tf.estimator.DNNEstimator
为回归和分类问题创建基线深度神经网络 (DNN) 模型。
TensorFlow 2:使用 Keras 创建自定义 DNN 模型
在 TensorFlow 2 中,可以创建一个自定义 DNN 模型来替代由 tf.estimator.DNNEstimator
生成的模型,此模型具有类似级别的用户指定自定义(例如,与前面的示例一样,能够自定义选定的模型优化器)。
可以使用类似的工作流将 tf.estimator.experimental.RNNEstimator
替换为 Keras RNN 模型。Keras 通过 tf.keras.layers.RNN
、tf.keras.layers.LSTM
和 tf.keras.layers.GRU
提供了许多内置的可自定义选项。要了解详情,请查看 使用 Keras 的 RNN 指南的内置 RNN 层:简单示例部分。
示例 3:从 DNNLinearCombinedEstimator 迁移
TensorFlow 1:使用 DNNLinearCombinedEstimator
在 TensorFlow 1 中,可以使用 tf.estimator.DNNLinearCombinedEstimator
为回归和分类问题创建基线组合模型,并为其线性和 DNN 组件提供自定义能力。
TensorFlow 2:使用 Keras WideDeepModel
在 TensorFlow 2 中,可以创建 Keras tf.compat.v1.keras.models.WideDeepModel
的一个实例来替代由 tf.estimator.DNNLinearCombinedEstimator
生成的实例,此实例具有类似级别的用户指定自定义(例如,与前面的示例一样,能够自定义选定的模型优化器)。
此 WideDeepModel
是在 LinearModel
组件和自定义 DNN 模型的基础上构造的,这两者均已在前面的两个示例中进行了探讨。如果需要,也可以使用自定义线性模型来替代内置的 Keras LinearModel
。
如果您想构建自己的模型而不是预设 Estimator,请查看 Keras 序贯模型指南。有关自定义训练和优化器的更多信息,请参阅自定义训练:演示指南。
示例 4:从 BoostedTreesEstimator 迁移
TensorFlow 1:使用 BoostedTreesEstimator
在 TensorFlow 1 中,可以使用 tf.estimator.BoostedTreesEstimator
创建基线,以使用用于回归和分类问题的决策树集合创建一个基线梯度提升模型。TensorFlow 2 中不再包含此功能。
TensorFlow 2:使用 TensorFlow Decision Forests
在 TensorFlow 2 中,tf.estimator.BoostedTreesEstimator
被 TensorFlow Decision Forests 软件包中的 tfdf.keras.GradientBoostedTreesModel 所替代。
与 tf.estimator.BoostedTreesEstimator
相比,TensorFlow Decision Forests 具备多项优势,尤其是在质量、速度、易用性和灵活性方面。要了解 TensorFlow Decision Forests,请从初学者 colab 开始。
以下示例显示了如何使用 TensorFlow 2 训练梯度提升树模型:
安装 TensorFlow Decision Forests。
创建一个 TensorFlow 数据集。请注意,Decision Forests 原生支持多种类型的特征,不需要预处理。
在 train_dataset
数据集上训练模型。
在 eval_dataset
数据集上评估模型的质量。
梯度提升树只是 TensorFlow Decision Forests 中可用的众多决策森林算法之一。例如,随机森林(以 tfdf.keras.GradientBoostedTreesModel 的形式提供,非常抗过拟合),而 CART(以 tfdf.keras.CartModel 的形式提供)则非常适合模型解释。
在下一个示例中,训练并绘制一个随机森林模型。
在最后一个示例中,训练和评估一个 CART 模型。