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, *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.
- 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, *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.
- 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.
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.
- 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, *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.
- 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, *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.
- 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'ο