2017-09-15 40 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

是的,可能是這樣的... –