ontolearn.learners.drill

Module Contents

Classes

Drill

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

DrillHeuristic

Heuristic in Convolutional DQL concept learning.

DrillNet

A neural model for Deep Q-Learning.

class ontolearn.learners.drill.Drill(knowledge_base, path_embeddings: str = None, refinement_operator: LengthBasedRefinement = None, use_inverse=True, use_data_properties=True, use_card_restrictions=True, card_limit=3, use_nominals=True, 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 = 100, num_episodes_per_replay: int = 2, learning_rate: float = 0.001, max_runtime=None, num_of_sequential_actions=1, stop_at_goal=True, num_episode=10)[source]

Bases: ontolearn.base_concept_learner.RefinementBasedConceptLearner

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

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(num_episode: int, 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 = 3, num_learning_problems: int = 3)[source]

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

save(directory: str) 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.

fit_from_iterable(dataset: List[Tuple[object, Set[owlapy.owl_individual.OWLNamedIndividual], Set[owlapy.owl_individual.OWLNamedIndividual]]], max_runtime: int = None) List[source]

Dataset is a list of tuples where the first item is either str or OWL class expression indicating target concept.

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]
get_embeddings(instances) None[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(dataset: Iterable[Tuple[str, Set, Set]] | None = None, num_of_target_concepts: int = 3, num_learning_problems: int = 5) Iterable[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.drill.DrillHeuristic(pos=None, neg=None, model=None, mode=None, model_args=None)[source]

Heuristic in Convolutional DQL concept learning. Heuristic implements a convolutional neural network.

score(node, parent_node=None)[source]

Compute heuristic value of root node only

apply(node, parent_node=None)[source]

Assign predicted Q-value to node object.

class ontolearn.learners.drill.DrillNet(args)[source]

Bases: torch.nn.Module

A neural model for Deep Q-Learning.

An input Drill has the following form:
  1. Indexes of individuals belonging to current state (s).

  2. Indexes of individuals belonging to next state (s_prime).

  3. Indexes of individuals provided as positive examples.

  4. Indexes of individuals provided as negative examples.

Given such input, we from a sparse 3D Tensor where each slice is a **** N * by ***D* where N is the number of individuals and D is the number of dimension of embeddings. Given that N on the current benchmark datasets < 10^3, we can get away with this computation. By doing so we do not need to subsample from given inputs.

init()[source]
forward(X: torch.FloatTensor)[source]

X n by 4 by d float tensor