|
6 | 6 | endpoint { |
7 | 7 | name: "add" |
8 | 8 | } |
| 9 | + description: <<END |
| 10 | +Example usages below. |
| 11 | +
|
| 12 | +Add a scalar and a list: |
| 13 | +
|
| 14 | +>>> x = [1, 2, 3, 4, 5] |
| 15 | +>>> y = 1 |
| 16 | +>>> tf.add(x, y) |
| 17 | +<tf.Tensor: shape=(5,), dtype=int32, numpy=array([2, 3, 4, 5, 6], dtype=int32)> |
| 18 | +
|
| 19 | +Note that binary `+` operator can be used instead: |
| 20 | +
|
| 21 | +>>> x = tf.convert_to_tensor([1, 2, 3, 4, 5]) |
| 22 | +>>> y = tf.convert_to_tensor(1) |
| 23 | +>>> x + y |
| 24 | +<tf.Tensor: shape=(5,), dtype=int32, numpy=array([2, 3, 4, 5, 6], dtype=int32)> |
| 25 | +
|
| 26 | +Add a tensor and a list of same shape: |
| 27 | +
|
| 28 | +>>> x = [1, 2, 3, 4, 5] |
| 29 | +>>> y = tf.constant([1, 2, 3, 4, 5]) |
| 30 | +>>> tf.add(x, y) |
| 31 | +<tf.Tensor: shape=(5,), dtype=int32, |
| 32 | +numpy=array([ 2, 4, 6, 8, 10], dtype=int32)> |
| 33 | +
|
| 34 | +**Warning**: If one of the inputs (`x` or `y`) is a tensor and the other is a |
| 35 | +non-tensor, the non-tensor input will adopt (or get casted to) the data type of |
| 36 | +the tensor input. This can potentially cause unwanted overflow or underflow |
| 37 | +conversion. |
| 38 | +
|
| 39 | +For example, |
| 40 | +
|
| 41 | +>>> x = [2**7 + 1, 2**7 + 2] |
| 42 | +>>> y = tf.constant([1, 2], dtype=tf.int8) |
| 43 | +>>> tf.add(x, y) |
| 44 | +<tf.Tensor: shape=(2,), dtype=int8, numpy=array([-126, -124], dtype=int8)> |
| 45 | +
|
| 46 | +When adding two input values of different shapes, `Add` follows the [general |
| 47 | +broadcasting rules](https://numpy.org/doc/stable/user/basics.broadcasting.html#general-broadcasting-rules) |
| 48 | +. The two input array shapes are compared element-wise. Starting with the |
| 49 | +trailing dimensions, the two dimensions either have to be equal or one of them |
| 50 | +needs to be `1`. |
| 51 | +
|
| 52 | +For example, |
| 53 | +
|
| 54 | +>>> x = np.ones(6).reshape(1, 2, 1, 3) |
| 55 | +>>> y = np.ones(6).reshape(2, 1, 3, 1) |
| 56 | +>>> tf.add(x, y).shape.as_list() |
| 57 | +[2, 2, 3, 3] |
| 58 | +
|
| 59 | +Another example with two arrays of different dimension. |
| 60 | +
|
| 61 | +>>> x = np.ones([1, 2, 1, 4]) |
| 62 | +>>> y = np.ones([3, 4]) |
| 63 | +>>> tf.add(x, y).shape.as_list() |
| 64 | +[1, 2, 3, 4] |
| 65 | +
|
| 66 | +END |
9 | 67 | } |
0 commit comments