从 softmax 中移除低质量张量预测
Removing low quality tensor predictions from softmax
我想对张量应用过滤器并删除不符合我条件的值。例如,假设我有一个如下所示的张量:
softmax_tensor = [[ 0.05 , 0.05, 0.2, 0.7], [ 0.25 , 0.25, 0.3, 0.2 ]]
现在,分类器选择 argmax
个张量进行预测:
predictions = [[3],[2]]
但这并不是我想要的,因为我丢失了有关该预测的置信度的信息。我宁愿不做预测,也不愿做出错误的预测。所以我想做的是 return 像这样过滤张量:
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])
但考虑到 tensorflow 在张量上运行,我不确定如何执行此操作 - 如果我这样做,它会破坏计算图吗?
一个 suggested using tf.gather_nd, but in that scenario they already had a tensor that they wated to filter on. I've also looked at tf.cond还是没看懂。我想很多其他人会从这个完全相同的解决方案中受益。
谢谢大家。
我会做两件事来解决你的问题:
首先,我会 return softmax 张量的值。你在某处寻找对它的引用(你在创建它时保留对它的引用,或者你在适当的张量集合中找到它)然后在 sess.run([softmaxtensor,prediction],feed_dict=..)
中评估它然后你用 python随你喜欢。
其次,如果您想留在图表中,我会使用 build-it tf.where()
,其工作方式与 numpy 包 doc there[=13= 中的 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
我想对张量应用过滤器并删除不符合我条件的值。例如,假设我有一个如下所示的张量:
softmax_tensor = [[ 0.05 , 0.05, 0.2, 0.7], [ 0.25 , 0.25, 0.3, 0.2 ]]
现在,分类器选择 argmax
个张量进行预测:
predictions = [[3],[2]]
但这并不是我想要的,因为我丢失了有关该预测的置信度的信息。我宁愿不做预测,也不愿做出错误的预测。所以我想做的是 return 像这样过滤张量:
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])
但考虑到 tensorflow 在张量上运行,我不确定如何执行此操作 - 如果我这样做,它会破坏计算图吗?
一个
谢谢大家。
我会做两件事来解决你的问题:
首先,我会 return softmax 张量的值。你在某处寻找对它的引用(你在创建它时保留对它的引用,或者你在适当的张量集合中找到它)然后在 sess.run([softmaxtensor,prediction],feed_dict=..)
中评估它然后你用 python随你喜欢。
其次,如果您想留在图表中,我会使用 build-it tf.where()
,其工作方式与 numpy 包 doc there[=13= 中的 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