Skip to content

mean

MeanLayer ¤

MeanLayer(
    name=None,
    input_dtype=None,
    output_dtype=None,
    mean_constant=None,
    **kwargs
)

Bases: BaseLayer

Performs the mean(x, y) operation on a given input tensor. If mean_constant is not set, inputs are assumed to be a list of tensors and the mean of all the tensors is computed. If mean_constant is set, inputs must be a tensor.

Initializes the Mean layer

Parameters:

Name Type Description Default
name Optional[str]

Name of the layer, defaults to None.

None
input_dtype Optional[str]

The dtype to cast the input to. Defaults to None.

None
output_dtype Optional[str]

The dtype to cast the output to. Defaults to None.

None
mean_constant Optional[float]

The constant to mean against the input, defaults to None.

None
Source code in src/kamae/tensorflow/layers/mean.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def __init__(
    self,
    name: Optional[str] = None,
    input_dtype: Optional[str] = None,
    output_dtype: Optional[str] = None,
    mean_constant: Optional[float] = None,
    **kwargs: Any,
) -> None:
    """
    Initializes the Mean layer

    :param name: Name of the layer, defaults to `None`.
    :param input_dtype: The dtype to cast the input to. Defaults to `None`.
    :param output_dtype: The dtype to cast the output to. Defaults to `None`.
    :param mean_constant: The constant to mean against the input, defaults
    to `None`.
    """
    super().__init__(
        name=name, input_dtype=input_dtype, output_dtype=output_dtype, **kwargs
    )
    self.mean_constant = mean_constant

compatible_dtypes property ¤

compatible_dtypes

Returns the compatible dtypes of the layer.

Returns:

Type Description
Optional[List[DType]]

The compatible dtypes of the layer.

_call ¤

_call(inputs, **kwargs)

Performs the mean(x, y) operation on either an iterable of input tensors or a single input tensor and a constant.

Decorated with @allow_single_or_multiple_tensor_input to ensure that the input is either a single tensor or an iterable of tensors. Returns this result as a list of tensors for easier use here.

Parameters:

Name Type Description Default
inputs Union[Tensor, Iterable[Tensor]]

Single tensor or iterable of tensors to perform the mean(x, y) operation on.

required

Returns:

Type Description
Tensor

The tensor resulting from the mean(x, y) operation.

Source code in src/kamae/tensorflow/layers/mean.py
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
@allow_single_or_multiple_tensor_input
def _call(self, inputs: Union[Tensor, Iterable[Tensor]], **kwargs: Any) -> Tensor:
    """
    Performs the mean(x, y) operation on either an iterable of input tensors or
    a single input tensor and a constant.

    Decorated with `@allow_single_or_multiple_tensor_input` to ensure that the input
    is either a single tensor or an iterable of tensors. Returns this result as a
    list of tensors for easier use here.

    :param inputs: Single tensor or iterable of tensors to perform the
    mean(x, y) operation on.
    :returns: The tensor resulting from the mean(x, y) operation.
    """
    if self.mean_constant is not None:
        if len(inputs) > 1:
            raise ValueError("If mean_constant is set, inputs must be a tensor")
        (
            cast_input,
            cast_mean_constant,
        ) = self._force_cast_to_compatible_numeric_type(
            inputs[0],
            self.mean_constant,
        )
        return tf.truediv(tf.math.add(cast_input, cast_mean_constant), 2)
    else:
        if not len(inputs) > 1:
            raise ValueError(
                "If mean_constant is not set, must have multiple inputs"
            )

        return tf.truediv(reduce(tf.math.add, inputs), len(inputs))

get_config ¤

get_config()

Gets the configuration of the Mean layer. Used for saving and loading from a model.

Specifically adds the mean_constant to the config dictionary.

Returns:

Type Description
Dict[str, Any]

Dictionary of the configuration of the layer.

Source code in src/kamae/tensorflow/layers/mean.py
113
114
115
116
117
118
119
120
121
122
123
124
def get_config(self) -> Dict[str, Any]:
    """
    Gets the configuration of the Mean layer.
    Used for saving and loading from a model.

    Specifically adds the `mean_constant` to the config dictionary.

    :returns: Dictionary of the configuration of the layer.
    """
    config = super().get_config()
    config.update({"mean_constant": self.mean_constant})
    return config