深度神经网络——常见的损失函数

分类任务

使用最多的是交叉熵损失函数

多分类任务

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

import tensorflow as tf
y_true=[[0,1,0],[0,0,1]]
y_pre=[[0.05,0.9,0.05],[0.3,0.2,0.5]]
cce=tf.keras.losses.CategoricalCrossentropy()
cce(y_true,y_pre)

<tf.Tensor: shape=(), dtype=float32, numpy=0.39925388>

二分类任务

在这里插入图片描述

y_true=[[0],[1]]
y_pre=[[0.4],[0.6]]
bce=tf.keras.losses.BinaryCrossentropy()
bce(y_true,y_pre)

<tf.Tensor: shape=(), dtype=float32, numpy=0.5108254>

回归任务

MAE损失(L1 Loss)

在这里插入图片描述
在这里插入图片描述

y_true=[[0.],[1.]]
y_pre=[[1.],[0.]]
mae=tf.keras.losses.MeanAbsoluteError()
mae(y_true,y_pre)

<tf.Tensor: shape=(), dtype=float32, numpy=1.0>

MSE损失(L2 Loss)

也称欧式距离
在这里插入图片描述
在这里插入图片描述

y_true=[[0.],[1.]]
y_pre=[[1.],[1.]]
mse=tf.keras.losses.MeanSquaredError()
mse(y_true,y_pre)

<tf.Tensor: shape=(), dtype=float32, numpy=0.5>

smooth L1损失

在这里插入图片描述

y_true=[[0.],[1.]]
y_pre=[[0.2],[0.6]]
smooth=tf.keras.losses.Huber()
smooth(y_true,y_pre)

<tf.Tensor: shape=(), dtype=float32, numpy=0.049999997>

猜你喜欢

转载自blog.csdn.net/qq_40527560/article/details/131493351
今日推荐