0
0
TensorFlowml~20 mins

Tensor shapes and reshaping in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tensor Shape Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the shape of the tensor after reshaping?
Given the following TensorFlow code, what is the shape of the tensor reshaped?
TensorFlow
import tensorflow as tf
original = tf.constant([[1, 2, 3], [4, 5, 6]])
reshaped = tf.reshape(original, [3, 2])
result_shape = reshaped.shape
A(3, 2)
B(2, 3)
C(6,)
D(1, 6)
Attempts:
2 left
💡 Hint
Remember that reshaping changes the shape but keeps the total number of elements the same.
Model Choice
intermediate
2:00remaining
Choosing the correct reshape for a batch of images
You have a batch of 10 grayscale images, each 28x28 pixels, stored in a tensor of shape (10, 28, 28). Which reshape will convert this batch into a 2D tensor suitable for feeding into a fully connected layer?
Atf.reshape(tensor, [10, 784])
Btf.reshape(tensor, [10, 28, 28, 1])
Ctf.reshape(tensor, [280, 28])
Dtf.reshape(tensor, [28, 280])
Attempts:
2 left
💡 Hint
Fully connected layers expect 2D inputs: (batch_size, features).
Metrics
advanced
2:00remaining
Calculating output shape after Conv2D layer
A Conv2D layer has input shape (batch_size, 64, 64, 3), kernel size (3, 3), stride 2, and 'valid' padding. What is the output shape (excluding batch size)?
A(33, 33, filters)
B(32, 32, filters)
C(31, 31, filters)
D(30, 30, filters)
Attempts:
2 left
💡 Hint
Output size = floor((input_size - kernel_size)/stride) + 1 for 'valid' padding.
🔧 Debug
advanced
2:00remaining
Why does this reshape cause an error?
Consider this code snippet:
import tensorflow as tf
x = tf.constant([[1, 2, 3], [4, 5, 6]])
y = tf.reshape(x, [4, 2])
Why does this raise an error?
AThe reshape function requires the new shape to have the same number of dimensions as the original.
BTensorFlow does not allow reshaping 2D tensors.
CThe new shape dimensions must be equal to the original shape dimensions.
DThe total number of elements does not match between original and new shape.
Attempts:
2 left
💡 Hint
Check how many elements are in the original tensor and the new shape.
🧠 Conceptual
expert
2:00remaining
Effect of -1 in TensorFlow reshape
What does using -1 in a TensorFlow reshape shape argument do?
AIt removes that dimension from the tensor.
BIt automatically calculates the dimension size to keep the total number of elements constant.
CIt sets that dimension size to zero.
DIt causes a runtime error because -1 is invalid.
Attempts:
2 left
💡 Hint
Think about how to reshape without manually calculating one dimension.