如何在张量流中的两个选定维度中执行tf.nn.softmax?

问题描述 投票:0回答:2

我想为形状为tf.nn.softmax()的张量的选定二维实现(batch_size=?, height, width, channel)

但是tf.nn.softmax()似乎不可能在同一时间接收2轴。使用tf.softmax(tensor, axis=[1, 2])会在张量流中引起轴误差。

如何在矢量化模式下优雅地实现这一点? thx:D

tensorflow softmax
2个回答
0
投票

我不是一次传递两个维度,而是首先相应地重塑输入,例如:

array = tf.constant([[1., 2.], [3., 4.]])

tf.nn.softmax(array, axis=0) # Calculate for axis 0
tf.nn.softmax(array, axis=1) # Calculate for axis 1

tf.nn.softmax(tf.reshape(array, [-1])) # Calculate for both axes

0
投票

你可以做

array = np.random.rand(1, 2, 2, 1)
s1 = tf.nn.softmax(array, axis=1)
s2 = tf.nn.softmax(array, axis=2)
rs = tf.reduce_sum([s1, s2], 0)

这将返回与初始数组相同形状的张量

© www.soinside.com 2019 - 2024. All rights reserved.