Tensorflow2.0 损失函数计算

交叉熵计算(分类)

"""
    分类问题 损失函数的计算
"""
import tensorflow as tf
import numpy as np

# 交叉熵 值越小 越有价值
print("普通函数 计算交叉熵", "--"*30)
a = tf.fill([4], 0.25)
b = - tf.reduce_sum(a * tf.math.log(a) / tf.math.log(2.0))
print(a.numpy(), "--->", b.numpy())

a = tf.constant([0.1, 0.1, 0.1, 0.7])
b = - tf.reduce_sum(a * tf.math.log(a) / tf.math.log(2.0))
print(a.numpy(), "--->", b.numpy())

a = tf.constant([0.01, 0.01, 0.01, 0.97])
b = - tf.reduce_sum(a * tf.math.log(a) / tf.math.log(2.0))
print(a.numpy(), "--->", b.numpy())

print("使用函数计算交叉熵")
# 使用tf函数实现
y_true = np.array([0, 1, 0, 0])
y_predict1 = np.array([0.25, 0.25, 0.25, 0.25])
y_predict2 = np.array([0.1, 0.1, 0.7, 0.1])
y_predict3 = np.array([0.1, 0.7, 0.1, 0.1])
y_predict4 = np.array([0.01, 0.97, 0.01, 0.01])
loss1 = tf.losses.categorical_crossentropy(y_true, y_predict1)
loss2 = tf.losses.categorical_crossentropy(y_true, y_predict2)
loss3 = tf.losses.categorical_crossentropy(y_true, y_predict3)
loss4 = tf.losses.categorical_crossentropy(y_true, y_predict4)
print("y_true:", y_true)
print(y_predict1, "--->", loss1.numpy())
print(y_predict2, "--->", loss2.numpy())
print(y_predict3, "--->", loss3.numpy())
print(y_predict4, "--->", loss4.numpy())


# 类调用方法
criten = tf.losses.CategoricalCrossentropy()
loss5 = criten(y_true, y_predict3)
print("类的调用法:\n",
      y_predict3, "--->", loss5.numpy())

loss6 = criten([0, 1], [0.9, 0.1])
print(loss6)

# 二分类调用
print("二分类交叉熵计算\n", tf.losses.BinaryCrossentropy()([1], [0.1]))  # 类对象方式调用
print(tf.losses.binary_crossentropy([1], [0.1]))  # 函数方式调用

x = tf.random.normal([1, 784])
w = tf.random.normal([784, 2])
b = tf.zeros([2])

logits = x @ w + b

print("模拟真实情况-------------------------------")
print("计算结果:", logits.numpy())

prob = tf.math.softmax(logits, axis=1)
print("softmax处理后:", prob.numpy())

logits = tf.reshape(logits, shape=(logits.shape[1],))
prob = tf.reshape(prob, shape=(prob.shape[1],))

loss1 = tf.losses.categorical_crossentropy([0, 1], logits, from_logits=True)
loss2 = tf.losses.categorical_crossentropy([0, 1], prob)

print("损失结果:", loss1.numpy())
print("损失结果:", loss2.numpy())

在这里插入图片描述

均方误差计算(回归问题)

"""
    MSE 均方差损失
"""
import tensorflow as tf

# 准备数据 y 真实值
y = tf.constant([1, 2, 3, 0, 2])
y = tf.one_hot(y, depth=4)
y = tf.cast(y, dtype=tf.float32)
print("真实值:\n", y.numpy())

# out 模拟预测值
out = tf.random.normal([5, 4])
print("out:\n", out.numpy())

# 不同方式计算损失 均方差
loss1 = tf.reduce_mean(tf.square(y - out))  # 均方差损失函数

loss2 = tf.square(tf.norm(y - out)) / (5.0 * 4.0)  # 范数损失函数

loss3 = tf.reduce_mean(tf.losses.MSE(y, out))  # 均方差损失函数(使用函数)

print("普通函数:", loss1.numpy())
print("范数计算:", loss2.numpy())
print("MSE函数:", loss3.numpy())



在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值