為多目標學習創建Head
。
繼承自:Head
用法
tf.estimator.MultiHead(
heads, head_weights=None
)
參數
-
heads
Head
實例的列表或元組。所有磁頭都必須指定name
。列表中的第一個頭部是服務時使用的默認值。 -
head_weights
可選的權重列表,長度與heads
相同。在合並損失時使用,以計算每個頭的損失的加權總和。如果None
,則所有損失的權重均等。
屬性
-
logits_dimension
有關詳細信息,請參閱base_head.Head
。 -
loss_reduction
有關詳細信息,請參閱base_head.Head
。 -
name
有關詳細信息,請參閱base_head.Head
。
此類合並多個Head
對象的輸出。具體來說:
- 對於訓練,將每個頭部的損失相加,調用
train_op_fn
並使用最終損失。 - 對於 eval,通過將
head.name
後綴添加到 eval 指標中的鍵(例如precision/head1.name
,precision/head2.name
)來合並指標。 - 對於預測,將預測字典中的預測和更新鍵合並為 2 元組
(head.name, prediction_key)
。合並export_outputs
以便默認情況下提供第一個頭部。
用法:
head1 = tf.estimator.MultiLabelHead(n_classes=2, name='head1')
head2 = tf.estimator.MultiLabelHead(n_classes=3, name='head2')
multi_head = tf.estimator.MultiHead([head1, head2])
logits = {
'head1':np.array([[-10., 10.], [-15., 10.]], dtype=np.float32),
'head2':np.array([[20., -20., 20.], [-30., 20., -20.]],
dtype=np.float32),}
labels = {
'head1':np.array([[1, 0], [1, 1]], dtype=np.int64),
'head2':np.array([[0, 1, 0], [1, 1, 0]], dtype=np.int64),}
features = {'x':np.array(((42,),), dtype=np.float32)}
# For large logits, sigmoid cross entropy loss is approximated as:
# loss = labels * (logits < 0) * (-logits) +
# (1 - labels) * (logits > 0) * logits =>
# head1:expected_unweighted_loss = [[10., 10.], [15., 0.]]
# loss1 = ((10 + 10) / 2 + (15 + 0) / 2) / 2 = 8.75
# head2:expected_unweighted_loss = [[20., 20., 20.], [30., 0., 0]]
# loss2 = ((20 + 20 + 20) / 3 + (30 + 0 + 0) / 3) / 2 = 15.00
# loss = loss1 + loss2 = 8.75 + 15.00 = 23.75
loss = multi_head.loss(labels, logits, features=features)
print('{:.2f}'.format(loss.numpy()))
23.75
eval_metrics = multi_head.metrics()
updated_metrics = multi_head.update_metrics(
eval_metrics, features, logits, labels)
for k in sorted(updated_metrics):
print('{}:{:.2f}'.format(k, updated_metrics[k].result().numpy()))
auc/head1:0.17
auc/head2:0.33
auc_precision_recall/head1:0.60
auc_precision_recall/head2:0.40
average_loss/head1:8.75
average_loss/head2:15.00
loss/head1:8.75
loss/head2:15.00
preds = multi_head.predictions(logits)
print(preds[('head1', 'logits')])
tf.Tensor(
[[-10. 10.]
[-15. 10.]], shape=(2, 2), dtype=float32)
與罐裝估算器一起使用:
# In `input_fn`, specify labels as a dict keyed by head name:
def input_fn():
features = ...
labels1 = ...
labels2 = ...
return features, {'head1.name':labels1, 'head2.name':labels2}
# In `model_fn`, specify logits as a dict keyed by head name:
def model_fn(features, labels, mode):
# Create simple heads and specify head name.
head1 = tf.estimator.MultiClassHead(n_classes=3, name='head1')
head2 = tf.estimator.BinaryClassHead(name='head2')
# Create MultiHead from two simple heads.
head = tf.estimator.MultiHead([head1, head2])
# Create logits for each head, and combine them into a dict.
logits1, logits2 = logit_fn()
logits = {'head1.name':logits1, 'head2.name':logits2}
# Return the merged EstimatorSpec
return head.create_estimator_spec(..., logits=logits, ...)
# Create an estimator with this model_fn.
estimator = tf.estimator.Estimator(model_fn=model_fn)
estimator.train(input_fn=input_fn)
還支持 logits
作為形狀為 [D0, D1, ... DN, logits_dimension]
的 Tensor
。它將沿最後一個維度拆分Tensor
,並將其適當地分布在頭部之間。例如:
# Input logits.
logits = np.array([[-1., 1., 2., -2., 2.], [-1.5, 1., -3., 2., -2.]],
dtype=np.float32)
# Suppose head1 and head2 have the following logits dimension.
head1.logits_dimension = 2
head2.logits_dimension = 3
# After splitting, the result will be:
logits_dict = {'head1_name':[[-1., 1.], [-1.5, 1.]],
'head2_name': [[2., -2., 2.], [-3., 2., -2.]]}
用法:
def model_fn(features, labels, mode):
# Create simple heads and specify head name.
head1 = tf.estimator.MultiClassHead(n_classes=3, name='head1')
head2 = tf.estimator.BinaryClassHead(name='head2')
# Create multi-head from two simple heads.
head = tf.estimator.MultiHead([head1, head2])
# Create logits for the multihead. The result of logits is a `Tensor`.
logits = logit_fn(logits_dimension=head.logits_dimension)
# Return the merged EstimatorSpec
return head.create_estimator_spec(..., logits=logits, ...)
相關用法
- Python tf.estimator.MultiLabelHead用法及代碼示例
- Python tf.estimator.MultiClassHead用法及代碼示例
- Python tf.estimator.TrainSpec用法及代碼示例
- Python tf.estimator.LogisticRegressionHead用法及代碼示例
- Python tf.estimator.PoissonRegressionHead用法及代碼示例
- Python tf.estimator.WarmStartSettings用法及代碼示例
- Python tf.estimator.experimental.stop_if_lower_hook用法及代碼示例
- Python tf.estimator.RunConfig用法及代碼示例
- Python tf.estimator.experimental.stop_if_no_increase_hook用法及代碼示例
- Python tf.estimator.BaselineEstimator用法及代碼示例
- Python tf.estimator.DNNLinearCombinedEstimator用法及代碼示例
- Python tf.estimator.Estimator用法及代碼示例
- Python tf.estimator.experimental.LinearSDCA用法及代碼示例
- Python tf.estimator.experimental.RNNClassifier用法及代碼示例
- Python tf.estimator.experimental.make_early_stopping_hook用法及代碼示例
- Python tf.estimator.LinearRegressor用法及代碼示例
- Python tf.estimator.LinearEstimator用法及代碼示例
- Python tf.estimator.DNNClassifier用法及代碼示例
- Python tf.estimator.BaselineClassifier用法及代碼示例
- Python tf.estimator.experimental.stop_if_higher_hook用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.estimator.MultiHead。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。