:py:mod:`ontolearn.concept_learner` =================================== .. py:module:: ontolearn.concept_learner .. autoapi-nested-parse:: Concept learning algorithms of Ontolearn. Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: ontolearn.concept_learner.CELOE ontolearn.concept_learner.OCEL ontolearn.concept_learner.EvoLearner ontolearn.concept_learner.CLIP ontolearn.concept_learner.NCES Attributes ~~~~~~~~~~ .. autoapisummary:: ontolearn.concept_learner.logger .. py:data:: logger .. py:class:: CELOE(knowledge_base: ontolearn.knowledge_base.KnowledgeBase, reasoner: Optional[owlapy.owl_reasoner.OWLReasoner] = None, refinement_operator: Optional[ontolearn.abstracts.BaseRefinement[ontolearn.search.OENode]] = None, quality_func: Optional[ontolearn.abstracts.AbstractScorer] = None, heuristic_func: Optional[ontolearn.abstracts.AbstractHeuristic] = None, terminate_on_goal: Optional[bool] = None, iter_bound: Optional[int] = None, max_num_of_concepts_tested: Optional[int] = None, max_runtime: Optional[int] = None, max_results: int = 10, best_only: bool = False, calculate_min_max: bool = True) Bases: :py:obj:`ontolearn.base_concept_learner.RefinementBasedConceptLearner`\ [\ :py:obj:`ontolearn.search.OENode`\ ] Class Expression Learning for Ontology Engineering. .. attribute:: best_descriptions Best hypotheses ordered. :type: EvaluatedDescriptionSet[OENode, QualityOrderedNode] .. attribute:: best_only If False pick only nodes with quality < 1.0, else pick without quality restrictions. :type: bool .. attribute:: calculate_min_max Calculate minimum and maximum horizontal expansion? Statistical purpose only. :type: bool .. attribute:: heuristic_func Function to guide the search heuristic. :type: AbstractHeuristic .. attribute:: heuristic_queue A sorted set that compares the nodes based on Heuristic. :type: SortedSet[OENode] .. attribute:: iter_bound Limit to stop the algorithm after n refinement steps are done. :type: int .. attribute:: kb The knowledge base that the concept learner is using. :type: KnowledgeBase .. attribute:: max_child_length Limit the length of concepts generated by the refinement operator. :type: int .. attribute:: max_he Maximal value of horizontal expansion. :type: int .. attribute:: max_num_of_concepts_tested :type: int .. attribute:: max_runtime Limit to stop the algorithm after n seconds. :type: int .. attribute:: min_he Minimal value of horizontal expansion. :type: int .. attribute:: name Name of the model = 'celoe_python'. :type: str .. attribute:: _number_of_tested_concepts Yes, you got it. This stores the number of tested concepts. :type: int .. attribute:: operator Operator used to generate refinements. :type: BaseRefinement .. attribute:: quality_func :type: AbstractScorer .. attribute:: reasoner The reasoner that this model is using. :type: OWLReasoner .. attribute:: search_tree Dict to store the TreeNode for a class expression. :type: Dict[OWLClassExpression, TreeNode[OENode]] .. attribute:: start_class The starting class expression for the refinement operation. :type: OWLClassExpression .. attribute:: start_time The time when :meth:`fit` starts the execution. Used to calculate the total time :meth:`fit` takes to execute. :type: float .. attribute:: terminate_on_goal Whether to stop the algorithm if a perfect solution is found. :type: bool .. py:attribute:: __slots__ :value: ('best_descriptions', 'max_he', 'min_he', 'best_only', 'calculate_min_max', 'heuristic_queue',... .. py:attribute:: name :value: 'celoe_python' .. py:attribute:: kb :type: ontolearn.knowledge_base.KnowledgeBase .. py:attribute:: max_he :type: int .. py:attribute:: min_he :type: int .. py:attribute:: best_only :type: bool .. py:attribute:: calculate_min_max :type: bool .. py:attribute:: search_tree :type: Dict[owlapy.class_expression.OWLClassExpression, ontolearn.search.TreeNode[ontolearn.search.OENode]] .. py:attribute:: seen_norm_concepts :type: Set[owlapy.class_expression.OWLClassExpression] .. py:attribute:: heuristic_queue :type: SortedSet[OENode] .. py:attribute:: best_descriptions :type: ontolearn.base.owl.utils.EvaluatedDescriptionSet[ontolearn.search.OENode, ontolearn.search.QualityOrderedNode] .. py:method:: next_node_to_expand(step: int) -> ontolearn.search.OENode 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. :rtype: _N .. py:method:: best_hypotheses(n: int = 1, return_node: bool = False) -> Union[Union[owlapy.class_expression.OWLClassExpression, Iterable[owlapy.class_expression.OWLClassExpression]], Union[ontolearn.search.OENode, Iterable[ontolearn.search.OENode]]] Get the current best found hypotheses according to the quality. :param n: Maximum number of results. :returns: Iterable with hypotheses in form of search tree nodes. .. py:method:: make_node(c: owlapy.class_expression.OWLClassExpression, parent_node: Optional[ontolearn.search.OENode] = None, is_root: bool = False) -> ontolearn.search.OENode Create a node for CELOE. :param c: The class expression of this node. :param parent_node: Parent node. :param is_root: Is this the root node? :returns: The node. :rtype: OENode .. py:method:: updating_node(node: ontolearn.search.OENode) Removes the node from the heuristic sorted set and inserts it again. :param Node to update.: :Yields: The node itself. .. py:method:: downward_refinement(node: ontolearn.search.OENode) -> Iterable[ontolearn.search.OENode] Execute one refinement step of a refinement based learning algorithm. :param node: the search tree node on which to refine. :type node: _N :returns: Refinement results as new search tree nodes (they still need to be added to the tree). :rtype: Iterable[_N] .. py:method:: fit(*args, **kwargs) Find hypotheses that explain pos and neg. .. py:method:: fit_async(*args, **kwargs) :async: Async method of fit. .. py:method:: encoded_learning_problem() -> Optional[ontolearn.abstracts.EncodedPosNegLPStandardKind] Fetch the most recently used learning problem from the fit method. .. py:method:: tree_node(node: ontolearn.search.OENode) -> ontolearn.search.TreeNode[ontolearn.search.OENode] Get the TreeNode of the given node. :param node: The node. :returns: TreeNode of the given node. .. py:method:: show_search_tree(heading_step: str, top_n: int = 10) -> None Show search tree. .. py:method:: update_min_max_horiz_exp(node: ontolearn.search.OENode) .. py:method:: clean() Clear all states of the concept learner. .. py:class:: OCEL(knowledge_base: ontolearn.knowledge_base.KnowledgeBase, reasoner: Optional[owlapy.owl_reasoner.OWLReasoner] = None, refinement_operator: Optional[ontolearn.abstracts.BaseRefinement[ontolearn.search.OENode]] = None, quality_func: Optional[ontolearn.abstracts.AbstractScorer] = None, heuristic_func: Optional[ontolearn.abstracts.AbstractHeuristic] = None, terminate_on_goal: Optional[bool] = None, iter_bound: Optional[int] = None, max_num_of_concepts_tested: Optional[int] = None, max_runtime: Optional[int] = None, max_results: int = 10, best_only: bool = False, calculate_min_max: bool = True) Bases: :py:obj:`CELOE` A limited version of CELOE. .. attribute:: best_descriptions Best hypotheses ordered. :type: EvaluatedDescriptionSet[OENode, QualityOrderedNode] .. attribute:: best_only If False pick only nodes with quality < 1.0, else pick without quality restrictions. :type: bool .. attribute:: calculate_min_max Calculate minimum and maximum horizontal expansion? Statistical purpose only. :type: bool .. attribute:: heuristic_func Function to guide the search heuristic. :type: AbstractHeuristic .. attribute:: heuristic_queue A sorted set that compares the nodes based on Heuristic. :type: SortedSet[OENode] .. attribute:: iter_bound Limit to stop the algorithm after n refinement steps are done. :type: int .. attribute:: kb The knowledge base that the concept learner is using. :type: KnowledgeBase .. attribute:: max_child_length Limit the length of concepts generated by the refinement operator. :type: int .. attribute:: max_he Maximal value of horizontal expansion. :type: int .. attribute:: max_num_of_concepts_tested :type: int .. attribute:: max_runtime Limit to stop the algorithm after n seconds. :type: int .. attribute:: min_he Minimal value of horizontal expansion. :type: int .. attribute:: name Name of the model = 'ocel_python'. :type: str .. attribute:: _number_of_tested_concepts Yes, you got it. This stores the number of tested concepts. :type: int .. attribute:: operator Operator used to generate refinements. :type: BaseRefinement .. attribute:: quality_func :type: AbstractScorer .. attribute:: reasoner The reasoner that this model is using. :type: OWLReasoner .. attribute:: search_tree Dict to store the TreeNode for a class expression. :type: Dict[OWLClassExpression, TreeNode[OENode]] .. attribute:: start_class The starting class expression for the refinement operation. :type: OWLClassExpression .. attribute:: start_time The time when :meth:`fit` starts the execution. Used to calculate the total time :meth:`fit` takes to execute. :type: float .. attribute:: terminate_on_goal Whether to stop the algorithm if a perfect solution is found. :type: bool .. py:attribute:: __slots__ :value: () .. py:attribute:: name :value: 'ocel_python' .. py:method:: make_node(c: owlapy.class_expression.OWLClassExpression, parent_node: Optional[ontolearn.search.OENode] = None, is_root: bool = False) -> ontolearn.search.OENode Create a node for OCEL. :param c: The class expression of this node. :param parent_node: Parent node. :param is_root: Is this the root node? :returns: The node. :rtype: OENode .. py:class:: EvoLearner(knowledge_base: ontolearn.knowledge_base.KnowledgeBase, reasoner: Optional[owlapy.owl_reasoner.OWLReasoner] = None, quality_func: Optional[ontolearn.abstracts.AbstractScorer] = None, fitness_func: Optional[ontolearn.abstracts.AbstractFitness] = None, init_method: Optional[ontolearn.ea_initialization.AbstractEAInitialization] = None, algorithm: Optional[ontolearn.ea_algorithms.AbstractEvolutionaryAlgorithm] = None, mut_uniform_gen: Optional[ontolearn.ea_initialization.AbstractEAInitialization] = None, value_splitter: Optional[ontolearn.value_splitter.AbstractValueSplitter] = None, terminate_on_goal: Optional[bool] = None, max_runtime: Optional[int] = 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) Bases: :py:obj:`ontolearn.base_concept_learner.BaseConceptLearner`\ [\ :py:obj:`ontolearn.search.EvoLearnerNode`\ ] An evolutionary approach to learn concepts in ALCQ(D). .. attribute:: algorithm The evolutionary algorithm. :type: AbstractEvolutionaryAlgorithm .. attribute:: card_limit The upper cardinality limit if using cardinality restriction on object properties. :type: int .. attribute:: fitness_func Fitness function. :type: AbstractFitness .. attribute:: height_limit The maximum value allowed for the height of the Crossover and Mutation operations. :type: int .. attribute:: init_method The evolutionary algorithm initialization method. :type: AbstractEAInitialization .. attribute:: kb The knowledge base that the concept learner is using. :type: KnowledgeBase .. attribute:: max_num_of_concepts_tested Limit to stop the algorithm after n concepts tested. :type: int .. attribute:: max_runtime max_runtime: Limit to stop the algorithm after n seconds. :type: int .. attribute:: mut_uniform_gen The initialization method to create the tree for mutation operation. :type: AbstractEAInitialization .. attribute:: name Name of the model = 'evolearner'. :type: str .. attribute:: num_generations Number of generation for the evolutionary algorithm. :type: int .. attribute:: _number_of_tested_concepts Yes, you got it. This stores the number of tested concepts. :type: int .. attribute:: population_size Population size for the evolutionary algorithm. :type: int .. attribute:: pset Contains the primitives that can be used to solve a Strongly Typed GP problem. :type: gp.PrimitiveSetTyped .. attribute:: quality_func Function to evaluate the quality of solution concepts. .. attribute:: reasoner The reasoner that this model is using. :type: OWLReasoner .. attribute:: start_time The time when :meth:`fit` starts the execution. Used to calculate the total time :meth:`fit` takes to execute. :type: float .. attribute:: terminate_on_goal Whether to stop the algorithm if a perfect solution is found. :type: bool .. attribute:: toolbox A toolbox for evolution that contains the evolutionary operators. :type: base.Toolbox .. attribute:: tournament_size The number of evolutionary individuals participating in each tournament. :type: int .. attribute:: use_card_restrictions Use cardinality restriction for object properties? :type: bool .. attribute:: use_data_properties Consider data properties? :type: bool .. attribute:: use_inverse Consider inversed concepts? :type: bool .. attribute:: value_splitter Used to calculate the splits for data properties values. :type: AbstractValueSplitter .. py:attribute:: __slots__ :value: ('fitness_func', 'init_method', 'algorithm', 'value_splitter', 'tournament_size',... .. py:attribute:: name :value: 'evolearner' .. py:attribute:: kb :type: ontolearn.knowledge_base.KnowledgeBase .. py:attribute:: fitness_func :type: ontolearn.abstracts.AbstractFitness .. py:attribute:: init_method :type: ontolearn.ea_initialization.AbstractEAInitialization .. py:attribute:: algorithm :type: ontolearn.ea_algorithms.AbstractEvolutionaryAlgorithm .. py:attribute:: mut_uniform_gen :type: ontolearn.ea_initialization.AbstractEAInitialization .. py:attribute:: value_splitter :type: ontolearn.value_splitter.AbstractValueSplitter .. py:attribute:: use_data_properties :type: bool .. py:attribute:: use_card_restrictions :type: bool .. py:attribute:: use_inverse :type: bool .. py:attribute:: tournament_size :type: int .. py:attribute:: card_limit :type: int .. py:attribute:: population_size :type: int .. py:attribute:: num_generations :type: int .. py:attribute:: height_limit :type: int .. py:attribute:: pset :type: deap.gp.PrimitiveSetTyped .. py:attribute:: toolbox :type: deap.base.Toolbox .. py:method:: register_op(alias: str, function: Callable, *args, **kargs) 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. :param alias: The name the operator will take in the toolbox. If the alias already exist it will overwrite the operator already present. :param function: The function to which refer the alias. :param args: One or more argument (and keyword argument) to pass automatically to the registered function when called, optional. .. py:method:: fit(*args, **kwargs) -> EvoLearner Find hypotheses that explain pos and neg. .. py:method:: best_hypotheses(n: int = 1, key: str = 'fitness', return_node: bool = False) -> Union[owlapy.class_expression.OWLClassExpression, Iterable[owlapy.class_expression.OWLClassExpression]] Get the current best found hypotheses according to the quality. :param n: Maximum number of results. :returns: Iterable with hypotheses in form of search tree nodes. .. py:method:: clean(partial: bool = False) Clear all states of the concept learner. .. py:class:: CLIP(knowledge_base: ontolearn.knowledge_base.KnowledgeBase, knowledge_base_path='', reasoner: Optional[owlapy.owl_reasoner.OWLReasoner] = None, refinement_operator: Optional[ontolearn.abstracts.BaseRefinement[ontolearn.search.OENode]] = ExpressRefinement, quality_func: Optional[ontolearn.abstracts.AbstractScorer] = None, heuristic_func: Optional[ontolearn.abstracts.AbstractHeuristic] = None, terminate_on_goal: Optional[bool] = None, iter_bound: Optional[int] = None, max_num_of_concepts_tested: Optional[int] = None, max_runtime: Optional[int] = 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) Bases: :py:obj:`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 .. attribute:: best_descriptions Best hypotheses ordered. :type: EvaluatedDescriptionSet[OENode, QualityOrderedNode] .. attribute:: best_only If False pick only nodes with quality < 1.0, else pick without quality restrictions. :type: bool .. attribute:: calculate_min_max Calculate minimum and maximum horizontal expansion? Statistical purpose only. :type: bool .. attribute:: heuristic_func Function to guide the search heuristic. :type: AbstractHeuristic .. attribute:: heuristic_queue A sorted set that compares the nodes based on Heuristic. :type: SortedSet[OENode] .. attribute:: iter_bound Limit to stop the algorithm after n refinement steps are done. :type: int .. attribute:: kb The knowledge base that the concept learner is using. :type: KnowledgeBase .. attribute:: max_child_length Limit the length of concepts generated by the refinement operator. :type: int .. attribute:: max_he Maximal value of horizontal expansion. :type: int .. attribute:: max_num_of_concepts_tested :type: int .. attribute:: max_runtime Limit to stop the algorithm after n seconds. :type: int .. attribute:: min_he Minimal value of horizontal expansion. :type: int .. attribute:: name Name of the model = 'celoe_python'. :type: str .. attribute:: _number_of_tested_concepts Yes, you got it. This stores the number of tested concepts. :type: int .. attribute:: operator Operator used to generate refinements. :type: BaseRefinement .. attribute:: quality_func :type: AbstractScorer .. attribute:: reasoner The reasoner that this model is using. :type: OWLReasoner .. attribute:: search_tree Dict to store the TreeNode for a class expression. :type: Dict[OWLClassExpression, TreeNode[OENode]] .. attribute:: start_class The starting class expression for the refinement operation. :type: OWLClassExpression .. attribute:: start_time The time when :meth:`fit` starts the execution. Used to calculate the total time :meth:`fit` takes to execute. :type: float .. attribute:: terminate_on_goal Whether to stop the algorithm if a perfect solution is found. :type: bool .. py:attribute:: __slots__ :value: ('best_descriptions', 'max_he', 'min_he', 'best_only', 'calculate_min_max', 'heuristic_queue',... .. py:attribute:: name :value: 'clip' .. py:method:: get_length_predictor() .. py:method:: refresh() .. py:method:: collate_batch(batch) .. py:method:: collate_batch_inference(batch) .. py:method:: pos_neg_to_tensor(pos: Union[Set[owlapy.owl_individual.OWLNamedIndividual]], neg: Union[Set[owlapy.owl_individual.OWLNamedIndividual], Set[str]]) .. py:method:: predict_length(models, x1, x2) .. py:method:: fit(*args, **kwargs) Find hypotheses that explain pos and neg. .. py:method:: 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) Train RL agent on learning problems. :returns: self. .. py:class:: NCES(knowledge_base_path, quality_func: Optional[ontolearn.abstracts.AbstractScorer] = 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) Bases: :py:obj:`ontolearn.base_nces.BaseNCES` Neural Class Expression Synthesis. .. py:method:: get_synthesizer() .. py:method:: refresh() .. py:method:: sample_examples(pos, neg) .. py:method:: get_prediction(models, x1, x2) .. py:method:: fit_one(pos: Union[Set[owlapy.owl_individual.OWLNamedIndividual], Set[str]], neg: Union[Set[owlapy.owl_individual.OWLNamedIndividual], Set[str]], verbose=False) .. py:method:: fit(pos: Union[Set[owlapy.owl_individual.OWLNamedIndividual], Set[str]], neg: Union[Set[owlapy.owl_individual.OWLNamedIndividual], Set[str]], verbose=False, **kwargs) .. py:method:: best_hypotheses(n=1) -> Union[owlapy.class_expression.OWLClassExpression, Iterable[owlapy.class_expression.OWLClassExpression]] .. py:method:: convert_to_list_str_from_iterable(data) .. py:method:: fit_from_iterable(dataset: Union[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 - 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 .. py:method:: 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)