【Keras】损失函数与评价指标详解

2020.12.03更新:pytorch不香么…

1、损失函数

先贴官网链接:https://keras.io/api/losses/

变量定义:

  • y_true: 真实标签
  • y_pred: 预测值

损失函数的分类:

Probabilistic losses
BinaryCrossentropy class
  • y_true,y_pred的shape都是 [batch_size]
  • y_true的不是0就1,整型
#  输入示例
#  y_true = [[0., 1.], [0., 0.]]
#  y_pred = [[0.6, 0.4], [0.4, 0.6]]
tf.keras.losses.BinaryCrossentropy(
    from_logits=False, label_smoothing=0, reduction="auto", name="binary_crossentropy"
)
CategoricalCrossentropy class
  • y_true,y_pred的shape都是 [batch_size, num_classes]
  • y_true 是 one_hot 形式
# 输入示例
# y_true = [[0, 1, 0], [0, 0, 1]]
# y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
tf.keras.losses.CategoricalCrossentropy(
    from_logits=False,
    label_smoothing=0,
    reduction="auto",
    name="categorical_crossentropy",
)
CategoricalCrossentropy class
  • y_true,y_pred的shape分别是,[batch_size] , [batch_size, num_classes]
  • y_true 是整型,相比 CategoricalCrossentropy class 使用简便
# y_true = [1, 2]
# y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
tf.keras.losses.SparseCategoricalCrossentropy(
    from_logits=False, reduction="auto", name="sparse_categorical_crossentropy"
)
Poisson class

泊松损失
参考:泊松回归详细介绍
定义:

loss = y_pred - y_true * log(y_pred)   
KLDivergence class

KL散度:
定义:loss = y_true * log(y_true / y_pred)
参考:https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence

Regression losses
MeanSquaredError class

均方差

loss = square(y_true - y_pred)
MeanAbsoluteError class

绝对值均差

loss = abs(y_true - y_pred)
MeanAbsolutePercentageError class

绝对值相对差

loss = 100 * abs(y_true - y_pred) / y_true
MeanSquaredLogarithmicError class

词穷了,咋翻译?

loss = square(log(y_true + 1.) - log(y_pred + 1.))
CosineSimilarity class

余弦相似度

loss = -sum(l2_norm(y_true) * l2_norm(y_pred))
Huber class

去掉离群点,减轻离群点对loss的影响

loss = 0.5 * x^2                  if |x| <= d
loss = 0.5 * d^2 + d * (|x| - d)  if |x| > d
Huber class
logcosh = log((exp(x) + exp(-x))/2), where x is the error y_pred - y_true.

2、Metrics

Keras的官方doc竟然也把loss作为metrics

Classification metrics based on True/False positives & negatives
AUC class (Area under the curve)
tf.keras.metrics.AUC(
    num_thresholds=200,
    curve="ROC",
    summation_method="interpolation",
    name=None,
    dtype=None,
    thresholds=None,
    multi_label=False,
    label_weights=None,
)
Precision class
Recall class
PrecisionAtRecall class

计算当 recall is >= specified value 时的 precision

TruePositives class,TrueNegatives class, FalsePositives class, FalseNegatives class
SensitivityAtSpecificity class

https://en.wikipedia.org/wiki/Sensitivity_and_specificity

SpecificityAtSensitivity class

https://en.wikipedia.org/wiki/Sensitivity_and_specificity

  • 7
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
keras.Model是Keras中的一个重要类,可以用来构建模型。它是一个包含多个层的对象,可以用来进行训练、评估和预测。 keras.Model的主要参数: 1. inputs:模型的输入张量或输入张量的列表。如果模型只有一个输入,则为单个张量。如果模型有多个输入,则为张量列表。 2. outputs:模型的输出张量或输出张量的列表。如果模型只有一个输出,则为单个张量。如果模型有多个输出,则为张量列表。 3. name:模型的名称。 4. trainable:布尔值,指示是否训练模型中的权重。 5. **kwargs:其他参数。 keras.Model的常用方法: 1. compile(optimizer, loss, metrics=None, loss_weights=None, sample_weight_mode=None, weighted_metrics=None, target_tensors=None, **kwargs):编译模型,指定优化器、损失函数和评估指标等。 2. fit(x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None, validation_batch_size=None, **kwargs):使用输入数据和标签来训练模型。 3. evaluate(x=None, y=None, batch_size=None, verbose=1, sample_weight=None, steps=None, callbacks=None, **kwargs):使用输入数据和标签来评估模型。 4. predict(x, batch_size=None, verbose=0, steps=None, callbacks=None, **kwargs):使用输入数据来预测模型的输出。 5. summary(line_length=None, positions=None, print_fn=None):打印模型的摘要信息。 6. save(filepath, overwrite=True, include_optimizer=True, save_format=None, signatures=None, options=None):将模型保存到文件中。 7. load(filepath, custom_objects=None, compile=True, options=None):从文件中加载模型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值