通过使用下面的损失函数,我试图训练一个模型,但在训练过程中,我得到了下面的错误


from keras import backend as K
import numpy as np

def Active_Contour_Loss(y_true, y_pred): 
"""
lenth term
"""
x = y_pred[:,:,1:,:] - y_pred[:,:,:-1,:] # horizontal and vertical directions 
y = y_pred[:,:,:,1:] - y_pred[:,:,:,:-1]
delta_x = x[:,:,1:,:-2]**2
delta_y = y[:,:,:-2,1:]**2
delta_u = K.abs(delta_x + delta_y) 
lenth = K.mean(K.sqrt(delta_u + 0.00000001)) # equ.(11) in the paper
"""
region term
"""
C_1 = np.ones((256, 256))
C_2 = np.zeros((256, 256))
region_in = K.abs(K.mean( y_pred[:,0,:,:] * ((y_true[:,0,:,:] - C_1)**2) ) ) # equ.(12) in the paper
region_out = K.abs(K.mean( (1-y_pred[:,0,:,:]) * ((y_true[:,0,:,:] - C_2)**2) )) # equ.(12) in the paper
lambdaP = 1 # lambda parameter could be various.
mu = 1 # mu parameter could be various.

return lenth + lambdaP * (mu * region_in + region_out)
Epoch 1/10
107/112 [===========================>..] - ETA: 3s - loss: nan - accuracy: 0.9701
---------------------------------------------------------------------------

这篇文章缺少信息来给出一个完整的答案,我只能给出一系列的建议来解决问题:

  1. 损失在nan意味着至少有一个输入网络无法计算损失值(这是因为keras在loss: nan显示的数字是所有损失的平均值),为了研究这一点,更好地观察训练并观察它是否总是nan或在特定样本后变为nan

  2. nan的损失意味着您的损失函数不起作用,要了解哪些部分不能很好地工作,请简化您的损失(因此不是返回lenth + lambdaP * (mu * region_in + region_out),而是尝试仅返回lenth,并查看输出是否再次nan(如果问题是在lenth计算上))使用此方法,您可以缩小代码的哪一部分不起作用

最新更新