Guest User

Untitled

a guest
Feb 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import tensorflow as tf
  2.  
  3. # true and predicted tensors
  4. y_p = tf.placeholder(dtype=tf.int64)
  5. y_t = tf.placeholder(dtype=tf.int64)
  6.  
  7. # Count true positives, true negatives, false positives and false negatives.
  8. tp = tf.count_nonzero(y_p * y_t)
  9. tn = tf.count_nonzero((y_p - 1) * (y_t - 1))
  10. fp = tf.count_nonzero(y_p * (y_t - 1))
  11. fn = tf.count_nonzero((y_p - 1) * y_t)
  12.  
  13. acc = tf.metrics.accuracy(y_p, y_t)
  14.  
  15. # Calculate accuracy, precision, recall and F1 score.
  16. accuracy = (tp + tn) / (tp + fp + fn + tn)
  17.  
  18. with tf.Session() as sess:
  19. sess.run(tf.global_variables_initializer())
  20. sess.run(tf.local_variables_initializer())
  21.  
  22. for i in range(4):
  23. if i == 0:
  24. yop = [0,0,0,0,0,0,0,0,0,0]
  25. elif i == 1:
  26. yop = [0,0,0,0,0,0,0,0,1,1]
  27. elif i == 2:
  28. yop = [1,1,1,0,0,0,0,0,0,1]
  29. else:
  30. yop = [0,1,1,1,1,1,1,0,0,0]
  31. tf_a = sess.run(acc, feed_dict={y_p: [0,0,0,0,0,0,0,0,0,0], y_t: yop})
  32. my_a = sess.run(accuracy, feed_dict={y_p: [0,0,0,0,0,0,0,0,0,0], y_t: yop})
  33. print("TF accuracy: {0}".format(tf_a))
  34. print("My accuracy: {0}".format(my_a))
  35.  
  36. TF accuracy: (0.0, 1.0)
  37. My accuracy: 1.0
  38. TF accuracy: (1.0, 0.9)
  39. My accuracy: 0.8
  40. TF accuracy: (0.9, 0.8)
  41. My accuracy: 0.6
  42. TF accuracy: (0.8, 0.7)
  43. My accuracy: 0.4
Add Comment
Please, Sign In to add comment