@创建于:20210415
@修改于:20210415
1、背景
loss 和 metrics 在compile接口中同时存在,同时默认值都是None,那么这两个到底什么作用?
def compile(self,
optimizer='rmsprop',
loss=None,
metrics=None,
loss_weights=None,
weighted_metrics=None,
run_eagerly=None,
**kwargs):
2、loss官网介绍
The purpose of loss functions is to compute the quantity that a model should seek to minimize during training.
翻译:损失函数的目的是计算模型在训练过程中最小化的数值。
实际的优化目标是所有数据点输出数组的平均值。
3、metrics官网介绍
A metric is a function that is used to judge the performance of your model.
Metric functions are similar to loss functions, except that the results from evaluating a metric are not used when training the model. Note that you may use any loss function as a metric.
翻译:
指标是用于判断模型性能的函数。
度量标准函数与损失函数相似,不同之处在于训练模型时不使用评估度量标准的结果。 请注意,您可以使用任何损失函数作为指标。
4、loss与metrics对比理解
项目 | loss | metrics |
---|---|---|
概念 | loss是整体网络进行优化的目标, 是需要参与到优化运算,更新权值W的过程的 | metric只是作为评价网络表现的一种“指标”, 比如accuracy,是为了直观地了解算法的效果,充当view的作用,并不参与到优化过程 |
必要性 | 必须有 | 可以没有 |
一个模型中的数量 | 有且仅有一个(loss是单数形式) | 可以是多个,1个或者没有(metrics是复数形式) |
可导性 | 通过梯度下降的方法进行反向传播。 这里使用的loss指标必须是可导的,比如mse | 没有要求 |
自定义 | 支持自定义 | 支持自定义 |
4.1 loss常用方法
参考自:https://keras.io/zh/losses/
更多细节参考:https://keras.io/api/losses/
连续型:
mean_squared_error 或 mse
mean_absolute_error 或 mae
mean_absolute_percentage_error 或 mape
mean_squared_logarithmic_error 或 msle
squared_hinge
hinge
categorical_hinge
logcosh 预测误差的双曲余弦的对数。
类别型:
categorical_crossentropy: 亦称作多类的对数损失,注意使用该目标函数时,需要将标签转化为形如(nb_samples, nb_classes)的二值序列
sparse_categorical_crossentropy
binary_crossentropy (亦称作对数损失,logloss)
kullback_leibler_divergence
poisson
cosine_proximity 即预测值与真实标签的余弦距离平均值的相反数
keras.Sequential.compile(loss='目标函数 ', optimizer=‘adam’, metrics=[‘accuracy’])
4.2 metrics常用方法
中文:https://keras.io/zh/metrics/
英文:https://keras.io/api/metrics/
binary_accuracy
categorical_accuracy
sparse_categorical_accuracy
top_k_categorical_accuracy
sparse_top_k_categorical_accuracy
自定义评价函数
4.3 使用方法
compile方法中的使用
# 方法1
model.compile(loss='mean_squared_error',
optimizer='sgd',
metrics=['mae', 'acc'])
# 方法2
from keras import metrics
model.compile(loss='mean_squared_error',
optimizer='sgd',
metrics=[metrics.mae, metrics.categorical_accuracy])
5、参考资料
官网资料
英文:https://keras.io/api/losses/
英文:https://keras.io/api/metrics/
中文:https://keras.io/zh/losses/
中文:https://keras.io/zh/metrics/
keras.Sequential.compile(loss='目标函数 ', optimizer=‘adam’, metrics=[‘accuracy’])
Keras.metrics中的accuracy总结