When adding two input values of different shapes, Add follows NumPy
broadcasting rules. The two input array shapes are compared element-wise.
Starting with the trailing dimensions, the two dimensions either have to be
equal or one of them needs to be 1.
The reduction version of this elementwise operation is tf.math.reduce_sum
Args
x
A tf.Tensor. Must be one of the following types: bfloat16, half,
float16, float32, float64, uint8, uint16, uint32, uint64, int8, int16,
int32, int64, complex64, complex128, string.
[null,null,["Last updated 2023-10-06 UTC."],[],[],null,["# tf.math.add\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/math_ops.py#L3972-L4050) |\n\nReturns x + y element-wise.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.add`](https://www.tensorflow.org/api_docs/python/tf/math/add)\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.add`](https://www.tensorflow.org/api_docs/python/tf/math/add), [`tf.compat.v1.math.add`](https://www.tensorflow.org/api_docs/python/tf/math/add)\n\n\u003cbr /\u003e\n\n tf.math.add(\n x, y, name=None\n )\n\nExample usages below.\n\nAdd a scalar and a list: \n\n x = [1, 2, 3, 4, 5]\n y = 1\n tf.add(x, y)\n \u003ctf.Tensor: shape=(5,), dtype=int32, numpy=array([2, 3, 4, 5, 6],\n dtype=int32)\u003e\n\nNote that binary `+` operator can be used instead: \n\n x = tf.convert_to_tensor([1, 2, 3, 4, 5])\n y = tf.convert_to_tensor(1)\n x + y\n \u003ctf.Tensor: shape=(5,), dtype=int32, numpy=array([2, 3, 4, 5, 6],\n dtype=int32)\u003e\n\nAdd a tensor and a list of same shape: \n\n x = [1, 2, 3, 4, 5]\n y = tf.constant([1, 2, 3, 4, 5])\n tf.add(x, y)\n \u003ctf.Tensor: shape=(5,), dtype=int32,\n numpy=array([ 2, 4, 6, 8, 10], dtype=int32)\u003e\n\n| **Warning:** If one of the inputs (`x` or `y`) is a tensor and the other is a non-tensor, the non-tensor input will adopt (or get casted to) the data type of the tensor input. This can potentially cause unwanted overflow or underflow conversion.\n\nFor example, \n\n x = tf.constant([1, 2], dtype=tf.int8)\n y = [2**7 + 1, 2**7 + 2]\n tf.add(x, y)\n \u003ctf.Tensor: shape=(2,), dtype=int8, numpy=array([-126, -124], dtype=int8)\u003e\n\nWhen adding two input values of different shapes, `Add` follows NumPy\nbroadcasting rules. The two input array shapes are compared element-wise.\nStarting with the trailing dimensions, the two dimensions either have to be\nequal or one of them needs to be `1`.\n\nFor example, \n\n x = np.ones(6).reshape(1, 2, 1, 3)\n y = np.ones(6).reshape(2, 1, 3, 1)\n tf.add(x, y).shape.as_list()\n [2, 2, 3, 3]\n\nAnother example with two arrays of different dimension. \n\n x = np.ones([1, 2, 1, 4])\n y = np.ones([3, 4])\n tf.add(x, y).shape.as_list()\n [1, 2, 3, 4]\n\nThe reduction version of this elementwise operation is [`tf.math.reduce_sum`](../../tf/math/reduce_sum)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `x` | A [`tf.Tensor`](../../tf/Tensor). Must be one of the following types: bfloat16, half, float16, float32, float64, uint8, uint16, uint32, uint64, int8, int16, int32, int64, complex64, complex128, string. |\n| `y` | A [`tf.Tensor`](../../tf/Tensor). Must have the same type as x. |\n| `name` | A name for the operation (optional) |\n\n\u003cbr /\u003e"]]