两个输出通道代表三种状态

时间:2018-04-07 21:22:08

标签: python tensorflow

我正在尝试训练一个卷积的,完全连接的网络。它应该评估为三种状态之一:" A"," B"或"既不是A也不是B."

我的训练标签设置有两个维度(或#34;图钉#34;),如下所示:

if condition_a_active:
    labels.append([1.0, 0.0])
elif condition_b_active:
    labels.append([0.0, 1.0])
else: # both conditions A and B inactive
    labels.append([0.0, 0.0])

我知道如果我保持三个不同的输出尺寸(因此额外的输出" pin" 条件A和条件B都不活动)我可以使用以下代码评估我的网络:

result = tf.equal(tf.argmax(labels, 1), tf.argmax(prediction_op, 1))
accuracy = tf.reduce_mean(tf.cast(result, tf.float32))

我可以用两个"引脚做同样的事情,"在哪里说都低于.5 意味着 A和B都不活动

2 个答案:

答案 0 :(得分:1)

是的,但你需要创建一些额外的张量,如下所示:

# creates 'both inactive' ground truth flag tensor
both_inactive_gt = tf.cast( 1.0 - tf.reduce_sum( labels, 1 ), tf.bool ) 

threshold = 0.5 # whatever threshold you want to use

# two steps to create prediction flag tensor
# First: -2, -1, 0, 1, or 2 : only -2 is interesting for us
both_inactive_pred_0 = tf.reduce_sum( tf.sign( prediction_op - threshold ), 1 )
# Second: True for 'both inactive' and False otherwise
both_inactive_pred = tf.cast( - tf.sign( both_inactive_pred_0 + 1.5 ), tf.bool ) 

# now tie it all together
result = tf.logical_or(
    tf.logical_and( both_inactive_gt, both_inactive_pred ),
    tf.logical_and( tf.logical_not( both_inactive_gt ),
                    tf.equal(tf.argmax(labels, 1), tf.argmax(prediction_op, 1 ) ) )

希望这有帮助!

答案 1 :(得分:0)

对于规范参考,我正在为那些喜欢明确算术解决方案的人添加此解决方案。

{{1}}