【问题标题】:Getting "NameError: name 'Reduction' is not defined" error in Tensorflow 1.13在 Tensorflow 1.13 中出现“NameError: name 'Reduction' is not defined”错误
【发布时间】:2019-05-08 07:24:29
【问题描述】:

我正在尝试使用 def 'tf.losses.mean_squared_error'。但我得到了

NameError: name 'Reduction' is not defined

我使用的是 TF 1.13。 这是一些导入问题还是 tf.losses.mean_squared_error 函数有任何更新?

我的代码如下。

def reg_loss(pred, GT):
    loss = tf.losses.mean_squared_error(
    labels = GT,
    predictions = pred,
    weights=1.0,
    scope=None,
    loss_collection=tf.GraphKeys.LOSSES,
    reduction=Reduction.SUM_BY_NONZERO_WEIGHTS)

    return reg_loss

谁能帮我解决这个问题?

【问题讨论】:

  • 你应该包括完整的回溯,只是最后的错误没有说太多。
  • 这个问题你解决了吗?
  • @AlbinDavid 抱歉,还没有
  • @Ashj 添加了对我有用的答案。

标签: python tensorflow loss-function reduction mean-square-error


【解决方案1】:

使用tf.losses.Reduction.SUM_BY_NONZERO_WEIGHTS 这解决了我的问题。

【讨论】:

    【解决方案2】:

    完整的类路径应该是这个documentation

    tf.compat.v1.losses.Reduction
    

    所以解决办法可以是

    import tensorflow as tf # optional alias
    from tf.compat.v1.losses import Reduction
    

    【讨论】:

      猜你喜欢
      • 2015-11-11
      • 1970-01-01
      • 2020-11-07
      • 2022-11-27
      相关资源
      最近更新 更多