neuralogic.core.builder packageο
Submodulesο
neuralogic.core.builder.builder moduleο
- class Builder(settings: SettingsProxy)[source]ο
Bases:
objectBuilder is responsible for grounding, neuralizing, and building models from models and sources.
- build_model(parsed_model: Any, settings: SettingsProxy) Any[source]ο
Builds a neural model from the parsed model.
- Parameters:
parsed_model (Any) β The parsed model.
settings (SettingsProxy) β The settings proxy.
- Returns:
The built neural model.
- Return type:
Any
- build_model_from_file(settings: SettingsProxy, filename: str) Any[source]ο
Builds a model from a file.
- Parameters:
settings (SettingsProxy) β The settings proxy.
filename (str) β The path to the model file.
- Returns:
The built model.
- Return type:
Any
- static get_builders(settings: SettingsProxy) Any[source]ο
- ground_from_logic_samples(parsed_model: Any, logic_samples: list[Any], progress: bool) Any[source]ο
Grounds the model from the provided logic samples.
- Parameters:
parsed_model (Any) β The parsed model.
logic_samples (list[Any]) β The logic samples.
progress (bool) β Whether to show progress.
- Returns:
The grounded logic samples.
- Return type:
Any
- ground_from_sources(parsed_model: Any, sources: Sources, progress: bool) Any[source]ο
Grounds the model from the provided sources.
- Parameters:
parsed_model (Any) β The parsed model.
sources (Sources) β The logic sources.
progress (bool) β Whether to show progress.
- Returns:
The grounded logic samples.
- Return type:
Any
- neuralize(groundings, progress: bool, length: int | None) list[NeuralSample][source]ο
Neuralizes the grounding samples.
- Parameters:
groundings (Any) β The logic groundings to neuralize.
progress (bool) β Whether to show progress.
length (int, optional) β The total number of groundings. Default: None.
- Returns:
The neuralized samples.
- Return type:
list[NeuralSample]
neuralogic.core.builder.components moduleο
- class Atom(atom: Any, substitutions: dict[str, Any])[source]ο
Bases:
objectRepresents an atom in the logic program, often corresponding to a node in the neural network.
- property arity: intο
- node_type() NeuronType[source]ο
Returns the type of the neuron.
- Returns:
The type of the neuron.
- Return type:
- property predicate: strο
- substitutionsο
- class Grounding(grounding: Any)[source]ο
Bases:
objectRepresents a grounded model, providing access to grounded atoms and facts.
- property atoms: dict[str, dict[tuple[str, ...], Any]]ο
- draw(filename: str | None = None, show: bool = True, img_type: str = 'png', value_detail: int = 0, graphviz_path: str | None = None, settings: DrawingSettings | None = None, *args: Any, **kwargs: Any) Any[source]ο
Draws the grounding.
- Parameters:
filename (str, optional) β The filename to save the drawing to. Default: None.
show (bool) β Whether to show the drawing. Default: True.
img_type (str) β The image type. Default: βpngβ.
value_detail (int) β The level of detail for values. Default: 0.
graphviz_path (str, optional) β The path to the Graphviz executable. Default: None.
settings (DrawingSettings, optional) β Styling settings. When provided, they control value detail and graph styling.
- Returns:
The drawing data or image object.
- Return type:
Any
- class NeuralSample(sample: Any)[source]ο
Bases:
objectRepresents a single training or testing sample, containing the query and its associated neural network (evidence).
- draw(filename: str | None = None, show: bool = True, img_type: str = 'png', value_detail: int = 0, graphviz_path: str | None = None, settings: DrawingSettings | None = None, *args: Any, **kwargs: Any) Any[source]ο
Draws the neural sample.
- Parameters:
filename (str, optional) β The filename to save the drawing to. Default: None.
show (bool) β Whether to show the drawing. Default: True.
img_type (str) β The image type. Default: βpngβ.
value_detail (int) β The level of detail for values. Default: 0.
graphviz_path (str, optional) β The path to the Graphviz executable. Default: None.
settings (DrawingSettings, optional) β Styling settings. When provided, they control value detail and graph styling.
- Returns:
The drawing data or image object.
- Return type:
Any
- get_fact(fact: Any) list[Neuron][source]ο
Returns the neuron corresponding to the provided fact.
- Parameters:
fact (Any) β The fact to look for.
- Returns:
The matching fact neuron(s).
- Return type:
list[Neuron]
- get_neurons(literal: Any, neuron_type: NeuronType | None = NeuronType.Atom) list[Neuron][source]ο
Returns a list of neurons matching the provided literal and neuron type.
- Parameters:
literal (Any) β The literal to match.
neuron_type (NeuronType, optional) β The type of neurons to search for. Default: NeuronType.Atom.
- Returns:
The list of matching neurons.
- Return type:
list[Neuron]
- property neurons: dict[str, dict[str, dict[tuple[str, ...], Any]]]ο
- set_fact_value(fact: Any, value: float) int[source]ο
Sets the value of a specific fact in the sample.
- Parameters:
fact (Any) β The fact to set the value for.
value (float) β The value to set.
- Returns:
The index of the fact neuron, or -1 if not found.
- Return type:
int
- property target: float | list | ndarrayο
- class Neuron(neuron: Any, substitutions: dict[str, Any])[source]ο
Bases:
AtomRepresents a neuron in the neural network, extending the Atom class with value and gradient properties.
- property gradient: float | list | ndarrayο
- substitutionsο
- property value: float | list | ndarrayο
- class NeuronType(value)[source]ο
Bases:
str,EnumEnum representing different types of neurons in the neural network.
- Aggregation = 'AggregationNeuron'ο
- Atom = 'AtomNeuron'ο
- Fact = 'FactNeuron'ο
- Negation = 'NegationNeuron'ο
- Rule = 'RuleNeuron'ο
- SplittableAggregation = 'SplittableAggregationNeuron'ο
- WeightedAtom = 'WeightedAtomNeuron'ο
- WeightedRule = 'WeightedRuleNeuron'ο
neuralogic.core.builder.dataset moduleο
- 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.
- load_state_dict(state_dict: dict, strict: bool = True) IncompatibleKeys[source]ο
Sets this datasetβs example-fact parameters from a
state_dict()shaped dictionary.With
strict=Falsethis is also how a parameter reaches a dataset it was not trained on - the embeddings learned for the training set, carried onto the test set for the constants the two share:test_data.load_state_dict(train_data.state_dict(), strict=False)
A constant the training set never saw keeps whatever its example wrote, untrained, and comes back in
missing_keys; a trained parameter with no fact to attach to here comes back inunexpected_keys. Both are ordinary in that use and neither is in the round trip of one dataset, which is whystrictdefaults to refusing them.- Parameters:
state_dict (dict) β
{"weights": {literal: value}}.strict (bool) β Whether the literals must match this datasetβs exactly, as in torch. Default: True.
- Returns:
missing_keys- this datasetβs learnable facts the dictionary said nothing about - andunexpected_keys- literals in the dictionary this dataset has no learnable fact for.- Return type:
- Raises:
ValueError β Under
strict, if either list is non-empty: the saved parameters and the data have drifted apart, and carrying on would leave a model half restored. Always, if two literals that share one weight are given different values - facts share a weight when the example named it, and then only one of the two values could survive, which one being an accident of iteration order.
- state_dict() dict[source]ο
The learnable values this datasetβs own example facts carry, keyed by ground literal.
A value written on an example fact and made learnable with
learnable_facts=Trueis a real parameter - it trains - but it belongs to the data, not to the template, so it is not in the modelβsstate_dict()and saving the model does not save it. This is where it is.The key is the ground literal (
emb(a)) because nothing else survives a rebuild: the weightβs index continues a counter that keeps running, so building the same dataset twice on one model gives the parameters indices1, 2and then5, 6, and the generated weight name iswplus that index.- Returns:
{"weights": {literal: value}}, empty when the dataset was not built withlearnable_facts=True.- Return type:
dict
- 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 IncompatibleKeys(missing_keys: list[str], unexpected_keys: list[str])[source]ο
Bases:
NamedTupleWhat
BuiltDataset.load_state_dict()could not match up, in torchβs shape.- missing_keys: list[str]ο
Alias for field number 0
- unexpected_keys: list[str]ο
Alias for field number 1
neuralogic.core.builder.dataset_builder moduleο
- class DatasetBuilder(parsed_model: Any, java_factory: JavaFactory)[source]ο
Bases:
objectDatasetBuilder is responsible for grounding and neuralizing datasets.
- build_dataset(dataset: BaseDataset | GroundedDataset, settings: SettingsProxy, *, batch_size: int = 1, learnable_facts: bool = False, progress: bool = False) BuiltDataset[source]ο
Builds the dataset (does grounding and neuralization).
- Parameters:
dataset (datasets.BaseDataset | GroundedDataset) β The dataset to build.
settings (SettingsProxy) β The settings proxy.
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:
- build_examples(examples: Iterable[Any], examples_builder: Any, learnable_facts: bool = False) tuple[list[Any], bool][source]ο
Builds examples from the provided examples and examples builder.
- Parameters:
examples (Iterable) β The examples to build.
examples_builder (Any) β The examples builder.
learnable_facts (bool) β Whether facts are learnable. Default: False.
- Returns:
A tuple containing the list of built logic samples and a boolean indicating if there are examples with queries.
- Return type:
tuple[list[Any], bool]
- build_queries(queries: Iterable[Any], query_builder: Any) tuple[list[Any], bool][source]ο
Builds queries from the provided queries and query builder.
- Parameters:
queries (Iterable) β The queries to build.
query_builder (Any) β The query builder.
- Returns:
A tuple containing the list of built logic samples and a boolean indicating if there is one query per example.
- Return type:
tuple[list[Any], bool]
- ground_dataset(dataset: BaseDataset, settings: SettingsProxy, *, batch_size: int = 1, learnable_facts: bool = False, progress: bool = False, raw_groundings: bool = False) GroundedDataset | Any[source]ο
Grounds the dataset.
- Parameters:
dataset (datasets.BaseDataset) β The dataset to ground.
settings (SettingsProxy) β The settings proxy.
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.
raw_groundings (bool) β Whether to return raw groundings. Default: False.
- Returns:
The grounded dataset or raw groundings.
- Return type:
GroundedDataset | Any
- static merge_queries_with_examples(queries: list[Any], examples: list[Any], one_query_per_example: bool, example_queries: bool = True) list[Any][source]ο
Merges queries with their corresponding examples.
- Parameters:
queries (list[Any]) β The list of queries.
examples (list[Any]) β The list of examples.
one_query_per_example (bool) β Whether there is one query per example.
example_queries (bool) β Whether examples contain queries. Default: True.
- Returns:
The list of merged logic samples.
- Return type:
list[Any]
- samples_to_examples_and_queries(samples: list[Any]) tuple[Iterable[Any], Iterable[Any]][source]ο
Converts a list of samples to two lists: examples and queries.
- Parameters:
samples (list[Any]) β The list of samples.
- Returns:
A tuple containing the iterable of examples and the iterable of queries.
- Return type:
tuple[Iterable[Any], Iterable[Any]]
Module contentsο
- class Atom(atom: Any, substitutions: dict[str, Any])[source]ο
Bases:
objectRepresents an atom in the logic program, often corresponding to a node in the neural network.
- property arity: intο
- node_type() NeuronType[source]ο
Returns the type of the neuron.
- Returns:
The type of the neuron.
- Return type:
- property predicate: strο
- substitutionsο
- class Builder(settings: SettingsProxy)[source]ο
Bases:
objectBuilder is responsible for grounding, neuralizing, and building models from models and sources.
- build_model(parsed_model: Any, settings: SettingsProxy) Any[source]ο
Builds a neural model from the parsed model.
- Parameters:
parsed_model (Any) β The parsed model.
settings (SettingsProxy) β The settings proxy.
- Returns:
The built neural model.
- Return type:
Any
- build_model_from_file(settings: SettingsProxy, filename: str) Any[source]ο
Builds a model from a file.
- Parameters:
settings (SettingsProxy) β The settings proxy.
filename (str) β The path to the model file.
- Returns:
The built model.
- Return type:
Any
- static get_builders(settings: SettingsProxy) Any[source]ο
- ground_from_logic_samples(parsed_model: Any, logic_samples: list[Any], progress: bool) Any[source]ο
Grounds the model from the provided logic samples.
- Parameters:
parsed_model (Any) β The parsed model.
logic_samples (list[Any]) β The logic samples.
progress (bool) β Whether to show progress.
- Returns:
The grounded logic samples.
- Return type:
Any
- ground_from_sources(parsed_model: Any, sources: Sources, progress: bool) Any[source]ο
Grounds the model from the provided sources.
- Parameters:
parsed_model (Any) β The parsed model.
sources (Sources) β The logic sources.
progress (bool) β Whether to show progress.
- Returns:
The grounded logic samples.
- Return type:
Any
- neuralize(groundings, progress: bool, length: int | None) list[NeuralSample][source]ο
Neuralizes the grounding samples.
- Parameters:
groundings (Any) β The logic groundings to neuralize.
progress (bool) β Whether to show progress.
length (int, optional) β The total number of groundings. Default: None.
- Returns:
The neuralized samples.
- Return type:
list[NeuralSample]
- 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.
- load_state_dict(state_dict: dict, strict: bool = True) IncompatibleKeys[source]ο
Sets this datasetβs example-fact parameters from a
state_dict()shaped dictionary.With
strict=Falsethis is also how a parameter reaches a dataset it was not trained on - the embeddings learned for the training set, carried onto the test set for the constants the two share:test_data.load_state_dict(train_data.state_dict(), strict=False)
A constant the training set never saw keeps whatever its example wrote, untrained, and comes back in
missing_keys; a trained parameter with no fact to attach to here comes back inunexpected_keys. Both are ordinary in that use and neither is in the round trip of one dataset, which is whystrictdefaults to refusing them.- Parameters:
state_dict (dict) β
{"weights": {literal: value}}.strict (bool) β Whether the literals must match this datasetβs exactly, as in torch. Default: True.
- Returns:
missing_keys- this datasetβs learnable facts the dictionary said nothing about - andunexpected_keys- literals in the dictionary this dataset has no learnable fact for.- Return type:
- Raises:
ValueError β Under
strict, if either list is non-empty: the saved parameters and the data have drifted apart, and carrying on would leave a model half restored. Always, if two literals that share one weight are given different values - facts share a weight when the example named it, and then only one of the two values could survive, which one being an accident of iteration order.
- state_dict() dict[source]ο
The learnable values this datasetβs own example facts carry, keyed by ground literal.
A value written on an example fact and made learnable with
learnable_facts=Trueis a real parameter - it trains - but it belongs to the data, not to the template, so it is not in the modelβsstate_dict()and saving the model does not save it. This is where it is.The key is the ground literal (
emb(a)) because nothing else survives a rebuild: the weightβs index continues a counter that keeps running, so building the same dataset twice on one model gives the parameters indices1, 2and then5, 6, and the generated weight name iswplus that index.- Returns:
{"weights": {literal: value}}, empty when the dataset was not built withlearnable_facts=True.- Return type:
dict
- class DatasetBuilder(parsed_model: Any, java_factory: JavaFactory)[source]ο
Bases:
objectDatasetBuilder is responsible for grounding and neuralizing datasets.
- build_dataset(dataset: BaseDataset | GroundedDataset, settings: SettingsProxy, *, batch_size: int = 1, learnable_facts: bool = False, progress: bool = False) BuiltDataset[source]ο
Builds the dataset (does grounding and neuralization).
- Parameters:
dataset (datasets.BaseDataset | GroundedDataset) β The dataset to build.
settings (SettingsProxy) β The settings proxy.
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:
- build_examples(examples: Iterable[Any], examples_builder: Any, learnable_facts: bool = False) tuple[list[Any], bool][source]ο
Builds examples from the provided examples and examples builder.
- Parameters:
examples (Iterable) β The examples to build.
examples_builder (Any) β The examples builder.
learnable_facts (bool) β Whether facts are learnable. Default: False.
- Returns:
A tuple containing the list of built logic samples and a boolean indicating if there are examples with queries.
- Return type:
tuple[list[Any], bool]
- build_queries(queries: Iterable[Any], query_builder: Any) tuple[list[Any], bool][source]ο
Builds queries from the provided queries and query builder.
- Parameters:
queries (Iterable) β The queries to build.
query_builder (Any) β The query builder.
- Returns:
A tuple containing the list of built logic samples and a boolean indicating if there is one query per example.
- Return type:
tuple[list[Any], bool]
- ground_dataset(dataset: BaseDataset, settings: SettingsProxy, *, batch_size: int = 1, learnable_facts: bool = False, progress: bool = False, raw_groundings: bool = False) GroundedDataset | Any[source]ο
Grounds the dataset.
- Parameters:
dataset (datasets.BaseDataset) β The dataset to ground.
settings (SettingsProxy) β The settings proxy.
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.
raw_groundings (bool) β Whether to return raw groundings. Default: False.
- Returns:
The grounded dataset or raw groundings.
- Return type:
GroundedDataset | Any
- static merge_queries_with_examples(queries: list[Any], examples: list[Any], one_query_per_example: bool, example_queries: bool = True) list[Any][source]ο
Merges queries with their corresponding examples.
- Parameters:
queries (list[Any]) β The list of queries.
examples (list[Any]) β The list of examples.
one_query_per_example (bool) β Whether there is one query per example.
example_queries (bool) β Whether examples contain queries. Default: True.
- Returns:
The list of merged logic samples.
- Return type:
list[Any]
- 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 Grounding(grounding: Any)[source]ο
Bases:
objectRepresents a grounded model, providing access to grounded atoms and facts.
- property atoms: dict[str, dict[tuple[str, ...], Any]]ο
- draw(filename: str | None = None, show: bool = True, img_type: str = 'png', value_detail: int = 0, graphviz_path: str | None = None, settings: DrawingSettings | None = None, *args: Any, **kwargs: Any) Any[source]ο
Draws the grounding.
- Parameters:
filename (str, optional) β The filename to save the drawing to. Default: None.
show (bool) β Whether to show the drawing. Default: True.
img_type (str) β The image type. Default: βpngβ.
value_detail (int) β The level of detail for values. Default: 0.
graphviz_path (str, optional) β The path to the Graphviz executable. Default: None.
settings (DrawingSettings, optional) β Styling settings. When provided, they control value detail and graph styling.
- Returns:
The drawing data or image object.
- Return type:
Any
- class NeuralSample(sample: Any)[source]ο
Bases:
objectRepresents a single training or testing sample, containing the query and its associated neural network (evidence).
- draw(filename: str | None = None, show: bool = True, img_type: str = 'png', value_detail: int = 0, graphviz_path: str | None = None, settings: DrawingSettings | None = None, *args: Any, **kwargs: Any) Any[source]ο
Draws the neural sample.
- Parameters:
filename (str, optional) β The filename to save the drawing to. Default: None.
show (bool) β Whether to show the drawing. Default: True.
img_type (str) β The image type. Default: βpngβ.
value_detail (int) β The level of detail for values. Default: 0.
graphviz_path (str, optional) β The path to the Graphviz executable. Default: None.
settings (DrawingSettings, optional) β Styling settings. When provided, they control value detail and graph styling.
- Returns:
The drawing data or image object.
- Return type:
Any
- get_fact(fact: Any) list[Neuron][source]ο
Returns the neuron corresponding to the provided fact.
- Parameters:
fact (Any) β The fact to look for.
- Returns:
The matching fact neuron(s).
- Return type:
list[Neuron]
- get_neurons(literal: Any, neuron_type: NeuronType | None = NeuronType.Atom) list[Neuron][source]ο
Returns a list of neurons matching the provided literal and neuron type.
- Parameters:
literal (Any) β The literal to match.
neuron_type (NeuronType, optional) β The type of neurons to search for. Default: NeuronType.Atom.
- Returns:
The list of matching neurons.
- Return type:
list[Neuron]
- property neurons: dict[str, dict[str, dict[tuple[str, ...], Any]]]ο
- set_fact_value(fact: Any, value: float) int[source]ο
Sets the value of a specific fact in the sample.
- Parameters:
fact (Any) β The fact to set the value for.
value (float) β The value to set.
- Returns:
The index of the fact neuron, or -1 if not found.
- Return type:
int
- property target: float | list | ndarrayο
- class Neuron(neuron: Any, substitutions: dict[str, Any])[source]ο
Bases:
AtomRepresents a neuron in the neural network, extending the Atom class with value and gradient properties.
- property gradient: float | list | ndarrayο
- substitutionsο
- property value: float | list | ndarrayο
- class NeuronType(value)[source]ο
Bases:
str,EnumEnum representing different types of neurons in the neural network.
- Aggregation = 'AggregationNeuron'ο
- Atom = 'AtomNeuron'ο
- Fact = 'FactNeuron'ο
- Negation = 'NegationNeuron'ο
- Rule = 'RuleNeuron'ο
- SplittableAggregation = 'SplittableAggregationNeuron'ο
- WeightedAtom = 'WeightedAtomNeuron'ο
- WeightedRule = 'WeightedRuleNeuron'ο
- class StaticGraphDataset(static_sample: NeuralSample, fact_mappings: list[list[tuple[BaseRelation, Any]]], batch_size: int = 1, targets: list[Any] | None = None)[source]ο
Bases:
objectA dataset that grounds and neuralizes only the first sample, then reuses the same neural graph for all subsequent samples by updating fact values via
NeuralSample.set_fact_value.This is useful when all samples share identical structure (same predicates, same term groundings) and differ only in fact values.
- Parameters:
static_sample (NeuralSample) β The single neuralized sample built from the first example.
fact_mappings (list[list[tuple[BaseRelation, float]]]) β For each original sample, a list of
(fact, value)pairs to set before evaluating that sample.batch_size (int) β The batch size.
- apply_mapping(index: int) int[source]ο
Apply the fact values for the sample at the given index.
- Parameters:
index (int) β The sample index.
- Returns:
Number of facts updated.
- Return type:
int
- property fact_mappings: list[list[tuple[BaseRelation, Any]]]ο
Return the per-sample factβvalue mappings.
- static_sampleο
- property targets: list[Any]ο
Each sampleβs own target, in the same order as
fact_mappings.The static sample carries the first sampleβs target, so without these every sample would be fitted to that one label.
- build_static_graph_dataset(dataset_builder: DatasetBuilder, dataset: Any, settings: SettingsProxy, *, batch_size: int = 1, learnable_facts: bool = False, progress: bool = False) StaticGraphDataset[source]ο
Build a
StaticGraphDatasetfrom a logicDataset.Only the first sample is grounded and neuralized. For every sample (including the first), a factβvalue mapping is stored so that the caller can update the static sample before each learning/evaluation step.
- Parameters:
dataset_builder (DatasetBuilder)
dataset (Dataset) β The dataset with one or more samples.
settings (SettingsProxy)
batch_size (int) β Batch size (default 1).
learnable_facts (bool) β Whether facts are learnable (default False).
progress (bool) β Whether to show a progress bar (default False).
- Return type: