torch_concepts.Variable

class Variable(concepts: List[str], parents: List[Variable | str], distribution: Type[Distribution] | List[Type[Distribution]] | None = None, size: int | List[int] = 1, metadata: Dict[str, Any] | None = None)[source]

Represents a random variable in a concept-based Probabilistic Model.

A Variable encapsulates one or more concepts along with their associated probability distribution, parent variables, and metadata. It supports multiple distribution types including Delta (deterministic), Bernoulli, Categorical, and Normal distributions.

The Variable class implements a special __new__ method that allows creating multiple Variable instances when initialized with multiple concepts, or a single instance for a single concept.

concepts

List of concept names represented by this variable.

Type:

List[str]

parents

List of parent variables in the graphical model.

Type:

List[Variable]

distribution

PyTorch distribution class for this variable.

Type:

Type[Distribution]

size

Size/cardinality of the variable (e.g., number of classes for Categorical).

Type:

int

metadata

Additional metadata associated with the variable.

Type:

Dict[str, Any]

Properties:

out_features (int): Number of output features this variable produces. in_features (int): Total input features from all parent variables.

Example

>>> import torch
>>> from torch.distributions import Bernoulli, Categorical, Normal
>>> from torch_concepts import Variable
>>> from torch_concepts.distributions import Delta
>>>
>>> # Create a binary concept variable
>>> var_binary = Variable(
...     concepts='has_wheels',
...     parents=[],
...     distribution=Bernoulli,
...     size=1
... )
>>> print(var_binary.concepts)  # ['has_wheels']
>>> print(var_binary.out_features)  # 1
>>>
>>> # Create a categorical variable with 3 color classes
>>> var_color = Variable(
...     concepts=['color'],
...     parents=[],
...     distribution=Categorical,
...     size=3  # red, green, blue
... )
>>> print(var_color.out_features)  # 3
>>>
>>> # Create a deterministic (Delta) variable
>>> var_delta = Variable(
...     concepts=['continuous_feature'],
...     parents=[],
...     distribution=Delta,
...     size=1
... )
>>>
>>> # Create multiple variables at once
>>> vars_list = Variable(
...     concepts=['A', 'B', 'C'],
...     parents=[],
...     distribution=Delta,
...     size=1
... )
>>> print(len(vars_list))  # 3
>>> print(vars_list[0].concepts)  # ['A']
>>> print(vars_list[1].concepts)  # ['B']
>>>
>>> # Create variables with parent dependencies
>>> parent_var = Variable(
...     concepts=['parent_concept'],
...     parents=[],
...     distribution=Bernoulli,
...     size=1
... )
>>> child_var = Variable(
...     concepts=['child_concept'],
...     parents=[parent_var],
...     distribution=Bernoulli,
...     size=1
... )
>>> print(child_var.in_features)  # 1 (from parent)
>>> print(child_var.out_features)  # 1
__init__(concepts: str | List[str], parents: List[Variable | str], distribution: Type[Distribution] | List[Type[Distribution]] | None = None, size: int | List[int] = 1, metadata: Dict[str, Any] | None = None)[source]

Initialize a Variable instance.

Parameters:
  • concepts – Single concept name or list of concept names.

  • parents – List of parent Variable instances.

  • distribution – Distribution type (Delta, Bernoulli, Categorical, or Normal).

  • size – Size parameter for the distribution.

  • metadata – Optional metadata dictionary.

Raises:
  • ValueError – If Categorical variable doesn’t have size > 1.

  • ValueError – If Bernoulli variable doesn’t have size=1.

Methods

__init__(concepts, parents[, distribution, ...])

Initialize a Variable instance.

Attributes

in_features

Calculate total input features from all parent variables.

out_features

Calculate the number of output features for this variable.