Calculate the sufficient statistics for the mean and variance of `x`. These sufficient statistics are computed using the one pass algorithm on an input that's optionally shifted. See: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Computing_shifted_data Args: x: A `T
(x, axes, shift=None, keep_dims=None, name=None,
keepdims=None)
| 1151 | |
| 1152 | @tf_export(v1=["nn.sufficient_statistics"]) |
| 1153 | def sufficient_statistics(x, axes, shift=None, keep_dims=None, name=None, |
| 1154 | keepdims=None): |
| 1155 | """Calculate the sufficient statistics for the mean and variance of `x`. |
| 1156 | |
| 1157 | These sufficient statistics are computed using the one pass algorithm on |
| 1158 | an input that's optionally shifted. See: |
| 1159 | https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Computing_shifted_data |
| 1160 | |
| 1161 | Args: |
| 1162 | x: A `Tensor`. |
| 1163 | axes: Array of ints. Axes along which to compute mean and variance. |
| 1164 | shift: A `Tensor` containing the value by which to shift the data for |
| 1165 | numerical stability, or `None` if no shift is to be performed. A shift |
| 1166 | close to the true mean provides the most numerically stable results. |
| 1167 | keep_dims: produce statistics with the same dimensionality as the input. |
| 1168 | name: Name used to scope the operations that compute the sufficient stats. |
| 1169 | keepdims: Alias for keep_dims. |
| 1170 | |
| 1171 | Returns: |
| 1172 | Four `Tensor` objects of the same type as `x`: |
| 1173 | |
| 1174 | * the count (number of elements to average over). |
| 1175 | * the (possibly shifted) sum of the elements in the array. |
| 1176 | * the (possibly shifted) sum of squares of the elements in the array. |
| 1177 | * the shift by which the mean must be corrected or None if `shift` is None. |
| 1178 | """ |
| 1179 | axes = list(set(axes)) |
| 1180 | keep_dims = deprecated_argument_lookup( |
| 1181 | "keepdims", keepdims, "keep_dims", keep_dims) |
| 1182 | if keep_dims is None: |
| 1183 | keep_dims = False |
| 1184 | with ops.name_scope(name, "sufficient_statistics", [x, shift]): |
| 1185 | x = ops.convert_to_tensor(x, name="x") |
| 1186 | x_shape = x.get_shape() |
| 1187 | if x_shape.rank is not None and all( |
| 1188 | x_shape.dims[d].value is not None for d in axes): |
| 1189 | counts = 1 |
| 1190 | for d in axes: |
| 1191 | counts *= x_shape.dims[d].value |
| 1192 | counts = constant_op.constant(counts, dtype=x.dtype) |
| 1193 | else: # shape needs to be inferred at runtime. |
| 1194 | x_dims = array_ops.gather( |
| 1195 | math_ops.cast(array_ops.shape(x), x.dtype), axes) |
| 1196 | counts = math_ops.reduce_prod(x_dims, name="count") |
| 1197 | if shift is not None: |
| 1198 | shift = ops.convert_to_tensor(shift, name="shift") |
| 1199 | m_ss = math_ops.subtract(x, shift) |
| 1200 | v_ss = math_ops.squared_difference(x, shift) |
| 1201 | else: # no shift. |
| 1202 | m_ss = x |
| 1203 | v_ss = math_ops.square(x) |
| 1204 | m_ss = math_ops.reduce_sum(m_ss, axes, keepdims=keep_dims, name="mean_ss") |
| 1205 | v_ss = math_ops.reduce_sum(v_ss, axes, keepdims=keep_dims, name="var_ss") |
| 1206 | return counts, m_ss, v_ss, shift |
| 1207 | |
| 1208 | |
| 1209 | @tf_export("nn.sufficient_statistics", v1=[]) |
no test coverage detected