当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python tf.estimator.LinearEstimator用法及代码示例


具有用户指定头部的 TensorFlow 线性模型的估计器。

警告:不建议将估算器用于新代码。估算器运行tf.compat.v1.Session-style 代码更难正确编写,并且可能出现意外行为,尤其是与 TF 2 代码结合使用时。估算器确实属于我们的兼容性保证,但不会收到除安全漏洞以外的任何修复。见迁移指南详情。

继承自:EstimatorEstimator

用法

tf.estimator.LinearEstimator(
    head, feature_columns, model_dir=None, optimizer='Ftrl', config=None,
    sparse_combiner='sum', warm_start_from=None
)

参数

  • head 使用 tf.estimator.MultiLabelHead 等方法构造的 Head 实例。
  • feature_columns 一个包含模型使用的所有特征列的迭代。集合中的所有项目都应该是派生自 FeatureColumn 的类的实例。
  • model_dir 保存模型参数、图形等的目录。这也可用于将检查点从目录加载到估计器中,以继续训练先前保存的模型。
  • optimizer tf.keras.optimizers.* 的实例用于训练模型。也可以是字符串('Adagrad'、'Adam'、'Ftrl'、'RMSProp'、'SGD' 之一)或可调用的。默认为 FTRL 优化器。
  • config RunConfig 对象来配置运行时设置。
  • sparse_combiner 如果分类列是多价的,则指定如何减少的字符串。 "mean"、"sqrtn" 和 "sum" 之一——这些是进行 example-level 归一化的有效不同方法,这对于 bag-of-words 函数很有用。有关详细信息,请参阅 tf.feature_column.linear_model
  • warm_start_from 检查点的字符串文件路径以进行热启动,或 WarmStartSettings 对象以完全配置热启动。如果提供了字符串文件路径而不是 WarmStartSettings ,则所有权重和偏差都是热启动的,并且假定词汇表和张量名称未更改。

属性

  • config
  • export_savedmodel
  • model_dir
  • model_fn 返回绑定到 self.paramsmodel_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.LinearEstimator(
    head=tf.estimator.MultiLabelHead(n_classes=3),
    feature_columns=[categorical_column_a,
                     categorical_feature_a_x_categorical_feature_b])

# Or estimator using an optimizer with a learning rate decay.
estimator = tf.estimator.LinearEstimator(
    head=tf.estimator.MultiLabelHead(n_classes=3),
    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 using the FTRL optimizer with regularization.
estimator = tf.estimator.LinearEstimator(
    head=tf.estimator.MultiLabelHead(n_classes=3),
    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
    ))

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)

trainevaluate 的输入应具有以下特征,否则会出现 KeyError

  • 如果 weight_column 不是 None ,则具有 key=weight_column 的特征,其值为 Tensor
  • 对于每个columnfeature_columns
    • 如果 columnCategoricalColumn ,则具有 key=column.name 的特征,其 valueSparseTensor
    • 如果 columnWeightedCategoricalColumn ,则有两个特征:第一个具有 key id 列名,第二个具有 key 权重列名。两个函数的 value 必须是 SparseTensor
    • 如果 columnDenseColumn ,则具有 key=column.name 的特征,其 valueTensor

损失和预测输出由指定的头部确定。

相关用法


注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.estimator.LinearEstimator。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。