输入等级为4时使用哪个tensorflow批量规范代码?

时间:2017-06-28 17:44:30

标签: python tensorflow batch-normalization

我正在使用layers中的slim.batch_norm并尝试了解我的用例中的代码流。在我看来,决定是否使用_fused_batch_norm()的逻辑或基类只会在我的情况下使用_fused_batch_norm(),如果输入等级为2.代码描述听起来也应该使用如果rank是4并且函数本身(_fused_batch_norm())支持4的等级,但逻辑似乎阻止调用它。下面是代码片段,显示了我所指的内容:

  # Only use _fused_batch_norm (1) if fused is set True or if it is
  # possible to use (currently it doesn't support batch weights,
  # renorm, and the case when rank is neither 2 nor 4),
  # and (2) if used with zero_debias_moving_mean, or an input shape of rank 2,
  # or non-default updates_collections (not implemented in
  # normalization_layers.BatchNormalization yet); otherwise use the fused
  # implementation in normalization_layers.BatchNormalization.
  inputs = ops.convert_to_tensor(inputs)
  rank = inputs.get_shape().ndims
  feature_supported = batch_weights is None and not renorm and rank in [2, 4]
  possible_to_fuse = fused is None and feature_supported
  if (fused or possible_to_fuse) and (
      zero_debias_moving_mean or rank == 2 or
      updates_collections is not ops.GraphKeys.UPDATE_OPS):
      return _fused_batch_norm(...)

对于我的用例,我在默认设置下都有以下参数:

batch_weights=None
fused=False
renorm=False
zero_debias_moving_mean=False
updates_collections=ops.GraphKeys.UPDATE_OPS

如果我的输入是第4级,看起来代码将使用normalization_layers.BatchNormalization中的融合实现我对逻辑的理解是否正确?

这是预期和正确的行为吗?我想知道条件rank==2是否应该是rank in [2,4]?如果后者是正确的,那么这将是一个潜在的错误。如果原件正确,那么为什么要rank in [2,4]来确定feature_supported

1 个答案:

答案 0 :(得分:1)

你是对的,这是一个错误。在rank=4fused=None(或True)时,可以并且应该使用优化的_fused_batch_norm。这与tf.nn.fused_batch_norm一致。

看起来他们混淆了逻辑表达式,无论其他一切是什么,都应该触发possible_to_fuse=True。此外,如果feature_supported=True and not fused=False_fused_batch_norm也符合条件。

您应该将其报告给tensorflow issue tracker