可以建立简单基线的分类器。
警告:不建议将估算器用于新代码。估算器运行tf.compat.v1.Session-style 代码更难正确编写,并且可能出现意外行为,尤其是与 TF 2 代码结合使用时。估算器确实属于我们的兼容性保证,但不会收到除安全漏洞以外的任何修复。见迁移指南详情。
继承自:Estimator
用法
tf.compat.v1.estimator.BaselineClassifier(
model_dir=None, n_classes=2, weight_column=None, label_vocabulary=None,
optimizer='Ftrl', config=None,
loss_reduction=tf.compat.v1.losses.Reduction.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
该分类器忽略特征值,将学习预测每个标签的平均值。对于single-label 问题,这将预测标签中看到的类的概率分布。对于多标签问题,这将预测每个类别中正面示例的比例。
例子:
# Build BaselineClassifier
classifier = tf.estimator.BaselineClassifier(n_classes=3)
# 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
# Fit model.
classifier.train(input_fn=input_fn_train)
# Evaluate cross entropy between the test and train labels.
loss = classifier.evaluate(input_fn=input_fn_eval)["loss"]
# predict outputs the probability distribution of the classes as seen in
# training.
predictions = classifier.predict(new_samples)
train 和 evaluate 的输入应具有以下特征,否则会出现 KeyError :
- 如果
weight_column不是None,则具有key=weight_column的特征,其值为Tensor。
相关用法
- Python tf.compat.v1.estimator.BaselineRegressor用法及代码示例
- Python tf.compat.v1.estimator.BaselineEstimator用法及代码示例
- 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.inputs.numpy_input_fn用法及代码示例
- Python tf.compat.v1.estimator.LinearRegressor用法及代码示例
- Python tf.compat.v1.estimator.LinearClassifier用法及代码示例
- 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.LinearEstimator用法及代码示例
- 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.BaselineClassifier。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
