激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

香港云服务器
服務器之家 - 腳本之家 - Python - keras自定義損失函數并且模型加載的寫法介紹

keras自定義損失函數并且模型加載的寫法介紹

2020-06-15 10:13Briwisdom Python

這篇文章主要介紹了keras自定義損失函數并且模型加載的寫法介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

keras自定義函數時候,正常在模型里自己寫好自定義的函數,然后在模型編譯的那行代碼里寫上接口即可。如下所示,focal_loss和fbeta_score是我們自己定義的兩個函數,在model.compile加入它們,metrics里‘accuracy'是keras自帶的度量函數。

?
1
2
3
4
5
6
7
def focal_loss():
 ...
 return xx
def fbeta_score():
 ...
 return yy
model.compile(optimizer=Adam(lr=0.0001), loss=[focal_loss],metrics=['accuracy',fbeta_score] )

訓練好之后,模型加載也需要再額外加一行,通過load_model里的custom_objects將我們定義的兩個函數以字典的形式加入就能正常加載模型啦。

weight_path = './weights.h5'
model = load_model(weight_path,custom_objects={'focal_loss': focal_loss,'fbeta_score':fbeta_score})

補充知識:keras如何使用自定義的loss及評價函數進行訓練及預測

1.有時候訓練模型,現有的損失及評估函數并不足以科學的訓練評估模型,這時候就需要自定義一些損失評估函數,比如focal loss損失函數及dice評價函數 for unet的訓練。

2.在訓練建模中導入自定義loss及評估函數。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#模型編譯時加入自定義loss及評估函數
model.compile(optimizer = Adam(lr=1e-4), loss=[binary_focal_loss()],
    metrics=['accuracy',dice_coef])
 
#自定義loss及評估函數
def binary_focal_loss(gamma=2, alpha=0.25):
 """
 Binary form of focal loss.
 適用于二分類問題的focal loss
 focal_loss(p_t) = -alpha_t * (1 - p_t)**gamma * log(p_t)
  where p = sigmoid(x), p_t = p or 1 - p depending on if the label is 1 or 0, respectively.
 References:
  https://arxiv.org/pdf/1708.02002.pdf
 Usage:
  model.compile(loss=[binary_focal_loss(alpha=.25, gamma=2)], metrics=["accuracy"], optimizer=adam)
 """
 alpha = tf.constant(alpha, dtype=tf.float32)
 gamma = tf.constant(gamma, dtype=tf.float32)
 
 def binary_focal_loss_fixed(y_true, y_pred):
  """
  y_true shape need be (None,1)
  y_pred need be compute after sigmoid
  """
  y_true = tf.cast(y_true, tf.float32)
  alpha_t = y_true * alpha + (K.ones_like(y_true) - y_true) * (1 - alpha)
 
  p_t = y_true * y_pred + (K.ones_like(y_true) - y_true) * (K.ones_like(y_true) - y_pred) + K.epsilon()
  focal_loss = - alpha_t * K.pow((K.ones_like(y_true) - p_t), gamma) * K.log(p_t)
  return K.mean(focal_loss)
 
 return binary_focal_loss_fixed
 
#'''
#smooth 參數防止分母為0
def dice_coef(y_true, y_pred, smooth=1):
 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])
 return K.mean( (2. * intersection + smooth) / (union + smooth), axis=0)

注意在模型保存時,記錄的loss函數名稱:你猜是哪個

a:binary_focal_loss()

b:binary_focal_loss_fixed

3.模型預測時,也要加載自定義loss及評估函數,不然會報錯。

該告訴上面的答案了,保存在模型中loss的名稱為:binary_focal_loss_fixed,在模型預測時,定義custom_objects字典,key一定要與保存在模型中的名稱一致,不然會找不到loss function。所以自定義函數時,盡量避免使用我這種函數嵌套的方式,免得帶來一些意想不到的煩惱。

model = load_model('./unet_' + label + '_20.h5',custom_objects={'binary_focal_loss_fixed': binary_focal_loss(),'dice_coef': dice_coef})

以上這篇keras自定義損失函數并且模型加載的寫法介紹就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/u010420283/article/details/90232926

延伸 · 閱讀

精彩推薦
1678
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 久久国产精品久久久久久电车 | 亚洲欧美aⅴ | 日韩黄在线观看 | 亚洲免费视频一区二区 | 一级做a爱片久久毛片a高清 | 久久我不卡 | 99影视在线视频免费观看 | 黄色av片在线观看 | lutube成人福利在线观看 | 最新欧美精品一区二区三区 | 欧美久久久一区二区三区 | 欧美亚洲免费 | 九九热精品免费视频 | 成年人黄色免费电影 | 欧美成人黄色小视频 | 黄视频网站免费在线观看 | 国内精品久久久久久久影视红豆 | 全免费午夜一级毛片真人 | 99久久超碰中文字幕伊人 | 五月激情久久 | 日本在线观看一区二区 | 国产午夜免费福利 | 亚洲精品在线观看免费 | 欧美国产一级片 | 91免费大全| 欧美大片一级毛片 | 玩偶姐姐 在线观看 | 毛片免费视频观看 | 羞羞的网址 | 久久久电影电视剧免费看 | 精品人伦一区二区三区蜜桃网站 | 欧美在线国产 | 护士xxxx | 99欧美精品 | 国产亚洲精品久久久久5区 日韩一级片一区二区三区 国产精品久久久久av | 亚洲国产精品一区二区精品 | 国产精品自拍啪啪 | 视频在线中文字幕 | 国产三级精品最新在线 | 99热高清| 久草在线观看资源 |