How to get correct acccuracy for multi label prediction?

时光毁灭记忆、已成空白 提交于 2019-12-13 02:46:33

问题


I am trying to get a tensorflow network that does multi-label predictions. Using softmax with one-hot (single label) predictions works correctly. The accuracy get's calculated perfectly and the network learns as it should.

My basic network setup is:

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))

Because the goal is to get multi-label predictions, I changed the loss and the accuracy: (based on 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))

However, this results in a accuracy of Train accuracy: 0.984375 Test accuracy: 0.984375 for each epoch (with the single label one-hot data). It doesn't change, it's always this number.

I tested with a lot of accuracy calculations for multi-label, but can't find one that actually does give me proper results. What am I missing here?


回答1:


At the end, after countless tries to get this fixed, it turned out that everything was ok, except for the optimizer. When using the AdamOptimizer() instead of the GradientDescentOptimizer(learning_rate) the network started learning fast, from 0.7 to 0.97 accuracy in 40 epochs.

Maybe some tweaking of the learning rate would also make it work, but for now this is finally solved!



来源:https://stackoverflow.com/questions/49210520/how-to-get-correct-acccuracy-for-multi-label-prediction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!