cnn中的tf.nn.softmax是什么意思

问题描述

我用网上的一些教程实现了一个用于图像分类的CNN,发现了softmax的这个功能,没看懂

score = tf.nn.softmax(predictions[0])

当我使用它时,我发现了一些我不理解其含义的值

谁能解释一下这个函数,它的用途是什么!

解决方法

嗯,一个 softmax 函数可以将您的 logits 映射到一个百分比,通常用于多类分类问题,该百分比总和为 1

函数可以这样计算

e.g [1. 0. 1.] -> [0.3,0.4,0.3]
softmax = tf.exp(logits) / tf.reduce_sum(tf.exp(logits),axis) # Took from the official tensorflow site

典型用法

import tensorflow as tf

logits = model(data) # make a forward pass(prediction)
predictions = tf.nn.softmax(logits) # so we know the score of our predictions
print(predictions.max())