keras损失函数详解
程序员文章站
2022-04-12 11:35:17
以下信息均来自官网 损失函数的使用 损失函数(或称目标函数、优化评分函数)是编译模型时所需的两个参数之一: 你可以传递一个现有的损失函数名,或者一个 TensorFlow/Theano 符号函数。 该符号函数为每个数据点返回一个标量,有以下两个参数: y_true: 真实标签。TensorFlow/ ......
以下信息均来自官网
------------------------------------------------------------------------------------------------------------
损失函数的使用
损失函数(或称目标函数、优化评分函数)是编译模型时所需的两个参数之一:
model.compile(loss='mean_squared_error', optimizer='sgd') from keras import losses model.compile(loss=losses.mean_squared_error, optimizer='sgd')
你可以传递一个现有的损失函数名,或者一个 tensorflow/theano 符号函数。 该符号函数为每个数据点返回一个标量,有以下两个参数:
- y_true: 真实标签。tensorflow/theano 张量。
- y_pred: 预测值。tensorflow/theano 张量,其 shape 与 y_true 相同。
实际的优化目标是所有数据点的输出数组的平均值。
可用损失函数
mean_squared_error
mean_squared_error(y_true, y_pred)
mean_absolute_error
mean_absolute_error(y_true, y_pred)
mean_absolute_percentage_error
mean_absolute_percentage_error(y_true, y_pred)
mean_squared_logarithmic_error
mean_squared_logarithmic_error(y_true, y_pred)
squared_hinge
squared_hinge(y_true, y_pred)
hinge
hinge(y_true, y_pred)
categorical_hinge
categorical_hinge(y_true, y_pred)
logcosh
logcosh(y_true, y_pred)
预测误差的双曲余弦的对数。
对于小的 x
,log(cosh(x))
近似等于 (x ** 2) / 2
。对于大的 x
,近似于 abs(x) - log(2)
。这表示 'logcosh' 与均方误差大致相同,但是不会受到偶尔疯狂的错误预测的强烈影响。
参数
- y_true: 目标真实值的张量。
- y_pred: 目标预测值的张量。
返回
每个样本都有一个标量损失的张量。
categorical_crossentropy
categorical_crossentropy(y_true, y_pred)
sparse_categorical_crossentropy
sparse_categorical_crossentropy(y_true, y_pred)
binary_crossentropy
binary_crossentropy(y_true, y_pred)
kullback_leibler_divergence
kullback_leibler_divergence(y_true, y_pred)
poisson
poisson(y_true, y_pred)
cosine_proximity
cosine_proximity(y_true, y_pred)
注意: 当使用 categorical_crossentropy
损失时,你的目标值应该是分类格式 (即,如果你有 10 个类,每个样本的目标值应该是一个 10 维的向量,这个向量除了表示类别的那个索引为 1,其他均为 0)。 为了将 整数目标值 转换为 分类目标值,你可以使用 keras 实用函数 to_categorical
:
from keras.utils.np_utils import to_categorical categorical_labels = to_categorical(int_labels, num_classes=none)
如果还不明白,请看下面的源码
1 """built-in loss functions. 2 """ 3 from __future__ import absolute_import 4 from __future__ import division 5 from __future__ import print_function 6 7 import six 8 from . import backend as k 9 from .utils.generic_utils import deserialize_keras_object 10 from .utils.generic_utils import serialize_keras_object 11 12 13 def mean_squared_error(y_true, y_pred): 14 return k.mean(k.square(y_pred - y_true), axis=-1) 15 16 17 def mean_absolute_error(y_true, y_pred): 18 return k.mean(k.abs(y_pred - y_true), axis=-1) 19 20 21 def mean_absolute_percentage_error(y_true, y_pred): 22 diff = k.abs((y_true - y_pred) / k.clip(k.abs(y_true), 23 k.epsilon(), 24 none)) 25 return 100. * k.mean(diff, axis=-1) 26 27 28 def mean_squared_logarithmic_error(y_true, y_pred): 29 first_log = k.log(k.clip(y_pred, k.epsilon(), none) + 1.) 30 second_log = k.log(k.clip(y_true, k.epsilon(), none) + 1.) 31 return k.mean(k.square(first_log - second_log), axis=-1) 32 33 34 def squared_hinge(y_true, y_pred): 35 return k.mean(k.square(k.maximum(1. - y_true * y_pred, 0.)), axis=-1) 36 37 38 def hinge(y_true, y_pred): 39 return k.mean(k.maximum(1. - y_true * y_pred, 0.), axis=-1) 40 41 42 def categorical_hinge(y_true, y_pred): 43 pos = k.sum(y_true * y_pred, axis=-1) 44 neg = k.max((1. - y_true) * y_pred, axis=-1) 45 return k.maximum(0., neg - pos + 1.) 46 47 48 def logcosh(y_true, y_pred): 49 """logarithm of the hyperbolic cosine of the prediction error. 50 `log(cosh(x))` is approximately equal to `(x ** 2) / 2` for small `x` and 51 to `abs(x) - log(2)` for large `x`. this means that 'logcosh' works mostly 52 like the mean squared error, but will not be so strongly affected by the 53 occasional wildly incorrect prediction. 54 # arguments 55 y_true: tensor of true targets. 56 y_pred: tensor of predicted targets. 57 # returns 58 tensor with one scalar loss entry per sample. 59 """ 60 def _logcosh(x): 61 return x + k.softplus(-2. * x) - k.log(2.) 62 return k.mean(_logcosh(y_pred - y_true), axis=-1) 63 64 65 def categorical_crossentropy(y_true, y_pred): 66 return k.categorical_crossentropy(y_true, y_pred) 67 68 69 def sparse_categorical_crossentropy(y_true, y_pred): 70 return k.sparse_categorical_crossentropy(y_true, y_pred) 71 72 73 def binary_crossentropy(y_true, y_pred): 74 return k.mean(k.binary_crossentropy(y_true, y_pred), axis=-1) 75 76 77 def kullback_leibler_divergence(y_true, y_pred): 78 y_true = k.clip(y_true, k.epsilon(), 1) 79 y_pred = k.clip(y_pred, k.epsilon(), 1) 80 return k.sum(y_true * k.log(y_true / y_pred), axis=-1) 81 82 83 def poisson(y_true, y_pred): 84 return k.mean(y_pred - y_true * k.log(y_pred + k.epsilon()), axis=-1) 85 86 87 def cosine_proximity(y_true, y_pred): 88 y_true = k.l2_normalize(y_true, axis=-1) 89 y_pred = k.l2_normalize(y_pred, axis=-1) 90 return -k.sum(y_true * y_pred, axis=-1) 91 92 93 # aliases. 94 95 mse = mse = mean_squared_error 96 mae = mae = mean_absolute_error 97 mape = mape = mean_absolute_percentage_error 98 msle = msle = mean_squared_logarithmic_error 99 kld = kld = kullback_leibler_divergence 100 cosine = cosine_proximity 101 102 103 def serialize(loss): 104 return serialize_keras_object(loss) 105 106 107 def deserialize(name, custom_objects=none): 108 return deserialize_keras_object(name, 109 module_objects=globals(), 110 custom_objects=custom_objects, 111 printable_module_name='loss function') 112 113 114 def get(identifier): 115 """get the `identifier` loss function. 116 # arguments 117 identifier: none or str, name of the function. 118 # returns 119 the loss function or none if `identifier` is none. 120 # raises 121 valueerror if unknown identifier. 122 """ 123 if identifier is none: 124 return none 125 if isinstance(identifier, six.string_types): 126 identifier = str(identifier) 127 return deserialize(identifier) 128 if isinstance(identifier, dict): 129 return deserialize(identifier) 130 elif callable(identifier): 131 return identifier 132 else: 133 raise valueerror('could not interpret ' 134 'loss function identifier:', identifier)
推荐阅读
-
PHP中header函数的用法及其注意事项详解_php实例
-
斯坦福大学深度学习公开课cs231n学习笔记(7)神经网络防止数据过拟合:损失函数和正则化
-
PHP4 与 MySQL 数据库操作函数详解(2)_MySQL
-
php之XML转数组函数的详解_PHP
-
php函数的传值与传址(引用)详解_PHP教程
-
php中mb_strlen与strlen函数详解
-
ThinkPHP函数详解之M方法和R方法_php实例
-
对python中的six.moves模块的下载函数urlretrieve详解
-
PHP函数篇详解十进制、二进制、八进制和十六进制转换函数说明
-
PHP error_reporting() 错误控制函数功能详解_PHP教程