TensorFlow 线性回归问题的估计器。
警告:不建议将估算器用于新代码。估算器运行tf.compat.v1.Session-style 代码更难正确编写,并且可能出现意外行为,尤其是与 TF 2 代码结合使用时。估算器确实属于我们的兼容性保证,但不会收到除安全漏洞以外的任何修复。见迁移指南详情。
继承自:Estimator
用法
tf.compat.v1.estimator.LinearRegressor(
feature_columns, model_dir=None, label_dimension=1, weight_column=None,
optimizer='Ftrl', config=None, partitioner=None, warm_start_from=None,
loss_reduction=tf.compat.v1.losses.Reduction.SUM,
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
训练线性回归模型以在给定特征值观察的情况下预测标签值。
例子:
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(...)
# Estimator using the default optimizer.
estimator = tf.estimator.LinearRegressor(
feature_columns=[categorical_column_a,
categorical_feature_a_x_categorical_feature_b])
# Or estimator using the FTRL optimizer with regularization.
estimator = tf.estimator.LinearRegressor(
feature_columns=[categorical_column_a,
categorical_feature_a_x_categorical_feature_b],
optimizer=tf.keras.optimizers.Ftrl(
learning_rate=0.1,
l1_regularization_strength=0.001
))
# Or estimator using an optimizer with a learning rate decay.
estimator = tf.estimator.LinearRegressor(
feature_columns=[categorical_column_a,
categorical_feature_a_x_categorical_feature_b],
optimizer=lambda:tf.keras.optimizers.Ftrl(
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))
# Or estimator with warm-starting from a previous checkpoint.
estimator = tf.estimator.LinearRegressor(
feature_columns=[categorical_column_a,
categorical_feature_a_x_categorical_feature_b],
warm_start_from="/path/to/checkpoint/dir")
# 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)
metrics = estimator.evaluate(input_fn=input_fn_eval)
predictions = estimator.predict(input_fn=input_fn_predict)
train和evaluate的输入应具有以下特点,否则会出现KeyError:
- 如果
weight_column不是None,则具有key=weight_column的特征,其值为Tensor。 - 对于每个
column在feature_columns:- 如果
column是SparseColumn,则具有key=column.name的特征,其value是SparseTensor。 - 如果
column是WeightedSparseColumn,则有两个特征:第一个具有keyid 列名,第二个具有key权重列名。两个函数的value必须是SparseTensor。 - 如果
column是RealValuedColumn,则具有key=column.name的特征,其value是Tensor。
- 如果
损失是使用均方误差计算的。
相关用法
- Python tf.compat.v1.estimator.LinearClassifier用法及代码示例
- Python tf.compat.v1.estimator.LinearEstimator用法及代码示例
- Python tf.compat.v1.estimator.DNNEstimator用法及代码示例
- 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.BaselineEstimator用法及代码示例
- Python tf.compat.v1.estimator.BaselineClassifier用法及代码示例
- Python tf.compat.v1.estimator.DNNLinearCombinedRegressor用法及代码示例
- Python tf.compat.v1.estimator.tpu.TPUEstimator用法及代码示例
- Python tf.compat.v1.estimator.DNNClassifier用法及代码示例
- Python tf.compat.v1.estimator.DNNRegressor用法及代码示例
- Python tf.compat.v1.estimator.tpu.experimental.EmbeddingConfigSpec用法及代码示例
- Python tf.compat.v1.estimator.DNNLinearCombinedEstimator用法及代码示例
- Python tf.compat.v1.estimator.Estimator用法及代码示例
- Python tf.compat.v1.estimator.classifier_parse_example_spec用法及代码示例
- Python tf.compat.v1.estimator.DNNLinearCombinedClassifier用法及代码示例
- Python tf.compat.v1.enable_eager_execution用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.compat.v1.estimator.LinearRegressor。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
