Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a tensor in TensorFlow?
A tensor is a multi-dimensional array used to represent data in TensorFlow. It can have any number of dimensions, like scalars (0D), vectors (1D), matrices (2D), or higher.
Click to reveal answer
beginner
How do you add two tensors in TensorFlow?
You can add two tensors using tf.add(tensor1, tensor2) or simply using the + operator if tensors have the same shape.
Click to reveal answer
intermediate
What does broadcasting mean in tensor operations?
Broadcasting lets TensorFlow automatically expand smaller tensors to match the shape of larger tensors during math operations, so you don't have to manually reshape them.
Click to reveal answer
beginner
How do you multiply two tensors element-wise in TensorFlow?
Use tf.multiply(tensor1, tensor2) or the * operator to multiply tensors element by element, assuming they have compatible shapes.
Click to reveal answer
intermediate
What function computes the dot product of two tensors in TensorFlow?
Use tf.tensordot(tensor1, tensor2, axes) to compute the dot product along specified axes. For matrix multiplication, tf.matmul is commonly used.
Click to reveal answer
Which TensorFlow function adds two tensors element-wise?
Atf.add()
Btf.matmul()
Ctf.multiply()
Dtf.reshape()
✗ Incorrect
tf.add() adds two tensors element-wise.
What happens if you add tensors of different shapes without broadcasting?
AThe smaller tensor is ignored
BTensorFlow automatically reshapes them
CAn error occurs
DThe operation returns zeros
✗ Incorrect
TensorFlow throws an error if shapes are incompatible and broadcasting is not possible.
Which operation performs element-wise multiplication of tensors?
Atf.tensordot()
Btf.matmul()
Ctf.add()
Dtf.multiply()
✗ Incorrect
tf.multiply() multiplies tensors element-wise.
What does tf.matmul() do?
AMatrix multiplication
BReshape tensor
CElement-wise addition
DCalculate mean
✗ Incorrect
tf.matmul() performs matrix multiplication between two tensors.
Broadcasting helps by:
AReducing tensor size
BAutomatically expanding smaller tensors to match larger ones
CChanging tensor data types
DSorting tensor elements
✗ Incorrect
Broadcasting automatically expands smaller tensors to compatible shapes for operations.
Explain how broadcasting works in TensorFlow tensor math operations.
Think about how smaller arrays can be used with bigger ones without errors.
You got /3 concepts.
Describe the difference between element-wise multiplication and matrix multiplication in TensorFlow.
One multiplies matching elements, the other combines rows and columns.
You got /3 concepts.
Practice
(1/5)
1. What does the TensorFlow function tf.add(tensor1, tensor2) do?
easy
A. Adds two tensors element-wise
B. Multiplies two tensors element-wise
C. Performs matrix multiplication of two tensors
D. Subtracts the second tensor from the first
Solution
Step 1: Understand the function name and purpose
The function tf.add is designed to add values, so it performs addition.
Step 2: Check the operation type
In TensorFlow, tf.add adds two tensors element-wise, meaning it adds corresponding elements from both tensors.
Final Answer:
Adds two tensors element-wise -> Option A
Quick Check:
tf.add = element-wise addition [OK]
Hint: Add means element-wise sum, not matrix multiply [OK]
Common Mistakes:
Confusing tf.add with matrix multiplication
Thinking tf.add subtracts tensors
Assuming tf.add multiplies tensors
2. Which of the following is the correct syntax to perform matrix multiplication of two tensors a and b in TensorFlow?
easy
A. tf.multiply(a, b)
B. tf.add(a, b)
C. tf.matmul(a, b)
D. a.dot(b)
Solution
Step 1: Identify the function for matrix multiplication
TensorFlow uses tf.matmul specifically for matrix multiplication.
Step 2: Check other options
tf.multiply does element-wise multiplication, tf.add adds tensors, and a.dot(b) is invalid since tf.Tensor has no .dot method.
Final Answer:
tf.matmul(a, b) -> Option C
Quick Check:
Matrix multiply = tf.matmul [OK]
Hint: Matrix multiply uses tf.matmul, not tf.multiply [OK]
Common Mistakes:
Using tf.multiply for matrix multiplication
Using a.dot(b) like in NumPy
Confusing addition with multiplication
3. What is the output of the following TensorFlow code?
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())
medium
A. [[6 12]
[10 32]]
B. [[6 8]
[10 12]]
C. [[5 12]
[21 32]]
D. [[1 2]
[3 4]]
Solution
Step 1: Understand the operation
The code uses tf.add to add two 2x2 tensors element-wise.
Assuming 1D tensors cause no shape errors in matmul
Unnecessarily reshaping y to 2D
Confusing matmul with element-wise operations
5. You have two tensors: 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?
hard
A. tf.multiply(a, b)
B. tf.matmul(a, b)
C. tf.add(a, b)
D. tf.tensordot(a, b, axes=1)
Solution
Step 1: Understand element-wise product
Element-wise product multiplies each element of a with the corresponding element of b.
Step 2: Identify TensorFlow function for element-wise multiplication
tf.multiply performs element-wise multiplication, while tf.matmul does matrix multiplication.
Final Answer:
tf.multiply(a, b) -> Option A
Quick Check:
Element-wise multiply = tf.multiply [OK]
Hint: Use tf.multiply for element-wise product, not tf.matmul [OK]