【问题标题】:Custom Loss Function in TF2.0TF2.0 中的自定义损失函数
【发布时间】:2020-02-28 07:50:21
【问题描述】:

对于一个图像分割问题,我需要编写一个自定义的损失函数。我遇到了上述错误。

代码库:https://www.tensorflow.org/tutorials/images/segmentation

最后一层: Conv2DTrans (128,128,2) [请注意,在我的情况下它只有 2 个值]

       def call(self, y_true, y_pred):
              ytrue = ytrue.numpy()
              .....

错误:

AttributeError: 'Tensor' object has no attribute 'numpy'

我尝试了 py_function 和 numpy_function 但都返回相同的错误 还有

      with tf.compat.v1.Session() as sess:
        for i,j in enumerate(sess.run(y_true),sess.run(y_pred)):
Current Model Layers:


Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_82 (InputLayer)           [(None, 128, 128, 3) 0                                            
__________________________________________________________________________________________________
model_80 (Model)                [(None, 64, 64, 96), 1841984     input_82[0][0]                   
__________________________________________________________________________________________________
sequential_160 (Sequential)     (None, 8, 8, 512)    1476608     model_80[1][4]                   
__________________________________________________________________________________________________
concatenate_160 (Concatenate)   (None, 8, 8, 1088)   0           sequential_160[0][0]             
                                                                 model_80[1][3]                   
__________________________________________________________________________________________________
sequential_161 (Sequential)     (None, 16, 16, 256)  2507776     concatenate_160[0][0]            
__________________________________________________________________________________________________
concatenate_161 (Concatenate)   (None, 16, 16, 448)  0           sequential_161[0][0]             
                                                                 model_80[1][2]                   
__________________________________________________________________________________________________
sequential_162 (Sequential)     (None, 32, 32, 128)  516608      concatenate_161[0][0]            
__________________________________________________________________________________________________
concatenate_162 (Concatenate)   (None, 32, 32, 272)  0           sequential_162[0][0]             
                                                                 model_80[1][1]                   
__________________________________________________________________________________________________
sequential_163 (Sequential)     (None, 64, 64, 64)   156928      concatenate_162[0][0]            
__________________________________________________________________________________________________
concatenate_163 (Concatenate)   (None, 64, 64, 160)  0           sequential_163[0][0]             
                                                                 model_80[1][0]                   
__________________________________________________________________________________________________
conv2d_transpose_204 (Conv2DTra (None, 128, 128, 2)  2882        concatenate_163[0][0]            
==================================================================================================

我需要一个 numpy 数组来只关注 1 而不是零。现在,大量零的存在使度量和准确性不堪重负。

def tumor_loss(y_true,y_pred):
  y_true = y_true.reshape((SHAPE,SHAPE))
  y_pred = y_pred.reshape((SHAPE,SHAPE))

  y_true_ind = np.where(y_true ==1)[1]
  y_pred_ind = np.where(y_pred==1)[1]

  if np.array_equal(y_true_ind,y_pred_ind):
    return 0

  if y_true_ind.shape[0] > y_pred_ind.shape[0]:
    return y_true_ind.shape[0] - np.setdiff1d(y_true_ind,y_pred_ind).shape[0]
  else:
    return y_true_ind.shape[0] - np.setdiff1d(y_pred_ind,y_true_ind).shape[0]

【问题讨论】:

  • 您是在尝试编写自定义损失还是自定义指标? stackoverflow.com/a/48281534/8342910 损失函数需要是可微的,而你的函数不是。
  • @seed 我这里的损失表达式类似于平均绝对误差 (MAE)。平均值当然是我最后必须计算的。如果不是这样,请纠正我。我在处理 y_pred 和 y_true 时遇到了困难!!!!
  • MAE 在除零之外的任何地方都是可微的。您的损失函数仅采用整数值。它的导数要么为零,要么未定义。
  • 很有趣...我深入了解了损失函数...但是,这是我面临的编程挑战...请在这方面帮助我

标签: python-3.x tensorflow keras


【解决方案1】:

如果您在 tf 版本 >= 2.0 上运行,请尝试使用

model.compile(loss=custom_loss, optimizer='adam', run_eagerly=True)

如果您使用的是 Keras api。

【讨论】:

    猜你喜欢
    • 2022-01-08
    • 2019-02-13
    • 2020-12-19
    • 2017-04-04
    • 2017-12-18
    • 2020-03-27
    • 2020-11-16
    • 2019-05-27
    • 2020-02-02
    相关资源
    最近更新 更多