-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(WIP) Triple classification #106
base: master
Are you sure you want to change the base?
Changes from 1 commit
49ed5ef
a6aec4f
af64a54
c18e5f8
b90c9d5
f9d8feb
ef7a7f8
1d92974
fc660a1
51c8ab2
1e609a7
b0b7791
162ff38
6344697
4df0c3b
bb5a575
2b98780
5b1a5b4
8a4416f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -330,7 +330,7 @@ eval: | |
# mean_reciprocal_rank_filtered_with_test. | ||
filter_with_test: True | ||
|
||
# Type of evaluation (entity_ranking only at the moment) | ||
# Type of evaluation (entity_ranking, triple_classification) | ||
type: entity_ranking | ||
|
||
# How to handle cases with ties between the correct answer and other answers, e.g., | ||
|
@@ -423,6 +423,14 @@ valid: | |
|
||
## EVALUATION ################################################################## | ||
|
||
triple_classification: | ||
random_seed: False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here: random_seed can be removed |
||
# How to obtain negative triple labels. Possible values are: | ||
# - corruption: Create negatives by randomly corrupting existing triples (positives) | ||
# - data : Obtain negative labels from the dataset. This assumes the data set | ||
# contains the splits 'valid_negatives' and 'test_negatives' | ||
negatives_from: corruption | ||
|
||
|
||
## HYPERPARAMETER SEARCH ####################################################### | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,12 @@ def __init__(self, config: Config, configuration_key: str, dataset: Dataset): | |
self.o_entities = None | ||
uni_sampler_config = config.clone() | ||
# uni_sampler_config.set("negative_sampling.num_samples.s", self.get_option("num_samples.s")) | ||
# TODO this is redundant as uniform.sample() is called with "num_samples" here in self.sample() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes that is correct |
||
uni_sampler_config.set("negative_sampling.num_samples.s", 1) | ||
# TODO maybe changing the API of KGEsampler.sample() to also accept a param "filter" | ||
# as it is the case already with "num_samples" | ||
# then we would not rely here on configuration options which actually | ||
# belong to a training job | ||
Comment on lines
+25
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolutely. Currently the negative sampling config is mixing the Job and the Sampler. This should be split up in a separate PR. |
||
uni_sampler_config.set("negative_sampling.filtering.s", True) | ||
# uni_sampler_config.set("negative_sampling.num_samples.o", self.get_option("num_samples.o")) | ||
uni_sampler_config.set("negative_sampling.num_samples.o", 1) | ||
|
@@ -31,6 +36,7 @@ def __init__(self, config: Config, configuration_key: str, dataset: Dataset): | |
|
||
def _prepare(self,): | ||
train_data = self.dataset.split("train") | ||
#TODO probably outdated as it refers to out-commented code | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. correct |
||
self.s_entities = train_data[:, S].unique().tolist() | ||
self.o_entities = train_data[:, O].unique().tolist() | ||
self._is_prepared = True | ||
|
@@ -105,6 +111,20 @@ def __init__(self, config, dataset, parent_job, model): | |
self.triple_classification_sampler = TripleClassificationSampler( | ||
config, "triple_classification", dataset | ||
) | ||
self.config.check( | ||
"triple_classification.negatives_from", ["corruption", "data"] | ||
) | ||
self.negatives_from = self.config.get("triple_classification.negatives_from") | ||
if self.negatives_from == "data": | ||
try: | ||
self.config.get("dataset.files.valid_negatives.type") | ||
self.config.get("dataset.files.test_negatives.type") | ||
except: | ||
raise KeyError( | ||
"No splits test/valid_negatives found for the dataset. " | ||
"Provide a dataset with splits valid_negatives and test_negatives " | ||
"or run triple classification with negatives_from=corruption" | ||
) | ||
|
||
def _prepare(self): | ||
"""Prepare the corrupted validation and test data. | ||
|
@@ -121,7 +141,8 @@ def _prepare(self): | |
|
||
self.config.log("Generate data with corrupted and true triples...") | ||
|
||
if self.eval_split == "test": | ||
# TODO maybe should be generalized to allow for other splits as valid_wo_unseen | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I agree, see my comment #115 (comment) |
||
if self.eval_split == "test" and self.negatives_from == "corruption": | ||
( | ||
self.tune_data, | ||
self.tune_labels, | ||
|
@@ -130,7 +151,25 @@ def _prepare(self): | |
self.eval_data, | ||
self.eval_labels, | ||
) = self.triple_classification_sampler.sample(self.dataset.split("test")) | ||
else: | ||
|
||
elif self.eval_split == "test" and self.negatives_from == "data": | ||
positives_valid = self.dataset.split("valid") | ||
negatives_valid = self.dataset.split("valid_negatives") | ||
self.tune_data = torch.cat((positives_valid, negatives_valid)).to(self.device) | ||
self.tune_labels = torch.cat( | ||
(torch.ones(positives_valid.size(0), torch.zeros(negatives_valid.size(0)))) | ||
).to(self.device) | ||
|
||
positives_test = self.dataset.split("test") | ||
negatives_test = self.dataset.split("test_negatives") | ||
self.tune_data = torch.cat((positives_test, negatives_test)).to( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. eval_data |
||
self.device) | ||
self.tune_labels = torch.cat( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. eval_labels |
||
(torch.ones(positives_test.size(0), | ||
torch.zeros(negatives_test.size(0)))) | ||
).to(self.device) | ||
|
||
elif self.eval_split == "valid" and self.negatives_from == "corruption": | ||
( | ||
self.tune_data, | ||
self.tune_labels, | ||
|
@@ -140,6 +179,17 @@ def _prepare(self): | |
self.eval_labels, | ||
) = self.triple_classification_sampler.sample(self.dataset.split("valid")) | ||
|
||
elif self.eval_split == "valid" and self.negatives_from == "data": | ||
positives = self.dataset.split("valid") | ||
negatives = self.dataset.split("valid_negatives") | ||
self.tune_data = torch.cat((positives, negatives)).to(self.device) | ||
self.tune_labels = torch.cat( | ||
(torch.ones(positives.size(0)), torch.zeros(negatives.size(0))) | ||
).to(self.device) | ||
|
||
self.eval_data = self.tune_data | ||
self.eval_labels = self.tune_labels | ||
|
||
# let the model add some hooks, if it wants to do so | ||
self.model.prepare_job(self) | ||
self.valid_data_is_prepared = True | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
triple_classification.random_seed is not used anymore