作用
损失函数(或称目标函数、优化评分函数)是编译模型时所需的两个参数之一
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_square_error(MSE) 平方损失函数
mean_square_error(y_true,y_pred)
M S E = 1 N ∑ i = 1 N ( Y ~ i − Y i ) 2 M S E=\frac{1}{N} \sum_{i=1}^{N}\left(\tilde{Y}_{i}-Y_{i}\right)^{2} MSE=N1i=1∑N(Y~i−Yi)2 -
mean_absolute_error 绝对值损失函数
mean_absolute_error(y_true,y_pred)
ℓ ( y i , y ^ i ) = ∣ y i − y ^ i ∣ y i , y ^ i ∈ R \ell\left(y_{i}, \hat{y}_{i}\right)=\left|y_{i}-\hat{y}_{i}\right| \quad y_{i}, \hat{y}_{i} \in R ℓ(yi,y^i)=∣yi−y^i∣yi,y^i∈R -
mean_absolute_percentage_error 预测值百分率损失函数
mean_absolute_percentage_error(y_true,y_pred)
∣ y i − y ^ i ∣ / y i \left|y_{i}-\hat{y}_{i}\right| /y_{i} ∣yi−y^i∣/yi
-
mean_squared_logarithmic_error ( MSE + log)
均方对数误差
mean_aquare_logarithmic_error(y_true,y_pred)
-square_hinge
square_hinge(y_true,y_pred) -
hinge
最常用在SVM中
hinge(y_true,y_pred) -
categorical_hinge
categorical_hinhe(y_true,y_pred) -
logcosh
logcosh(y_true,y_pred) -
categorical_crossentropy
多类的对数损失 -
sparse_categorical_crossentropy
-
binary_crossentropy
逻辑回归,就是常用的交叉熵 -
kullback_leibler_divergence
-
poisson
-
cosine_proximity
别名
mse = MSE = mean_squared_error
mae = MAE = mean_absolute_error
mape = MAPE = mean_absolute_percentage_error
msle = MSLE = mean_squared_logarithmic_error
cosine = cosine_similarity = cosine_proximity
应用
- 通过传递预定义目标函数指定目标函数
- 自定义一个Theano/Tensorflow 函数并使用