亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在keras中創建自定義損失函數

在keras中創建自定義損失函數

一只名叫tom的貓 2019-09-24 14:59:38
嗨,我一直在嘗試為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'到張量并失敗。錯誤:不支持任何值。

查看完整回答
反對 回復 2019-09-24
  • 2 回答
  • 0 關注
  • 1863 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號