import tensorflow as tf
softmax 将值的向量转换为概率分布
inputs = tf.random.normal(shape=(3, 2))
inputs
<tf.Tensor: shape=(3, 2), dtype=float32, numpy= array([[ 1.1692467 , -0.9458285 ], [-0.16728269, -1.9227076 ], [-0.5457223 , 0.1041782 ]], dtype=float32)>
outputs = tf.keras.activations.softmax(inputs)
outputs
<tf.Tensor: shape=(3, 2), dtype=float32, numpy= array([[0.89235985, 0.10764022], [0.85263574, 0.14736427], [0.34301195, 0.656988 ]], dtype=float32)>
tf.reduce_sum(outputs[0, :]) # 总概率为1
<tf.Tensor: shape=(), dtype=float32, numpy=1.0000001>