首页 文章

测试张量流网络:in_top_k()替换多标签分类

提问于
浏览
0

我在tensorflow中创建了一个神经网络 . 这个网络是多标签的 . Ergo:它尝试为一个输入集预测多个输出标签,在本例中为三个 . 目前,我使用此代码来测试我的网络在预测三个标签时的准确度:

_, indices_1 = tf.nn.top_k(prediction, 3)
_, indices_2 = tf.nn.top_k(item_data, 3)
correct = tf.equal(indices_1, indices_2)
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
percentage = accuracy.eval({champion_data:input_data, item_data:output_data})

该代码工作正常 . 现在的问题是我正在尝试创建代码来测试它在indices_1中找到的前3个项目是否属于indices_2中的前5个图像 . 我知道tensorflow有一个in_top_k()方法,但据我所知,它不接受multilabel . 目前我一直在尝试使用for循环比较它们:

_, indices_1 = tf.nn.top_k(prediction, 5)
_, indices_2 = tf.nn.top_k(item_data, 3)
indices_1 = tf.unpack(tf.transpose(indices_1, (1, 0)))
indices_2 = tf.unpack(tf.transpose(indices_2, (1, 0)))
correct = []
for element in indices_1:
    for element_2 in indices_2:
        if element == element_2:
            correct.append(True)
        else:
            correct.append(False)
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
percentage = accuracy.eval({champion_data:input_data, item_data:output_data})

但是,这不起作用 . 代码运行但我的准确度始终为0.0 .

So I have one of two questions:

1)是否有一个简单的替代in_top_k()接受多标签分类,我可以使用而不是自定义编写代码?

2)如果不是1:我做错了什么导致我得到0.0的准确度?

1 回答

  • 0

    当你这样做

    correct = tf.equal(indices_1, indices_2)
    

    您不仅检查这两个索引是否包含相同的元素,而且它们是否包含相同位置的相同元素 . 这听起来不像你想要的 .

    setdiff1d op将告诉您哪些索引在indices_1中但不在indices_2中,然后您可以使用它们来计算错误 .

    我认为对正确性检查过于严格可能是导致您得到错误结果的原因 .

相关问题