Re-ranking retrieved results in Prompt Engineering / GenAI - ML Experiment: Train & Evaluate
Start learning this pattern below
Jump into concepts and practice - no test required
import numpy as np import tensorflow as tf from tensorflow.keras import layers, models, losses, optimizers # Simulated data: queries, documents, and relevance labels # For simplicity, embeddings are random but in practice use real embeddings np.random.seed(42) num_samples = 1000 embedding_dim = 50 # Generate random embeddings for queries and documents query_embeddings = np.random.rand(num_samples, embedding_dim).astype(np.float32) doc_embeddings = np.random.rand(num_samples, embedding_dim).astype(np.float32) # Generate binary relevance labels (1=relevant, 0=not relevant) labels = np.random.randint(0, 2, size=(num_samples, 1)).astype(np.float32) # Combine query and doc embeddings as input features inputs = np.concatenate([query_embeddings, doc_embeddings], axis=1) # Split into train and validation sets split = int(0.8 * num_samples) X_train, X_val = inputs[:split], inputs[split:] y_train, y_val = labels[:split], labels[split:] # Define a simple neural network for re-ranking model = models.Sequential([ layers.Input(shape=(embedding_dim * 2,)), layers.Dense(64, activation='relu'), layers.Dropout(0.3), layers.Dense(32, activation='relu'), layers.Dense(1, activation='sigmoid') ]) model.compile(optimizer=optimizers.Adam(learning_rate=0.001), loss=losses.BinaryCrossentropy(), metrics=['accuracy']) # Train the model history = model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_val, y_val), verbose=0) # Evaluate precision@5 and recall@5 on validation set # For simplicity, simulate retrieval of 5 docs per query (here each sample is a pair) # Sort validation samples by predicted score descending val_preds = model.predict(X_val).flatten() # Simulate grouping by queries: assume each 5 samples correspond to one query num_val_queries = len(X_val) // 5 precision_at_5 = [] recall_at_5 = [] for i in range(num_val_queries): start = i * 5 end = start + 5 scores = val_preds[start:end] true_labels = y_val[start:end].flatten() # Sort by scores sorted_indices = np.argsort(scores)[::-1] sorted_labels = true_labels[sorted_indices] # Calculate precision@5 and recall@5 relevant_retrieved = np.sum(sorted_labels) total_relevant = np.sum(true_labels) precision = relevant_retrieved / 5 recall = relevant_retrieved / total_relevant if total_relevant > 0 else 0 precision_at_5.append(precision) recall_at_5.append(recall) avg_precision_at_5 = np.mean(precision_at_5) * 100 avg_recall_at_5 = np.mean(recall_at_5) * 100 print(f"Precision@5 after re-ranking: {avg_precision_at_5:.2f}%") print(f"Recall@5 after re-ranking: {avg_recall_at_5:.2f}%")
Before re-ranking: Precision@5 = 60%, Recall@5 = 55%
After re-ranking: Precision@5 = 78.5%, Recall@5 = 62.3%
Practice
What is the main purpose of re-ranking retrieved results in a search system?
Solution
Step 1: Understand the role of re-ranking
Re-ranking means sorting results again after the first search to improve order.Step 2: Identify the goal of re-ranking
The goal is to use a smarter scoring method to show the most relevant results at the top.Final Answer:
To sort the initial search results again using a better scoring method -> Option AQuick Check:
Re-ranking = better sorting [OK]
- Confusing re-ranking with removing duplicates
- Thinking re-ranking speeds up initial search
- Assuming re-ranking translates results
Which of the following code snippets correctly represents a simple re-ranking step that sorts a list of results by their score in descending order?
results = [{'id': 1, 'score': 0.5}, {'id': 2, 'score': 0.9}, {'id': 3, 'score': 0.7}]
# Re-rank results hereSolution
Step 1: Identify sorting by score descending
We want to sort by 'score' in descending order, so reverse=True is needed.Step 2: Check each option
results.sort(key=lambda x: x['score'], reverse=True) sorts by 'score' with reverse=True, which is correct. Others either sort by 'id' or ascending score or missing key.Final Answer:
results.sort(key=lambda x: x['score'], reverse=True) -> Option DQuick Check:
Sort by score descending = results.sort(key=lambda x: x['score'], reverse=True) [OK]
- Forgetting reverse=True for descending sort
- Sorting by wrong key like 'id'
- Using sort without key causing error
Given the following code that re-ranks search results by a new score, what will be the output after re-ranking?
results = [
{'id': 'a', 'score': 0.3},
{'id': 'b', 'score': 0.8},
{'id': 'c', 'score': 0.5}
]
# New scores from a re-ranker
new_scores = {'a': 0.9, 'b': 0.4, 'c': 0.7}
for r in results:
r['score'] = new_scores[r['id']]
results.sort(key=lambda x: x['score'], reverse=True)
print([r['id'] for r in results])Solution
Step 1: Update scores with new_scores
Results get scores: 'a' = 0.9, 'b' = 0.4, 'c' = 0.7.Step 2: Sort results by updated score descending
Sorted order by score: 0.9 ('a'), 0.7 ('c'), 0.4 ('b').Final Answer:
['a', 'c', 'b'] -> Option BQuick Check:
Sort by new scores descending = ['a', 'c', 'b'] [OK]
- Sorting by old scores instead of new
- Sorting ascending instead of descending
- Mixing up ids and scores
Identify the error in this re-ranking code snippet and select the fix:
results = [{'id': 1, 'score': 0.2}, {'id': 2, 'score': 0.5}]
new_scores = {1: 0.7, 2: 0.9}
for r in results:
r['score'] = new_scores[r['id']]
results.sort(key=lambda x: x['score'], reverse=True)
print(results)Solution
Step 1: Check key types in new_scores and results
Both use integer keys for 'id', so lookup works correctly.Step 2: Verify sorting and printing
Sorting by updated 'score' descending is valid and prints sorted list.Final Answer:
No error; code runs correctly and sorts results -> Option CQuick Check:
Matching key types = no error [OK]
- Assuming string keys when they are integers
- Thinking sort() causes error without reason
- Adding unnecessary try-except blocks
You have a list of 5 retrieved documents with initial scores. You want to re-rank them using a machine learning model that outputs a relevance score. Which approach best improves the final ranking?
- Use the model scores to replace initial scores and sort descending.
- Combine initial and model scores by averaging, then sort descending.
- Sort only by initial scores, ignoring model scores.
- Randomly shuffle results to avoid bias.
Solution
Step 1: Understand re-ranking with model scores
Replacing scores fully may ignore useful initial info; combining scores balances both.Step 2: Evaluate options for best ranking
Averaging initial and model scores uses all info, improving relevance and stability.Final Answer:
Combine initial and model scores by averaging, then sort descending -> Option AQuick Check:
Combine scores for best re-ranking [OK]
- Replacing scores blindly losing initial info
- Ignoring model scores completely
- Random shuffling breaks relevance
