Constant Creates Tensor: Basic Math Operations

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Tensor basic methods

9
Basic Math Operations
Function Description
add(x, y, name=None) Adds two tensors

subtract(x, y, name=None) Subtracts two tensors

multiply(x, y, name=None) Multiplies two tensors

divide(x, y, name=None) Divides the elements of two tensors

div(x, y, name=None) Divides the elements of two tensors

constant() Creates tensor


…add_n(inputs, name=None) Adds multiple tensors

scalar_mul(scalar, x) Scales a tensor by a scalar value

mod(x, y, name=None) Performs the modulo operation

abs(x, name=None) Computes the absolute value

negative(x, name=None) Negates the tensor's elements


Extracts the signs of the tensor’s
sign(x, name=None)
element
reciprocal(x, name=None) Computes the reciprocals
Math Operation Examples
a = tf.constant([3., 3., 3.])

b = tf.constant([2., 2., 2.])

sum = tf.add(a, b) # [ 5. 5. 5. ]

diff = tf.subtract(a, b) # [ 1. 1. 1. ]

prod = tf.multiply(a, b) # [ 6. 6. 6. ]

a = tf.constant([3, 3, 3])

b = tf.constant([2, 2, 2])

div1 = tf.divide(a, b) # [ 1.5 1.5 1.5 ]

div2 = a / b # [ 1.5 1.5 1.5 ]

div3 = tf.div(a, b) #[111]


Rounding & ComparisonDescription
Function
Rounds to the nearest integer, rounding
round(x, name=None)
up if there are two nearest integers
Rounds to the nearest integer, rounding
rint(x, name=None) to the nearest even integer if there are
two nearest integers
Returns the smallest integer greater
ceil(x, name=None)
than the value
Returns the greatest integer less than
floor(x, name=None)
the value
Returns a tensor containing the larger
maximum(x, y, name=None)
element of each input tensor

Returns a tensor containing the
minimum(x, y, name=None) smaller element of each input
tensor

argmax(x, axis=None, Returns the index of the


name=None, dimension=None) greatest element in the tensor

argmin(x, axis=None, Returns the index of the


name=None, dimension=None) smallest element in the tensor
Rounding & Comparison Examples
t = tf.constant([-6.5, -3.5, 3.5, 6.5])
r1 = tf.round(t) # [-6. -4. 4. 6.]
r2 = tf.rint(t) # [-6. -4. 4. 6.]
r3 = tf.ceil(t) # [-6. -3. 4. 7.]
r4 = tf.floor(t) # [-7. -4. 3. 6.]
t1 = tf.constant([0, -2, 4, 6])

t2 = tf.constant([[1, 3], [7, 2]])

r1 = tf.argmin(t1) #1

r2 = tf.argmax(t2) #[10]
Rank 0 Tensor
- Also known as Scalar
- Contains a single value and no axes
- E.g. 34

Sample code
- rank_0 = tf.constant(34)
- print(rank_0)

Output
- tf.Tensor(34, shape=(), dtype=int32)
Rank 1 Tensor
- Also known as Vector
- Contains a list of values and 1 axis
- E.g. (30, 31, 32)

Sample code
rank_1 = tf.constant([30, 31, 32])
print(rank_1)

Output
tf.Tensor([30 31 32], shape=(3,), dtype=int32)
Rank 2 Tensor
- Also known as Matrix
- Sequence of arrays with 2 axis
- E.g. (30, 31, 32)

Sample code
rank_1 = tf.constant([[30, 31, 32], Output
[40, 41, 42],
tf.Tensor(
[50, 51, 52]]) [[30 31 32]
print(rank_1) [40 41 42]
[50 51 52]], shape=(3, 3), dtype=int32)
Visualizing Rank 0, 1 & 2 Tensors

You might also like