2016-04-25 68 views
7

我试图实现类似完全卷积网络的东西,其中最后的卷积层使用过滤器大小1x1并输出'分数'张量。得分张量具有形状[批次,高度,宽度,num_classes]。完全卷积网络的每像素softmax

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

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

谢谢!

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

+0

您可以将其重塑为2d矩阵,做softmax,然后重新塑形。 – Aaron

+0

啊哈,与您的意见同时更新。但是,谢谢! –

+0

嘿,你终于实现了这个?我被困在这里。 –

回答

3

将它重塑成2d,然后重塑它,就像你猜测的那样,是正确的做法。

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 
相关问题