嗨,我一直在嘗試為dice_error_coefficient在keras中創建自定義損失函數。它有它的實現tensorboard,我嘗試使用相同的功能與tensorflow keras但它一直返回NoneType當我用model.train_on_batch或model.fit其中在模型中的指標使用時,它提供正確的價值觀??梢哉垊e人幫我做什么嗎?我嘗試過使用ahundt編寫的Keras-FCN之類的庫,其中他使用了自定義損失函數,但似乎都不起作用。代碼中的目標和輸出分別為y_true和y_pred,如keras中的loss.py文件中所使用。def dice_hard_coe(target, output, threshold=0.5, axis=[1,2], smooth=1e-5): """References ----------- - `Wiki-Dice <https://en.wikipedia.org/wiki/S?rensen–Dice_coefficient>`_ """ output = tf.cast(output > threshold, dtype=tf.float32) target = tf.cast(target > threshold, dtype=tf.float32) inse = tf.reduce_sum(tf.multiply(output, target), axis=axis) l = tf.reduce_sum(output, axis=axis) r = tf.reduce_sum(target, axis=axis) hard_dice = (2. * inse + smooth) / (l + r + smooth) hard_dice = tf.reduce_mean(hard_dice) return hard_dice
2 回答

鳳凰求蠱
TA貢獻1825條經驗 獲得超4個贊
'def dice(y_true,y_pred):return -dice_coef(y_true,y_pred,1e-5,0.5)def dice_coef(y_true,y_pred,smooth,thresh):y_pred = K.cast(y_pred> thresh,thresh,dtype = tf.float32 )y_true = K.cast(y_true> thresh,dtype = tf.float32)y_true_f = K.flatten(y_true)y_pred_f = K.flatten(y_pred)交集= K.sum(y_true_f * y_pred_f)返回(2. *交集+平滑)/(K.sum(y_true_f)+ K.sum(y_pred_f)+平滑)Final_Model.compile(optimizer = opt,loss = dice,metrics = ['acc'])'這給了我一個錯誤'試圖轉換'x'到張量并失敗。錯誤:不支持任何值。
添加回答
舉報
0/150
提交
取消