def test_top_k_categorical_accuracy(self): with self.test_session(): y_pred = K.variable(np.array([[0.3, 0.2, 0.1], [0.1, 0.2, 0.7]])) y_true = K.variable(np.array([[0, 1, 0], [1, 0, 0]])) result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred, k=3)) self.assertEqual(result, 1) result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred, k=2)) self.assertEqual(result, 0.5) result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred, k=1)) self.assertEqual(result, 0.)
def test_top_k_categorical_accuracy(self): with self.cached_session(): y_pred = K.variable(np.array([[0.3, 0.2, 0.1], [0.1, 0.2, 0.7]])) y_true = K.variable(np.array([[0, 1, 0], [1, 0, 0]])) result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred, k=3)) self.assertEqual(result, 1) result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred, k=2)) self.assertEqual(result, 0.5) result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred, k=1)) self.assertEqual(result, 0.)
def evaluate(self, model, x, y_true, config): y_pred = model.predict(x, batch_size=64) return { 'acc': top_k_categorical_accuracy(y_true, y_pred, k=1), 'y_true': np.argmax(y_true, axis=1), 'y_pred': np.argsort(y_pred, axis=1).T[-5:].T }
def tkca(y_true,y_pred): # last_good_index = max([i for i in range(19) if int(tfksum(y_true[i,:]))!=0]) mc = 0.0 for i in range(19): print(y_true) print(y_pred) mc = mc + top_k_categorical_accuracy(y_true[:,i,:],y_pred[:,i,:]) if y_true.numpy()[0][i][0] == 1: return mc/i
def cls_loss(label_logits, label): with tf.name_scope('cls_label_metrics'): label_pred = tf.nn.softmax(label_logits) top3_acc = top_k_categorical_accuracy(label, label_pred, k=3) add_moving_summary(top3_acc) #label_loss = tf.nn.sparse_softmax_cross_entropy_with_logits( # labels=label, logits=label_logits) label_loss = tf.nn.softmax_cross_entropy_with_logits(labels=label, logits=label_logits) label_loss = tf.reduce_mean(label_loss, name='label_loss') return label_loss
def top_3_accuracy(true, pred): return top_k_categorical_accuracy(true, pred, 3)
def top_5_accuracy(y_true, y_pred): y_true = y_true[:, :256] y_pred = y_pred[:, :256] return top_k_categorical_accuracy(y_true, y_pred)
def top_2_accuracy(y_true, y_pred): return top_k_categorical_accuracy(y_true, y_pred, k=2)