如何正确识别多标签预测?

时间:2018-03-10 15:03:36

标签: python tensorflow machine-learning multilabel-classification

我正在尝试获得一个执行多标签预测的张量流网络。使用softmax和单热(单标签)预测可以正常工作。准确度得到了很好的计算,网络可以学到它。

我的基本网络设置是:

X = tf.placeholder(features.dtype, (None, 300), name="input")
y = tf.placeholder(hots.dtype, (None,64), name="labels")    

with tf.name_scope("dnn"):
    hidden1 = fully_connected(X, 900, scope="hidden1")
    hidden2 = fully_connected(hidden1, 450, scope="hidden2")
    hidden3 = fully_connected(hidden2, 225, scope="hidden3")
    logits = fully_connected(hidden3, max, scope="outputs", activation_fn=None)

with tf.name_scope("loss"):
    xentropy = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=logits)
    loss = tf.reduce_mean(xentropy, name="loss")

learning_rate = 0.05

with tf.name_scope("train"):
    optimizer = tf.train.GradientDescentOptimizer(learning_rate)
    training_op = optimizer.minimize(loss)

with tf.name_scope("eval"):
    correct = tf.nn.in_top_k(logits, tf.argmax(y,1), 1)
    accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

因为目标是获得多标签预测,所以我改变了损失和准确性:(基于Tensorflow, multi label accuracy calculation

with tf.name_scope("loss"):
    xentropy = tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.cast(y, tf.float32), logits=logits)
    loss = tf.reduce_mean(xentropy, name="loss")

with tf.name_scope("eval"): 
    correct_prediction = tf.equal(tf.round(tf.nn.sigmoid(logits)), tf.round(tf.cast(y, tf.float32)))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

然而,这导致每个纪元的精确度为Train accuracy: 0.984375 Test accuracy: 0.984375(单标签单热数据)。它不会改变,它始终是这个数字。

我测试了多标签的大量精度计算,但找不到实际上给我正确结果的那个。我在这里错过了什么?

1 个答案:

答案 0 :(得分:0)

最后,经过无数次尝试将其修复后,事实证明一切正常,除了优化器。当使用AdamOptimizer()代替GradientDescentOptimizer(learning_rate)时,网络开始快速学习,在40个时期内精确度从0.7到0.97。

也许一些学习率的调整也会使它起作用,但现在终于解决了!