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


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


为多目标学习创建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, ...)

相关用法


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