-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #350 from dice-group/develop
Develop
- Loading branch information
Showing
12 changed files
with
996 additions
and
79 deletions.
There are no files selected for viewing
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,234 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "blond-letter", | ||
"metadata": {}, | ||
"source": [ | ||
"# CLIP Notebook\n", | ||
"This is a jupyter notebook file to execute [CLIP](ontolearn.concept_learner.CLIP) and generate predictive results. We recommend you to see the [concept learners](../docs/usage/06_concept_learners.md) guide before continuing with the execution.\n", | ||
"Also if you have not done it already, from the main directory \"Ontolearn\", run the commands for Datasets mentioned [here](https://ontolearn-docs-dice-group.netlify.app/usage/02_installation#download-external-files) to download the datasets." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "japanese-ivory", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"\n", | ||
"Warning: SQLite3 version 3.40.0 and 3.41.2 have huge performance regressions; please install version 3.41.1 or 3.42!\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import json\n", | ||
"from ontolearn.knowledge_base import KnowledgeBase\n", | ||
"from ontolearn.concept_learner import CLIP\n", | ||
"from ontolearn.refinement_operators import ExpressRefinement\n", | ||
"from ontolearn.learning_problem import PosNegLPStandard\n", | ||
"from owlapy.model import OWLNamedIndividual, IRI\n", | ||
"from ontolearn.utils import setup_logging\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "pending-coast", | ||
"metadata": {}, | ||
"source": [ | ||
"Open `uncle_lp.json` where we have stored the learning problem for the concept of 'Uncle' and the path to the 'family' ontology." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "beginning-syntax", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"with open('uncle_lp.json') as json_file:\n", | ||
" settings = json.load(json_file)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "humanitarian-heating", | ||
"metadata": {}, | ||
"source": [ | ||
"Create an instance of the class `KnowledeBase` by using the path that is stored in `settings`." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "caroline-indiana", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"kb = KnowledgeBase(path=settings['data_path'])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "lucky-activation", | ||
"metadata": {}, | ||
"source": [ | ||
"Retreive the IRIs of the positive and negative examples of Uncle from `settings` and create an instance of `PosNegLPStandard`. (more info about this [here](../docs/usage/06_concept_learners.md#configure-the-learning-problem))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "processed-patrick", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"examples = settings['Uncle']\n", | ||
"p = set(examples['positive_examples'])\n", | ||
"n = set(examples['negative_examples'])\n", | ||
"typed_pos = set(map(OWLNamedIndividual, map(IRI.create, p)))\n", | ||
"typed_neg = set(map(OWLNamedIndividual, map(IRI.create, n)))\n", | ||
"lp = PosNegLPStandard(pos=typed_pos, neg=typed_neg)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "mechanical-latin", | ||
"metadata": {}, | ||
"source": [ | ||
"Create a model of [CLIP](ontolearn.concept_learner.CLIP) and fit the learning problem to the model." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "171d1aa4-6c12-42c0-b7e9-8cf2dce85ff9", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"op = ExpressRefinement(knowledge_base=kb, use_inverse=False,\n", | ||
" use_numeric_datatypes=False)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "binding-moderator", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"\n", | ||
" Loaded length predictor!\n", | ||
"\n", | ||
" Loaded length predictor!\n", | ||
"\n", | ||
" Loaded length predictor!\n", | ||
"\n", | ||
" Loaded length predictor!\n", | ||
"\n", | ||
"***** Predicted length: 5 *****\n", | ||
"\n", | ||
"***** Predicted length: 5 *****\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"<ontolearn.concept_learner.CLIP at 0x7f762ae039a0>" | ||
] | ||
}, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"model = CLIP(knowledge_base=kb, path_of_embeddings=\"../CLIPData/family/embeddings/ConEx_entity_embeddings.csv\",\n", | ||
" refinement_operator=op, load_pretrained=True, max_runtime=200)\n", | ||
"model.fit(lp)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "d981f2b9-3489-494e-825d-6a72ee480d4f", | ||
"metadata": {}, | ||
"source": [ | ||
"## Retrieve top 3 hypotheses and print them." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"id": "c6a90b21-3594-441d-bed0-eb822db5f993", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"<class 'ontolearn.search.OENode'> at 0x0304774\tMale ⊓ (∀ hasParent.Grandparent)\tQuality:0.90476\tHeuristic:0.40407\tDepth:2\tH_exp:6\t|RC|:7\t|Indv.|:None\n", | ||
"<class 'ontolearn.search.OENode'> at 0x0ca154a\tMale ⊓ (∀ hasChild.Grandchild)\tQuality:0.90476\tHeuristic:0.36919\tDepth:1\tH_exp:7\t|RC|:7\t|Indv.|:None\n", | ||
"<class 'ontolearn.search.OENode'> at 0x2adbb89\tMale ⊓ (∀ hasChild.(¬Grandfather))\tQuality:0.88889\tHeuristic:0.39044\tDepth:3\tH_exp:6\t|RC|:0\t|Indv.|:None\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[None, None, None]" | ||
] | ||
}, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"hypotheses = list(model.best_hypotheses(n=3))\n", | ||
"[print(_) for _ in hypotheses]" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "onto", | ||
"language": "python", | ||
"name": "onto" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.18" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
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
Oops, something went wrong.