Keras loss函数

'''
Created on 2018-4-16

'''

def compile(
self,
optimizer, #优化器
loss, #损失函数,可以为已经定义好的loss函数名称,也可以为自己写的loss函数
metrics=None, #
sample_weight_mode=None, #如果你需要按时间步为样本赋权(2D权矩阵),将该值设为“temporal”。默认为“None”,代表按样本赋权(1D权),和fit中sample_weight在赋值样本权重中配合使用
weighted_metrics=None, 
target_tensors=None,
**kwargs #这里的设定的参数可以和后端交互。
)

实质调用的是Keras\engine\training.py 中的class Model中的def compile
一般使用model.compile(loss='categorical_crossentropy',optimizer='sgd',metrics=['accuracy'])

# keras所有定义好的损失函数loss:
# keras\losses.py
# 有些loss函数可以使用简称:
# mse = MSE = mean_squared_error
# mae = MAE = mean_absolute_error
# mape = MAPE = mean_absolute_percentage_error
# msle = MSLE = mean_squared_logarithmic_error
# kld = KLD = kullback_leibler_divergence
# cosine = cosine_proximity
# 使用到的数学方法:
# mean:求均值
# sum:求和
# square:平方
# abs:绝对值
# clip:[裁剪替换](https://blog.csdn.net/qq1483661204/article/details)
# epsilon:1e-7
# log:以e为底
# maximum(x,y):x与 y逐位比较取其大者
# reduce_sum(x,axis):沿着某个维度求和
# l2_normalize:l2正则化
# softplus:softplus函数
# 
# import cntk as C
# 1.mean_squared_error:
#     return K.mean(K.square(y_pred - y_true), axis=-1) 
# 2.mean_absolute_error:
#     return K.mean(K.abs(y_pred - y_true), axis=-1)
# 3.mean_absolute_percentage_error:
#     diff = K.abs((y_true - y_pred) / K.clip(K.abs(y_true),K.epsilon(),None))
#     return 100. * K.mean(diff, axis=-1)
# 4.mean_squared_logarithmic_error:
#     first_log = K.log(K.clip(y_pred, K.epsilon(), None) + 1.)
#     second_log = K.log(K.clip(y_true, K.epsilon(), None) + 1.)
#     return K.mean(K.square(first_log - second_log), axis=-1)
# 5.squared_hinge:
#     return K.mean(K.square(K.maximum(1. - y_true * y_pred, 0.)), axis=-1)
# 6.hinge(SVM损失函数):
#     return K.mean(K.maximum(1. - y_true * y_pred, 0.), axis=-1)
# 7.categorical_hinge:
#     pos = K.sum(y_true * y_pred, axis=-1)
#     neg = K.max((1. - y_true) * y_pred, axis=-1)
#     return K.maximum(0., neg - pos + 1.)
# 8.logcosh:
#     def _logcosh(x):
#         return x + K.softplus(-2. * x) - K.log(2.)
#     return K.mean(_logcosh(y_pred - y_true), axis=-1)
# 9.categorical_crossentropy:
#     output /= C.reduce_sum(output, axis=-1)
#     output = C.clip(output, epsilon(), 1.0 - epsilon())
#     return -sum(target * C.log(output), axis=-1)
# 10.sparse_categorical_crossentropy:
#     target = C.one_hot(target, output.shape[-1])
#     target = C.reshape(target, output.shape)
#     return categorical_crossentropy(target, output, from_logits)
# 11.binary_crossentropy:
#     return K.mean(K.binary_crossentropy(y_true, y_pred), axis=-1)
# 12.kullback_leibler_divergence:
#     y_true = K.clip(y_true, K.epsilon(), 1)
#     y_pred = K.clip(y_pred, K.epsilon(), 1)
#     return K.sum(y_true * K.log(y_true / y_pred), axis=-1)
# 13.poisson:
#     return K.mean(y_pred - y_true * K.log(y_pred + K.epsilon()), axis=-1)
# 14.cosine_proximity:
#     y_true = K.l2_normalize(y_true, axis=-1)
#     y_pred = K.l2_normalize(y_pred, axis=-1)
#     return -K.sum(y_true * y_pred, axis=-1)

你可能感兴趣的:(机器学习,keras,损失函数,loss函数,神经网络,机器学习)