Bird
Raised Fist0
TensorFlowml~10 mins

Why tensors are the fundamental data unit in TensorFlow - Test Your Understanding

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a 2D tensor with TensorFlow.

TensorFlow
import tensorflow as tf

matrix = tf.constant([1])
print(matrix)
Drag options to blanks, or click blank then click option'
A[[1, 2], [3, 4]]
B[1, 2, 3, 4]
C1, 2, 3, 4
Dtf.constant([1, 2, 3, 4])
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a flat list instead of a nested list for a 2D tensor.
Using commas without brackets which causes syntax errors.
2fill in blank
medium

Complete the code to get the shape of a tensor.

TensorFlow
import tensorflow as tf

tensor = tf.constant([[1, 2], [3, 4], [5, 6]])
shape = tensor.[1]
print(shape)
Drag options to blanks, or click blank then click option'
Asize
Bndim
Crank
Dshape
Attempts:
3 left
💡 Hint
Common Mistakes
Using size which returns total elements, not shape.
Using rank which returns the number of dimensions.
3fill in blank
hard

Fix the error in the code to multiply two tensors element-wise.

TensorFlow
import tensorflow as tf

a = tf.constant([1, 2, 3])
b = tf.constant([4, 5, 6])
result = a [1] b
print(result)
Drag options to blanks, or click blank then click option'
A*
B@
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ performs matrix multiplication (dot product) instead of element-wise.
Using + or - changes the operation to addition or subtraction.
4fill in blank
hard

Fill both blanks to create a tensor of zeros with shape (3, 4) and type float32.

TensorFlow
import tensorflow as tf

zeros_tensor = tf.[1](shape=[2], dtype=tf.float32)
print(zeros_tensor)
Drag options to blanks, or click blank then click option'
Azeros
Bones
C(3, 4)
D[3, 4]
Attempts:
3 left
💡 Hint
Common Mistakes
Using tf.ones instead of tf.zeros.
Using a list for shape instead of a tuple.
5fill in blank
hard

Fill all three blanks to create a tensor from a list, reshape it to (2, 3), and print its new shape.

TensorFlow
import tensorflow as tf

list_data = [1, 2, 3, 4, 5, 6]
tensor = tf.constant([1])
reshaped = tf.reshape(tensor, [2])
print(reshaped.[3])
Drag options to blanks, or click blank then click option'
Alist_data
B[2, 3]
Cshape
D(2, 3)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a tuple for reshape shape.
Trying to call reshaped.shape() (it's a property, not a method).

Practice

(1/5)
1. Why are tensors considered the fundamental data unit in TensorFlow?
easy
A. Because they can represent data in multiple dimensions efficiently
B. Because they are only used for storing images
C. Because they are simple lists of numbers with no structure
D. Because they only work with text data

Solution

  1. Step 1: Understand the role of tensors in data representation

    Tensors can hold numbers arranged in many dimensions, like scalars, vectors, matrices, or higher-dimensional arrays.
  2. Step 2: Recognize why this flexibility matters in TensorFlow

    This multi-dimensional structure allows TensorFlow to efficiently represent and process different types of data such as images, text, and more.
  3. Final Answer:

    Because they can represent data in multiple dimensions efficiently -> Option A
  4. Quick Check:

    Multi-dimensional data = fundamental tensor use [OK]
Hint: Tensors hold multi-dimensional data, not just simple lists [OK]
Common Mistakes:
  • Thinking tensors only store images
  • Confusing tensors with simple lists
  • Believing tensors only work with text
2. Which of the following is the correct way to create a 2D tensor in TensorFlow?
easy
A. tf.array([1, 2], [3, 4])
B. tf.constant([[1, 2], [3, 4]])
C. tf.tensor([1, 2, 3, 4])
D. tf.list([[1, 2], [3, 4]])

Solution

  1. Step 1: Identify the correct TensorFlow function for tensor creation

    TensorFlow uses tf.constant() to create tensors from nested lists or arrays.
  2. Step 2: Check the syntax for creating a 2D tensor

    Passing a nested list like [[1, 2], [3, 4]] to tf.constant() creates a 2D tensor with shape (2, 2).
  3. Final Answer:

    tf.constant([[1, 2], [3, 4]]) -> Option B
  4. Quick Check:

    tf.constant with nested list = 2D tensor [OK]
Hint: Use tf.constant() with nested lists for multi-dimensional tensors [OK]
Common Mistakes:
  • Using non-existent tf.tensor() function
  • Trying tf.array() which is not a TensorFlow function
  • Using tf.list() which does not create tensors
3. What will be the output shape of the following TensorFlow tensor?
import tensorflow as tf
t = tf.constant([[[1], [2]], [[3], [4]]])
print(t.shape)
medium
A. (2, 2, 1)
B. (2, 1, 2)
C. (1, 2, 2)
D. (3, 2)

Solution

  1. Step 1: Analyze the nested list structure used to create the tensor

    The tensor is created from [[[1], [2]], [[3], [4]]], which is a list of 2 elements, each containing 2 elements, each containing 1 element.
  2. Step 2: Determine the shape based on the nesting levels

    The outermost list has 2 elements, each inner list has 2 elements, and each innermost list has 1 element, so shape is (2, 2, 1).
  3. Final Answer:

    (2, 2, 1) -> Option A
  4. Quick Check:

    Nested list depth = tensor shape (2, 2, 1) [OK]
Hint: Count nested list levels and lengths for tensor shape [OK]
Common Mistakes:
  • Mixing up order of dimensions
  • Ignoring innermost list size
  • Assuming shape is (3, 2) from total elements
4. Identify the error in this TensorFlow code snippet that tries to create a tensor:
import tensorflow as tf
t = tf.constant([1, 2, 3], shape=(2, 2))
print(t)
medium
A. TensorFlow does not support 2D tensors
B. tf.constant cannot create tensors from lists
C. The print statement is missing parentheses
D. The shape (2, 2) does not match the number of elements (3)

Solution

  1. Step 1: Check the number of elements and the specified shape

    The list has 3 elements, but the shape (2, 2) requires 4 elements (2*2=4).
  2. Step 2: Understand TensorFlow's shape requirement

    TensorFlow requires the total number of elements to match the product of the shape dimensions exactly.
  3. Final Answer:

    The shape (2, 2) does not match the number of elements (3) -> Option D
  4. Quick Check:

    Elements count must match shape product [OK]
Hint: Shape product must equal total elements in data [OK]
Common Mistakes:
  • Ignoring mismatch between data size and shape
  • Thinking tf.constant can't use lists
  • Confusing print syntax errors
5. You have image data stored as a list of 100 images, each image is 28x28 pixels grayscale. How should you represent this data as a tensor in TensorFlow for model input?
hard
A. A 3D tensor with shape (100, 28, 28)
B. A 2D tensor with shape (100, 784)
C. A 4D tensor with shape (100, 28, 28, 1)
D. A 1D tensor with shape (100)

Solution

  1. Step 1: Understand the data dimensions for grayscale images

    Each image is 28x28 pixels with 1 color channel (grayscale), so each image is 3D with shape (28, 28, 1).
  2. Step 2: Combine all images into a batch tensor

    Stacking 100 images creates a 4D tensor with shape (100, 28, 28, 1), where 100 is the batch size.
  3. Final Answer:

    A 4D tensor with shape (100, 28, 28, 1) -> Option C
  4. Quick Check:

    Batch + height + width + channels = 4D tensor [OK]
Hint: Include channel dimension for grayscale images in tensor shape [OK]
Common Mistakes:
  • Using 3D tensor without channel dimension
  • Flattening images to 2D without channels
  • Using 1D tensor ignoring image size