Skip to content

Aggregation Layers

Bases: Aggregation

k3_node.layers.SumAggregation Compute and return the sum of two numbers.

Parameters:

Name Type Description Default
`**kwargs`

Additional keyword arguments passed to the Layer superclass.

required

Returns:

Type Description

A callable layer instance.

Source code in k3_node/layers/aggr/basic.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class SumAggregation(Aggregation):
    """`k3_node.layers.SumAggregation`
    Compute and return the sum of two numbers.

    Args:
        `**kwargs`: Additional keyword arguments passed to the `Layer` superclass.

    Returns:
        A callable layer instance.
    """

    def call(self, x, index=None, axis=-2):
        return self.reduce(x, index, axis=axis, reduce_fn=ops.segment_sum)

Bases: Aggregation

k3_node.layers.MaxAggregation Compute and return the sum of two numbers.

Parameters:

Name Type Description Default
`**kwargs`

Additional keyword arguments passed to the Layer superclass.

required

Returns:

Type Description

A callable layer instance.

Source code in k3_node/layers/aggr/basic.py
23
24
25
26
27
28
29
30
31
32
33
34
class MaxAggregation(Aggregation):
    """`k3_node.layers.MaxAggregation`
    Compute and return the sum of two numbers.

    Args:
        `**kwargs`: Additional keyword arguments passed to the `Layer` superclass.

    Returns:
        A callable layer instance.
    """
    def call(self, x, index=None, axis=-2):
        return self.reduce(x, index, axis=axis, reduce_fn=ops.segment_max)

Bases: Aggregation

k3_node.layers.MeanAggregation Compute and return the sum of two numbers.

Parameters:

Name Type Description Default
`**kwargs`

Additional keyword arguments passed to the Layer superclass.

required

Returns:

Type Description

A callable layer instance.

Source code in k3_node/layers/aggr/basic.py
37
38
39
40
41
42
43
44
45
46
47
48
class MeanAggregation(Aggregation):
    """`k3_node.layers.MeanAggregation`
    Compute and return the sum of two numbers.

    Args:
        `**kwargs`: Additional keyword arguments passed to the `Layer` superclass.

    Returns:
        A callable layer instance.
    """
    def call(self, x, index=None, axis=-2):
        return self.reduce(x, index, axis=axis, reduce_fn=_segment_mean)

Bases: Aggregation

k3_node.layers.SoftmaxAggregation Compute and return the sum of two numbers.

Parameters:

Name Type Description Default
`**kwargs`

Additional keyword arguments passed to the Layer superclass.

required

Returns:

Type Description

A callable layer instance.

Source code in k3_node/layers/aggr/basic.py
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
81
82
83
84
85
86
class SoftmaxAggregation(Aggregation):
    """`k3_node.layers.SoftmaxAggregation`
    Compute and return the sum of two numbers.

    Args:
        `**kwargs`: Additional keyword arguments passed to the `Layer` superclass.

    Returns:
        A callable layer instance.
    """
    def __init__(self, t=1.0, trainable=False, channels=1):
        super().__init__()

        if not trainable and channels != 1:
            raise ValueError(
                "Cannot set 'channels' greater than '1' in case 'SoftmaxAggregation' is not trainable"
            )

        self._init_t = t
        self.trainable = trainable
        self.channels = channels

        self.t = self.add_weight((channels,), initializer="zeros") if trainable else t

    def call(self, x, index=None, axis=-2):
        t = self.t
        if self.channels != 1:
            self.assert_two_dimensional_input(x, axis)
            assert ops.is_tensor(t)
            t = ops.reshape(t, (1, self.channels))

        alpha = x
        if not isinstance(t, (int, float)) or t != 1:
            alpha = x * t
        alpha = ops.softmax(alpha, axis=axis)
        return self.reduce(x * alpha, index=index, axis=axis, reduce_fn=ops.segment_sum)

Bases: Aggregation

k3_node.layers.SoftmaxAggregation Compute and return the sum of two numbers.

Parameters:

Name Type Description Default
**kwargs

Additional keyword arguments passed to the Layer superclass.

required

Returns:

Type Description

A callable layer instance.

Source code in k3_node/layers/aggr/basic.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
class PowerMeanAggregation(Aggregation):
    """`k3_node.layers.SoftmaxAggregation`
    Compute and return the sum of two numbers.

    Args:
        **kwargs: Additional keyword arguments passed to the `Layer` superclass.

    Returns:
        A callable layer instance.

    """
    def __init__(self, p=1.0, trainable=False, channels=1):
        super().__init__()

        if not trainable and channels != 1:
            raise ValueError(
                f"Cannot set 'channels' greater than '1' in case '{self.__class__.__name__}' is not trainable"
            )

        self._init_p = p
        self.trainable = trainable
        self.channels = channels

        self.p = self.add_weight((channels,), "zeros") if trainable else p

    def call(self, x, index=None, axis=-2):
        p = self.p
        if self.channels != 1:
            assert ops.is_tensor(p)
            self.assert_two_dimensional_input(x, axis)
            p = ops.reshape(p, (-1, self.channels))
        if not isinstance(p, (int, float)) or p != 1.0:
            x = ops.clip(x, 0, 100) ** p
        out = self.reduce(x, index, axis, reduce_fn=_segment_mean)
        if not isinstance(p, (int, float)) or p != 1:
            out = ops.clip(out, 0, 100) ** (1.0 / p)
        return out