ontolearn.learners

Submodules

Classes

Drill

Neuro-Symbolic Class Expression Learning (https://www.ijcai.org/proceedings/2023/0403.pdf)

TDL

Tree-based Description Logic Concept Learner

CELOE

Class Expression Learning for Ontology Engineering.

OCEL

A limited version of CELOE.

Package Contents

class ontolearn.learners.Drill(knowledge_base, path_embeddings: str = None, refinement_operator: LengthBasedRefinement = None, use_inverse: bool = True, use_data_properties: bool = True, use_card_restrictions: bool = True, use_nominals: bool = True, min_cardinality_restriction: int = 2, max_cardinality_restriction: int = 5, positive_type_bias: int = 1, quality_func: Callable = None, reward_func: object = None, batch_size=None, num_workers: int = 1, iter_bound=None, max_num_of_concepts_tested=None, verbose: int = 0, terminate_on_goal=None, max_len_replay_memory=256, epsilon_decay: float = 0.01, epsilon_min: float = 0.0, num_epochs_per_replay: int = 2, num_episodes_per_replay: int = 2, learning_rate: float = 0.001, max_runtime=None, num_of_sequential_actions=3, stop_at_goal=True, num_episode: int = 10)[source]

Bases: ontolearn.base_concept_learner.RefinementBasedConceptLearner

Neuro-Symbolic Class Expression Learning (https://www.ijcai.org/proceedings/2023/0403.pdf)

name = 'DRILL'
verbose
learning_problem = None
num_workers
learning_rate
num_episode
num_of_sequential_actions
num_epochs_per_replay
max_len_replay_memory
epsilon_decay
epsilon_min
batch_size
num_episodes_per_replay
seen_examples
pos: FrozenSet[owlapy.owl_individual.OWLNamedIndividual] = None
neg: FrozenSet[owlapy.owl_individual.OWLNamedIndividual] = None
positive_type_bias
start_time = None
goal_found = False
search_tree
stop_at_goal
epsilon = 1
quality_func
initialize_training_class_expression_learning_problem(pos: FrozenSet[owlapy.owl_individual.OWLNamedIndividual], neg: FrozenSet[owlapy.owl_individual.OWLNamedIndividual]) RL_State[source]

Initialize

rl_learning_loop(pos_uri: FrozenSet[owlapy.owl_individual.OWLNamedIndividual], neg_uri: FrozenSet[owlapy.owl_individual.OWLNamedIndividual]) List[float][source]

Reinforcement Learning Training Loop

Initialize RL environment for a given learning problem (E^+ pos_iri and E^- neg_iri )

Training:

2.1 Obtain a trajectory: A sequence of RL states/DL concepts T, Person, (Female and

orall hasSibling Female).

Rewards at each transition are also computed

train(dataset: Iterable[Tuple[str, Set, Set]] | None = None, num_of_target_concepts: int = 1, num_learning_problems: int = 1)[source]

Training RL agent (1) Generate Learning Problems (2) For each learning problem, perform the RL loop

save(directory: str = None) None[source]

save weights of the deep Q-network

load(directory: str = None) None[source]

load weights of the deep Q-network

fit(learning_problem: PosNegLPStandard, max_runtime=None)[source]

Run the concept learning algorithm according to its configuration.

Once finished, the results can be queried with the best_hypotheses function.

init_embeddings_of_examples(pos_uri: FrozenSet[owlapy.owl_individual.OWLNamedIndividual], neg_uri: FrozenSet[owlapy.owl_individual.OWLNamedIndividual])[source]
create_rl_state(c: owlapy.class_expression.OWLClassExpression, parent_node: RL_State | None = None, is_root: bool = False) RL_State[source]

Create an RL_State instance.

compute_quality_of_class_expression(state: RL_State) None[source]

Compute Quality of owl class expression. # (1) Perform concept retrieval # (2) Compute the quality w.r.t. (1), positive and negative examples # (3) Increment the number of tested concepts attribute.

apply_refinement(rl_state: RL_State) Generator[source]

Downward refinements

select_next_state(current_state, next_rl_states) Tuple[RL_State, float][source]
sequence_of_actions(root_rl_state: RL_State) Tuple[List[Tuple[RL_State, RL_State]], List[SupportsFloat]][source]

Performing sequence of actions in an RL env whose root state is ⊤

form_experiences(state_pairs: List, rewards: List) None[source]

Form experiences from a sequence of concepts and corresponding rewards.

state_pairs - A list of tuples containing two consecutive states. reward - A list of reward.

Gamma is 1.

Return X - A list of embeddings of current concept, next concept, positive examples, negative examples. y - Argmax Q value.

learn_from_replay_memory() None[source]

Learning by replaying memory.

@param concepts: @param predicted_Q_values: @return:

get_embeddings_individuals(individuals: List[str]) torch.FloatTensor[source]
get_individuals(rl_state: RL_State) List[str][source]
assign_embeddings(rl_state: RL_State) None[source]

Assign embeddings to a rl state. A rl state is represented with vector representation of all individuals belonging to a respective OWLClassExpression.

save_weights(path: str = None) None[source]

Save weights DQL

exploration_exploitation_tradeoff(current_state: AbstractNode, next_states: List[AbstractNode]) AbstractNode[source]

Exploration vs Exploitation tradeoff at finding next state. (1) Exploration. (2) Exploitation.

exploitation(current_state: AbstractNode, next_states: List[AbstractNode]) RL_State[source]

Find next node that is assigned with highest predicted Q value.

  1. Predict Q values : predictions.shape => torch.Size([n, 1]) where n = len(next_states).

  2. Find the index of max value in predictions.

  3. Use the index to obtain next state.

  4. Return next state.

predict_values(current_state: RL_State, next_states: List[RL_State]) torch.Tensor[source]

Predict promise of next states given current state.

Returns:

Predicted Q values.

static retrieve_concept_chain(rl_state: RL_State) List[RL_State][source]
generate_learning_problems(num_of_target_concepts, num_learning_problems) List[Tuple[str, Set, Set]][source]

Generate learning problems if none is provided.

Time complexity: O(n^2) n = named concepts

learn_from_illustration(sequence_of_goal_path: List[RL_State])[source]
Parameters:

sequence_of_goal_path – ⊤,Parent,Parent ⊓ Daughter.

best_hypotheses(n=1, return_node: bool = False) owlapy.class_expression.OWLClassExpression | List[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()[source]

Clear all states of the concept learner.

next_node_to_expand() RL_State[source]

Return a node that maximizes the heuristic function at time t.

downward_refinement(*args, **kwargs)[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]

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

A debugging function to print out the current search tree and the current n best found hypotheses to standard output.

Parameters:
  • heading_step – A message to display at the beginning of the output.

  • top_n – The number of current best hypotheses to print out.

terminate_training()[source]
class ontolearn.learners.TDL(knowledge_base, use_inverse: bool = False, use_data_properties: bool = False, use_nominals: bool = False, use_card_restrictions: bool = False, kwargs_classifier: dict = None, max_runtime: int = 1, grid_search_over: dict = None, grid_search_apply: bool = False, report_classification: bool = True, plot_tree: bool = False, plot_embeddings: bool = False, plot_feature_importance: bool = False, verbose: int = 10)[source]

Tree-based Description Logic Concept Learner

use_nominals
use_card_restrictions
grid_search_over
knowledge_base
report_classification
plot_tree
plot_embeddings
plot_feature_importance
clf = None
kwargs_classifier
max_runtime
features = None
disjunction_of_conjunctive_concepts = None
conjunctive_concepts = None
owl_class_expressions
cbd_mapping: Dict[str, Set[Tuple[str, str]]]
types_of_individuals
verbose
data_property_cast
X = None
y = None
extract_expressions_from_owl_individuals(individuals: List[owlapy.owl_individual.OWLNamedIndividual]) Tuple[Dict[str, owlapy.class_expression.OWLClassExpression], Dict[str, str]][source]
construct_sparse_binary_representations(features: List[owlapy.class_expression.OWLClassExpression], examples: List[owlapy.owl_individual.OWLNamedIndividual], examples_to_features) numpy.array[source]
create_training_data(learning_problem: PosNegLPStandard) Tuple[pandas.DataFrame, pandas.DataFrame][source]
construct_owl_expression_from_tree(X: pandas.DataFrame, y: pandas.DataFrame) List[owlapy.class_expression.OWLObjectIntersectionOf][source]

Construct an OWL class expression from a decision tree

fit(learning_problem: PosNegLPStandard = None, max_runtime: int = None)[source]

Fit the learner to the given learning problem

  1. Extract multi-hop information about E^+ and E^-.

  2. Create OWL Class Expressions from (1)

  3. Build a binary sparse training data X where first |E+| rows denote the binary representations of positives Remaining rows denote the binary representations of E⁻

(4) Create binary labels. (4) Construct a set of DL concept for each e in E^+ (5) Union (4)

Parameters:

learning_problem – The learning problem

:param max_runtime:total runtime of the learning

property classification_report: str
best_hypotheses(n=1) Tuple[owlapy.class_expression.OWLClassExpression, List[owlapy.class_expression.OWLClassExpression]][source]

Return the prediction

abstract predict(X: List[owlapy.owl_individual.OWLNamedIndividual], proba=True) numpy.ndarray[source]

Predict the likelihoods of individuals belonging to the classes

class ontolearn.learners.CELOE(knowledge_base: KnowledgeBase = None, reasoner: owlapy.abstracts.AbstractOWLReasoner | 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

Class Expression Learning for Ontology Engineering. .. attribute:: 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 = 'celoe_python'
search_tree: Dict[owlapy.class_expression.OWLClassExpression, TreeNode[OENode]]
heuristic_queue
best_descriptions
best_only
calculate_min_max
max_he = 0
min_he = 1
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]
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.

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.learners.OCEL(knowledge_base: KnowledgeBase, reasoner: owlapy.abstracts.AbstractOWLReasoner | 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.learners.celoe.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:

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__ = ()
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