Tensorflow2.0笔记
本博客为Tensorflow2.0学习笔记,感谢北京大学微电子学院曹建老师
7.3 损失函数
tf.keras.losses.MSE
tf.keras.losses.MSE(
y_true,
y_pred
)
功能:计算y_true和y_pred的均方误差.
示例:
y_true = tf.constant([0.5, 0.8])
y_pred = tf.constant([1.0, 1.0])
print(tf.keras.losses.MSE(y_true, y_pred))
>>> tf.Tensor(0.145, shape=(), dtype=float32)
# 等价实现
print(tf.reduce_mean(tf.square(y_true - y_pred)))
>>> tf.Tensor(0.145, shape=(), dtype=float32)
tf.keras.losses.categorical_crossentropy
tf.keras.losses.categorical_crossentropy(
y_true, y_pred, from_logits=False, label_smoothing=0
)
功能:计算交叉熵.
等价API:tf.losses.categorical_crossentropy
参数:
y_true: 真实值
y_pred: 预测值
from_logits: y_pred是否为logits张量
label_smoothing: [0,1]之间的小数
返回:交叉熵损失值.
链接: tf.keras.losses.categorical_crossentropy
y_true = [1, 0, 0]
y_pred1 = [0.5, 0.4, 0.1]
y_pred2 = [0.8, 0.1, 0.1]
print(tf.keras.losses.categorical_crossentropy(y_true, y_pred1)) print(tf.keras.losses.categorical_crossentropy(y_true, y_pred2))
>>> tf.Tensor(0.6931472, shape=(), dtype=float32)
tf.Tensor(0.22314353, shape=(), dtype=float32)
# 等价实现
print(-tf.reduce_sum(y_true * tf.math.log(y_pred1)))
print(-tf.reduce_sum(y_true * tf.math.log(y_pred2)))
>>> tf.Tensor(0.6931472, shape=(), dtype=float32)
tf.Tensor(0.22314353, shape=(), dtype=float32)
tf.nn.softmax_cross_entropy_with_logits
tf.nn.softmax_cross_entropy_with_logits(
labels, logits, axis=-1, name=None
)
功能:logits经过softmax后,与labels进行交叉熵计算
参数:
labels: 在类别这一维度上,每个向量应服从有效的概率分布. 例如,在labels的shape为[batch_size, num_classes]的情况下,labels[i]应服从概率分布
logits: 每个类别的激活值,通常是线性层的输出. 激活值需要经过softmax归一化. axis: 类别所在维度,默认是-1,即最后一个维度.
axis: 类别所在维度,默认是-1,即最后一个维度
返回:softmax交叉熵损失值.
链接: tf.nn.softmax_cross_entropy_with_logits
labels = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
logits = [[4.0, 2.0, 1.0], [0.0, 5.0, 1.0]]
print(tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits))
>>> tf.Tensor([0.16984604 0.02474492], shape=(2,), dtype=float32)
# 等价实现
print(-tf.reduce_sum(labels * tf.math.log(tf.nn.softmax(logits)), axis=1))
>>> tf.Tensor([0.16984606 0.02474495], shape=(2,), dtype=float32)
tf.nn.sparse_softmax_cross_entropy_with_logits
tf.nn.sparse_softmax_cross_entropy_with_logits(
labels, logits, name=None
)
功能:labels经过one-hot编码,logits经过softmax,两者进行交叉熵计算. 通常labels的shape为[batch_size],logits的shape为[batch_size, num_classes]. sparse 可理解为对labels进行稀疏化处理(即进行one-hot编码).
参数:
labels: 标签的索引值
logits: 每个类别的激活值,通常是线性层的输出. 激活值需要经过softmax归一化
返回:softmax交叉熵损失值.
链接: tf.nn.sparse_softmax_cross_entropy_with_logits
例子:(下例中先对labels进行one-hot编码为[[1,0,0], [0,1,0]],logits经过softmax变为[[0.844, 0.114,0.042], [0.007,0.976,0.018]],两者再进行交叉熵运算)
labels = [0, 1]
logits = [[4.0, 2.0, 1.0], [0.0, 5.0, 1.0]]
print(tf.nn.sparse_softmax_cross_entropy_with_logits(labels1, logits))
>>> tf.Tensor([0.16984604 0.02474492], shape=(2,), dtype=float32)
# 等价实现
print(-tf.reduce_sum(tf.one_hot(labels, tf.shape(logits)[1]) * tf.math.log(tf.nn.softmax(logits)), axis=1))
>>> tf.Tensor([0.16984606 0.02474495], shape=(2,), dtype=float32)
7.4 其他
tf.cast
tf.cast(
x, dtype, name=None
)
功能:转换数据(张量)类型。
参数:
x: 待转换的数据(张量)
dtype: 目标数据类型
name: 定义操作的名称(可选参数)
返回:数据类型为dtype,shape与x相同的张量.
链接: tf.cast
示例:
x = tf.constant([1.8, 2.2], dtype=tf.float32)
print(tf.cast(x, tf.int32))
>>> tf.Tensor([1 2], shape=(2,), dtype=int32)
tf.random.normal
tf.random.normal(
shape, mean=0.0, stddev=1.0, dtype=tf.dtypes.float32, seed=None, name=None
)
功能:生成服从正态分布的随机值。
参数:
x: 一维张量
mean: 正态分布的均值
stddev: 正态分布的方差
返回:满足指定shape并且服从正态分布的张量.
链接: tf.random.normal
示例:
tf.random.normal([3, 5])
tf.where
tf.where(
condition, x=None, y=None, name=None
)
功能:根据condition,取x或y中的值。如果为True,对应位置取x的值;如果为False,对应位置取y的值。
参数:
condition: bool型张量.
x: 与y shape相同的张量
y: 与x shape相同的张量
返回:
shape与x相同的张量
链接: tf.where
示例;
print(tf.where([True, False, True, False], [1,2,3,4], [5,6,7,8]))
>>> tf.Tensor([1 6 3 8], shape=(4,), dtype=int32)