ontolearn.concept_learner

Concept learning algorithms of Ontolearn.

Classes

EvoLearner

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

CLIP

Concept Learner with Integrated Length Prediction.

NCES

Neural Class Expression Synthesis.

Module Contents

class ontolearn.concept_learner.EvoLearner(knowledge_base: KnowledgeBase, reasoner: owlapy.abstracts.AbstractOWLReasoner | 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

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:

AbstractOWLReasoner

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
reasoner
total_fits = 0
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.abstracts.AbstractOWLReasoner | 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: ontolearn.learners.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:

AbstractOWLReasoner

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'
predictor_name
pretrained_predictor_name
knowledge_base_path
load_pretrained
num_workers
output_size
num_examples
path_of_embeddings
instance_embeddings
input_size
device
length_predictor
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_names=['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=4, max_length=48, load_pretrained=True, sorted_examples=False, verbose: int = 0)[source]

Bases: ontolearn.base_nces.BaseNCES

Neural Class Expression Synthesis.

quality_func
num_predictions
path_of_embeddings
device
max_length
proj_dim
rnn_n_layers
drop_prob
num_heads
num_seeds
num_inds
ln
load_pretrained
sorted_examples
verbose
model
dl_parser
best_predictions = None
get_synthesizer(path=None)[source]
refresh(path=None)[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])[source]
fit(learning_problem: PosNegLPStandard, **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

static generate_training_data(kb_path, num_lps=1000, storage_dir='./NCES_Training_Data')[source]
train(data: Iterable[List[Tuple]] = None, epochs=50, batch_size=64, num_lps=1000, 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]