neuralogic.core packageο
Subpackagesο
- neuralogic.core.builder package
- Submodules
- neuralogic.core.builder.builder module
- neuralogic.core.builder.components module
- neuralogic.core.builder.dataset module
- neuralogic.core.builder.dataset_builder module
- Module contents
- neuralogic.core.constructs package
- Subpackages
- neuralogic.core.constructs.function package
- Submodules
- neuralogic.core.constructs.function.concat module
- neuralogic.core.constructs.function.enum module
- neuralogic.core.constructs.function.function module
- neuralogic.core.constructs.function.function_container module
- neuralogic.core.constructs.function.function_graph module
- neuralogic.core.constructs.function.reshape module
- neuralogic.core.constructs.function.slice module
- neuralogic.core.constructs.function.softmax module
- Module contents
- neuralogic.core.constructs.function package
- Submodules
- neuralogic.core.constructs.factories module
AtomFactoryConstantFactoryHiddenPredicateFactorySpecialPredicateFactorySpecialPredicateFactory.add()SpecialPredicateFactory.add_eval()SpecialPredicateFactory.alldiff()SpecialPredicateFactory.anypred()SpecialPredicateFactory.div_eval()SpecialPredicateFactory.eq()SpecialPredicateFactory.geq()SpecialPredicateFactory.get()SpecialPredicateFactory.gt()SpecialPredicateFactory.hiddenSpecialPredicateFactory.leq()SpecialPredicateFactory.lt()SpecialPredicateFactory.max_eval()SpecialPredicateFactory.maxcard()SpecialPredicateFactory.min_eval()SpecialPredicateFactory.mod()SpecialPredicateFactory.mod_eval()SpecialPredicateFactory.mul_eval()SpecialPredicateFactory.neq()SpecialPredicateFactory.next()SpecialPredicateFactory.sub()SpecialPredicateFactory.sub_eval()SpecialPredicateFactory.truepred()
VariableFactory
- neuralogic.core.constructs.java_objects module
JavaFactoryJavaFactory.add_metadata_function()JavaFactory.add_parametrized_function()JavaFactory.atom_to_clause()JavaFactory.get_conjunction()JavaFactory.get_generic_relation()JavaFactory.get_lifted_example()JavaFactory.get_metadata()JavaFactory.get_new_weight_factory()JavaFactory.get_predicate()JavaFactory.get_predicate_metadata_pair()JavaFactory.get_query()JavaFactory.get_relation()JavaFactory.get_rule()JavaFactory.get_term()JavaFactory.get_valued_fact()JavaFactory.get_variable_factory()JavaFactory.get_weight()JavaFactory.to_clause()
ValueFactory
- neuralogic.core.constructs.metadata module
- neuralogic.core.constructs.predicate module
- neuralogic.core.constructs.relation module
- neuralogic.core.constructs.rule module
- neuralogic.core.constructs.term module
- Module contents
- Subpackages
- neuralogic.core.settings package
- Submodules
- neuralogic.core.settings.settings_proxy module
SettingsProxySettingsProxy.chain_pruningSettingsProxy.debug_exportingSettingsProxy.default_fact_valueSettingsProxy.error_functionSettingsProxy.get_aggregation_function()SettingsProxy.get_combination_function()SettingsProxy.get_transformation_function()SettingsProxy.grounderSettingsProxy.initializerSettingsProxy.initializer_constSettingsProxy.initializer_uniform_scaleSettingsProxy.iso_value_compressionSettingsProxy.optimizerSettingsProxy.prune_only_identitiesSettingsProxy.relation_combinationSettingsProxy.relation_transformationSettingsProxy.rule_aggregationSettingsProxy.rule_combinationSettingsProxy.rule_transformationSettingsProxy.to_json()
- Module contents
- neuralogic.core.torch package
Submodulesο
neuralogic.core.enums moduleο
neuralogic.core.model moduleο
- class Model(*, model_file: str | None = None)[source]ο
Bases:
NeuralModuleModel is a collection of rules and relations that define the structure of the neural model.
- add_module(module: Module) None[source]ο
Expands the module into rules and adds them into the model.
- Parameters:
module (Module) β The module to expand and add.
- add_rule(rule: BaseRelation | WeightedRelation | Rule | PredicateMetadata) None[source]ο
Adds one rule to the model.
- Parameters:
rule (ModelEntries) β The rule to add.
- add_rules(rules: list[BaseRelation | WeightedRelation | Rule | PredicateMetadata]) None[source]ο
Adds multiple rules to the model.
- Parameters:
rules (list[ModelEntries]) β The rules to add.
- build(settings: Settings | None = None, torch: bool = False) Model[source]ο
Builds the model into a neural model.
- derivable_queries(example: list[BaseRelation | Rule] | None = None) list[BaseRelation] | dict[source]ο
Returns all derivable queries for the provided example.
- Parameters:
example (list[BaseRelation | Rule], optional) β The example to derive queries from. Default: None.
- Returns:
The list of derivable queries.
- Return type:
list[BaseRelation] | dict
- load(path: str | Path) None[source]ο
Load model weights from a file saved by
save().The model must already be built with the same architecture that produced the saved file.
- Parameters:
path (str | Path) β Path to the saved file.
- q(query: BaseRelation, examples: list[BaseRelation | Rule] | None = None) list[dict] | dict[source]ο
- query(query: BaseRelation, examples: list[BaseRelation | Rule] | None = None) list[dict] | dict[source]ο
Performs a query on the model with the provided examples.
- Parameters:
query (BaseRelation) β The query to perform.
examples (list[BaseRelation | Rule], optional) β The examples to use for the query. Default: None.
- Returns:
The list of query results (substitutions).
- Return type:
list[dict] | dict
neuralogic.core.neural_module moduleο
- class NeuralModule[source]ο
Bases:
objectNeuralModule is the base class for all neural models. It provides methods for grounding, building, training, and testing.
- build_dataset(dataset: BaseDataset | GroundedDataset, *, batch_size: int = 1, learnable_facts: bool = False, progress: bool = False) BuiltDataset[source]ο
Builds (ground and neuralize) the provided dataset.
- Parameters:
dataset (Union[BaseDataset, GroundedDataset]) β The dataset to build.
batch_size (int) β The batch size. Default: 1.
learnable_facts (bool) β Whether facts are learnable. Default: False.
progress (bool) β Whether to show progress. Default: False.
- Returns:
The built dataset.
- Return type:
- draw(filename: str | None = None, show=True, img_type='png', value_detail: int = 0, graphviz_path: str | None = None, *args, **kwargs)[source]ο
- ground(dataset: BaseDataset, *, batch_size: int = 1, learnable_facts: bool = False, progress: bool = False) GroundedDataset[source]ο
Grounds the provided dataset using the modelβs settings.
- Parameters:
dataset (BaseDataset) β The dataset to ground.
batch_size (int) β The batch size for grounding. Default: 1.
learnable_facts (bool) β Whether facts are learnable. Default: False.
progress (bool) β Whether to show progress. Default: False.
- Returns:
The grounded dataset.
- Return type:
- parameters() dict[source]ο
Returns the model parameters.
- Returns:
The model parameters.
- Return type:
dict
- state_dict() dict[source]ο
Returns the state dictionary of the model.
- Returns:
The state dictionary (weights and weight names).
- Return type:
dict
- test(dataset) list | float[source]ο
Tests the model on the provided dataset.
- Parameters:
dataset (Any) β The dataset to test on.
- Returns:
The test results (outputs).
- Return type:
Union[Value, List[Value]]
- train(dataset, epochs: int = 1) list | float[source]ο
Trains the model on the provided dataset.
- Parameters:
dataset (Any) β The dataset to train on. Can be a Dataset, GroundedDataset, BuiltDataset, or a list of samples.
epochs (int) β The number of epochs to train. Default: 1.
- Returns:
The training results (target, output, error).
- Return type:
Union[Tuple[Value, Value, Value], List[Tuple[Value, Value, Value]]]
neuralogic.core.sources moduleο
- class Sources(sources: Any)[source]ο
Bases:
objectRepresents the logic sources (models, examples, queries) for the NeuraLogic backend.
- static from_args(args: list[str], settings: SettingsProxy) Sources[source]ο
Creates Sources from command line arguments and settings.
- Parameters:
args (List[str]) β The command line arguments.
settings (SettingsProxy) β The settings proxy.
- Returns:
The created Sources object.
- Return type:
- static from_settings(settings: SettingsProxy) Sources[source]ο
Creates Sources from the provided settings.
- Parameters:
settings (SettingsProxy) β The settings proxy.
- Returns:
The created Sources object.
- Return type:
Module contentsο
- class Aggregation[source]ο
Bases:
objectCollection of aggregation functions. Aggregation functions are used to aggregate multiple inputs, typically from different groundings of the same rule.
- AVG: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>ο
- CONCAT: ConcatAggregation = <neuralogic.core.constructs.function.concat.ConcatAggregation object>ο
- COUNT: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>ο
- MAX: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>ο
- MIN: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>ο
- SOFTMAX: SoftmaxAggregation = <neuralogic.core.constructs.function.softmax.SoftmaxAggregation object>ο
- SUM: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>ο
- class BuiltDataset(samples: list[NeuralSample], batch_size: int)[source]ο
Bases:
objectBuiltDataset represents an already built dataset - that is, a dataset that has been grounded and neuralized.
- class Combination[source]ο
Bases:
objectCollection of combination functions. Combination functions are used to combine multiple inputs into a single output, often as part of a rule body or for combining multiple rules.
- AVG: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- CONCAT: ConcatCombination = <neuralogic.core.constructs.function.concat.ConcatCombination object>ο
- COSSIM: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- COUNT: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- CROSSSUM: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- ELPRODUCT: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- MAX: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- MIN: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- PRODUCT: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- SOFTMAX: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- SPARSEMAX: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- SUM: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- class F[source]ο
Bases:
objectUtility class providing a flat namespace for common functions. It includes transformation, combination, and aggregation functions.
- avg: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- avg_agg: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>ο
- concat: ConcatCombination = <neuralogic.core.constructs.function.concat.ConcatCombination object>ο
- concat_agg: ConcatAggregation = <neuralogic.core.constructs.function.concat.ConcatAggregation object>ο
- cossim: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- count: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- count_agg: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>ο
- crossum: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- elproduct: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- exp: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- identity: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- inverse: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- leaky_relu: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- log: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- lukasiewicz: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- max: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- max_agg: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>ο
- min: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- min_agg: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>ο
- norm: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- product: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- relu: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- reverse: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- sigmoid: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- signum: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- softmax: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- softmax_agg: SoftmaxAggregation = <neuralogic.core.constructs.function.softmax.SoftmaxAggregation object>ο
- softmax_comb: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- sparsemax: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- sparsemax_comb: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- sqrt: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- sum: CombinationFunction = <neuralogic.core.constructs.function.function.CombinationFunction object>ο
- sum_agg: AggregationFunction = <neuralogic.core.constructs.function.function.AggregationFunction object>ο
- tanh: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- transp: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- class GroundedDataset(groundings, builder: Builder)[source]ο
Bases:
objectGroundedDataset represents grounded examples that are not neuralized yet.
- neuralize(*, batch_size: int = 1, progress: bool = False) BuiltDataset[source]ο
- class Metadata(learnable: bool | None = None, transformation: TransformationFunction | CombinationFunction | None = None, combination: CombinationFunction | None = None, aggregation: AggregationFunction | None = None, duplicate_grounding: bool | None = None)[source]ο
Bases:
objectRepresents metadata for a logic construct (e.g., rule, predicate).
Metadata can specify properties like learnability, transformation functions, aggregation functions, and combination functions.
- aggregationο
- combinationο
- combine(other: Metadata) Metadata[source]ο
Combines this metadata with another Metadata object. Values from the other object take precedence.
- copy() Metadata[source]ο
Returns a shallow copy of the metadata.
- Returns:
The copy of the metadata.
- Return type:
- duplicate_groundingο
- static from_iterable(iterable: Iterable[Any]) Metadata[source]ο
Creates a Metadata object from an iterable of functions or values.
- Parameters:
iterable (Iterable) β The iterable containing metadata entries.
- Returns:
The created Metadata object.
- Return type:
- learnableο
- transformationο
- class Model(*, model_file: str | None = None)[source]ο
Bases:
NeuralModuleModel is a collection of rules and relations that define the structure of the neural model.
- add_module(module: Module) None[source]ο
Expands the module into rules and adds them into the model.
- Parameters:
module (Module) β The module to expand and add.
- add_rule(rule: BaseRelation | WeightedRelation | Rule | PredicateMetadata) None[source]ο
Adds one rule to the model.
- Parameters:
rule (ModelEntries) β The rule to add.
- add_rules(rules: list[BaseRelation | WeightedRelation | Rule | PredicateMetadata]) None[source]ο
Adds multiple rules to the model.
- Parameters:
rules (list[ModelEntries]) β The rules to add.
- build(settings: Settings | None = None, torch: bool = False) Model[source]ο
Builds the model into a neural model.
- derivable_queries(example: list[BaseRelation | Rule] | None = None) list[BaseRelation] | dict[source]ο
Returns all derivable queries for the provided example.
- Parameters:
example (list[BaseRelation | Rule], optional) β The example to derive queries from. Default: None.
- Returns:
The list of derivable queries.
- Return type:
list[BaseRelation] | dict
- load(path: str | Path) None[source]ο
Load model weights from a file saved by
save().The model must already be built with the same architecture that produced the saved file.
- Parameters:
path (str | Path) β Path to the saved file.
- q(query: BaseRelation, examples: list[BaseRelation | Rule] | None = None) list[dict] | dict[source]ο
- query(query: BaseRelation, examples: list[BaseRelation | Rule] | None = None) list[dict] | dict[source]ο
Performs a query on the model with the provided examples.
- Parameters:
query (BaseRelation) β The query to perform.
examples (list[BaseRelation | Rule], optional) β The examples to use for the query. Default: None.
- Returns:
The list of query results (substitutions).
- Return type:
list[dict] | dict
- class Rule(head: BaseRelation, body: RuleBody | Iterable[BodyItem] | BodyItem)[source]ο
Bases:
objectRepresents a rule in the model, consisting of a head and a body.
- body: list[BodyItem] | FContainerο
- headο
- class RuleBody(lit1: BaseRelation, lit2: BaseRelation)[source]ο
Bases:
objectRepresents the body of a rule, which is a collection of literals.
- literals: list['BaseRelation' | FContainer]ο
- class Settings(*, optimizer: Optimizer = <neuralogic.nn.optim.adam.Adam object>, error_function: ErrorFunction = <neuralogic.nn.loss.MSE object>, initializer: Initializer = <neuralogic.nn.init.Uniform object>, iso_value_compression: bool = True, chain_pruning: bool = True, prune_only_identities: bool = False, grounder: Grounder = Grounder.BUP)[source]ο
Bases:
object- property chain_pruning: boolο
- create_disconnected_proxy() SettingsProxy[source]ο
- create_proxy() SettingsProxy[source]ο
- property error_function: ErrorFunctionο
- property initializer: Initializerο
- property iso_value_compression: boolο
- property prune_only_identities: boolο
- class SettingsProxy(*, optimizer: Optimizer, error_function: ErrorFunction, initializer: Initializer, iso_value_compression: bool, chain_pruning: bool, prune_only_identities: bool, grounder: Grounder)[source]ο
Bases:
objectProxy class for the Java Settings object.
It provides a Pythonic interface to configure various parameters of the NeuraLogic backend, such as optimizers, initializers, error functions, and grounding algorithms.
- property chain_pruning: boolο
Whether to use chain pruning (reducing redundant chains of operations).
- property debug_exporting: boolο
- property default_fact_value: floatο
- property error_function: Anyο
The error function used for training.
- get_aggregation_function(aggregation: Aggregation) Any[source]ο
- get_combination_function(combination: Combination) Any[source]ο
Returns the Java combination function for the given Python enum value.
- Parameters:
combination (Combination) β The combination function enum value.
- Returns:
The Java combination function object.
- Return type:
Any
- get_transformation_function(transformation: Transformation) Any[source]ο
- property grounder: Anyο
The grounding algorithm to use.
- property initializer: Anyο
The weight initializer used for model parameters.
- property initializer_const: floatο
- property initializer_uniform_scale: floatο
- property iso_value_compression: boolο
Whether to use iso-value compression.
- property prune_only_identities: boolο
- property relation_combination: CombinationFunctionο
- property relation_transformation: TransformationFunctionο
- property rule_aggregation: AggregationFunctionο
- property rule_combination: CombinationFunctionο
- property rule_transformation: TransformationFunctionο
- class Transformation[source]ο
Bases:
objectCollection of transformation functions. Transformation functions are applied element-wise or as join operations (e.g., Softmax, Transpose).
- EXP: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- IDENTITY: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- INVERSE: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- LEAKY_RELU: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- LOG: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- LUKASIEWICZ: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- NORM: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- RELU: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- REVERSE: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- SIGMOID: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- SIGNUM: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- SOFTMAX: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- SPARSEMAX: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- SQRT: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- TANH: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο
- TRANSP: TransformationFunction = <neuralogic.core.constructs.function.function.TransformationFunction object>ο