9

我正在尝试实现类似完全卷积网络的东西,其中最后一个卷积层使用过滤器大小 1x1 并输出“分数”张量。分数张量的形状为 [Batch, height, width, num_classes]。

我的问题是,tensorflow 中的什么函数可以对每个像素应用 softmax 操作,而与其他像素无关。tf.nn.softmax 操作似乎不是为了这个目的。

如果没有这样的操作可用,我想我必须自己写一个。

谢谢!

更新:如果我必须自己实现,我想我可能需要将输入张量重塑为 [N, num_claees] 其中 N = Batch x width x height,并应用 tf.nn.softmax,然后将其重新整形。是否有意义?

4

2 回答 2

4

就像你猜的那样,将其重塑为 2d,然后再将其重塑回来,是正确的方法。

于 2016-04-25T20:45:11.147 回答
1

您可以使用此功能。

我是通过从GitHub搜索找到的。

import tensorflow as tf

"""
Multi dimensional softmax,
refer to https://github.com/tensorflow/tensorflow/issues/210
compute softmax along the dimension of target
the native softmax only supports batch_size x dimension
"""
def softmax(target, axis, name=None):
    with tf.name_scope(name, 'softmax', values=[target]):
        max_axis = tf.reduce_max(target, axis, keep_dims=True)
        target_exp = tf.exp(target-max_axis)
        normalize = tf.reduce_sum(target_exp, axis, keep_dims=True)
        softmax = target_exp / normalize
        return softmax
于 2016-12-22T11:53:55.177 回答