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

Keras loss函數(shù)剖析

 更新時(shí)間:2020年07月06日 16:38:03   作者:姚賢賢  
這篇文章主要介紹了Keras loss函數(shù)剖析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,大家還是直接看代碼吧~

'''
Created on 2018-4-16
'''
def compile(
self,
optimizer, #優(yōu)化器
loss, #損失函數(shù),可以為已經(jīng)定義好的loss函數(shù)名稱,也可以為自己寫的loss函數(shù)
metrics=None, #
sample_weight_mode=None, #如果你需要按時(shí)間步為樣本賦權(quán)(2D權(quán)矩陣),將該值設(shè)為“temporal”。默認(rèn)為“None”,代表按樣本賦權(quán)(1D權(quán)),和fit中sample_weight在賦值樣本權(quán)重中配合使用
weighted_metrics=None, 
target_tensors=None,
**kwargs #這里的設(shè)定的參數(shù)可以和后端交互。
)

實(shí)質(zhì)調(diào)用的是Keras\engine\training.py 中的class Model中的def compile
一般使用model.compile(loss='categorical_crossentropy',optimizer='sgd',metrics=['accuracy'])

# keras所有定義好的損失函數(shù)loss:
# keras\losses.py
# 有些loss函數(shù)可以使用簡稱:
# mse = MSE = mean_squared_error
# mae = MAE = mean_absolute_error
# mape = MAPE = mean_absolute_percentage_error
# msle = MSLE = mean_squared_logarithmic_error
# kld = KLD = kullback_leibler_divergence
# cosine = cosine_proximity
# 使用到的數(shù)學(xué)方法:
# mean:求均值
# sum:求和
# square:平方
# abs:絕對(duì)值
# clip:[裁剪替換](https://blog.csdn.net/qq1483661204/article/details)
# epsilon:1e-7
# log:以e為底
# maximum(x,y):x與 y逐位比較取其大者
# reduce_sum(x,axis):沿著某個(gè)維度求和
# l2_normalize:l2正則化
# softplus:softplus函數(shù)
# 
# import cntk as C
# 1.mean_squared_error:
#  return K.mean(K.square(y_pred - y_true), axis=-1) 
# 2.mean_absolute_error:
#  return K.mean(K.abs(y_pred - y_true), axis=-1)
# 3.mean_absolute_percentage_error:
#  diff = K.abs((y_true - y_pred) / K.clip(K.abs(y_true),K.epsilon(),None))
#  return 100. * K.mean(diff, axis=-1)
# 4.mean_squared_logarithmic_error:
#  first_log = K.log(K.clip(y_pred, K.epsilon(), None) + 1.)
#  second_log = K.log(K.clip(y_true, K.epsilon(), None) + 1.)
#  return K.mean(K.square(first_log - second_log), axis=-1)
# 5.squared_hinge:
#  return K.mean(K.square(K.maximum(1. - y_true * y_pred, 0.)), axis=-1)
# 6.hinge(SVM損失函數(shù)):
#  return K.mean(K.maximum(1. - y_true * y_pred, 0.), axis=-1)
# 7.categorical_hinge:
#  pos = K.sum(y_true * y_pred, axis=-1)
#  neg = K.max((1. - y_true) * y_pred, axis=-1)
#  return K.maximum(0., neg - pos + 1.)
# 8.logcosh:
#  def _logcosh(x):
#   return x + K.softplus(-2. * x) - K.log(2.)
#  return K.mean(_logcosh(y_pred - y_true), axis=-1)
# 9.categorical_crossentropy:
#  output /= C.reduce_sum(output, axis=-1)
#  output = C.clip(output, epsilon(), 1.0 - epsilon())
#  return -sum(target * C.log(output), axis=-1)
# 10.sparse_categorical_crossentropy:
#  target = C.one_hot(target, output.shape[-1])
#  target = C.reshape(target, output.shape)
#  return categorical_crossentropy(target, output, from_logits)
# 11.binary_crossentropy:
#  return K.mean(K.binary_crossentropy(y_true, y_pred), axis=-1)
# 12.kullback_leibler_divergence:
#  y_true = K.clip(y_true, K.epsilon(), 1)
#  y_pred = K.clip(y_pred, K.epsilon(), 1)
#  return K.sum(y_true * K.log(y_true / y_pred), axis=-1)
# 13.poisson:
#  return K.mean(y_pred - y_true * K.log(y_pred + K.epsilon()), axis=-1)
# 14.cosine_proximity:
#  y_true = K.l2_normalize(y_true, axis=-1)
#  y_pred = K.l2_normalize(y_pred, axis=-1)
#  return -K.sum(y_true * y_pred, axis=-1)

補(bǔ)充知識(shí):一文總結(jié)Keras的loss函數(shù)和metrics函數(shù)

Loss函數(shù)

定義:

keras.losses.mean_squared_error(y_true, y_pred)

用法很簡單,就是計(jì)算均方誤差平均值,例如

loss_fn = keras.losses.mean_squared_error
a1 = tf.constant([1,1,1,1])
a2 = tf.constant([2,2,2,2])
loss_fn(a1,a2)
<tf.Tensor: id=718367, shape=(), dtype=int32, numpy=1>

Metrics函數(shù)

Metrics函數(shù)也用于計(jì)算誤差,但是功能比Loss函數(shù)要復(fù)雜。

定義

tf.keras.metrics.Mean(
  name='mean', dtype=None
)

這個(gè)定義過于簡單,舉例說明

mean_loss([1, 3, 5, 7])
mean_loss([1, 3, 5, 7])
mean_loss([1, 1, 1, 1])
mean_loss([2,2])

輸出結(jié)果

<tf.Tensor: id=718929, shape=(), dtype=float32, numpy=2.857143>

這個(gè)結(jié)果等價(jià)于

np.mean([1, 3, 5, 7, 1, 3, 5, 7, 1, 1, 1, 1, 2, 2])

這是因?yàn)镸etrics函數(shù)是狀態(tài)函數(shù),在神經(jīng)網(wǎng)絡(luò)訓(xùn)練過程中會(huì)持續(xù)不斷地更新狀態(tài),是有記憶的。因?yàn)镸etrics函數(shù)還帶有下面幾個(gè)Methods

reset_states()
Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.

result()
Computes and returns the metric value tensor.
Result computation is an idempotent operation that simply calculates the metric value using the state variables

update_state(
  values, sample_weight=None
)
Accumulates statistics for computing the reduction metric.

另外注意,Loss函數(shù)和Metrics函數(shù)的調(diào)用形式,

loss_fn = keras.losses.mean_squared_error mean_loss = keras.metrics.Mean()

mean_loss(1)等價(jià)于keras.metrics.Mean()(1),而不是keras.metrics.Mean(1),這個(gè)從keras.metrics.Mean函數(shù)的定義可以看出。

但是必須先令生成一個(gè)實(shí)例mean_loss=keras.metrics.Mean(),而不能直接使用keras.metrics.Mean()本身。

以上這篇Keras loss函數(shù)剖析就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Pytorch之上/下采樣函數(shù)torch.nn.functional.interpolate插值詳解

    Pytorch之上/下采樣函數(shù)torch.nn.functional.interpolate插值詳解

    這篇文章主要介紹了Pytorch之上/下采樣函數(shù)torch.nn.functional.interpolate插值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • Python+Tkinter制作專屬圖形化界面

    Python+Tkinter制作專屬圖形化界面

    這篇文章主要是帶著大家通過Python Tkinter制作一個(gè)屬于自己的GUI圖形化界面,可以用于設(shè)計(jì)簽名的哦,感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧
    2022-04-04
  • Python3 翻轉(zhuǎn)二叉樹的實(shí)現(xiàn)

    Python3 翻轉(zhuǎn)二叉樹的實(shí)現(xiàn)

    這篇文章主要介紹了Python3 翻轉(zhuǎn)二叉樹的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • python使用pandas讀取json文件并進(jìn)行刷選導(dǎo)出xlsx文件的方法示例

    python使用pandas讀取json文件并進(jìn)行刷選導(dǎo)出xlsx文件的方法示例

    這篇文章主要介紹了python使用pandas讀取json文件并進(jìn)行刷選導(dǎo)出xlsx文件的方法,結(jié)合實(shí)例形式分析了python調(diào)用pandas模塊針對(duì)json數(shù)據(jù)操作的相關(guān)使用技巧,需要的朋友可以參考下
    2023-06-06
  • python 實(shí)現(xiàn)按對(duì)象傳值

    python 實(shí)現(xiàn)按對(duì)象傳值

    今天小編就為大家分享一篇python 實(shí)現(xiàn)按對(duì)象傳值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • new_zeros() pytorch版本的轉(zhuǎn)換方式

    new_zeros() pytorch版本的轉(zhuǎn)換方式

    今天小編就為大家分享一篇new_zeros() pytorch版本的轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Pytorch框架之one_hot編碼函數(shù)解讀

    Pytorch框架之one_hot編碼函數(shù)解讀

    這篇文章主要介紹了Pytorch框架之one_hot編碼函數(shù)解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Python3內(nèi)置模塊之json編解碼方法小結(jié)【推薦】

    Python3內(nèi)置模塊之json編解碼方法小結(jié)【推薦】

    這篇文章主要介紹了Python3內(nèi)置模塊之json編解碼方法小結(jié),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • python中setuptools的作用是什么

    python中setuptools的作用是什么

    在本篇文章里小編給大家分享的是一篇關(guān)于python中setuptools的作用以及相關(guān)用法,需要的朋友們可以跟著學(xué)習(xí)下。
    2020-06-06
  • 如何將自己的python庫打包成wheel文件并上傳到pypi

    如何將自己的python庫打包成wheel文件并上傳到pypi

    這篇文章主要介紹了如何將自己的python庫打包成wheel文件并上傳到pypi,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04

最新評(píng)論

乌拉特中旗| 宁乡县| 安义县| 喜德县| 馆陶县| 海城市| 扬中市| 梁河县| 万山特区| 沙坪坝区| 施秉县| 县级市| 琼海市| 永福县| 闽侯县| 泗洪县| 台北市| 沾化县| 望奎县| 类乌齐县| 南陵县| 建宁县| 密云县| 蕲春县| 兴义市| 濉溪县| 文昌市| 涞水县| 陆川县| 米林县| 玛多县| 宁河县| 新建县| 林口县| 阿拉善盟| 新乡市| 隆林| 江安县| 陵水| 曲靖市| 绵阳市|