for j in range(predictions.shape[1]):
if predictions[i][j] in np.arange(labels[i][j]):
flags[i][j] = 1.
return np.mean((np.sum(flags, -1) >= 1.).astype(float))
@register_metric("rank_response")
After Change
return recall_at_k(labels, predictions, k=10)
def recall_at_k(y_true, y_pred, k):
num_examples = float(len(y_pred))
predictions = np.array(y_pred)
predictions = np.flip(np.argsort(predictions, -1), -1)[:, :k]
num_correct = 0
for el in predictions:
if 0 in el:
num_correct += 1
return float(num_correct) / num_examples
@register_metric("rank_response")
def rank_response(y_true, y_pred):
num_examples = float(len(y_pred))