1 均方差损失函数:MSE¶
均方误差(Mean Square Error),应该是最常用的误差计算方法了,数学公式为: $$loss = \frac{1}{N}\sum { { {(y - pred)}^2}} $$
其中,$y$是真实值,$pred$是预测值,$N$通常指的是batch_size,也有时候是指特征属性个数。
In [1]:
import tensorflow as tf
y = tf.random.uniform((5,),maxval=5,dtype=tf.int32) # 假设这是真实值
print(y)
y = tf.one_hot(y,depth=5) # 转为热独编码
print(y)
tf.Tensor([2 4 4 0 2], shape=(5,), dtype=int32) tf.Tensor( [[0. 0. 1. 0. 0.] [0. 0. 0. 0. 1.] [0. 0. 0. 0. 1.] [1. 0. 0. 0. 0.] [0. 0. 1. 0. 0.]], shape=(5, 5), dtype=float32)
In [2]:
y
Out[2]:
<tf.Tensor: id=7, shape=(5, 5), dtype=float32, numpy= array([[0., 0., 1., 0., 0.], [0., 0., 0., 0., 1.], [0., 0., 0., 0., 1.], [1., 0., 0., 0., 0.], [0., 0., 1., 0., 0.]], dtype=float32)>