From dbd8b3c536f1041b7d482ebdc56389fd8b6e20f8 Mon Sep 17 00:00:00 2001 From: MatthewJA Date: Sat, 24 Dec 2016 15:11:03 +1100 Subject: [PATCH] Improved MMR speed #56 --- acton/recommenders.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/acton/recommenders.py b/acton/recommenders.py index d1da97c..f7e81de 100644 --- a/acton/recommenders.py +++ b/acton/recommenders.py @@ -1,10 +1,12 @@ """Recommender classes.""" from abc import ABC, abstractmethod +import logging from typing import Iterable, Sequence import acton.database import numpy +import scipy.spatial def mmr_choose(features: numpy.ndarray, scores: numpy.ndarray, n: int, @@ -40,11 +42,17 @@ def mmr_choose(features: numpy.ndarray, scores: numpy.ndarray, n: int, selections = [scores.argmax()] selections_set = set(selections) + logging.debug('Running MMR.') dists = [] - + dists_matrix = None while len(selections) < n: - dists.append([numpy.linalg.norm(features[selections[-1]] - features[i]) - for i in range(len(scores))]) + if len(selections) % (n // 10) == 0: + logging.debug('\rMMR epoch {}/{}.'.format(len(selections), n)) + # Compute distances for last selection. + last = features[selections[-1]:selections[-1] + 1] + last_dists = numpy.linalg.norm(features - last, axis=1) + dists.append(last_dists) + dists_matrix = numpy.array(dists) next_best = None next_best_margin = float('-inf') @@ -53,7 +61,7 @@ def mmr_choose(features: numpy.ndarray, scores: numpy.ndarray, n: int, if i in selections_set: continue - margin = l * (scores[i] - (1 - l) * max(d[i] for d in dists)) + margin = l * (scores[i] - (1 - l) * dists_matrix[:, i].max()) if margin > next_best_margin: next_best_margin = margin next_best = i