【问题标题】:costum loss : check max and min values of variable of type <class 'tensorflow.python.framework.ops.Tensor'成本损失:检查 <class \'tensorflow.python.framework.ops.Tensor\' 类型变量的最大值和最小值
【发布时间】:2022-10-25 21:35:44
【问题描述】:

我在张量流中写了一个成本损失函数。但是某事出错了,因为错误大于 1,这是不可能的。所以我想查找 y_predicted 和 y_true 的值以及新的计算损失。但我不知道如何获得这些值。 y_pred 和 y_true 都是 &lt;class 'tensorflow.python.framework.ops.Tensor' 类型

这是我的自定义损失函数:

def square_loss_invalid_pixel(y_true,y_pred):
   print("type ",type(y_true),type(y_pred))
   print("y_true: ", y_true)
   print("max:_ ",(tf.reduce_max(y_true)))
   print("min:_ ",(tf.reduce_min(y_pred)))

   loss = square_loss(y_true, y_pred) # (b, h, w)

   print("max:_ ",(tf.reduce_max(loss)))
   print("min:_ ",(tf.reduce_min(loss)))

   loss = tf.reduce_sum(loss)
   return loss

输出是:

y_true:  Tensor("IteratorGetNext:2", shape=(None, None, None, None), dtype=float32)
max:_  Tensor("square_loss_invalid_pixel/Max:0", shape=(), dtype=float32)
min:_  Tensor("square_loss_invalid_pixel/Min:0", shape=(), dtype=float32)
max:_  Tensor("square_loss_invalid_pixel/Max_1:0", shape=(), dtype=float32)
min:_  Tensor("square_loss_invalid_pixel/Min_1:0", shape=(), dtype=float32)

如您所见,我没有从中获得太多信息。你知道如何获得正确的价值观吗? 我有批量大小 8 。我从生成器 fct 获得输入。

提前致谢! 此致

【问题讨论】:

  • 尝试使用tf.print 而不是print

标签: python tensorflow loss-function


【解决方案1】:

我通过使用 run_eagerly=True 编译模型解决了我的问题:

model.compile(optimizer=opt, loss=square_loss_costum, run_eagerly=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-10
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 2017-07-09
    • 2020-10-03
    • 2021-07-06
    相关资源
    最近更新 更多