Tensors are like multi-dimensional arrays. Doing math with tensors helps computers learn patterns and solve problems.
Tensor math operations in TensorFlow
Start learning this pattern below
Jump into concepts and practice - no test required
import tensorflow as tf # Basic math operations result = tf.add(tensor1, tensor2) # addition result = tf.subtract(tensor1, tensor2) # subtraction result = tf.multiply(tensor1, tensor2) # element-wise multiplication result = tf.divide(tensor1, tensor2) # element-wise division # Other operations result = tf.matmul(tensor1, tensor2) # matrix multiplication result = tf.sqrt(tensor) # square root result = tf.exp(tensor) # exponentiation
Use tf.add, tf.subtract, tf.multiply, and tf.divide for element-wise math.
Use tf.matmul for matrix multiplication, which is different from element-wise multiplication.
a = tf.constant([1, 2, 3]) b = tf.constant([4, 5, 6]) sum_ab = tf.add(a, b)
a = tf.constant([[1, 2], [3, 4]]) b = tf.constant([[5, 6], [7, 8]]) product = tf.matmul(a, b)
a = tf.constant([9.0, 16.0, 25.0]) sqrt_a = tf.sqrt(a)
This program shows basic tensor math: addition, element-wise multiplication, matrix multiplication, and square root.
import tensorflow as tf # Define two tensors x = tf.constant([[1, 2], [3, 4]], dtype=tf.float32) y = tf.constant([[5, 6], [7, 8]], dtype=tf.float32) # Element-wise addition add_result = tf.add(x, y) # Element-wise multiplication mul_result = tf.multiply(x, y) # Matrix multiplication matmul_result = tf.matmul(x, y) # Square root of elements in x sqrt_x = tf.sqrt(x) # Print results print("Addition:\n", add_result.numpy()) print("Element-wise Multiplication:\n", mul_result.numpy()) print("Matrix Multiplication:\n", matmul_result.numpy()) print("Square Root of x:\n", sqrt_x.numpy())
Tensors must have compatible shapes for operations like addition or multiplication.
Matrix multiplication requires the inner dimensions to match (e.g., (2,3) x (3,4)).
Use .numpy() to convert tensors to arrays for easy printing.
Tensors are multi-dimensional arrays used in machine learning.
Tensor math operations include element-wise and matrix multiplication.
TensorFlow provides simple functions like tf.add and tf.matmul to do these operations.
Practice
tf.add(tensor1, tensor2) do?Solution
Step 1: Understand the function name and purpose
The functiontf.addis designed to add values, so it performs addition.Step 2: Check the operation type
In TensorFlow,tf.addadds two tensors element-wise, meaning it adds corresponding elements from both tensors.Final Answer:
Adds two tensors element-wise -> Option AQuick Check:
tf.add = element-wise addition [OK]
- Confusing tf.add with matrix multiplication
- Thinking tf.add subtracts tensors
- Assuming tf.add multiplies tensors
a and b in TensorFlow?Solution
Step 1: Identify the function for matrix multiplication
TensorFlow usestf.matmulspecifically for matrix multiplication.Step 2: Check other options
tf.multiplydoes element-wise multiplication,tf.addadds tensors, anda.dot(b)is invalid since tf.Tensor has no.dotmethod.Final Answer:
tf.matmul(a, b) -> Option CQuick Check:
Matrix multiply = tf.matmul [OK]
- Using tf.multiply for matrix multiplication
- Using a.dot(b) like in NumPy
- Confusing addition with multiplication
import tensorflow as tf x = tf.constant([[1, 2], [3, 4]]) y = tf.constant([[5, 6], [7, 8]]) result = tf.add(x, y) print(result.numpy())
Solution
Step 1: Understand the operation
The code usestf.addto add two 2x2 tensors element-wise.Step 2: Calculate element-wise addition
Adding corresponding elements: [[1+5, 2+6], [3+7, 4+8]] = [[6, 8], [10, 12]]Final Answer:
[[6 8] [10 12]] -> Option BQuick Check:
Element-wise add = [[6 8] [10 12]] [OK]
- Confusing element-wise add with matrix multiply
- Adding rows or columns incorrectly
- Printing tensor object instead of numpy array
import tensorflow as tf x = tf.constant([[1, 2], [3, 4]]) y = tf.constant([5, 6]) result = tf.matmul(x, y) print(result.numpy())
Solution
Step 1: Understand matrix multiplication shape rules
x is shape (2,2), y is shape (2,). TensorFlowtf.matmulrequires both inputs to be at least rank 2 tensors.Step 2: Identify the error
Passing a 1D tensorytotf.matmulcauses a shape error becausetf.matmulexpects rank >= 2 tensors.Step 3: Fix the error
Changeyto a 2D tensor with shape (2,1):tf.constant([[5], [6]])to make matrix multiplication valid.Final Answer:
Change y to a 2x1 tensor: tf.constant([[5], [6]]) -> Option DQuick Check:
tf.matmul requires rank 2 tensors [OK]
- Assuming 1D tensors cause no shape errors in matmul
- Unnecessarily reshaping y to 2D
- Confusing matmul with element-wise operations
a = tf.constant([[1, 2], [3, 4]])b = tf.constant([[2, 0], [1, 2]])Which TensorFlow operation will give the element-wise product of
a and b?Solution
Step 1: Understand element-wise product
Element-wise product multiplies each element ofawith the corresponding element ofb.Step 2: Identify TensorFlow function for element-wise multiplication
tf.multiplyperforms element-wise multiplication, whiletf.matmuldoes matrix multiplication.Final Answer:
tf.multiply(a, b) -> Option AQuick Check:
Element-wise multiply = tf.multiply [OK]
- Using tf.matmul instead of tf.multiply
- Confusing addition with multiplication
- Using tf.tensordot incorrectly
