可以建立簡單基線的估計器。
警告:不建議將估算器用於新代碼。估算器運行tf.compat.v1.Session-style 代碼更難正確編寫,並且可能出現意外行為,尤其是與 TF 2 代碼結合使用時。估算器確實屬於我們的兼容性保證,但不會收到除安全漏洞以外的任何修複。見遷移指南詳情。
繼承自:Estimator
用法
tf.compat.v1.estimator.BaselineEstimator(
head, model_dir=None, optimizer='Ftrl', config=None
)參數
-
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 baseline multi-label classifier.
estimator = tf.estimator.BaselineEstimator(
head=tf.estimator.MultiLabelHead(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.
estimator.train(input_fn=input_fn_train)
# Evaluates cross entropy between the test and train labels.
loss = estimator.evaluate(input_fn=input_fn_eval)["loss"]
# For each class, predicts the ratio of training examples that contain the
# class.
predictions = estimator.predict(new_samples)
train 和 evaluate 的輸入應具有以下特征,否則會出現 KeyError :
- 如果
weight_column在head構造函數(而不是 None )中為傳遞給 BaselineEstimator 構造函數的頭部指定了key=weight_column,其值為Tensor。
相關用法
- Python tf.compat.v1.estimator.BaselineRegressor用法及代碼示例
- Python tf.compat.v1.estimator.BaselineClassifier用法及代碼示例
- 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.BaselineEstimator。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
