Tensorflow 输出层中的softmax函数存在问题

Tensorflow 输出层中的softmax函数存在问题,tensorflow,tensor,softmax,Tensorflow,Tensor,Softmax,我在a中存储了一个张量,如下所示: <tf.Tensor: shape=(5, 1), dtype=float32, numpy= array([[0.1 ], [0.2 ], [0.4 ], [0.15], [0.15]], dtype=float32)> 我得到一个张量,如下所示 <tf.Tensor: shape=(5, 1), dtype=float32, numpy= array([[1.], [

我在
a
中存储了一个张量,如下所示:

<tf.Tensor: shape=(5, 1), dtype=float32, numpy=
array([[0.1 ],
       [0.2 ],
       [0.4 ],
       [0.15],
       [0.15]], dtype=float32)>
我得到一个张量,如下所示

<tf.Tensor: shape=(5, 1), dtype=float32, numpy=
array([[1.],
       [1.],
       [1.],
       [1.],
       [1.]], dtype=float32)>
我得到了预期的张量:

<tf.Tensor: shape=(5, 1), dtype=float32, numpy=
array([[0.1799347 ],
       [0.19885859],
       [0.24288644],
       [0.18916014],
       [0.18916014]], dtype=float32)>

有人能解释一下为什么第一个代码段没有按预期工作吗?

您的输入数组(或张量)具有形状
(5,1)
,默认情况下,
tf.nn.softmax
在最后一个维度上运行。现在您可能会看到问题所在,因为最后一个维度是一个单独的元素,然后使用softmax将其规格化为1.0

您有两个选择:

  • 指定
    axis=0
    tf.nn.softmax
    ,以便在第一个维度而不是最后一个维度上执行操作
  • 重新调整数组的形状,以形成
    (1,5)
    ,这将与对
    tf.nn.softmax的默认调用一起工作

非常感谢。这消除了我对softmax功能如何工作的疑虑。
a_tf = out_put = (tf.exp(a)) / (tf.reduce_sum(tf.exp(a)))
<tf.Tensor: shape=(5, 1), dtype=float32, numpy=
array([[0.1799347 ],
       [0.19885859],
       [0.24288644],
       [0.18916014],
       [0.18916014]], dtype=float32)>