2017-09-15 106 views
1

我想对张量应用过滤器并删除不符合我的标准的值。例如,可以说我有一个张,看起来像这样:从softmax中删除低质量张量预测

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

眼下,分类挑选张量argmax预测:

predictions = [[3],[2]]

但是这ISN”这正是我想要的,因为我放弃了有关该预测信心的信息。我宁愿不做出预测,也不愿做出错误的预测。所以,我希望做的是返回过滤张量,像这样:

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

如果这是直线上升的蟒蛇,我有没有问题:

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]) 

但鉴于tensorflow上工作的张量,我不知道如何做到这一点 - 如果我这样做,它会打破计算图吗?

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

谢谢大家。

回答

0

好了不太一样的np.where功能工作。我已经把它整理出来了。这是一个工作示例。

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 
0

两件事情,我会做的为您解决问题:

首先,我将返回SOFTMAX张量的值。你在某个地方寻找它的引用(当你创建它的时候你可以参考它,或者你找到它在适当的张量集合中)然后在sess.run([softmaxtensor,prediction],feed_dict=..)中评估它然后你用python和它一起玩喜欢。

其次如果你想留在图形中,我会使用build-它tf.where(),从numpy的包doc there

+0

你的意思是这样吗? '' #Compute顶部2 SOFTMAX分数 actual_diff = tf.subtract(tf.nn.top_k(softmax_tensor中,k = 2,分类= TRUE)) #创建一个布尔张量,指出保持或之间的差不 cond_result = tf.cond(actual_diff

+0

是的,可能是这样的... –