我正在编写自定义损失函数,我想取1-计算值的对数。我正在使用具有tensorflow后端的keras编写如下:
nonlabels = list(np.where(predictions<threshold)[1])
loss_fg_nT = 0
probs_nT = tf.gather(probs_fg,nonlabels,axis=3)
for i in range(len(nonlabels)):
probs_temp = tf.reshape(probs_nT[:,:,i],[-1])
prob_max = tf.math.reduce_max(probs_temp)
const = tf.constant(0.000001)
prob_max = tf.math.add(prob_max,const)
#prob_max = tf.math.subtract(tf.constant(1.0),prob_max)
val = K.log(prob_max)
loss_fg_nT -= val
loss_fg_nT = loss_fg_nT/(len(nonlabels)+0.000001)
如果我删除了prob_max = tf.math.subtract(tf.constant(1.0),prob_max)
行,则该功能运行正常,但是当我记录(1-prob_max)值时,它将给出nan
值。我无法理解其背后的原因,因为我要添加标称值以避免同时使用log 0
。有人可以帮助我纠正此错误。