Professional Documents
Culture Documents
Constant Creates Tensor: Basic Math Operations
Constant Creates Tensor: Basic Math Operations
Constant Creates Tensor: Basic Math Operations
9
Basic Math Operations
Function Description
add(x, y, name=None) Adds two tensors
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])
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