用于 TensorFlow 线性和 DNN 的估计器加入了带有自定义头部的模型。
警告:不建议将估算器用于新代码。估算器运行tf.compat.v1.Session-style 代码更难正确编写,并且可能出现意外行为,尤其是与 TF 2 代码结合使用时。估算器确实属于我们的兼容性保证,但不会收到除安全漏洞以外的任何修复。见迁移指南详情。
继承自:Estimator
用法
tf.compat.v1.estimator.DNNLinearCombinedEstimator(
head, model_dir=None, linear_feature_columns=None,
linear_optimizer='Ftrl', dnn_feature_columns=None,
dnn_optimizer='Adagrad', dnn_hidden_units=None,
dnn_activation_fn=tf.nn.relu, dnn_dropout=None, input_layer_partitioner=None,
config=None, batch_norm=False, linear_sparse_combiner='sum'
)参数
-
model_fn模型函数。遵循签名:features-- 这是从input_fn返回的第一项,传递给train,evaluate和predict。这应该是一个相同的tf.Tensor或dict。labels-- 这是从传递给train,evaluate和predict的input_fn返回的第二项。这应该是相同的单个tf.Tensor或dict(对于multi-head 型号)。如果 mode 是tf.estimator.ModeKeys.PREDICT,labels=None将被传递。如果model_fn的签名不接受mode,则model_fn必须仍然能够处理labels=None。mode-- 可选。指定这是训练、评估还是预测。见tf.estimator.ModeKeys。params-- 可选的dict超参数。将接收params参数中传递给 Estimator 的内容。这允许通过超参数调整来配置 Estimator。config-- 可选的estimator.RunConfig对象。将接收作为其config参数或默认值传递给 Estimator 的内容。允许根据num_ps_replicas或model_dir等配置在model_fn中进行设置。- 返回 --
tf.estimator.EstimatorSpec
-
model_dir保存模型参数、图形等的目录。这也可用于将检查点从目录加载到估计器中,以继续训练先前保存的模型。如果PathLike对象,路径将被解析。如果None,如果设置,将使用config中的 model_dir。如果两者都设置,则它们必须相同。如果两者都是None,将使用临时目录。 -
configestimator.RunConfig配置对象。 -
paramsdict的超参数将被传递到model_fn。键是参数的名称,值是基本的 Python 类型。 -
warm_start_from检查点或 SavedModel 的可选字符串文件路径以进行热启动,或tf.estimator.WarmStartSettings对象以完全配置热启动。如果没有,只有 TRAINABLE 变量是热启动的。如果提供了字符串文件路径而不是tf.estimator.WarmStartSettings,则所有变量都是热启动的,并且假定词汇表和tf.Tensor名称不变。
抛出
-
ValueErrormodel_fn的参数与params不匹配。 -
ValueError如果这是通过子类调用的,并且该类覆盖了Estimator的成员。
属性
-
config -
model_dir -
model_fn返回绑定到self.params的model_fn。 -
params
注意:此估计器也称为wide-n-deep。
例子:
numeric_feature = numeric_column(...)
categorical_column_a = categorical_column_with_hash_bucket(...)
categorical_column_b = categorical_column_with_hash_bucket(...)
categorical_feature_a_x_categorical_feature_b = crossed_column(...)
categorical_feature_a_emb = embedding_column(
categorical_column=categorical_feature_a, ...)
categorical_feature_b_emb = embedding_column(
categorical_column=categorical_feature_b, ...)
estimator = tf.estimator.DNNLinearCombinedEstimator(
head=tf.estimator.MultiLabelHead(n_classes=3),
# wide settings
linear_feature_columns=[categorical_feature_a_x_categorical_feature_b],
linear_optimizer=tf.keras.optimizers.Ftrl(...),
# deep settings
dnn_feature_columns=[
categorical_feature_a_emb, categorical_feature_b_emb,
numeric_feature],
dnn_hidden_units=[1000, 500, 100],
dnn_optimizer=tf.keras.optimizers.Adagrad(...))
# To apply L1 and L2 regularization, you can set dnn_optimizer to:
tf.compat.v1.train.ProximalAdagradOptimizer(
learning_rate=0.1,
l1_regularization_strength=0.001,
l2_regularization_strength=0.001)
# To apply learning rate decay, you can set dnn_optimizer to a callable:
lambda:tf.keras.optimizers.Adam(
learning_rate=tf.compat.v1.train.exponential_decay(
learning_rate=0.1,
global_step=tf.compat.v1.train.get_global_step(),
decay_steps=10000,
decay_rate=0.96)
# It is the same for linear_optimizer.
# Input builders
def input_fn_train:
# Returns tf.data.Dataset of (x, y) tuple where y represents label's class
# index.
pass
def input_fn_eval:
# Returns tf.data.Dataset of (x, y) tuple where y represents label's class
# index.
pass
def input_fn_predict:
# Returns tf.data.Dataset of (x, None) tuple.
pass
estimator.train(input_fn=input_fn_train, steps=100)
metrics = estimator.evaluate(input_fn=input_fn_eval, steps=10)
predictions = estimator.predict(input_fn=input_fn_predict)
train 和 evaluate 的输入应具有以下特征,否则会出现 KeyError :
- 对于每个
column在dnn_feature_columns+linear_feature_columns:- 如果
column是CategoricalColumn,则具有key=column.name的特征,其value是SparseTensor。 - 如果
column是WeightedCategoricalColumn,则有两个特征:第一个具有keyid 列名,第二个具有key权重列名。两个函数的value必须是SparseTensor。 - 如果
column是DenseColumn,则具有key=column.name的特征,其value是Tensor。
- 如果
损失是使用均方误差计算的。
相关用法
- Python tf.compat.v1.estimator.DNNLinearCombinedRegressor用法及代码示例
- Python tf.compat.v1.estimator.DNNLinearCombinedClassifier用法及代码示例
- Python tf.compat.v1.estimator.DNNEstimator用法及代码示例
- Python tf.compat.v1.estimator.DNNClassifier用法及代码示例
- Python tf.compat.v1.estimator.DNNRegressor用法及代码示例
- Python tf.compat.v1.estimator.experimental.KMeans用法及代码示例
- Python tf.compat.v1.estimator.tpu.RunConfig用法及代码示例
- Python tf.compat.v1.estimator.regressor_parse_example_spec用法及代码示例
- Python tf.compat.v1.estimator.BaselineRegressor用法及代码示例
- Python tf.compat.v1.estimator.inputs.numpy_input_fn用法及代码示例
- Python tf.compat.v1.estimator.LinearRegressor用法及代码示例
- Python tf.compat.v1.estimator.BaselineEstimator用法及代码示例
- Python tf.compat.v1.estimator.BaselineClassifier用法及代码示例
- Python tf.compat.v1.estimator.LinearClassifier用法及代码示例
- Python tf.compat.v1.estimator.tpu.TPUEstimator用法及代码示例
- Python tf.compat.v1.estimator.tpu.experimental.EmbeddingConfigSpec用法及代码示例
- Python tf.compat.v1.estimator.Estimator用法及代码示例
- Python tf.compat.v1.estimator.LinearEstimator用法及代码示例
- Python tf.compat.v1.estimator.classifier_parse_example_spec用法及代码示例
- Python tf.compat.v1.enable_eager_execution用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.compat.v1.estimator.DNNLinearCombinedEstimator。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
