MaartenGr / MaartenGr/KeyBERT

Bug: possible mistake in MMR calculation

Open
#192 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.2k
Forks
385
PR merge metrics
No merged PRs in 30d

Description

Hello,

If I am reading the code correctly, there is a mistake in the implementation of maximal marginal relevance (MMR) calculation.

Referring to the original publication https://doi.org/10.1145/290941.291025, the calculation is:

![image](https://github.com/MaartenGr/KeyBERT/assets/46572317/7852f986-ed54-46f5-82d2-fda9f735ebeb)

and the code as currently implemented:
```python
mmr = (
1 - diversity
) * candidate_similarities - diversity * target_similarities.reshape(-1, 1)
mmr_idx = candidates_idx[np.argmax(mmr)]
```

assuming:

- `diversity` is equal to 1-λ
- _Sim_1(D_i,Q)_ corresponds to `candidate similarities`
- _max Sim_2(D_i,D_j)_ corresponds to `target_similarities`

and I am assuming the last point because of the code:

```python
target_similarities = np.max(
word_similarity[candidates_idx][:, keywords_idx], axis=1
)
```

the code should be:

```python
mmr = (1 - diversity) *
(candidate_similarities - diversity * target_similarities.reshape(-1, 1))
mmr_idx = candidates_idx[np.argmax(mmr)]
```

So it appears to me that `diversity` is not distributed to both similarity terms as in the original equation; there needs to be parens around the difference between the similarity terms

I would note that I have seen a similar lack of parentheses, which distribute the diversity term (λ), in other works, for example http://www.cs.bilkent.edu.tr/~canf/CS533/hwSpring14/eightMinPresentations/handoutMMR.pdf

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate the Python MMR implementation containing the shown `mmr` expression and trace how `candidate_similarities`, `target_similarities`, and `diversity` are computed. Compare the implementation with the cited publication and determine the intended λ mapping; done means the formula is resolved and its behavior is checked by the project's existing tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.