Skip to content
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

Integrate NCES in ontolearn-web service and fix dllearner binding script in examples #450

Merged
merged 13 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,52 @@ response = requests.get('http://0.0.0.0:8000/cel',
"model": "TDL"})
print(response.json())
```
NCES (another scalable learner). The following will first train NCES if the provided path `path_to_pretrained_nces` does not exist
```python
import json
import requests
with open(f"LPs/Mutagenesis/lps.json") as json_file:
learning_problems = json.load(json_file)["problems"]
## This trains NCES before solving the provided learning problems. Expect poor performance for this number of epochs, and this training data size.
## If GPU is available, set `num_of_training_learning_problems` t0 10_000 or more. Set `nces_train_epochs` to 300 or more, and increase `nces_batch_size`.
for str_target_concept, examples in learning_problems.items():
response = requests.get('http://0.0.0.0:8000/cel',
headers={'accept': 'application/json', 'Content-Type': 'application/json'},
json={"pos": examples['positive_examples'],
"neg": examples['negative_examples'],
"model": "NCES",
"path_embeddings": "mutagenesis_embeddings/Keci_entity_embeddings.csv",
"path_to_pretrained_nces": None,
# if pretrained_nces exists, load weghts, otherwise train one and save it
"num_of_training_learning_problems": 100,
"nces_train_epochs": 5,
"nces_batch_size": 16
})
print(response.json())
```

Now this will use pretrained weights for NCES

```python
import json
import requests
with open(f"LPs/Mutagenesis/lps.json") as json_file:
learning_problems = json.load(json_file)["problems"]
for str_target_concept, examples in learning_problems.items():
response = requests.get('http://0.0.0.0:8000/cel',
headers={'accept': 'application/json', 'Content-Type': 'application/json'},
json={"pos": examples['positive_examples'],
"neg": examples['negative_examples'],
"model": "NCES",
"path_embeddings": "./NCESData/mutagenesis/embeddings/ConEx_entity_embeddings.csv",
"path_to_pretrained_nces": "./NCESData/mutagenesis/trained_models/",
# if pretrained_nces exists, load weghts, otherwise train one and save it
"num_of_training_learning_problems": 100,
"nces_train_epochs": 5,
"nces_batch_size": 16
})
print(response.json())
```

</details>

Expand Down
6 changes: 4 additions & 2 deletions examples/concept_learning_cv_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ def dl_concept_learning(args):
kwargs_classifier={"random_state": 1},
max_runtime=args.max_runtime,
verbose=0)

nces = NCES(knowledge_base_path=args.kb,
quality_func=F1(),
path_of_embeddings=args.path_of_nces_embeddings,
pretrained_model_name=["LSTM", "GRU", "SetTransformer"],
learner_names=["LSTM", "GRU", "SetTransformer"],
num_predictions=100,
verbose=0)

clip = CLIP(knowledge_base=kb,
refinement_operator=ModifiedCELOERefinement(kb),
quality_func=F1(),
Expand Down Expand Up @@ -260,7 +262,7 @@ def dl_concept_learning(args):

start_time = time.time()
# () Fit model training dataset
pred_nces = nces.fit(train_lp.pos, train_lp.neg).best_hypotheses(n=1)
pred_nces = nces.fit(train_lp).best_hypotheses(n=1)
print("NCES ends..", end="\t")
rt_nces = time.time() - start_time

Expand Down
16 changes: 13 additions & 3 deletions examples/dl_learner.py
alkidbaci marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
Author: Caglar Demir
"""
from ontolearn.binders import DLLearnerBinder
from owlapy.iri import IRI
from owlapy.owl_individual import OWLNamedIndividual
import json

from ontolearn.learning_problem import PosNegLPStandard

# (1) Load learning problems
with open('synthetic_problems.json') as json_file:
settings = json.load(json_file)
Expand All @@ -36,9 +41,14 @@
p = examples['positive_examples']
n = examples['negative_examples']

best_pred_celoe = celoe.fit(pos=p, neg=n, max_runtime=1).best_hypothesis()
positives = {OWLNamedIndividual(IRI.create(i)) for i in p}
negatives = {OWLNamedIndividual(IRI.create(i)) for i in n}

lp = PosNegLPStandard(pos=positives, neg=positives)
alkidbaci marked this conversation as resolved.
Show resolved Hide resolved

best_pred_celoe = celoe.fit(lp, max_runtime=1).best_hypothesis()
print(best_pred_celoe)
best_pred_ocel = ocel.fit(pos=p, neg=n, max_runtime=1).best_hypothesis()
best_pred_ocel = ocel.fit(lp, max_runtime=1).best_hypothesis()
print(best_pred_ocel)
best_pred_eltl = eltl.fit(pos=p, neg=n, max_runtime=1).best_hypothesis()
best_pred_eltl = eltl.fit(lp, max_runtime=1).best_hypothesis()
print(best_pred_eltl)
Loading
Loading