ontolearn.concept_learner

Concept learning algorithms of Ontolearn.

Module Contents

Classes

CELOE

Class Expression Learning for Ontology Engineering.

OCEL

A limited version of CELOE.

EvoLearner

An evolutionary approach to learn concepts in ALCQ(D).

CLIP

Concept Learner with Integrated Length Prediction.

NCES

Neural Class Expression Synthesis.

Attributes

logger

ontolearn.concept_learner.logger
class ontolearn.concept_learner.CELOE(knowledge_base: KnowledgeBase, reasoner: owlapy.owl_reasoner.OWLReasoner | None = None, refinement_operator: BaseRefinement[OENode] | None = None, quality_func: AbstractScorer | None = None, heuristic_func: AbstractHeuristic | None = None, terminate_on_goal: bool | None = None, iter_bound: int | None = None, max_num_of_concepts_tested: int | None = None, max_runtime: int | None = None, max_results: int = 10, best_only: bool = False, calculate_min_max: bool = True)[source]

Bases: ontolearn.base_concept_learner.RefinementBasedConceptLearner[ontolearn.search.OENode]

Class Expression Learning for Ontology Engineering.

best_descriptions

Best hypotheses ordered.

Type:

EvaluatedDescriptionSet[OENode, QualityOrderedNode]

best_only

If False pick only nodes with quality < 1.0, else pick without quality restrictions.

Type:

bool

calculate_min_max

Calculate minimum and maximum horizontal expansion? Statistical purpose only.

Type:

bool

heuristic_func

Function to guide the search heuristic.

Type:

AbstractHeuristic

heuristic_queue

A sorted set that compares the nodes based on Heuristic.

Type:

SortedSet[OENode]

iter_bound

Limit to stop the algorithm after n refinement steps are done.

Type:

int

kb

The knowledge base that the concept learner is using.

Type:

KnowledgeBase

max_child_length

Limit the length of concepts generated by the refinement operator.

Type:

int

max_he

Maximal value of horizontal expansion.

Type:

int

max_num_of_concepts_tested
Type:

int

max_runtime

Limit to stop the algorithm after n seconds.

Type:

int

min_he

Minimal value of horizontal expansion.

Type:

int

name

Name of the model = ‘celoe_python’.

Type:

str

_number_of_tested_concepts

Yes, you got it. This stores the number of tested concepts.

Type:

int

operator

Operator used to generate refinements.

Type:

BaseRefinement

quality_func
Type:

AbstractScorer

reasoner

The reasoner that this model is using.

Type:

OWLReasoner

search_tree

Dict to store the TreeNode for a class expression.

Type:

Dict[OWLClassExpression, TreeNode[OENode]]

start_class

The starting class expression for the refinement operation.

Type:

OWLClassExpression

start_time

The time when fit() starts the execution. Used to calculate the total time fit() takes to execute.

Type:

float

terminate_on_goal

Whether to stop the algorithm if a perfect solution is found.

Type:

bool

__slots__ = ('best_descriptions', 'max_he', 'min_he', 'best_only', 'calculate_min_max', 'heuristic_queue',...
name = 'celoe_python'
kb: KnowledgeBase
max_he: int
min_he: int
best_only: bool
calculate_min_max: bool
search_tree: Dict[owlapy.class_expression.OWLClassExpression, TreeNode[OENode]]
seen_norm_concepts: Set[owlapy.class_expression.OWLClassExpression]
heuristic_queue: SortedSet[OENode]
best_descriptions: EvaluatedDescriptionSet[OENode, QualityOrderedNode]
next_node_to_expand(step: int) OENode[source]

Return from the search tree the most promising search tree node to use for the next refinement step.

Returns:

Next search tree node to refine.

Return type:

_N

best_hypotheses(n: int = 1, return_node: bool = False) owlapy.class_expression.OWLClassExpression | Iterable[owlapy.class_expression.OWLClassExpression] | OENode | Iterable[OENode][source]

Get the current best found hypotheses according to the quality.

Parameters:

n – Maximum number of results.

Returns:

Iterable with hypotheses in form of search tree nodes.

make_node(c: owlapy.class_expression.OWLClassExpression, parent_node: OENode | None = None, is_root: bool = False) OENode[source]

Create a node for CELOE.

Parameters:
  • c – The class expression of this node.

  • parent_node – Parent node.

  • is_root – Is this the root node?

Returns:

The node.

Return type:

OENode

updating_node(node: OENode)[source]

Removes the node from the heuristic sorted set and inserts it again.

Parameters:

update. (Node to)

Yields:

The node itself.

downward_refinement(node: OENode) Iterable[OENode][source]

Execute one refinement step of a refinement based learning algorithm.

Parameters:

node (_N) – the search tree node on which to refine.

Returns:

Refinement results as new search tree nodes (they still need to be added to the tree).

Return type:

Iterable[_N]

fit(*args, **kwargs)[source]

Find hypotheses that explain pos and neg.

async fit_async(*args, **kwargs)[source]

Async method of fit.

encoded_learning_problem() EncodedPosNegLPStandardKind | None[source]

Fetch the most recently used learning problem from the fit method.

tree_node(node: OENode) TreeNode[OENode][source]

Get the TreeNode of the given node.

Parameters:

node – The node.

Returns:

TreeNode of the given node.

show_search_tree(heading_step: str, top_n: int = 10) None[source]

Show search tree.

update_min_max_horiz_exp(node: OENode)[source]
clean()[source]

Clear all states of the concept learner.

class ontolearn.concept_learner.OCEL(knowledge_base: KnowledgeBase, reasoner: owlapy.owl_reasoner.OWLReasoner | None = None, refinement_operator: BaseRefinement[OENode] | None = None, quality_func: AbstractScorer | None = None, heuristic_func: AbstractHeuristic | None = None, terminate_on_goal: bool | None = None, iter_bound: int | None = None, max_num_of_concepts_tested: int | None = None, max_runtime: int | None = None, max_results: int = 10, best_only: bool = False, calculate_min_max: bool = True)[source]

Bases: CELOE

A limited version of CELOE.

best_descriptions

Best hypotheses ordered.

Type:

EvaluatedDescriptionSet[OENode, QualityOrderedNode]

best_only

If False pick only nodes with quality < 1.0, else pick without quality restrictions.

Type:

bool

calculate_min_max

Calculate minimum and maximum horizontal expansion? Statistical purpose only.

Type:

bool

heuristic_func

Function to guide the search heuristic.

Type:

AbstractHeuristic

heuristic_queue

A sorted set that compares the nodes based on Heuristic.

Type:

SortedSet[OENode]

iter_bound

Limit to stop the algorithm after n refinement steps are done.

Type:

int

kb

The knowledge base that the concept learner is using.

Type:

KnowledgeBase

max_child_length

Limit the length of concepts generated by the refinement operator.

Type:

int

max_he

Maximal value of horizontal expansion.

Type:

int

max_num_of_concepts_tested
Type:

int

max_runtime

Limit to stop the algorithm after n seconds.

Type:

int

min_he

Minimal value of horizontal expansion.

Type:

int

name

Name of the model = ‘ocel_python’.

Type:

str

_number_of_tested_concepts

Yes, you got it. This stores the number of tested concepts.

Type:

int

operator

Operator used to generate refinements.

Type:

BaseRefinement

quality_func
Type:

AbstractScorer

reasoner

The reasoner that this model is using.

Type:

OWLReasoner

search_tree

Dict to store the TreeNode for a class expression.

Type:

Dict[OWLClassExpression, TreeNode[OENode]]

start_class

The starting class expression for the refinement operation.

Type:

OWLClassExpression

start_time

The time when fit() starts the execution. Used to calculate the total time fit() takes to execute.

Type:

float

terminate_on_goal

Whether to stop the algorithm if a perfect solution is found.

Type:

bool

__slots__ = ()
name = 'ocel_python'
make_node(c: owlapy.class_expression.OWLClassExpression, parent_node: OENode | None = None, is_root: bool = False) OENode[source]

Create a node for OCEL.

Parameters:
  • c – The class expression of this node.

  • parent_node – Parent node.

  • is_root – Is this the root node?

Returns:

The node.

Return type:

OENode

class ontolearn.concept_learner.EvoLearner(knowledge_base: KnowledgeBase, reasoner: owlapy.owl_reasoner.OWLReasoner | None = None, quality_func: AbstractScorer | None = None, fitness_func: AbstractFitness | None = None, init_method: AbstractEAInitialization | None = None, algorithm: AbstractEvolutionaryAlgorithm | None = None, mut_uniform_gen: AbstractEAInitialization | None = None, value_splitter: AbstractValueSplitter | None = None, terminate_on_goal: bool | None = None, max_runtime: int | None = None, use_data_properties: bool = True, use_card_restrictions: bool = True, use_inverse: bool = False, tournament_size: int = 7, card_limit: int = 10, population_size: int = 800, num_generations: int = 200, height_limit: int = 17)[source]

Bases: ontolearn.base_concept_learner.BaseConceptLearner[ontolearn.search.EvoLearnerNode]

An evolutionary approach to learn concepts in ALCQ(D).

algorithm

The evolutionary algorithm.

Type:

AbstractEvolutionaryAlgorithm

card_limit

The upper cardinality limit if using cardinality restriction on object properties.

Type:

int

fitness_func

Fitness function.

Type:

AbstractFitness

height_limit

The maximum value allowed for the height of the Crossover and Mutation operations.

Type:

int

init_method

The evolutionary algorithm initialization method.

Type:

AbstractEAInitialization

kb

The knowledge base that the concept learner is using.

Type:

KnowledgeBase

max_num_of_concepts_tested

Limit to stop the algorithm after n concepts tested.

Type:

int

max_runtime

max_runtime: Limit to stop the algorithm after n seconds.

Type:

int

mut_uniform_gen

The initialization method to create the tree for mutation operation.

Type:

AbstractEAInitialization

name

Name of the model = ‘evolearner’.

Type:

str

num_generations

Number of generation for the evolutionary algorithm.

Type:

int

_number_of_tested_concepts

Yes, you got it. This stores the number of tested concepts.

Type:

int

population_size

Population size for the evolutionary algorithm.

Type:

int

pset

Contains the primitives that can be used to solve a Strongly Typed GP problem.

Type:

gp.PrimitiveSetTyped

quality_func

Function to evaluate the quality of solution concepts.

reasoner

The reasoner that this model is using.

Type:

OWLReasoner

start_time

The time when fit() starts the execution. Used to calculate the total time fit() takes to execute.

Type:

float

terminate_on_goal

Whether to stop the algorithm if a perfect solution is found.

Type:

bool

toolbox

A toolbox for evolution that contains the evolutionary operators.

Type:

base.Toolbox

tournament_size

The number of evolutionary individuals participating in each tournament.

Type:

int

use_card_restrictions

Use cardinality restriction for object properties?

Type:

bool

use_data_properties

Consider data properties?

Type:

bool

use_inverse

Consider inversed concepts?

Type:

bool

value_splitter

Used to calculate the splits for data properties values.

Type:

AbstractValueSplitter

__slots__ = ('fitness_func', 'init_method', 'algorithm', 'value_splitter', 'tournament_size',...
name = 'evolearner'
kb: KnowledgeBase
fitness_func: AbstractFitness
init_method: AbstractEAInitialization
algorithm: AbstractEvolutionaryAlgorithm
mut_uniform_gen: AbstractEAInitialization
value_splitter: AbstractValueSplitter
use_data_properties: bool
use_card_restrictions: bool
use_inverse: bool
tournament_size: int
card_limit: int
population_size: int
num_generations: int
height_limit: int
pset: deap.gp.PrimitiveSetTyped
toolbox: deap.base.Toolbox
register_op(alias: str, function: Callable, *args, **kargs)[source]

Register a function in the toolbox under the name alias. You may provide default arguments that will be passed automatically when calling the registered function. Fixed arguments can then be overriden at function call time.

Parameters:
  • alias – The name the operator will take in the toolbox. If the alias already exist it will overwrite the operator already present.

  • function – The function to which refer the alias.

  • args – One or more argument (and keyword argument) to pass automatically to the registered function when called, optional.

fit(*args, **kwargs) EvoLearner[source]

Find hypotheses that explain pos and neg.

best_hypotheses(n: int = 1, key: str = 'fitness', return_node: bool = False) owlapy.class_expression.OWLClassExpression | Iterable[owlapy.class_expression.OWLClassExpression][source]

Get the current best found hypotheses according to the quality.

Parameters:

n – Maximum number of results.

Returns:

Iterable with hypotheses in form of search tree nodes.

clean(partial: bool = False)[source]

Clear all states of the concept learner.

class ontolearn.concept_learner.CLIP(knowledge_base: KnowledgeBase, knowledge_base_path='', reasoner: owlapy.owl_reasoner.OWLReasoner | None = None, refinement_operator: BaseRefinement[OENode] | None = ExpressRefinement, quality_func: AbstractScorer | None = None, heuristic_func: AbstractHeuristic | None = None, terminate_on_goal: bool | None = None, iter_bound: int | None = None, max_num_of_concepts_tested: int | None = None, max_runtime: int | None = None, max_results: int = 10, best_only: bool = False, calculate_min_max: bool = True, path_of_embeddings='', predictor_name=None, pretrained_predictor_name=['SetTransformer', 'LSTM', 'GRU', 'CNN'], load_pretrained=False, num_workers=4, num_examples=1000, output_size=15)[source]

Bases: CELOE

Concept Learner with Integrated Length Prediction. This algorithm extends the CELOE algorithm by using concept length predictors and a different refinement operator, i.e., ExpressRefinement

best_descriptions

Best hypotheses ordered.

Type:

EvaluatedDescriptionSet[OENode, QualityOrderedNode]

best_only

If False pick only nodes with quality < 1.0, else pick without quality restrictions.

Type:

bool

calculate_min_max

Calculate minimum and maximum horizontal expansion? Statistical purpose only.

Type:

bool

heuristic_func

Function to guide the search heuristic.

Type:

AbstractHeuristic

heuristic_queue

A sorted set that compares the nodes based on Heuristic.

Type:

SortedSet[OENode]

iter_bound

Limit to stop the algorithm after n refinement steps are done.

Type:

int

kb

The knowledge base that the concept learner is using.

Type:

KnowledgeBase

max_child_length

Limit the length of concepts generated by the refinement operator.

Type:

int

max_he

Maximal value of horizontal expansion.

Type:

int

max_num_of_concepts_tested
Type:

int

max_runtime

Limit to stop the algorithm after n seconds.

Type:

int

min_he

Minimal value of horizontal expansion.

Type:

int

name

Name of the model = ‘celoe_python’.

Type:

str

_number_of_tested_concepts

Yes, you got it. This stores the number of tested concepts.

Type:

int

operator

Operator used to generate refinements.

Type:

BaseRefinement

quality_func
Type:

AbstractScorer

reasoner

The reasoner that this model is using.

Type:

OWLReasoner

search_tree

Dict to store the TreeNode for a class expression.

Type:

Dict[OWLClassExpression, TreeNode[OENode]]

start_class

The starting class expression for the refinement operation.

Type:

OWLClassExpression

start_time

The time when fit() starts the execution. Used to calculate the total time fit() takes to execute.

Type:

float

terminate_on_goal

Whether to stop the algorithm if a perfect solution is found.

Type:

bool

__slots__ = ('best_descriptions', 'max_he', 'min_he', 'best_only', 'calculate_min_max', 'heuristic_queue',...
name = 'clip'
get_length_predictor()[source]
refresh()[source]
collate_batch(batch)[source]
collate_batch_inference(batch)[source]
pos_neg_to_tensor(pos: Set[owlapy.owl_individual.OWLNamedIndividual], neg: Set[owlapy.owl_individual.OWLNamedIndividual] | Set[str])[source]
predict_length(models, x1, x2)[source]
fit(*args, **kwargs)[source]

Find hypotheses that explain pos and neg.

train(data: Iterable[List[Tuple]], epochs=300, batch_size=256, learning_rate=0.001, decay_rate=0.0, clip_value=5.0, save_model=True, storage_path=None, optimizer='Adam', record_runtime=True, example_sizes=None, shuffle_examples=False)[source]

Train RL agent on learning problems.

Returns:

self.

class ontolearn.concept_learner.NCES(knowledge_base_path, quality_func: AbstractScorer | None = None, num_predictions=5, learner_name='SetTransformer', path_of_embeddings='', proj_dim=128, rnn_n_layers=2, drop_prob=0.1, num_heads=4, num_seeds=1, num_inds=32, ln=False, learning_rate=0.0001, decay_rate=0.0, clip_value=5.0, batch_size=256, num_workers=8, max_length=48, load_pretrained=True, sorted_examples=False, pretrained_model_name=None)[source]

Bases: ontolearn.base_nces.BaseNCES

Neural Class Expression Synthesis.

get_synthesizer()[source]
refresh()[source]
sample_examples(pos, neg)[source]
get_prediction(models, x1, x2)[source]
fit_one(pos: Set[owlapy.owl_individual.OWLNamedIndividual] | Set[str], neg: Set[owlapy.owl_individual.OWLNamedIndividual] | Set[str], verbose=False)[source]
fit(pos: Set[owlapy.owl_individual.OWLNamedIndividual] | Set[str], neg: Set[owlapy.owl_individual.OWLNamedIndividual] | Set[str], verbose=False, **kwargs)[source]
best_hypotheses(n=1) owlapy.class_expression.OWLClassExpression | Iterable[owlapy.class_expression.OWLClassExpression][source]
convert_to_list_str_from_iterable(data)[source]
fit_from_iterable(dataset: List[Tuple[str, Set[owlapy.owl_individual.OWLNamedIndividual], Set[owlapy.owl_individual.OWLNamedIndividual]]] | List[Tuple[str, Set[str], Set[str]]], shuffle_examples=False, verbose=False, **kwargs) List[source]
  • Dataset is a list of tuples where the first items are strings corresponding to target concepts.

  • This function returns predictions as owl class expressions, not nodes as in fit

train(data: Iterable[List[Tuple]], epochs=300, batch_size=None, learning_rate=0.0001, decay_rate=0.0, clip_value=5.0, num_workers=8, save_model=True, storage_path=None, optimizer='Adam', record_runtime=True, example_sizes=None, shuffle_examples=False)[source]