DataLearner logoDataLearnerAI
Latest AI Insights
Model Evaluations
Model Directory
Model Comparison
Resource Center
Tools

加载中...

DataLearner logoDataLearner AI

A knowledge platform focused on LLM benchmarking, datasets, and practical instruction with continuously updated capability maps.

产品

  • Leaderboards
  • 模型对比
  • Datasets

资源

  • Tutorials
  • Editorial
  • Tool directory

关于

  • 关于我们
  • 隐私政策
  • 数据收集方法
  • 联系我们

© 2026 DataLearner AI. DataLearner curates industry data and case studies so researchers, enterprises, and developers can rely on trustworthy intelligence.

隐私政策服务条款
  1. Home/
  2. Blog List/
  3. Blog Detail

tf.nn.softmax_cross_entropy_with_logits函数

2019/03/27 21:17:48
4,558 views
tensorflowtf.nn.softmax_cross_entropy_with_logits函数

        tf.nn.softmax_cross_entropy_with_logits函数在TensorFlow中计算分类问题交叉熵损失函数时会用到。

        这个函数的返回值不是一个数,而是一个向量。如果要求最终的交叉熵损失,我们需要再做一步tf.reduce_sum操作,即对向量中的所有元素求和。

        让我们以具体示例来说明tf.nn.softmax_cross_entropy_with_logits函数是怎样工作的:

#--coding:utf-8-- """ @author:taoshouzheng @time:2019/3/25 20:29 @email:tsz1216@sina.com """

import tensorflow as tf

"""定义计算图中的计算"""

神经网络的最后一层的输出:三条样本在神经网络中的输出

logits = tf.constant([[1.0, 2.0, 3.0], [1.0, 2.0, 3.0], [1.0, 2.0, 3.0]]) # 常量

样本的真实标签,用one-hot编码形式表示:三条样本的真实标签

y_ = tf.constant([[0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0]])

1(1):softmax操作

y = tf.nn.softmax(logits)

1(2):cross_entropy操作

y_mul = -(y_ * tf.log(y)) # 所有样本的真实标签乘以其预测概率的对数 cross_entropy = tf.reduce_sum(y_mul) # 计算所有元素之和作为损失函数

2(1):使用tf.nn.softmax_cross_entropy_with_logits

softmax_cross_entropy_with_logits = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y_) cross_entropy_2 = tf.reduce_sum(softmax_cross_entropy_with_logits) # 计算所有元素之和作为损失函数

在会话中执行计算

with tf.Session() as sess: softmax = sess.run(y) print('tf.nn.softmax(logits):') print(softmax) print('-------------------------------') cross_entropy = sess.run(cross_entropy) y_mul = sess.run(y_mul) print('- y_ * tf.log(y):') print(y_mul) print('tf.reduce_sum(y_mul):') print(cross_entropy) print('-------------------------------') softmax_cross_entropy_with_logits = sess.run(softmax_cross_entropy_with_logits) print('tf.nn.softmax_cross_entropy_with_logits:') print(softmax_cross_entropy_with_logits) cross_entropy2 = sess.run(cross_entropy_2) print('tf.reduce_sum(softmax_cross_entropy_with_logits):') print(cross_entropy2)

        这里我们以两种方式计算三条样本的交叉熵,结果如下:

tf.nn.softmax(logits): [[0.09003057 0.24472848 0.66524094] [0.09003057 0.24472848 0.66524094] [0.09003057 0.24472848 0.66524094]]

  • y_ * tf.log(y): [[0. 0. 0.407606 ] [0. 0. 0.407606 ] [0. 0. 0.40760598]] tf.reduce_sum(y_mul): 1.222818

tf.nn.softmax_cross_entropy_with_logits: [0.40760595 0.40760595 0.40760595] tf.reduce_sum(softmax_cross_entropy_with_logits): 1.2228179         可以看到tf.nn.softmax_cross_entropy_with_logits函数内部所做的操作其实就是将每条样本真实标签的ont-hot向量与其预测概率的对数相乘,将结果按元素取负号,然后再按样本将元素逐行相加。

        注意:这个函数的返回值不是一个数,而是一个向量。如果要求最终的交叉熵损失,我们需要再做一步tf.reduce_sum操作,即对向量中的所有元素求和。

        谢谢!

参考了以下内容:

tf.nn.softmax_cross_entropy_with_logits的用法

DataLearner WeChat

Follow DataLearner WeChat for the latest AI updates

DataLearner 官方微信二维码
Back to Blog List

Related Blogs

  • 谷歌官方高性能大规模高维数据处理库TensorStore发布!
  • Stable Diffusion的Tensorflow/Keras实现及使用
  • TensorFlow与PyTorch近几年发展对比
  • TensorFlow中常见的错误解释及解决方法
  • Tensorflow中数据集的使用方法(tf.data.Dataset)
  • Tensorflow自定义训练模型的样例写法
  • Tensorflow中关于tf.metrics的返回值详解及其用法以及它和tf.losses的区别
  • 自定义Tensorflow模型的训练

Hot Blogs

  • 1Dirichlet Distribution(狄利克雷分布)与Dirichlet Process(狄利克雷过程)
  • 2回归模型中的交互项简介(Interactions in Regression)
  • 3贝塔分布(Beta Distribution)简介及其应用
  • 4矩母函数简介(Moment-generating function)
  • 5普通最小二乘法(Ordinary Least Squares,OLS)的详细推导过程
  • 6使用R语言进行K-means聚类并分析结果
  • 7深度学习技巧之Early Stopping(早停法)
  • 8手把手教你本地部署清华大学的ChatGLM-6B模型——Windows+6GB显卡本地部署