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

Keras之自定義損失(loss)函數(shù)用法說(shuō)明

 更新時(shí)間:2020年06月10日 08:41:51   作者:鵲踏枝  
這篇文章主要介紹了Keras之自定義損失(loss)函數(shù)用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

在Keras中可以自定義損失函數(shù),在自定義損失函數(shù)的過(guò)程中需要注意的一點(diǎn)是,損失函數(shù)的參數(shù)形式,這一點(diǎn)在Keras中是固定的,須如下形式:

def my_loss(y_true, y_pred):
# y_true: True labels. TensorFlow/Theano tensor
# y_pred: Predictions. TensorFlow/Theano tensor of the same shape as y_true
 .
 .
 .
 return scalar #返回一個(gè)標(biāo)量值

然后在model.compile中指定即可,如:

model.compile(loss=my_loss, optimizer='sgd')

具體參考Keras官方metrics的定義keras/metrics.py

"""Built-in metrics.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import six
from . import backend as K
from .losses import mean_squared_error
from .losses import mean_absolute_error
from .losses import mean_absolute_percentage_error
from .losses import mean_squared_logarithmic_error
from .losses import hinge
from .losses import logcosh
from .losses import squared_hinge
from .losses import categorical_crossentropy
from .losses import sparse_categorical_crossentropy
from .losses import binary_crossentropy
from .losses import kullback_leibler_divergence
from .losses import poisson
from .losses import cosine_proximity
from .utils.generic_utils import deserialize_keras_object
from .utils.generic_utils import serialize_keras_object
 
def binary_accuracy(y_true, y_pred):
 return K.mean(K.equal(y_true, K.round(y_pred)), axis=-1)
 
 
def categorical_accuracy(y_true, y_pred):
 return K.cast(K.equal(K.argmax(y_true, axis=-1),
       K.argmax(y_pred, axis=-1)),
     K.floatx())
 
def sparse_categorical_accuracy(y_true, y_pred):
 # reshape in case it's in shape (num_samples, 1) instead of (num_samples,)
 if K.ndim(y_true) == K.ndim(y_pred):
  y_true = K.squeeze(y_true, -1)
 # convert dense predictions to labels
 y_pred_labels = K.argmax(y_pred, axis=-1)
 y_pred_labels = K.cast(y_pred_labels, K.floatx())
 return K.cast(K.equal(y_true, y_pred_labels), K.floatx())
 
def top_k_categorical_accuracy(y_true, y_pred, k=5):
 return K.mean(K.in_top_k(y_pred, K.argmax(y_true, axis=-1), k), axis=-1)
 
def sparse_top_k_categorical_accuracy(y_true, y_pred, k=5):
 # If the shape of y_true is (num_samples, 1), flatten to (num_samples,)
 return K.mean(K.in_top_k(y_pred, K.cast(K.flatten(y_true), 'int32'), k),
     axis=-1)
 
# Aliases
 
mse = MSE = mean_squared_error
mae = MAE = mean_absolute_error
mape = MAPE = mean_absolute_percentage_error
msle = MSLE = mean_squared_logarithmic_error
cosine = cosine_proximity
 
def serialize(metric):
 return serialize_keras_object(metric)
 
def deserialize(config, custom_objects=None):
 return deserialize_keras_object(config,
         module_objects=globals(),
         custom_objects=custom_objects,
         printable_module_name='metric function')
 
def get(identifier):
 if isinstance(identifier, dict):
  config = {'class_name': str(identifier), 'config': {}}
  return deserialize(config)
 elif isinstance(identifier, six.string_types):
  return deserialize(str(identifier))
 elif callable(identifier):
  return identifier
 else:
  raise ValueError('Could not interpret '
       'metric function identifier:', identifier)

以上這篇Keras之自定義損失(loss)函數(shù)用法說(shuō)明就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python語(yǔ)言的自我介紹一起來(lái)看看

    Python語(yǔ)言的自我介紹一起來(lái)看看

    這篇文章主要為大家詳細(xì)介紹了Python語(yǔ)言的自我介紹,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • python+opencv輪廓檢測(cè)代碼解析

    python+opencv輪廓檢測(cè)代碼解析

    這篇文章主要介紹了python+opencv輪廓檢測(cè)代碼解析,本文實(shí)例實(shí)現(xiàn)對(duì)圖片的簡(jiǎn)單處理,比如圖片的讀取,灰度顯示等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • Python利用matplotlib生成圖片背景及圖例透明的效果

    Python利用matplotlib生成圖片背景及圖例透明的效果

    這篇文章主要給大家介紹了Python利用matplotlib生成圖片背景及圖例透明效果的相關(guān)資料,文中給出了詳細(xì)的示例代碼,相信對(duì)大家具有一定的參考家價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-04-04
  • 簡(jiǎn)單了解python中的f.b.u.r函數(shù)

    簡(jiǎn)單了解python中的f.b.u.r函數(shù)

    這篇文章主要介紹了簡(jiǎn)單了解python中的f.b.u.r函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • python3使用diagrams繪制架構(gòu)圖的步驟

    python3使用diagrams繪制架構(gòu)圖的步驟

    這篇文章主要介紹了python3使用diagrams生成架構(gòu)圖的步驟,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-04-04
  • python2與python3的print及字符串格式化小結(jié)

    python2與python3的print及字符串格式化小結(jié)

    最近一直在用python寫(xiě)程序,對(duì)于python的print一直很惱火,老是不按照預(yù)期輸出。今天特來(lái)總結(jié)一樣print和format,也希望能幫助大家徹底理解它們
    2018-11-11
  • 基于Python實(shí)現(xiàn)自動(dòng)摳圖小程序

    基于Python實(shí)現(xiàn)自動(dòng)摳圖小程序

    這篇文章主要為了大家利用用Python制作一款界面化的摳圖小程序,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,感興趣的可以學(xué)習(xí)一下
    2022-01-01
  • python中 chr unichr ord函數(shù)的實(shí)例詳解

    python中 chr unichr ord函數(shù)的實(shí)例詳解

    這篇文章主要介紹了python中 chr unichr ord函數(shù)的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-08-08
  • python多行字符串拼接使用小括號(hào)的方法

    python多行字符串拼接使用小括號(hào)的方法

    今天小編就為大家分享一篇python多行字符串拼接使用小括號(hào)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • python運(yùn)行腳本文件的三種方法實(shí)例

    python運(yùn)行腳本文件的三種方法實(shí)例

    在計(jì)算中,腳本一詞用于指代包含訂單邏輯序列的文件或批處理文件,下面這篇文章主要給大家介紹了關(guān)于python運(yùn)行腳本文件的三種方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06

最新評(píng)論

启东市| 佛教| 金昌市| 临西县| 镇远县| 牡丹江市| 陵川县| 苍溪县| 沙河市| 长岛县| 三原县| 时尚| 昆山市| 天长市| 华容县| 北海市| 屏东县| 邮箱| 怀来县| 浑源县| 钟山县| 梧州市| 靖安县| 滨海县| 沁阳市| 建阳市| 河曲县| 肥乡县| 随州市| 永顺县| 嵊泗县| 香格里拉县| 台安县| 房山区| 福建省| 大同县| 成都市| 洪泽县| 潼南县| 凤城市| 虹口区|