tf.math.abs
Stay organized with collections
Save and categorize content based on your preferences.
Computes the absolute value of a tensor.
tf.math.abs(
x, name=None
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
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 returns a tensor of type
float32
or float64
that is the absolute value of each element in x
. For
a complex number \(a + bj\), its absolute value is computed as
\(\sqrt{a^2 + b^2}\).
For example:
# real number
x = tf.constant([-2.25, 3.25])
tf.abs(x)
<tf.Tensor: shape=(2,), dtype=float32,
numpy=array([2.25, 3.25], dtype=float32)>
# complex number
x = tf.constant([[-2.25 + 4.75j], [-3.25 + 5.75j]])
tf.abs(x)
<tf.Tensor: shape=(2, 1), dtype=float64, numpy=
array([[5.25594901],
[6.60492241]])>
Args |
x
|
A Tensor or SparseTensor of type float16 , float32 , float64 ,
int32 , int64 , complex64 or complex128 .
|
name
|
A name for the operation (optional).
|
Returns |
A Tensor or SparseTensor of the same size, type and sparsity as x ,
with absolute values. Note, for complex64 or complex128 input, the
returned Tensor will be of type float32 or float64 , respectively.
If x is a SparseTensor , returns
SparseTensor(x.indices, tf.math.abs(x.values, ...), x.dense_shape)
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.math.abs\n\n|----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/math_ops.py#L359-L403) |\n\nComputes the absolute value of a tensor.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.abs`](https://www.tensorflow.org/api_docs/python/tf/math/abs)\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.abs`](https://www.tensorflow.org/api_docs/python/tf/math/abs)\n\n\u003cbr /\u003e\n\n tf.math.abs(\n x, name=None\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Training checkpoints](https://www.tensorflow.org/guide/checkpoint) - [Matrix approximation with Core APIs](https://www.tensorflow.org/guide/core/matrix_core) - [Estimators](https://www.tensorflow.org/guide/estimator) - [Better performance with tf.function](https://www.tensorflow.org/guide/function) - [TF-NumPy Type Promotion](https://www.tensorflow.org/guide/tf_numpy_type_promotion) | - [CycleGAN](https://www.tensorflow.org/tutorials/generative/cyclegan) - [Neural style transfer](https://www.tensorflow.org/tutorials/generative/style_transfer) - [Simple audio recognition: Recognizing keywords](https://www.tensorflow.org/tutorials/audio/simple_audio) - [pix2pix: Image-to-image translation with a conditional GAN](https://www.tensorflow.org/tutorials/generative/pix2pix) - [Integrated gradients](https://www.tensorflow.org/tutorials/interpretability/integrated_gradients) |\n\nGiven a tensor of integer or floating-point values, this operation returns a\ntensor of the same type, where each element contains the absolute value of the\ncorresponding element in the input.\n\nGiven a tensor `x` of complex numbers, this operation returns a tensor of type\n`float32` or `float64` that is the absolute value of each element in `x`. For\na complex number \\\\(a + bj\\\\), its absolute value is computed as\n\\\\(\\\\sqrt{a\\^2 + b\\^2}\\\\).\n\n#### For example:\n\n # real number\n x = tf.constant([-2.25, 3.25])\n tf.abs(x)\n \u003ctf.Tensor: shape=(2,), dtype=float32,\n numpy=array([2.25, 3.25], dtype=float32)\u003e\n\n # complex number\n x = tf.constant([[-2.25 + 4.75j], [-3.25 + 5.75j]])\n tf.abs(x)\n \u003ctf.Tensor: shape=(2, 1), dtype=float64, numpy=\n array([[5.25594901],\n [6.60492241]])\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------|----------------------------------------------------------------------------------------------------------------------|\n| `x` | A `Tensor` or `SparseTensor` of type `float16`, `float32`, `float64`, `int32`, `int64`, `complex64` or `complex128`. |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor` or `SparseTensor` of the same size, type and sparsity as `x`, with absolute values. Note, for `complex64` or `complex128` input, the returned `Tensor` will be of type `float32` or `float64`, respectively. \u003cbr /\u003e If `x` is a `SparseTensor`, returns `SparseTensor(x.indices, tf.math.abs(x.values, ...), x.dense_shape)` ||\n\n\u003cbr /\u003e"]]