官网链接:https://tensorflow.google.cn/api_docs/python/tf/nn/softmax
tf.nn.softmax(
logits,
axis=None,
name=None,
dim=None # 废弃
)
logits
是一个张量,数据类型必须是half
, float32
, float64
s
o
f
t
m
a
x
=
e
l
o
g
i
t
s
∑
e
l
o
g
i
t
s
softmax=\frac{e^{logits}}{\sum_{}^{}{e^{logits}}}
softmax=∑elogitselogits
以上图为例:
import tensorflow as tf
x = tf.constant([[3., 1., -3.]])
tf.global_variables_initializer()
with tf.Session() as sess:
print(sess.run(tf.nn.softmax(x)))
[[0.87887824 0.11894324 0.00217852]]
注意,官网指定的用法,是可以指定哪个轴的,这个暂时没自己尝试
# 官网给的公式
softmax = tf.exp(logits) / tf.reduce_sum(tf.exp(logits), axis)