最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于keras多任務(wù)多l(xiāng)oss回傳的思考

 更新時間:2021年05月25日 08:53:00   作者:chestnut--  
這篇文章主要介紹了關(guān)于keras多任務(wù)多l(xiāng)oss回傳的思考,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

如果有一個多任務(wù)多l(xiāng)oss的網(wǎng)絡(luò),那么在訓(xùn)練時,loss是如何工作的呢?

比如下面:

model = Model(inputs = input, outputs = [y1, y2])
l1 = 0.5
l2 = 0.3
model.compile(loss = [loss1, loss2], loss_weights=[l1, l2], ...)

其實我們最終得到的loss為

final_loss = l1 * loss1 + l2 * loss2

我們最終的優(yōu)化效果是最小化final_loss。

問題來了,在訓(xùn)練過程中,是否loss2只更新得到y(tǒng)2的網(wǎng)絡(luò)通路,還是loss2會更新所有的網(wǎng)絡(luò)層呢?

此問題的關(guān)鍵在梯度回傳上,即反向傳播算法。

在這里插入圖片描述

所以loss1只對x1和x2有影響,而loss2只對x1和x3有影響。

補充:keras 多個LOSS總和定義

在這里插入圖片描述

用字典形式,名字是模型中輸出那一層的名字,這里的loss可以是自己定義的,也可是自帶的

補充:keras實戰(zhàn)-多類別分割loss實現(xiàn)

本文樣例均為3d數(shù)據(jù)的onehot標(biāo)簽形式,即y_true(batch_size,x,y,z,class_num)

1、dice loss

def dice_coef_fun(smooth=1):
    def dice_coef(y_true, y_pred):
        #求得每個sample的每個類的dice
        intersection = K.sum(y_true * y_pred, axis=(1,2,3))
        union = K.sum(y_true, axis=(1,2,3)) + K.sum(y_pred, axis=(1,2,3))
        sample_dices=(2. * intersection + smooth) / (union + smooth) #一維數(shù)組 為各個類別的dice
        #求得每個類的dice
        dices=K.mean(sample_dices,axis=0)
        return K.mean(dices) #所有類別dice求平均的dice
    return dice_coef
 
def dice_coef_loss_fun(smooth=0):
    def dice_coef_loss(y_true,y_pred):
        return 1-1-dice_coef_fun(smooth=smooth)(y_true=y_true,y_pred=y_pred)
    return dice_coef_loss

2、generalized dice loss

def generalized_dice_coef_fun(smooth=0):
    def generalized_dice(y_true, y_pred):
        # Compute weights: "the contribution of each label is corrected by the inverse of its volume"
        w = K.sum(y_true, axis=(0, 1, 2, 3))
        w = 1 / (w ** 2 + 0.00001)
        # w為各個類別的權(quán)重,占比越大,權(quán)重越小
        # Compute gen dice coef:
        numerator = y_true * y_pred
        numerator = w * K.sum(numerator, axis=(0, 1, 2, 3))
        numerator = K.sum(numerator)
 
        denominator = y_true + y_pred
        denominator = w * K.sum(denominator, axis=(0, 1, 2, 3))
        denominator = K.sum(denominator)
 
        gen_dice_coef = numerator / denominator
 
        return  2 * gen_dice_coef
    return generalized_dice
 
def generalized_dice_loss_fun(smooth=0):
    def generalized_dice_loss(y_true,y_pred):
        return 1 - generalized_dice_coef_fun(smooth=smooth)(y_true=y_true,y_pred=y_pred)
    return generalized_dice_loss

3、tversky coefficient loss

# Ref: salehi17, "Twersky loss function for image segmentation using 3D FCDN"
# -> the score is computed for each class separately and then summed
# alpha=beta=0.5 : dice coefficient
# alpha=beta=1   : tanimoto coefficient (also known as jaccard)
# alpha+beta=1   : produces set of F*-scores
# implemented by E. Moebel, 06/04/18
def tversky_coef_fun(alpha,beta):
    def tversky_coef(y_true, y_pred):
        p0 = y_pred  # proba that voxels are class i
        p1 = 1 - y_pred  # proba that voxels are not class i
        g0 = y_true
        g1 = 1 - y_true
 
        # 求得每個sample的每個類的dice
        num = K.sum(p0 * g0, axis=( 1, 2, 3))
        den = num + alpha * K.sum(p0 * g1,axis= ( 1, 2, 3)) + beta * K.sum(p1 * g0, axis=( 1, 2, 3))
        T = num / den  #[batch_size,class_num]
        
        # 求得每個類的dice
        dices=K.mean(T,axis=0) #[class_num]
        
        return K.mean(dices)
    return tversky_coef
 
def tversky_coef_loss_fun(alpha,beta):
    def tversky_coef_loss(y_true,y_pred):
        return 1-tversky_coef_fun(alpha=alpha,beta=beta)(y_true=y_true,y_pred=y_pred)
    return tversky_coef_loss

4、IoU loss

def IoU_fun(eps=1e-6):
    def IoU(y_true, y_pred):
        # if np.max(y_true) == 0.0:
        #     return IoU(1-y_true, 1-y_pred) ## empty image; calc IoU of zeros
        intersection = K.sum(y_true * y_pred, axis=[1,2,3])
        union = K.sum(y_true, axis=[1,2,3]) + K.sum(y_pred, axis=[1,2,3]) - intersection
        #
        ious=K.mean((intersection + eps) / (union + eps),axis=0)
        return K.mean(ious)
    return IoU
 
def IoU_loss_fun(eps=1e-6):
    def IoU_loss(y_true,y_pred):
        return 1-IoU_fun(eps=eps)(y_true=y_true,y_pred=y_pred)
    return IoU_loss

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 教你輕松解決selenium打開瀏覽器自動退出

    教你輕松解決selenium打開瀏覽器自動退出

    這篇文章主要給大家介紹了關(guān)于如何輕松解決selenium打開瀏覽器自動退出的相關(guān)資料,Selenium是一個用于Web應(yīng)用程序測試的工具,Selenium測試直接運行在瀏覽器中,今天在打開網(wǎng)頁時,瀏覽器總是一閃而退,需要的朋友可以參考下
    2023-08-08
  • pyqt5 lineEdit設(shè)置密碼隱藏,刪除lineEdit已輸入的內(nèi)容等屬性方法

    pyqt5 lineEdit設(shè)置密碼隱藏,刪除lineEdit已輸入的內(nèi)容等屬性方法

    今天小編就為大家分享一篇pyqt5 lineEdit設(shè)置密碼隱藏,刪除lineEdit已輸入的內(nèi)容等屬性方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • Python中處理NaN值的技巧分享

    Python中處理NaN值的技巧分享

    在數(shù)據(jù)科學(xué)和數(shù)據(jù)分析領(lǐng)域,NaN(Not a Number)是一個常見的概念,它表示一個缺失或未定義的數(shù)值,在 Python 中,尤其是在使用pandas庫處理數(shù)據(jù)時,NaN 值的處理尤為重要,本文給大家介紹了Python中處理NaN值的技巧,需要的朋友可以參考下
    2024-12-12
  • Python中特殊函數(shù)集錦

    Python中特殊函數(shù)集錦

    這篇文章主要介紹了Python中特殊函數(shù),主要介紹一下四個函數(shù):1 過濾函數(shù)filter 2 映射和歸并函數(shù)map/reduce 3 裝飾器@ 4 匿名函數(shù)lamda,需要的朋友可以參考下
    2015-07-07
  • python開根號實例講解

    python開根號實例講解

    在本篇文章里小編給大家整理的是關(guān)于python開根號實例講解內(nèi)容,有需要的朋友們可以參考下。
    2020-08-08
  • Django基于Models定制Admin后臺實現(xiàn)過程解析

    Django基于Models定制Admin后臺實現(xiàn)過程解析

    這篇文章主要介紹了Django基于Models定制Admin后臺實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • Python進階之遞歸函數(shù)的用法及其示例

    Python進階之遞歸函數(shù)的用法及其示例

    本篇文章主要介紹了Python進階之遞歸函數(shù)的用法及其示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Python基于requests庫爬取網(wǎng)站信息

    Python基于requests庫爬取網(wǎng)站信息

    這篇文章主要介紹了python基于requests庫爬取網(wǎng)站信息,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • python 中的collections.OrderedDict() 用法

    python 中的collections.OrderedDict() 用法

    這篇文章主要介紹了python 中的collections.OrderedDict() 用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • FP-growth算法發(fā)現(xiàn)頻繁項集——構(gòu)建FP樹

    FP-growth算法發(fā)現(xiàn)頻繁項集——構(gòu)建FP樹

    常見的挖掘頻繁項集算法有兩類,一類是Apriori算法,另一類是FP-growth。Apriori通過不斷的構(gòu)造候選集、篩選候選集挖掘出頻繁項集,需要多次掃描原始數(shù)據(jù),當(dāng)原始數(shù)據(jù)較大時,磁盤I/O次數(shù)太多,效率比較低下
    2021-06-06

最新評論

抚远县| 桂阳县| 龙门县| 台中市| 海宁市| 前郭尔| 昂仁县| 昆山市| 息烽县| 江安县| 乌拉特后旗| 韩城市| 封丘县| 天柱县| 晋州市| 浮梁县| 浏阳市| 工布江达县| 永泰县| 隆回县| 东兴市| 宁强县| 公安县| 双牌县| 双城市| 巴彦县| 元谋县| 日喀则市| 天门市| 子长县| 伊吾县| 饶阳县| 南和县| 安塞县| 新沂市| 徐汇区| 张家口市| 崇左市| 靖宇县| 宣威市| 左权县|