Bases: Module
Aggregates a list of input Variables into a single output Variable.
This module utilizes the Sum operation from
afnio.autodiff.basic_ops. It supports both numerical (int, float) and string data
types. For numerical data, it computes the sum. For string data, it concatenates
the values and wraps each in <ITEM></ITEM> tags.
Note
This module does not have any trainable parameters.
Examples:
>>> from afnio import cognitive as cog
>>> class Summation(cog.Module):
... def __init__(self):
... super().__init__()
... self.sum = cog.Sum()
>>> def forward(self, x):
... return self.sum(x)
>>> input1 = afnio.Variable(data="abc", role="input1")
>>> input2 = afnio.Variable(data="def", role="input2")
>>> input3 = afnio.Variable(data="ghi", role="input3")
>>> summation = Summation()
>>> result = summation([input1, input2, input3])
>>> print(result.data)
'<ITEM>abc</ITEM><ITEM>def</ITEM><ITEM>ghi</ITEM>'
>>> print(result.role)
'input1 and input2 and input3'
Raises:
| Type |
Description |
TypeError
|
If any element in x is not an instance of
Variable or a sequence of Variable
instances, or if addition between the data types
is not allowed.
|
See Also
afnio.autodiff.basic_ops.Sum
for the underlying operation.
Source code in afnio/cognitive/modules/sum.py
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 | class Sum(Module):
"""
Aggregates a list of input Variables into a single output Variable.
This module utilizes the [`Sum`][afnio.autodiff.basic_ops.Sum] operation from
`afnio.autodiff.basic_ops`. It supports both numerical (int, float) and string data
types. For numerical data, it computes the sum. For string data, it concatenates
the values and wraps each in `<ITEM></ITEM>` tags.
Note:
This module does not have any trainable parameters.
Examples:
>>> from afnio import cognitive as cog
>>> class Summation(cog.Module):
... def __init__(self):
... super().__init__()
... self.sum = cog.Sum()
>>> def forward(self, x):
... return self.sum(x)
>>> input1 = afnio.Variable(data="abc", role="input1")
>>> input2 = afnio.Variable(data="def", role="input2")
>>> input3 = afnio.Variable(data="ghi", role="input3")
>>> summation = Summation()
>>> result = summation([input1, input2, input3])
>>> print(result.data)
'<ITEM>abc</ITEM><ITEM>def</ITEM><ITEM>ghi</ITEM>'
>>> print(result.role)
'input1 and input2 and input3'
Raises:
TypeError: If any element in `x` is not an instance of
[`Variable`][afnio.Variable] or a sequence of [`Variable`][afnio.Variable]
instances, or if addition between the [`data`][afnio.Variable.data] types
is not allowed.
See Also:
[`afnio.autodiff.basic_ops.Sum`][afnio.autodiff.basic_ops.Sum]
for the underlying operation.
"""
def __init__(self):
super().__init__()
def forward(self, x: List[Variable]) -> Variable:
"""
Forward pass for summation.
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: A list of `Variable` instances to be summed.
Returns:
A new `Variable` instance representing the result of the summation, \
with appropriately aggregated [`data`][afnio.Variable.data], \
[`role`][afnio.Variable.role], and \
[`requires_grad`][afnio.Variable.requires_grad] attributes.
Raises:
TypeError: If any element in `x` is not an instance
of [`Variable`][afnio.Variable] or a sequence
of [`Variable`][afnio.Variable] instances, or if addition between
the [`data`][afnio.Variable.data] types is not allowed.
"""
if not isinstance(x, list) and all(isinstance(y, Variable) for y in x):
raise TypeError("All inputs must be instances of 'Variable'.")
return SumOp.apply(x)
|
forward(x)
Forward pass for summation.
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
|
list[Variable]
|
A list of Variable instances to be summed.
|
required
|
Returns:
| Type |
Description |
Variable
|
A new Variable instance representing the result of the summation, with appropriately aggregated data, role, and requires_grad attributes.
|
Raises:
| Type |
Description |
TypeError
|
If any element in x is not an instance
of Variable or a sequence
of Variable instances, or if addition between
the data types is not allowed.
|
Source code in afnio/cognitive/modules/sum.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 | def forward(self, x: List[Variable]) -> Variable:
"""
Forward pass for summation.
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: A list of `Variable` instances to be summed.
Returns:
A new `Variable` instance representing the result of the summation, \
with appropriately aggregated [`data`][afnio.Variable.data], \
[`role`][afnio.Variable.role], and \
[`requires_grad`][afnio.Variable.requires_grad] attributes.
Raises:
TypeError: If any element in `x` is not an instance
of [`Variable`][afnio.Variable] or a sequence
of [`Variable`][afnio.Variable] instances, or if addition between
the [`data`][afnio.Variable.data] types is not allowed.
"""
if not isinstance(x, list) and all(isinstance(y, Variable) for y in x):
raise TypeError("All inputs must be instances of 'Variable'.")
return SumOp.apply(x)
|