文章目录[隐藏]
![]()
【参考资料】
【1】https://www.tensorflow.org/api_docs/python/tf
备注:走读 https://github.com/akkaze/tf2-yolo3 涉及tf2的备注
1 张量计算广播机制
# 同维度张量相加,对应元素逐个相加
a = tf.ones([1, 2])
b = tf.ones([1, 2])
"""
print(a + b)
tf.Tensor([[2. 2.]], shape=(1, 2), dtype=float32)
"""
# 不同维度张量相加 - 此处应属于 tensorflow2的广播机制
# 不能相加的规则:
# 1)若对应维度的大小不同,且都不为1 不能相加
# 2)若维度形状不同,如(4,2,3)和 (1,3) 则此时低维往高维对应
# 此时 2对1,3对3,可以相加
a = tf.ones([1, 2, 1])
b = tf.ones([1, 3])
"""
print(a)
print(b)
print(a + b)
tf.Tensor(
[[[1.]
[1.]]], shape=(1, 2, 1), dtype=float32)
tf.Tensor([[1. 1. 1.]], shape=(1, 3), dtype=float32)
tf.Tensor(
[[[2. 2. 2.]
[2. 2. 2.]]], shape=(1, 2, 3), dtype=float32)
"""
a = tf.ones([1, 2, 1])
b = tf.ones([2, 3, 1])
#print(a + b) # 出错
a = tf.ones([1, 2, 2])
b = tf.ones([1, 3])
# print(a + b) # 出错
a = tf.ones([4, 2, 3])
b = tf.ones([1, 3])
# print((a + b).shape) # (4, 2, 3)
2 损失函数
2.1 BinaryCrossentropy
# loss = -1/size * sum(y_true * log(y_pred) + (1 - y_true)*log(1 - y_pred))
# 因此下面的例子转换为实际的计算逻辑为:
# 0 * ln(0.5) + 1*ln(1-0.5) = -0.69314718
# 1 * ln(0.5) + 0*ln(1-0.5) = -0.69314718
# loss = - 1/2 * (--0.69314718 + --0.69314718) ~= -0.69314718
bce = tf.keras.losses.BinaryCrossentropy(from_logits=False)
y_true = [0., 1.]
y_pred = [0.5, 0.5]
loss = bce(y_true, y_pred)
# print(np.array(loss)) # 输出 0.69314694
2.2 categorical_crossentropy
# 此处 y_true 代表 one-hot编码
# loss = -sum(y_true*ln(y_pred))
# 因此下面的例子转换为实际的计算逻辑为:
# -1 * [0*ln(0.05) + 1*ln(0.95) + 0.ln(0)] = -ln(0.95) = 0.0512
# -1 * [0*ln(0.1) + 0*ln(0.8) + 1*ln(0.1)] = -ln(0.1) = 2.302585
y_true = [[0, 1, 0], [0, 0, 1]]
y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
loss = tf.keras.losses.categorical_crossentropy(y_true, y_pred)
#print(loss.numpy()) # [0.05129331 2.3025851 ]
2.3 sparse_categorical_crossentropy
# 与 categorical_crossentropy 不同 y_true 不需要是one-hot编码
# 如下面的例子 y_true 表示有1,2两种分类
# 则y_pred预测为 1的概率 2的概率 同时不为1和2的概率
y_true = [1, 2]
y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
loss = tf.keras.losses.sparse_categorical_crossentropy(y_true, y_pred)
#print(loss.numpy()) # [0.05129344 2.3025851 ]
2.4 reduce_sum
a = tf.random.uniform([2, 3])
# print(a)
# print(tf.reduce_sum(a)) #全部元素相加
# print(tf.reduce_sum(a, 0)) # 从0维度元素相加,即 0.24254632 + 0.9145287
# print(tf.reduce_sum(a, 1)) # 从1维度元素相加,即 0.24254632 + 0.64041877 + 0.15211129
"""
tf.Tensor(
[[0.24254632 0.64041877 0.15211129]
[0.9145287 0.5195917 0.44804764]], shape=(2, 3), dtype=float32)
tf.Tensor(2.9172444, shape=(), dtype=float32)
tf.Tensor([1.157075 1.1600105 0.60015893], shape=(3,), dtype=float32)
tf.Tensor([1.0350764 1.882168 ], shape=(2,), dtype=float32)
"""
2.4 reduce_max
a = tf.random.uniform([2, 3])
print(a)
print(tf.reduce_max(a)) #全部元素最大值
print(tf.reduce_max(a, 0)) # 从0维度元素取最大值
print(tf.reduce_max(a, 1)) # 从1维度元素取最大值
"""
tf.Tensor(
[[2.4550915e-02 7.6561868e-01 7.9512596e-04]
[8.3080280e-01 2.7695847e-01 8.6583126e-01]], shape=(2, 3), dtype=float32)
tf.Tensor(0.86583126, shape=(), dtype=float32)
tf.Tensor([0.8308028 0.7656187 0.86583126], shape=(3,), dtype=float32)
tf.Tensor([0.7656187 0.86583126], shape=(2,), dtype=float32)
"""
版权声明:本文为CSDN博主「Fred-XU」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Fredric_2014/article/details/122702479
![]()

暂无评论