ontolearn.concept_learner ========================= .. py:module:: ontolearn.concept_learner .. autoapi-nested-parse:: Concept learning algorithms of Ontolearn. Classes ------- .. autoapisummary:: ontolearn.concept_learner.EvoLearner ontolearn.concept_learner.CLIP ontolearn.concept_learner.NCES ontolearn.concept_learner.NCES2 ontolearn.concept_learner.ROCES Module Contents --------------- .. py:class:: EvoLearner(knowledge_base: ontolearn.abstracts.AbstractKnowledgeBase, reasoner: Optional[owlapy.abstracts.AbstractOWLReasoner] = 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` 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: AbstractKnowledgeBase .. 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: AbstractOWLReasoner .. 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.abstracts.AbstractKnowledgeBase .. 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:: generator :type: ontolearn.concept_generator.ConceptGenerator .. py:attribute:: pset :type: deap.gp.PrimitiveSetTyped .. py:attribute:: toolbox :type: deap.base.Toolbox .. py:attribute:: reasoner :value: None .. py:attribute:: total_fits :value: 0 .. 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.abstracts.AbstractKnowledgeBase, knowledge_base_path='', reasoner: Optional[owlapy.abstracts.AbstractOWLReasoner] = 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:`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 .. 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: AbstractKnowledgeBase .. 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: AbstractOWLReasoner .. 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:attribute:: predictor_name :value: None .. py:attribute:: pretrained_predictor_name :value: ['SetTransformer', 'LSTM', 'GRU', 'CNN'] .. py:attribute:: knowledge_base_path :value: '' .. py:attribute:: load_pretrained :value: False .. py:attribute:: num_workers :value: 4 .. py:attribute:: output_size :value: 15 .. py:attribute:: num_examples :value: 1000 .. py:attribute:: path_of_embeddings :value: '' .. py:attribute:: device .. py:attribute:: length_predictor .. 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[List[owlapy.owl_individual.OWLNamedIndividual], List[str]], neg: Union[List[owlapy.owl_individual.OWLNamedIndividual], List[str]]) .. py:method:: predict_length(models, x_pos, x_neg) .. 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, nces2_or_roces=False, quality_func: Optional[ontolearn.abstracts.AbstractScorer] = None, num_predictions=5, learner_names=['SetTransformer', 'LSTM', 'GRU'], path_of_embeddings=None, path_temp_embeddings=None, path_of_trained_models=None, auto_train=True, proj_dim=128, rnn_n_layers=2, drop_prob=0.1, num_heads=4, num_seeds=1, m=32, ln=False, dicee_model='DeCaL', dicee_epochs=5, dicee_lr=0.01, dicee_emb_dim=128, learning_rate=0.0001, tmax=20, eta_min=1e-05, clip_value=5.0, batch_size=256, num_workers=4, max_length=48, load_pretrained=True, sorted_examples=False, verbose: int = 0) Bases: :py:obj:`ontolearn.base_nces.BaseNCES` Neural Class Expression Synthesis. .. py:attribute:: name :value: 'NCES' .. py:attribute:: learner_names :value: ['SetTransformer', 'LSTM', 'GRU'] .. py:attribute:: path_of_embeddings :value: None .. py:attribute:: path_temp_embeddings :value: None .. py:attribute:: path_of_trained_models :value: None .. py:attribute:: dicee_model :value: 'DeCaL' .. py:attribute:: dicee_emb_dim :value: 128 .. py:attribute:: dicee_epochs :value: 5 .. py:attribute:: dicee_lr :value: 0.01 .. py:attribute:: rnn_n_layers :value: 2 .. py:attribute:: sorted_examples :value: False .. py:attribute:: has_renamed_inds :value: False .. py:method:: get_synthesizer(path=None) .. py:method:: refresh(path=None) .. py:method:: get_prediction(x_pos, x_neg) .. py:method:: fit_one(pos: Union[List[owlapy.owl_individual.OWLNamedIndividual], List[str]], neg: Union[List[owlapy.owl_individual.OWLNamedIndividual], List[str]]) .. py:method:: fit(learning_problem: ontolearn.learning_problem.PosNegLPStandard, **kwargs) .. py:method:: best_hypotheses(n=1, return_node: bool = False) -> Union[owlapy.class_expression.OWLClassExpression, Iterable[owlapy.class_expression.OWLClassExpression], ontolearn.abstracts.AbstractNode, Iterable[ontolearn.abstracts.AbstractNode], None] .. 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]] = None, epochs=50, batch_size=64, max_num_lps=1000, refinement_expressivity=0.2, refs_sample_size=50, learning_rate=0.0001, tmax=20, eta_min=1e-05, clip_value=5.0, num_workers=8, save_model=True, storage_path=None, optimizer='Adam', record_runtime=True, example_sizes=None, shuffle_examples=False) .. py:class:: NCES2(knowledge_base_path, nces2_or_roces=True, quality_func: Optional[ontolearn.abstracts.AbstractScorer] = None, num_predictions=5, path_of_trained_models=None, auto_train=True, proj_dim=128, drop_prob=0.1, num_heads=4, num_seeds=1, m=[32, 64, 128], ln=False, embedding_dim=128, sampling_strategy='nces2', input_dropout=0.0, feature_map_dropout=0.1, kernel_size=4, num_of_output_channels=32, learning_rate=0.0001, tmax=20, eta_min=1e-05, clip_value=5.0, batch_size=256, num_workers=4, max_length=48, load_pretrained=True, verbose: int = 0, data=[]) Bases: :py:obj:`ontolearn.base_nces.BaseNCES` Neural Class Expression Synthesis in ALCHIQ(D). .. py:attribute:: name :value: 'NCES2' .. py:attribute:: triples_data .. py:attribute:: num_entities .. py:attribute:: num_relations .. py:attribute:: path_of_trained_models :value: None .. py:attribute:: embedding_dim :value: 128 .. py:attribute:: sampling_strategy :value: 'nces2' .. py:attribute:: input_dropout :value: 0.0 .. py:attribute:: feature_map_dropout :value: 0.1 .. py:attribute:: kernel_size :value: 4 .. py:attribute:: num_of_output_channels :value: 32 .. py:method:: get_synthesizer(path=None, verbose=True) .. py:method:: refresh(path=None) .. py:method:: get_prediction(dataloaders) .. py:method:: fit_one(pos: Union[List[owlapy.owl_individual.OWLNamedIndividual], List[str]], neg: Union[List[owlapy.owl_individual.OWLNamedIndividual], List[str]]) .. py:method:: fit(learning_problem: ontolearn.learning_problem.PosNegLPStandard, **kwargs) .. py:method:: best_hypotheses(n=1, return_node: bool = False) -> Union[owlapy.class_expression.OWLClassExpression, Iterable[owlapy.class_expression.OWLClassExpression], ontolearn.abstracts.AbstractNode, Iterable[ontolearn.abstracts.AbstractNode], None] .. py:method:: convert_to_list_str_from_iterable(data) .. py:method:: fit_from_iterable(data: 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 - data 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]] = None, epochs=50, batch_size=64, max_num_lps=1000, refinement_expressivity=0.2, refs_sample_size=50, learning_rate=0.0001, tmax=20, eta_min=1e-05, clip_value=5.0, num_workers=8, save_model=True, storage_path=None, optimizer='Adam', record_runtime=True, shuffle_examples=False) .. py:class:: ROCES(knowledge_base_path, nces2_or_roces=True, quality_func: Optional[ontolearn.abstracts.AbstractScorer] = None, num_predictions=5, k=5, path_of_trained_models=None, auto_train=True, proj_dim=128, rnn_n_layers=2, drop_prob=0.1, num_heads=4, num_seeds=1, m=[32, 64, 128], ln=False, embedding_dim=128, sampling_strategy='p', input_dropout=0.0, feature_map_dropout=0.1, kernel_size=4, num_of_output_channels=32, learning_rate=0.0001, tmax=20, eta_min=1e-05, clip_value=5.0, batch_size=256, num_workers=4, max_length=48, load_pretrained=True, verbose: int = 0, data=[]) Bases: :py:obj:`NCES2` Robust Class Expression Synthesis in Description Logics via Iterative Sampling. .. py:attribute:: name :value: 'ROCES' .. py:attribute:: k :value: 5