Bird
Raised Fist0
TensorFlowml~20 mins

Tensor creation (constant, variable, zeros, ones) in TensorFlow - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Tensor Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What is the output shape of this tensor?
Given the following TensorFlow code, what is the shape of the tensor t?
TensorFlow
import tensorflow as tf
t = tf.constant([[1, 2, 3], [4, 5, 6]])
shape = t.shape
A(6,)
B(3, 2)
C(2, 3)
D(1, 6)
Attempts:
2 left
💡 Hint
Think about how many rows and columns the list has.
Model Choice
intermediate
1:30remaining
Which tensor creation method creates a mutable tensor?
In TensorFlow, which method creates a tensor whose values can be changed after creation?
Atf.Variable()
Btf.zeros()
Ctf.constant()
Dtf.ones()
Attempts:
2 left
💡 Hint
Mutable means you can change the values after creation.
Predict Output
advanced
2:00remaining
What is the output of this code?
What does the following TensorFlow code print?
TensorFlow
import tensorflow as tf
x = tf.zeros(shape=(2, 3), dtype=tf.int32)
print(x.numpy())
A
[[1 1 1]
 [1 1 1]]
B
[[0. 0. 0.]
 [0. 0. 0.]]
C
[[1. 1. 1.]
 [1. 1. 1.]]
D
[[0 0 0]
 [0 0 0]]
Attempts:
2 left
💡 Hint
Check the dtype and the function used to create the tensor.
Hyperparameter
advanced
1:30remaining
Which argument changes the data type of a tensor?
When creating a tensor with tf.ones(), which argument sets the data type of the tensor elements?
Adtype
Bsize
Cvalue
Dshape
Attempts:
2 left
💡 Hint
This argument controls if the tensor holds integers, floats, or other types.
🔧 Debug
expert
2:00remaining
Why does this code raise an error?
Examine the code below. Why does it raise an error?
TensorFlow
import tensorflow as tf
v = tf.Variable(tf.constant([1, 2, 3]))
v.assign([4, 5])
Atf.Variable cannot be created from tf.constant.
BThe assign method requires the new value to have the same shape as the variable.
CThe list passed to assign must be a tensor, not a Python list.
DThe variable v is immutable and cannot be assigned new values.
Attempts:
2 left
💡 Hint
Check the shapes of the original variable and the new value.

Practice

(1/5)
1. Which TensorFlow function creates a tensor with arbitrary fixed values that cannot be changed later?
easy
A. tf.ones
B. tf.Variable
C. tf.zeros
D. tf.constant

Solution

  1. Step 1: Understand tensor mutability

    tf.constant creates tensors with fixed values that cannot be changed after creation.
  2. Step 2: Compare with other functions

    tf.Variable creates tensors that can be changed, while tf.zeros and tf.ones create tensors filled with zeros or ones but are also constants by default.
  3. Final Answer:

    tf.constant -> Option D
  4. Quick Check:

    Fixed tensor = tf.constant [OK]
Hint: Fixed tensors use tf.constant, variables use tf.Variable [OK]
Common Mistakes:
  • Confusing tf.constant with tf.Variable
  • Thinking tf.zeros creates changeable tensors
  • Assuming tf.ones creates variables
2. Which of the following is the correct syntax to create a TensorFlow variable with initial value 5?
easy
A. tf.zeros(5)
B. tf.Variable(5)
C. tf.constant(5)
D. tf.ones(5)

Solution

  1. Step 1: Identify variable creation syntax

    tf.Variable(5) creates a variable tensor with initial value 5.
  2. Step 2: Check other options

    tf.constant(5) creates a constant, not a variable. tf.zeros(5) and tf.ones(5) create tensors of shape 5, not a single value 5.
  3. Final Answer:

    tf.Variable(5) -> Option B
  4. Quick Check:

    Variable init = tf.Variable(value) [OK]
Hint: Variables use tf.Variable(value), constants use tf.constant(value) [OK]
Common Mistakes:
  • Using tf.constant instead of tf.Variable for changeable tensors
  • Using tf.zeros or tf.ones with a single number instead of shape tuple
  • Confusing value and shape in function arguments
3. What is the output of this code?
import tensorflow as tf
x = tf.zeros((2, 3))
print(x.numpy())
medium
A. [[1 1 1] [1 1 1]]
B. [5 5 5 5 5 5]
C. [[0. 0. 0.] [0. 0. 0.]]
D. Error: shape must be a single integer

Solution

  1. Step 1: Understand tf.zeros with shape (2, 3)

    This creates a 2-row, 3-column tensor filled with zeros.
  2. Step 2: Print tensor as numpy array

    Calling .numpy() converts tensor to numpy array, showing zeros in 2x3 shape.
  3. Final Answer:

    [[0. 0. 0.] [0. 0. 0.]] -> Option C
  4. Quick Check:

    tf.zeros((2,3)) = 2x3 zeros [OK]
Hint: tf.zeros(shape) creates zeros tensor of given shape [OK]
Common Mistakes:
  • Confusing tf.zeros with tf.ones output
  • Misunderstanding shape argument as single integer
  • Expecting a flat list instead of 2D array
4. The following code throws an error. What is the mistake?
import tensorflow as tf
x = tf.ones(3, 4)
print(x)
medium
A. tf.ones expects a single shape tuple, not separate integers
B. tf.ones cannot create tensors with more than 2 dimensions
C. tf.ones requires dtype argument
D. tf.ones only creates scalar tensors

Solution

  1. Step 1: Check tf.ones argument format

    tf.ones expects a single shape argument as a tuple, e.g., (3, 4), not two separate integers.
  2. Step 2: Identify error cause

    Passing two integers separately causes a TypeError because the function signature expects one shape argument.
  3. Final Answer:

    tf.ones expects a single shape tuple, not separate integers -> Option A
  4. Quick Check:

    Shape must be tuple for tf.ones [OK]
Hint: Pass shape as tuple like (3,4) to tf.ones [OK]
Common Mistakes:
  • Passing shape as separate arguments instead of tuple
  • Assuming dtype is mandatory
  • Thinking tf.ones only creates scalars
5. You want to create a TensorFlow variable initialized with a 3x3 identity matrix (ones on diagonal, zeros elsewhere). Which code correctly does this?
hard
A. tf.Variable(tf.eye(3))
B. tf.Variable(tf.ones((3,3)))
C. tf.Variable(tf.zeros((3,3)))
D. tf.Variable(tf.constant(3))

Solution

  1. Step 1: Identify identity matrix creation

    tf.eye(3) creates a 3x3 identity matrix with ones on the diagonal and zeros elsewhere.
  2. Step 2: Wrap identity matrix in variable

    Using tf.Variable makes this tensor changeable during training or updates.
  3. Step 3: Check other options

    tf.ones and tf.zeros create all ones or zeros, not identity. tf.constant(3) creates scalar 3, not matrix.
  4. Final Answer:

    tf.Variable(tf.eye(3)) -> Option A
  5. Quick Check:

    Identity matrix = tf.eye + tf.Variable [OK]
Hint: Use tf.eye(shape) inside tf.Variable for identity matrix [OK]
Common Mistakes:
  • Using tf.ones or tf.zeros instead of tf.eye for identity
  • Passing scalar to tf.Variable instead of matrix
  • Forgetting to wrap tensor in tf.Variable for mutability