r"""Computes the absolute value of a tensor. Given a tensor of integer or floating-point values, this operation returns a tensor of the same type, where each element contains the absolute value of the corresponding element in the input. Given a tensor `x` of complex numbers, this operation
(x, name=None)
| 245 | @tf_export("math.abs", "abs") |
| 246 | @dispatch.add_dispatch_support |
| 247 | def abs(x, name=None): # pylint: disable=redefined-builtin |
| 248 | r"""Computes the absolute value of a tensor. |
| 249 | |
| 250 | Given a tensor of integer or floating-point values, this operation returns a |
| 251 | tensor of the same type, where each element contains the absolute value of the |
| 252 | corresponding element in the input. |
| 253 | |
| 254 | Given a tensor `x` of complex numbers, this operation returns a tensor of type |
| 255 | `float32` or `float64` that is the absolute value of each element in `x`. All |
| 256 | elements in `x` must be complex numbers of the form \\(a + bj\\). The |
| 257 | absolute value is computed as \\( \sqrt{a^2 + b^2}\\). For example: |
| 258 | ```python |
| 259 | x = tf.constant([[-2.25 + 4.75j], [-3.25 + 5.75j]]) |
| 260 | tf.abs(x) # [5.25594902, 6.60492229] |
| 261 | ``` |
| 262 | |
| 263 | Args: |
| 264 | x: A `Tensor` or `SparseTensor` of type `float16`, `float32`, `float64`, |
| 265 | `int32`, `int64`, `complex64` or `complex128`. |
| 266 | name: A name for the operation (optional). |
| 267 | |
| 268 | Returns: |
| 269 | A `Tensor` or `SparseTensor` the same size, type, and sparsity as `x` with |
| 270 | absolute values. |
| 271 | Note, for `complex64` or `complex128` input, the returned `Tensor` will be |
| 272 | of type `float32` or `float64`, respectively. |
| 273 | """ |
| 274 | with ops.name_scope(name, "Abs", [x]) as name: |
| 275 | x = ops.convert_to_tensor(x, name="x") |
| 276 | if x.dtype.is_complex: |
| 277 | return gen_math_ops.complex_abs(x, Tout=x.dtype.real_dtype, name=name) |
| 278 | return gen_math_ops._abs(x, name=name) |
| 279 | |
| 280 | |
| 281 | # pylint: enable=g-docstring-has-escape |