欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

在Keras中可以自定義損失函數(shù),在自定義損失函數(shù)的過程中需要注意的一點是,損失函數(shù)的參數(shù)形式,這一點在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 #返回一個標量值

然后在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ù)用法說明就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 超詳細注釋之OpenCV按位AND OR XOR和NOT

    超詳細注釋之OpenCV按位AND OR XOR和NOT

    這篇文章主要介紹了OpenCV按位AND OR XOR和NOT運算,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Puppeteer使用示例詳解

    Puppeteer使用示例詳解

    這篇文章主要介紹了Puppeteer使用示例詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-06-06
  • python多維列表總是只轉(zhuǎn)為一維數(shù)組問題解決

    python多維列表總是只轉(zhuǎn)為一維數(shù)組問題解決

    這篇文章主要為大家介紹了python多維列表總是只轉(zhuǎn)為一維數(shù)組問題解決實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • python通過安裝itchat包實現(xiàn)微信自動回復收到的春節(jié)祝福

    python通過安裝itchat包實現(xiàn)微信自動回復收到的春節(jié)祝福

    這篇文章主要介紹了python通過安裝itchat包實現(xiàn)微信自動回復收到的春節(jié)祝福,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-01-01
  • Python讀取txt某幾列繪圖的方法

    Python讀取txt某幾列繪圖的方法

    今天小編就為大家分享一篇Python讀取txt某幾列繪圖的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • python列表使用實現(xiàn)名字管理系統(tǒng)

    python列表使用實現(xiàn)名字管理系統(tǒng)

    這篇文章主要為大家詳細介紹了python列表使用實現(xiàn)名字管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • python opencv 圖像處理之圖像算數(shù)運算及修改顏色空間

    python opencv 圖像處理之圖像算數(shù)運算及修改顏色空間

    這篇文章主要介紹了python opencv 圖像處理之圖像算數(shù)運算及修改顏色空間,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-08-08
  • Python雙鏈表原理與實現(xiàn)方法詳解

    Python雙鏈表原理與實現(xiàn)方法詳解

    這篇文章主要介紹了Python雙鏈表原理與實現(xiàn)方法,結(jié)合實例形式詳細分析了Python雙鏈表的概念、原理、用法及相關(guān)操作注意事項,需要的朋友可以參考下
    2020-02-02
  • 用Python爬取各大高校并可視化幫弟弟選大學,弟弟直呼牛X

    用Python爬取各大高校并可視化幫弟弟選大學,弟弟直呼牛X

    高考結(jié)束了,接下來最重要的就是玩玩玩,然后準備報志愿吧.中國教育在線網(wǎng)顯示國內(nèi)目前共有2857所高等院校,報一個理想的學校簡直是千里挑一.正好表弟求著我讓我?guī)退x學校,我想著十年寒窗苦讀也不容易不如就用python幫幫他.分析一下目前國內(nèi)的大學,需要的朋友可以參考下
    2021-06-06
  • Python爬蟲爬取杭州24時溫度并展示操作示例

    Python爬蟲爬取杭州24時溫度并展示操作示例

    這篇文章主要介紹了Python爬蟲爬取杭州24時溫度并展示操作,結(jié)合實例形式分析了Python爬蟲進行頁面爬取與數(shù)據(jù)分析、展示相關(guān)操作技巧,需要的朋友可以參考下
    2020-03-03

最新評論