以下信息均來自官網
------------------------------------------------------------------------------------------------------------
損失函數的使用
損失函數(或稱目標函數、優化評分函數)是編譯模型時所需的兩個參數之一:
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)