tensorflow: predicting multi-label accuracy considering top-k predicted values -


my set if given set of data labels of them can have multiple ones (the max number of one's each example known denoted n):

1,0,0,0,1,0 0,0,1,0,1,1 .... 1,1,1,0,0,0 

when predict want see indices top n logits if prediction indices contains label indices one's it's correct prediction. how can achieve in tensorflow?

you can this:

#inputs  labels = tf.constant([[1,1,0,0,0,0],[0,0,1,0,1,1]]) logits = tf.constant([[.6,.5,.5,.4,.2,0.1],[.05,.15,.2,.15,.05,.5]]) k = 2  # predict top-k each row topk, idx = tf.nn.top_k(logits, k)  # sort index input sparse_to_dense matrix  idx, _ = tf.nn.top_k(-idx, k)  # obtain full indices indices = tf.stack([tf.tile(tf.range(0, idx.get_shape()[0])[...,tf.newaxis], [1, k]), -idx], axis=2) indices = tf.reshape(tf.squeeze(indices), [-1,2])  #convert them dense matrix pred_labels = tf.sparse_to_dense(indices, logits.get_shape(),   tf.ones(idx.get_shape()[0]*k))  #calculate whether each row of labels contained in logits sum1 = tf.reduce_sum(tf.cast(labels, tf.float32),1) sum2 = tf.reduce_sum(tf.multiply(tf.cast(labels, tf.float32), tf.cast(pred_labels, tf.float32)), 1) acc = tf.equal(sum1, sum2) sess = tf.interactivesession() print(sess.run(pred_labels)) sess.run(acc)  # outputs #[[ 1.  1.  1.  0.  0.  0.] #[ 0.  1.  1.  0.  0.  1.]]  #[ true, false] 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -