从softmax中删除低质量张量预测

时间:2017-09-15 14:02:07

标签: python numpy tensorflow tensor

我想将过滤器应用于张量并删除不符合我标准的值。例如,假设我有一个看起来像这样的张量:

softmax_tensor = [[ 0.05 , 0.05, 0.2, 0.7], [ 0.25 , 0.25, 0.3, 0.2 ]]

现在,分类器选择张量的argmax进行预测:

predictions = [[3],[2]]

但这并不是我想要的,因为我放弃了关于该预测的信心的信息。我宁愿做出预测而不是做出不正确的预测。所以我想做的是返回过滤后的张量,如下:

new_softmax_tensor = [[ 0.05 , 0.05, 0.2, 0.7]]
new_predictions    = [[3]]

如果这是直接的python,我没有遇到麻烦:

new_softmax_tensor = []
new_predictions    = []

for idx,listItem in enumerate(softmax_tensor):
    # get two highest max values and see if they are far enough apart
    M  = max(listItem)
    M2 = max(n for n in listItem if n!=M)
    if M2 - M > 0.3: # just making up a criteria here
        new_softmax_tensor.append(listItem) 
        new_predictions.append(predictions[idx])

但鉴于张量流对张量有效,我不知道该怎么做 - 如果我这样做了,它会打破计算图吗?

previous SO post建议使用tf.gather_nd,但在这种情况下,他们已经有了一个他们想要过滤的张量。我也看过tf.cond,但仍然不明白。我想,很多其他人会从这个完全相同的解决方案中受益。

谢谢大家。

2 个答案:

答案 0 :(得分:0)

我会采取两件措施来解决你的问题:

首先,我将返回softmax张量的值。你在某个地方寻找它的引用(你在创建它时保持对它的引用,或者你在适当的张量集合中找到它)然后在sess.run([softmaxtensor,prediction],feed_dict=..)中对它进行评估然后你用python来玩它尽可能多。

第二个如果你想留在图表中,我会使用build-it tf.where(),与numpy包doc there中的np.where函数非常相似

答案 1 :(得分:0)

确定。我现在整理好了。这是一个有效的例子。

import tensorflow as tf

#Set dummy example tensor
original_softmax_tensor = tf.Variable([
    [0.4,0.2,0.2,0.9,0.1],
    [0.5,0.2,0.2,0.9,0.1],
    [0.6,0.2,0.2,0.1,0.99],
    [0.1,0.8,0.2,0.09,0.99]
    ],name='original_softmax_tensor')

#Set dummy prediction tensor
original_predictions    = tf.Variable([3,3,4,4],name='original_predictions')

#Now create a place to store my new variables
new_softmax_tensor = original_softmax_tensor
new_predictions    = original_predictions


#set my cutoff variable
min_diff = tf.constant(0.3)

#initialize
init_op = tf.global_variables_initializer()


with tf.Session() as sess:
    sess.run(init_op) #execute init_op
    #There's probably a better way to do this, but I had to do this hack to get
    # the difference between the top 2 scores
    tmp_diff1, _ = tf.nn.top_k(original_softmax_tensor,k=2,sorted=True)
    tmp_diff2, _ = tf.nn.top_k(original_softmax_tensor,k=1,sorted=True)
    #subtracting the max scores from both, makes the largest one '0'
    actual_diff = tf.subtract(tmp_diff2,tmp_diff1)
    #The max value for each will be the actual value of interest
    actual_diff = tf.reduce_max(actual_diff,reduction_indices=[1])
    #Create a boolean tensor that says to keep or not
    cond_result = actual_diff  > min_diff
    #Keep only the values I want
    new_predictions = tf.boolean_mask(original_predictions,cond_result)
    new_softmax_tensor = tf.boolean_mask(new_softmax_tensor,cond_result)
    new_predictions.eval()
    new_softmax_tensor.eval()
    # return these if this is in a function