Skip to content

initializers

initializers ¤

ConstantTensorInitializer ¤

Bases: Initializer

A symbolic constant initializer, which initializes all the entries of a tensor with the same value, which can be either a scalar or a Numpy array of the same shape.

Source code in cirkit/symbolic/initializers.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class ConstantTensorInitializer(Initializer):
    """A symbolic constant initializer, which initializes all the entries of a tensor with the
    same value, which can be either a scalar or a Numpy array of the same shape."""

    def __init__(self, value: int | float | complex | np.number | np.ndarray) -> None:
        """Initializes a constant tensor initializer.

        Args:
            value: The value used for initialization, which must be either an integer,
                a real number, a complex number or a Numpy array.

        Raises:
            ValueError: If the initiaization value is not of the allowed types.
        """
        if not isinstance(value, (int, float, complex, np.number, np.ndarray)):
            raise ValueError("The value must be either a number or a Numpy array")
        self.value = value

    @property
    def config(self) -> dict[str, Any]:
        return {"value": self.value}

    def allows_shape(self, shape: tuple[int, ...]) -> bool:
        if isinstance(self.value, (int, float, complex, np.number)):
            return True
        assert isinstance(self.value, np.ndarray)
        try:
            return np.broadcast_shapes(self.value.shape, shape) == shape
        except ValueError:
            return False

config property ¤

value = value instance-attribute ¤

__init__(value) ¤

Initializes a constant tensor initializer.

Parameters:

Name Type Description Default
value int | float | complex | number | ndarray

The value used for initialization, which must be either an integer, a real number, a complex number or a Numpy array.

required

Raises:

Type Description
ValueError

If the initiaization value is not of the allowed types.

Source code in cirkit/symbolic/initializers.py
50
51
52
53
54
55
56
57
58
59
60
61
62
def __init__(self, value: int | float | complex | np.number | np.ndarray) -> None:
    """Initializes a constant tensor initializer.

    Args:
        value: The value used for initialization, which must be either an integer,
            a real number, a complex number or a Numpy array.

    Raises:
        ValueError: If the initiaization value is not of the allowed types.
    """
    if not isinstance(value, (int, float, complex, np.number, np.ndarray)):
        raise ValueError("The value must be either a number or a Numpy array")
    self.value = value

allows_shape(shape) ¤

Source code in cirkit/symbolic/initializers.py
68
69
70
71
72
73
74
75
def allows_shape(self, shape: tuple[int, ...]) -> bool:
    if isinstance(self.value, (int, float, complex, np.number)):
        return True
    assert isinstance(self.value, np.ndarray)
    try:
        return np.broadcast_shapes(self.value.shape, shape) == shape
    except ValueError:
        return False

DirichletInitializer ¤

Bases: Initializer

A symbolic Dirichlet initializer, which initializes all the entries of a tensor along one axis by sampling independently from a Dirichlet distribution.

Source code in cirkit/symbolic/initializers.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
class DirichletInitializer(Initializer):
    """A symbolic Dirichlet initializer, which initializes all the entries of a tensor
    along one axis by sampling independently from a Dirichlet distribution."""

    def __init__(self, alpha: float | list[float] = 1.0, *, axis: int = -1) -> None:
        """Initializes a Dirichlet initializer, given the concentration parameters
        and the axis along which the sampled values will sum to one.

        Args:
            alpha: The concentration parameter. If it is a list, then different concentrations
                will be used for each random variable being sampled.
            axis: The axis along which the sampled values will sum to one.

        Raises:
            ValueError: If the concentration parameter is not positive or if it contains
                non-positve values.
        """
        if not isinstance(alpha, (float, list)):
            raise ValueError("The concentration parameters should be either a scalar or a list")
        if (isinstance(alpha, float) and alpha <= 0.0) or (
            isinstance(alpha, list) and any(a <= 0.0 for a in alpha)
        ):
            raise ValueError("The concentration parameters should be positive")
        self.alpha = alpha
        self.axis = axis

    @property
    def config(self) -> dict[str, Any]:
        return {"alpha": self.alpha, "axis": self.axis}

    def allows_shape(self, shape: tuple[int, ...]) -> bool:
        axis = self.axis + len(shape) if self.axis < 0 else self.axis
        if axis >= len(shape):
            return False
        if isinstance(self.alpha, float):
            return True
        assert isinstance(self.alpha, list)
        return shape[axis] == len(self.alpha)

alpha = alpha instance-attribute ¤

axis = axis instance-attribute ¤

config property ¤

__init__(alpha=1.0, *, axis=-1) ¤

Initializes a Dirichlet initializer, given the concentration parameters and the axis along which the sampled values will sum to one.

Parameters:

Name Type Description Default
alpha float | list[float]

The concentration parameter. If it is a list, then different concentrations will be used for each random variable being sampled.

1.0
axis int

The axis along which the sampled values will sum to one.

-1

Raises:

Type Description
ValueError

If the concentration parameter is not positive or if it contains non-positve values.

Source code in cirkit/symbolic/initializers.py
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
def __init__(self, alpha: float | list[float] = 1.0, *, axis: int = -1) -> None:
    """Initializes a Dirichlet initializer, given the concentration parameters
    and the axis along which the sampled values will sum to one.

    Args:
        alpha: The concentration parameter. If it is a list, then different concentrations
            will be used for each random variable being sampled.
        axis: The axis along which the sampled values will sum to one.

    Raises:
        ValueError: If the concentration parameter is not positive or if it contains
            non-positve values.
    """
    if not isinstance(alpha, (float, list)):
        raise ValueError("The concentration parameters should be either a scalar or a list")
    if (isinstance(alpha, float) and alpha <= 0.0) or (
        isinstance(alpha, list) and any(a <= 0.0 for a in alpha)
    ):
        raise ValueError("The concentration parameters should be positive")
    self.alpha = alpha
    self.axis = axis

allows_shape(shape) ¤

Source code in cirkit/symbolic/initializers.py
156
157
158
159
160
161
162
163
def allows_shape(self, shape: tuple[int, ...]) -> bool:
    axis = self.axis + len(shape) if self.axis < 0 else self.axis
    if axis >= len(shape):
        return False
    if isinstance(self.alpha, float):
        return True
    assert isinstance(self.alpha, list)
    return shape[axis] == len(self.alpha)

ElementwiseInitializer ¤

Bases: Initializer, ABC

An elementwise initializer initializes a parameter tensor by setting each entry using the same function, such as by sampling independently from a univariate distribution. Therefore, an elementwise initializer allows any parameter shape by default.

Source code in cirkit/symbolic/initializers.py
36
37
38
39
40
41
42
43
class ElementwiseInitializer(Initializer, ABC):
    """An elementwise initializer initializes a parameter tensor by setting each entry
    using the same function, such as by sampling independently from a univariate distribution.
    Therefore, an elementwise initializer allows any parameter shape by default.
    """

    def allows_shape(self, shape: tuple[int, ...]) -> bool:
        return True

allows_shape(shape) ¤

Source code in cirkit/symbolic/initializers.py
42
43
def allows_shape(self, shape: tuple[int, ...]) -> bool:
    return True

Initializer ¤

Bases: ABC

The abstract symbolic initializer class. Symbolic initializers are usually assigned to TensorParameter upon their instantiation.

Source code in cirkit/symbolic/initializers.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Initializer(ABC):
    """The abstract symbolic initializer class. Symbolic initializers are usually assigned to
    [TensorParameter][cirkit.symbolic.parameters.TensorParameter] upon their instantiation."""

    @property
    def config(self) -> dict[str, Any]:
        """Retrieves the hyperparameters of the initializer.

        Returns:
            A dictionary mapping hyperparameter names to their values.
        """
        return {}

    @abstractmethod
    def allows_shape(self, shape: tuple[int, ...]) -> bool:
        """Checks whether the given parameter shape is supported by the initializer.

        Args:
            shape: The parameter shape.

        Returns:
            True if the shape is supported, False otherwise.
        """

    def __repr__(self) -> str:
        config_repr = ", ".join(f"{k}={v}" for k, v in self.config.items())
        return f"{self.__class__.__name__}({config_repr})"

config property ¤

Retrieves the hyperparameters of the initializer.

Returns:

Type Description
dict[str, Any]

A dictionary mapping hyperparameter names to their values.

__repr__() ¤

Source code in cirkit/symbolic/initializers.py
31
32
33
def __repr__(self) -> str:
    config_repr = ", ".join(f"{k}={v}" for k, v in self.config.items())
    return f"{self.__class__.__name__}({config_repr})"

allows_shape(shape) abstractmethod ¤

Checks whether the given parameter shape is supported by the initializer.

Parameters:

Name Type Description Default
shape tuple[int, ...]

The parameter shape.

required

Returns:

Type Description
bool

True if the shape is supported, False otherwise.

Source code in cirkit/symbolic/initializers.py
20
21
22
23
24
25
26
27
28
29
@abstractmethod
def allows_shape(self, shape: tuple[int, ...]) -> bool:
    """Checks whether the given parameter shape is supported by the initializer.

    Args:
        shape: The parameter shape.

    Returns:
        True if the shape is supported, False otherwise.
    """

NormalInitializer ¤

Bases: ElementwiseInitializer

A symbolic normal initializer, which initializes all the entries of a tensor by sampling independently from a univariate normal distribution.

Source code in cirkit/symbolic/initializers.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
class NormalInitializer(ElementwiseInitializer):
    """A symbolic normal initializer, which initializes all the entries of a tensor
    by sampling independently from a univariate normal distribution."""

    def __init__(self, mean: float = 0.0, stddev: float = 1.0) -> None:
        """Initializes a normal initializer, given mean and standard deviation.

        Args:
            mean: The mean.
            stddev: The standard deviation.

        Raises:
            ValueError: If the standard deviation is not a positive number.
        """
        if stddev <= 0.0:
            raise ValueError("The standard deviation should be a positive number")
        self.mean = mean
        self.stddev = stddev

    @property
    def config(self) -> dict[str, Any]:
        return {"mean": self.mean, "stddev": self.stddev}

config property ¤

mean = mean instance-attribute ¤

stddev = stddev instance-attribute ¤

__init__(mean=0.0, stddev=1.0) ¤

Initializes a normal initializer, given mean and standard deviation.

Parameters:

Name Type Description Default
mean float

The mean.

0.0
stddev float

The standard deviation.

1.0

Raises:

Type Description
ValueError

If the standard deviation is not a positive number.

Source code in cirkit/symbolic/initializers.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def __init__(self, mean: float = 0.0, stddev: float = 1.0) -> None:
    """Initializes a normal initializer, given mean and standard deviation.

    Args:
        mean: The mean.
        stddev: The standard deviation.

    Raises:
        ValueError: If the standard deviation is not a positive number.
    """
    if stddev <= 0.0:
        raise ValueError("The standard deviation should be a positive number")
    self.mean = mean
    self.stddev = stddev

UniformInitializer ¤

Bases: ElementwiseInitializer

A symbolic uniform initializer, which initializes all the entries of a tensor by sampling independently from a univariate uniform distribution.

Source code in cirkit/symbolic/initializers.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class UniformInitializer(ElementwiseInitializer):
    """A symbolic uniform initializer, which initializes all the entries of a tensor
    by sampling independently from a univariate uniform distribution."""

    def __init__(self, a: float = 0.0, b: float = 1.0) -> None:
        """Initializes a uniform initializer, given minimum and maximum.

        Args:
            a: The minimum.
            b: The maximum.

        Raises:
            ValueError: If the minimum is not strictly less than the maximum.
        """
        if a >= b:
            raise ValueError("The minimum should be strictly less than the maximum")
        self.a = a
        self.b = b

    @property
    def config(self) -> dict[str, Any]:
        return {"a": self.a, "b": self.b}

a = a instance-attribute ¤

b = b instance-attribute ¤

config property ¤

__init__(a=0.0, b=1.0) ¤

Initializes a uniform initializer, given minimum and maximum.

Parameters:

Name Type Description Default
a float

The minimum.

0.0
b float

The maximum.

1.0

Raises:

Type Description
ValueError

If the minimum is not strictly less than the maximum.

Source code in cirkit/symbolic/initializers.py
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def __init__(self, a: float = 0.0, b: float = 1.0) -> None:
    """Initializes a uniform initializer, given minimum and maximum.

    Args:
        a: The minimum.
        b: The maximum.

    Raises:
        ValueError: If the minimum is not strictly less than the maximum.
    """
    if a >= b:
        raise ValueError("The minimum should be strictly less than the maximum")
    self.a = a
    self.b = b