When subtracting two input values of different shapes, tf.subtract follows the
general 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.
A Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128, uint32, uint64.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.math.subtract\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/math_ops.py#L539-L543) |\n\nReturns x - y element-wise.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.subtract`](https://www.tensorflow.org/api_docs/python/tf/math/subtract)\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.math.subtract`](https://www.tensorflow.org/api_docs/python/tf/math/subtract), [`tf.compat.v1.subtract`](https://www.tensorflow.org/api_docs/python/tf/math/subtract)\n\n\u003cbr /\u003e\n\n tf.math.subtract(\n x, y, name=None\n )\n\n| **Note:** [`tf.subtract`](../../tf/math/subtract) supports broadcasting. More about broadcasting [here](http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)\n\nBoth input and output have a range `(-inf, inf)`.\n\nExample usages below.\n\nSubtract operation between an array and a scalar: \n\n x = [1, 2, 3, 4, 5]\n y = 1\n tf.subtract(x, y)\n \u003ctf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)\u003e\n tf.subtract(y, x)\n \u003ctf.Tensor: shape=(5,), dtype=int32,\n numpy=array([ 0, -1, -2, -3, -4], 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([0, 1, 2, 3, 4], dtype=int32)\u003e\n\nSubtract operation between an array and a tensor of same shape: \n\n x = [1, 2, 3, 4, 5]\n y = tf.constant([5, 4, 3, 2, 1])\n tf.subtract(y, x)\n \u003ctf.Tensor: shape=(5,), dtype=int32,\n numpy=array([ 4, 2, 0, -2, -4], 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**8 + 1, 2**8 + 2]\n tf.subtract(x, y)\n \u003ctf.Tensor: shape=(2,), dtype=int8, numpy=array([0, 0], dtype=int8)\u003e\n\nWhen subtracting two input values of different shapes, [`tf.subtract`](../../tf/math/subtract) follows the\n[general broadcasting rules](https://numpy.org/doc/stable/user/basics.broadcasting.html#general-broadcasting-rules)\n. The two input array shapes are compared element-wise. Starting with the\ntrailing dimensions, the two dimensions either have to be equal or one of them\nneeds to be `1`.\n\nFor example, \n\n x = np.ones(6).reshape(2, 3, 1)\n y = np.ones(6).reshape(2, 1, 3)\n tf.subtract(x, y)\n \u003ctf.Tensor: shape=(2, 3, 3), dtype=float64, numpy=\n array([[[0., 0., 0.],\n [0., 0., 0.],\n [0., 0., 0.]],\n [[0., 0., 0.],\n [0., 0., 0.],\n [0., 0., 0.]]])\u003e\n\nExample with inputs of different dimensions: \n\n x = np.ones(6).reshape(2, 3, 1)\n y = np.ones(6).reshape(1, 6)\n tf.subtract(x, y)\n \u003ctf.Tensor: shape=(2, 3, 6), dtype=float64, numpy=\n array([[[0., 0., 0., 0., 0., 0.],\n [0., 0., 0., 0., 0., 0.],\n [0., 0., 0., 0., 0., 0.]],\n [[0., 0., 0., 0., 0., 0.],\n [0., 0., 0., 0., 0., 0.],\n [0., 0., 0., 0., 0., 0.]]])\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `x` | A `Tensor`. Must be one of the following types: `bfloat16`, `half`, `float32`, `float64`, `uint8`, `int8`, `uint16`, `int16`, `int32`, `int64`, `complex64`, `complex128`, `uint32`, `uint64`. |\n| `y` | A `Tensor`. Must have the same type as `x`. |\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`. Has the same type as `x`. ||\n\n\u003cbr /\u003e"]]