Computes the variance of ``x`` along ``axis`` . .. note:: Alias Support: The parameter name ``input`` can be used as an alias for ``x``, and ``dim`` can be used as an alias for ``axis``. For example, ``var(input=tensor_x, dim=1, ...)`` is equivalent to ``var(x=tensor_x, axi
(
x: Tensor,
axis: int | Sequence[int] | None = None,
unbiased: bool | None = None,
keepdim: bool = False,
name: str | None = None,
*,
correction: float = 1,
out: Tensor | None = None,
)
| 184 | |
| 185 | @param_two_alias(["x", "input"], ["axis", "dim"]) |
| 186 | def var( |
| 187 | x: Tensor, |
| 188 | axis: int | Sequence[int] | None = None, |
| 189 | unbiased: bool | None = None, |
| 190 | keepdim: bool = False, |
| 191 | name: str | None = None, |
| 192 | *, |
| 193 | correction: float = 1, |
| 194 | out: Tensor | None = None, |
| 195 | ) -> Tensor: |
| 196 | """ |
| 197 | Computes the variance of ``x`` along ``axis`` . |
| 198 | |
| 199 | .. note:: |
| 200 | Alias Support: The parameter name ``input`` can be used as an alias for ``x``, and ``dim`` can be used as an alias for ``axis``. |
| 201 | For example, ``var(input=tensor_x, dim=1, ...)`` is equivalent to ``var(x=tensor_x, axis=1, ...)``. |
| 202 | |
| 203 | Args: |
| 204 | x (Tensor): The input Tensor with data type float16, float32, float64. |
| 205 | alias: ``input``. |
| 206 | axis (int|list|tuple|None, optional): The axis along which to perform variance calculations. ``axis`` should be int, list(int) or tuple(int). |
| 207 | alias: ``dim``. |
| 208 | |
| 209 | - If ``axis`` is a list/tuple of dimension(s), variance is calculated along all element(s) of ``axis`` . ``axis`` or element(s) of ``axis`` should be in range [-D, D), where D is the dimensions of ``x`` . |
| 210 | - If ``axis`` or element(s) of ``axis`` is less than 0, it works the same way as :math:`axis + D` . |
| 211 | - If ``axis`` is None, variance is calculated over all elements of ``x``. Default is None. |
| 212 | |
| 213 | unbiased (bool, optional): Whether to use the unbiased estimation. If ``unbiased`` is True, the divisor used in the computation is :math:`N - 1`, where :math:`N` represents the number of elements along ``axis`` , otherwise the divisor is :math:`N`. Default is True. |
| 214 | keep_dim (bool, optional): Whether to reserve the reduced dimension in the output Tensor. The result tensor will have one fewer dimension than the input unless keep_dim is true. Default is False. |
| 215 | name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. |
| 216 | correction (int|float, optional): Difference between the sample size and sample degrees of freedom. |
| 217 | Defaults to 1 (Bessel's correction). If unbiased is specified, this parameter is ignored. |
| 218 | out (Tensor|None, optional): Output tensor. Default is None. |
| 219 | |
| 220 | Returns: |
| 221 | Tensor, results of variance along ``axis`` of ``x``, with the same data type as ``x``. |
| 222 | |
| 223 | Examples: |
| 224 | .. code-block:: pycon |
| 225 | |
| 226 | >>> import paddle |
| 227 | |
| 228 | >>> x = paddle.to_tensor([[1.0, 2.0, 3.0], [1.0, 4.0, 5.0]]) |
| 229 | >>> out1 = paddle.var(x) |
| 230 | >>> print(out1.numpy()) |
| 231 | 2.6666667 |
| 232 | >>> out2 = paddle.var(x, axis=1) |
| 233 | >>> print(out2.numpy()) |
| 234 | [1. 4.3333335] |
| 235 | """ |
| 236 | if unbiased is not None and correction != 1: |
| 237 | raise ValueError("Only one of unbiased and correction may be given") |
| 238 | |
| 239 | if unbiased is not None: |
| 240 | actual_correction = 1.0 if unbiased else 0.0 |
| 241 | else: |
| 242 | actual_correction = float(correction) |
| 243 |
no test coverage detected