3-Tensorflow-demo_10_01-softmax和交叉熵使用

本文演示了如何使用TensorFlow实现Softmax函数和交叉熵损失函数,包括单个样本的多分类交叉熵计算,以及如何通过不同API进行计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



import tensorflow as tf
import numpy as np

def softmax_func():
    """
    演示tf.nn.softmax激活函数
    :return:
    """
    with tf.Graph().as_default():
        input_logits = tf.placeholder(tf.float32)


        # 对Logits使用softmax激活函数
        prediction = tf.nn.softmax(input_logits)
        # 求预测的真实标签。
        pred_true_label = tf.argmax(prediction)

        with tf.Session() as sess:
            logits_data = [2.0, 1.0, 5.1]
            prediction_, pred_true_label_ = sess.run(
                [prediction, pred_true_label], feed_dict={input_logits: logits_data}
            )
            print(prediction_, pred_true_label_)

def cross_entropy_compute():
    """
    自己实现单个样本的多分类交叉熵损失函数
    :return:
    """
    with tf.Graph().as_default():
        logits = tf.placeholder(tf.float32)
        one_hot = tf.placeholder(tf.float32)
        true_label = tf.placeholder(tf.int32)

        logits_data = [2.7, 3.2, 0.8]
        one_hot_data = [1.0, 0.0, 0.0]

        # 计算预测概率
        prediction = tf.nn.softmax(logits)

        # 一、自己实现交叉熵损失
        cross_entropy = -tf.reduce_sum(tf.log(prediction) * one_hot)

        # 二、调用tf的api来求交叉熵损失
        cross_entropy1 = tf.nn.softmax_cross_entropy_with_logits(
            logits=logits, labels=one_hot
        )

        # todo 该api传入的标签,是没有做过one-hot的真实标签
        cross_entropy2 = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=true_label, logits=logits
        )
        with tf.Session() as sess:
            feed = {logits: logits_data, one_hot: one_hot_data}
            print(sess.run(cross_entropy, feed_dict=feed))
            print(sess.run(cross_entropy1, feed_dict=feed))
            feed = {logits: logits_data, true_label: 0}
            print(sess.run(cross_entropy2, feed_dict=feed))


if __name__ == '__main__':
    # softmax_func()
    cross_entropy_compute()
D:\Anaconda\python.exe D:/AI20/HJZ/04-深度学习/2-TensorFlow基础/tf-代码/10_01softmax和交叉熵使用.py
WARNING:tensorflow:From D:/AI20/HJZ/04-深度学习/2-TensorFlow基础/tf-代码/10_01softmax和交叉熵使用.py:48: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:

Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.

See @{tf.nn.softmax_cross_entropy_with_logits_v2}.

2019-12-05 21:39:56.942095: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
1.0290085
1.0290084
1.0290084

Process finished with exit code 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值