Skip to content

afnio.cognitive.parameter

afnio.cognitive.parameter.Parameter

Bases: Variable

A kind of Variable that is to be considered a module parameter.

Parameters are Variable subclasses, that have a very special property when used with Modules - when assigned as an attribute to a Module, they are automatically added to the list of its parameters, and will appear e.g. in parameters() iterator. Assigning a Variable doesn't have such effect. This is because one might want to cache some temporary state, like a piece of context memory for ChatCompletion, in the agent. If there was no such class as Parameter, these temporaries would get registered too.

Attributes:

Name Type Description
data str | int | float | list[str | int | float] | None

The raw data, which can be a scalar string or number, or a sequence of such scalars.

role str

A specific description of the role of the parameter in the agent that provides context to the optimizer.

requires_grad bool

Whether to track operations for automatic differentiation and compute gradients.

Source code in afnio/cognitive/parameter.py
 6
 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Parameter(Variable):
    """
    A kind of `Variable` that is to be considered a module parameter.

    Parameters are [`Variable`][afnio.Variable] subclasses, that have a very special
    property when used with [`Module`][afnio.cognitive.modules.Module]s - when assigned
    as an attribute to a `Module`, they are automatically added to the list of its
    parameters, and will appear e.g. in
    [`parameters()`][afnio.cognitive.modules.Module.parameters] iterator.
    Assigning a `Variable` doesn't have such effect. This is because one might want to
    cache some temporary state, like a piece of context memory for
    [`ChatCompletion`][afnio.cognitive.modules.ChatCompletion], in
    the agent. If there was no such class as [`Parameter`][.], these temporaries would
    get registered too.

    Attributes:
        data: The raw data, which can be a scalar string or number, or a sequence of
            such scalars.
        role: A specific description of the role of the parameter in the agent that
            provides context to the optimizer.
        requires_grad: Whether to track operations for automatic differentiation and
            compute gradients.
    """

    def __init__(
        self,
        data: Optional[Union[str, int, float, List[Union[str, int, float]]]] = None,
        role: str = None,
        requires_grad: bool = True,
    ):
        """
        Initializes a `Parameter` instance.

        Args:
            data: The raw data for the parameter, which can be a scalar string or
                number, or a sequence of such scalars.
            role: A specific description of the role of the parameter in the agent that
                provides context to the [`Optimizer`][afnio.optim.optimizer.Optimizer].
            requires_grad: A boolean indicating whether to compute gradients for this
                parameter during backpropagation. Defaults to `True`.
        """
        super().__init__(data=data, role=role, requires_grad=requires_grad)

    def __deepcopy__(self, memo):
        if id(self) in memo:
            return memo[id(self)]
        else:
            result = type(self)(self.data, self.role, self.requires_grad)
            memo[id(self)] = result
            return result

    def __repr__(self):
        return "Parameter containing:\n  " + super().__repr__()

__init__(data=None, role=None, requires_grad=True)

Initializes a Parameter instance.

Parameters:

Name Type Description Default
data str | int | float | list[str | int | float] | None

The raw data for the parameter, which can be a scalar string or number, or a sequence of such scalars.

None
role str

A specific description of the role of the parameter in the agent that provides context to the Optimizer.

None
requires_grad bool

A boolean indicating whether to compute gradients for this parameter during backpropagation. Defaults to True.

True
Source code in afnio/cognitive/parameter.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def __init__(
    self,
    data: Optional[Union[str, int, float, List[Union[str, int, float]]]] = None,
    role: str = None,
    requires_grad: bool = True,
):
    """
    Initializes a `Parameter` instance.

    Args:
        data: The raw data for the parameter, which can be a scalar string or
            number, or a sequence of such scalars.
        role: A specific description of the role of the parameter in the agent that
            provides context to the [`Optimizer`][afnio.optim.optimizer.Optimizer].
        requires_grad: A boolean indicating whether to compute gradients for this
            parameter during backpropagation. Defaults to `True`.
    """
    super().__init__(data=data, role=role, requires_grad=requires_grad)