Skip to content

afnio.cognitive.modules.add

afnio.cognitive.modules.add.Add

Bases: Module

Performs element-wise addition of two input Variables.

This module utilizes the Add operation from afnio.autodiff.basic_ops. The inputs must be instances of the Variable class. The forward method applies the addition operation to the data field of the inputs and returns the resulting Variable.

Note

This module does not have any trainable parameters.

Examples:

>>> from afnio import cognitive as cog
>>> class Addition(cog.Module):
...     def __init__(self):
...         super().__init__()
...         self.add = cog.Add()
>>>     def forward(self, x, y):
...         return self.add(x, y)
>>> input1 = afnio.Variable(data="abc", role="input1")
>>> input2 = afnio.Variable(data="def", role="input2")
>>> addition = Addition()
>>> result = addition(input1, input2)
>>> print(result.data)
'abcdef'
>>> print(result.role)
'input1 and input2'

Raises:

Type Description
TypeError

If either input is not an instance of Variable.

TypeError

If addition between the input types is not allowed.

ValueError

If a scalar data is added to a listdata.

ValueError

If list data fields have mismatched lengths.

See Also

afnio.autodiff.basic_ops.Add for the underlying operation.

Source code in afnio/cognitive/modules/add.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
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class Add(Module):
    """
    Performs element-wise addition of two input [`Variable`][afnio.Variable]s.

    This module utilizes the [`Add`][afnio.autodiff.basic_ops.Add] operation from
    `afnio.autodiff.basic_ops`. The inputs must be instances of the
    [`Variable`][afnio.Variable] class. The `forward` method applies the addition
    operation to the [`data`][afnio.Variable.data] field of the inputs and returns
    the resulting [`Variable`][afnio.Variable].

    Note:
        This module does not have any trainable parameters.

    Examples:
        >>> from afnio import cognitive as cog
        >>> class Addition(cog.Module):
        ...     def __init__(self):
        ...         super().__init__()
        ...         self.add = cog.Add()
        >>>     def forward(self, x, y):
        ...         return self.add(x, y)
        >>> input1 = afnio.Variable(data="abc", role="input1")
        >>> input2 = afnio.Variable(data="def", role="input2")
        >>> addition = Addition()
        >>> result = addition(input1, input2)
        >>> print(result.data)
        'abcdef'
        >>> print(result.role)
        'input1 and input2'

    Raises:
        TypeError: If either input is not an instance of [`Variable`][afnio.Variable].
        TypeError: If addition between the input types is not allowed.
        ValueError: If a scalar [`data`][afnio.Variable.data] is added
            to a list[`data`][afnio.Variable.data].
        ValueError: If list [`data`][afnio.Variable.data] fields
            have mismatched lengths.

    See Also:
        [`afnio.autodiff.basic_ops.Add`][afnio.autodiff.basic_ops.Add]
        for the underlying operation.
    """

    def __init__(self):
        super().__init__()

    def forward(self, x: Variable, y: Variable) -> Variable:
        """
        Forward pass for element-wise addition.

        Warning:
            Users should not call this method directly. Instead, they should call the
            module instance itself, which will internally invoke this `forward` method.

        Args:
            x: The first input `Variable`.
            y: The second input `Variable`.

        Returns:
            A new `Variable` instance representing the result of the addition, \
            with appropriately aggregated [`data`][afnio.Variable.data], \
            [`role`][afnio.Variable.role], and \
            [`requires_grad`][afnio.Variable.requires_grad] attributes.

        Raises:
            TypeError: If either input is not an instance
                of [`Variable`][afnio.Variable].
            TypeError: If addition between the input types is not allowed.
            ValueError: If a scalar [`data`][afnio.Variable.data] is added
                to a list[`data`][afnio.Variable.data].
            ValueError: If list [`data`][afnio.Variable.data] fields
                have mismatched lengths.
        """
        return AddOp.apply(x, y)

forward(x, y)

Forward pass for element-wise addition.

Warning

Users should not call this method directly. Instead, they should call the module instance itself, which will internally invoke this forward method.

Parameters:

Name Type Description Default
x Variable

The first input Variable.

required
y Variable

The second input Variable.

required

Returns:

Type Description
Variable

A new Variable instance representing the result of the addition, with appropriately aggregated data, role, and requires_grad attributes.

Raises:

Type Description
TypeError

If either input is not an instance of Variable.

TypeError

If addition between the input types is not allowed.

ValueError

If a scalar data is added to a listdata.

ValueError

If list data fields have mismatched lengths.

Source code in afnio/cognitive/modules/add.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def forward(self, x: Variable, y: Variable) -> Variable:
    """
    Forward pass for element-wise addition.

    Warning:
        Users should not call this method directly. Instead, they should call the
        module instance itself, which will internally invoke this `forward` method.

    Args:
        x: The first input `Variable`.
        y: The second input `Variable`.

    Returns:
        A new `Variable` instance representing the result of the addition, \
        with appropriately aggregated [`data`][afnio.Variable.data], \
        [`role`][afnio.Variable.role], and \
        [`requires_grad`][afnio.Variable.requires_grad] attributes.

    Raises:
        TypeError: If either input is not an instance
            of [`Variable`][afnio.Variable].
        TypeError: If addition between the input types is not allowed.
        ValueError: If a scalar [`data`][afnio.Variable.data] is added
            to a list[`data`][afnio.Variable.data].
        ValueError: If list [`data`][afnio.Variable.data] fields
            have mismatched lengths.
    """
    return AddOp.apply(x, y)