Editing
Information Retrieval
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== <span style="color: #FFFFFF;">Applying</span> == '''Hybrid BM25 + dense retrieval pipeline:''' <syntaxhighlight lang="python"> from rank_bm25 import BM25Okapi from sentence_transformers import SentenceTransformer, util import numpy as np docs = [ "Machine learning is a subset of artificial intelligence.", "Neural networks are computational models inspired by the brain.", "Gradient descent optimizes model parameters by following the loss gradient.", "Transformers use self-attention mechanisms to process sequences.", ] tokenized = [d.lower().split() for d in docs] bm25 = BM25Okapi(tokenized) embedder = SentenceTransformer("BAAI/bge-small-en-v1.5") doc''embeddings = embedder.encode(docs, normalize''embeddings=True) def hybrid''retrieve(query: str, top''k: int = 3, alpha: float = 0.5) -> list: """alpha: weight for dense scores (1-alpha for BM25).""" # BM25 scores (sparse) bm25''scores = np.array(bm25.get''scores(query.lower().split())) bm25''norm = (bm25''scores - bm25''scores.min()) / (bm25''scores.max() - bm25_scores.min() + 1e-10) # Dense scores q''emb = embedder.encode(query, normalize''embeddings=True) dense''scores = util.dot''score(q''emb, doc''embeddings).numpy().flatten() dense''norm = (dense''scores - dense''scores.min()) / (dense''scores.max() - dense_scores.min() + 1e-10) # Combine combined = alpha * dense''norm + (1 - alpha) * bm25''norm top''idx = combined.argsort()[-top''k:][::-1] return [(docs[i], combined[i]) for i in top_idx] results = hybrid_retrieve("How do neural networks learn?") for doc, score in results: print(f"Score: {score:.3f} | {doc}") </div>
Summary:
Please note that all contributions to BloomWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
BloomWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information