Tensorflow 张量运算:tf.softmax()

烂漫一生 提交于 2020-03-11 13:08:10
def softmax(logits, axis=None, name=None):
'''
Computes softmax activations.
This function performs the equivalent of
softmax = tf.exp(logits) / tf.reduce_sum(tf.exp(logits), axis)
'''
import tensorflow as tf 
# 用正态分布来模拟100个样本的预测结果,可以认为是100个MNIST样本
# out = tf.random.normal([100,10])
out = tf.constant([[1.,2,3],
[2,0,1]])
# 计算每一行转换为概率,输出
exp = tf.exp(out)
'''输出
tf.Tensor(
[[ 2.7182817  7.389056  20.085537 ]
 [ 7.389056   1.         2.7182817]], shape=(2, 3), dtype=float32)'''
sum = tf.reduce_sum(exp,axis=1)
'''输出
tf.Tensor([30.192875 11.107338], shape=(2,), dtype=float32)
'''
out = tf.nn.softmax(out, axis=1)
'''输出
tf.Tensor(
[[0.09003057 0.24472848 0.66524094]
 [0.6652409  0.09003057 0.24472846]], shape=(2, 3), dtype=float32)
tf.Tensor([2 0], shape=(2,), dtype=int64)'''

运算过程:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!