-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cross encoder and bi encoders implementation
- Loading branch information
1 parent
22aff92
commit a63fe50
Showing
18 changed files
with
337 additions
and
192 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
class BaseChain: | ||
class Chain: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from pirate.models.chains import MineChainConfig | ||
from pirate.chains.base import Chain | ||
|
||
class MineChain: | ||
def __init__(self, config: MineChainConfig): | ||
class MineChain(Chain): | ||
def __init__(self, ): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import random | ||
from typing import List, Optional | ||
|
||
from tqdm import tqdm | ||
|
||
from pirate.miner.base import BaseMiner | ||
from pirate.data import ( | ||
Passages, | ||
Queries, | ||
Triples | ||
) | ||
from pirate.models import ( | ||
Sampling, | ||
HardMinerParams | ||
) | ||
|
||
class HardMiner(BaseMiner): | ||
def __init__(self, mining_params: HardMinerParams): | ||
super().__init__(mining_params) | ||
|
||
self.mining_params = mining_params | ||
self.triples = self.mining_params.triples | ||
|
||
assert self.triples is not None, "Triples must be provided." | ||
assert len(self.triples) > 0, "Triples must not be empty." | ||
assert max([len(i) for i in self.triples]) == 2, "Triples must be in the pair format [qid, pid]." | ||
|
||
passage_dict = {} | ||
query_dict = {} | ||
for qid, pid in self.triples: | ||
assert qid in self.mining_params.queries, f"Query {qid} not found." | ||
assert pid in self.mining_params.passages, f"Passage {pid} not found." | ||
|
||
passage_dict[pid] = self.mining_params.passages[pid] | ||
query_dict[qid] = self.mining_params.queries[qid] | ||
|
||
self.passages = Passages(passage_dict) | ||
self.queries = Queries(query_dict) | ||
|
||
self.encoder = self._get_model() | ||
self._seed() | ||
|
||
|
||
def mine( | ||
self, | ||
num_negs_per_pair: int = 1, | ||
exclude_pairs: Optional[List[List[str]]] = None | ||
) -> Triples: | ||
self.encoder.index(self.passages) | ||
rankings = self.encoder.rank_passages(self.queries, self.mining_params.top_k) | ||
|
||
if self.mining_params.score_threshold: | ||
rankings = rankings.filter_by_score(self.mining_params.score_threshold) | ||
|
||
triples_list = [] | ||
for qid, pos_pid in tqdm(self.triples, desc="Mining hard negatives", total=len(self.triples), disable=self.mining_params.verbose): | ||
if exclude_pairs and [qid, pos_pid] in exclude_pairs: | ||
continue | ||
|
||
passage_groups = rankings.get_passage_groups(qid)["pid"].to_list() | ||
|
||
passage_sample_set = [] | ||
match self.mining_params.sampling: | ||
case Sampling.RANDOM: | ||
passage_sample_set = passage_groups | ||
case Sampling.RTOP_K: | ||
passage_sample_set = passage_groups[:self.mining_params.top_k] if self.mining_params.top_k else passage_groups | ||
|
||
random_negative_passages = random.sample(passage_sample_set, num_negs_per_pair) | ||
|
||
for neg_pid in random_negative_passages: | ||
triples_list.append([qid, pos_pid, neg_pid]) | ||
|
||
triples = Triples(triples_list) | ||
return triples |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.