Bird
Raised Fist0
TensorFlowml~10 mins

Input shape specification in TensorFlow - Interactive Code Practice

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 specify the input shape for a Keras model.

TensorFlow
model = tf.keras.Sequential([
    tf.keras.layers.InputLayer(input_shape=[1]),
    tf.keras.layers.Dense(10, activation='relu')
])
Drag options to blanks, or click blank then click option'
A(28, 28)
B[28, 28]
C28, 28
D28
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a tuple for input_shape.
Including the batch size in the input shape.
2fill in blank
medium

Complete the code to define an input layer with a 3D input shape.

TensorFlow
inputs = tf.keras.Input(shape=[1])
Drag options to blanks, or click blank then click option'
A(64, 64)
B(64, 64, 3)
C[64, 64, 3]
D64, 64, 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a tuple.
Omitting the color channels dimension.
3fill in blank
hard

Fix the error in the input shape specification for a grayscale image.

TensorFlow
model = tf.keras.Sequential([
    tf.keras.layers.InputLayer(input_shape=[1]),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(10, activation='softmax')
])
Drag options to blanks, or click blank then click option'
A(28, 28, 1)
B28, 28, 1
C[28, 28, 1]
D(28, 28)
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the channel dimension for grayscale images.
Using a list instead of a tuple.
4fill in blank
hard

Fill both blanks to create a model input layer and a dense layer with correct input shape.

TensorFlow
inputs = tf.keras.Input(shape=[1])
x = tf.keras.layers.Dense(32, activation=[2])(inputs)
Drag options to blanks, or click blank then click option'
A(100,)
B'relu'
C'sigmoid'
D(100, 1)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a 2D shape for 1D input data.
Using 'sigmoid' activation in hidden layers instead of 'relu'.
5fill in blank
hard

Fill all three blanks to define a model with input shape, flatten layer, and output layer activation.

TensorFlow
model = tf.keras.Sequential([
    tf.keras.layers.InputLayer(input_shape=[1]),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(10, activation=[2])
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=[[3]])
Drag options to blanks, or click blank then click option'
A(28, 28)
B'softmax'
C'accuracy'
D(28, 28, 3)
Attempts:
3 left
💡 Hint
Common Mistakes
Using color image shape for grayscale input.
Using 'relu' instead of 'softmax' in output layer.
Omitting metrics or using incorrect metric names.

Practice

(1/5)
1. What does the input_shape parameter specify in a TensorFlow Keras model?
easy
A. The size and format of the input data the model expects
B. The number of layers in the model
C. The learning rate for training
D. The number of output classes

Solution

  1. Step 1: Understand the role of input_shape

    The input_shape tells the model what size and type of data it will receive as input.
  2. Step 2: Differentiate from other parameters

    Other parameters like number of layers, learning rate, or output classes do not describe input data format.
  3. Final Answer:

    The size and format of the input data the model expects -> Option A
  4. Quick Check:

    input_shape = data size/type [OK]
Hint: Input shape defines data size, not layers or learning rate [OK]
Common Mistakes:
  • Confusing input_shape with number of layers
  • Thinking input_shape sets learning rate
  • Mixing input_shape with output classes
2. Which of the following is the correct way to specify an input shape for a model expecting 28x28 grayscale images in TensorFlow?
easy
A. tf.keras.layers.Input(shape=(28, 28))
B. tf.keras.layers.Input(shape=(28, 28, 1))
C. tf.keras.layers.Input(shape=(1, 28, 28))
D. tf.keras.layers.Input(shape=(784,))

Solution

  1. Step 1: Identify the correct shape for grayscale images

    Grayscale images have height, width, and 1 channel, so shape is (28, 28, 1).
  2. Step 2: Check each option

    tf.keras.layers.Input(shape=(28, 28, 1)) matches (28, 28, 1). tf.keras.layers.Input(shape=(28, 28)) misses channel dimension. tf.keras.layers.Input(shape=(1, 28, 28)) has wrong channel position. tf.keras.layers.Input(shape=(784,)) flattens input, not raw shape.
  3. Final Answer:

    tf.keras.layers.Input(shape=(28, 28, 1)) -> Option B
  4. Quick Check:

    Grayscale image shape = (height, width, 1) [OK]
Hint: Grayscale images need channel=1 as last dimension [OK]
Common Mistakes:
  • Omitting the channel dimension
  • Placing channel dimension first incorrectly
  • Using flattened input shape instead of 2D+channel
3. What will be the output shape of the following TensorFlow model's first layer?
model = tf.keras.Sequential([
  tf.keras.layers.Input(shape=(32, 32, 3)),
  tf.keras.layers.Conv2D(16, 3)
])
medium
A. (None, 32, 32, 3)
B. (None, 32, 32, 16)
C. (None, 30, 30, 3)
D. (None, 30, 30, 16)

Solution

  1. Step 1: Understand Conv2D output shape calculation

    Conv2D with kernel size 3 and default 'valid' padding reduces height and width by 2 (3-1) each.
  2. Step 2: Calculate output dimensions

    Input shape is (32, 32, 3). Output height and width = 32 - 3 + 1 = 30. Number of filters = 16, so output shape is (None, 30, 30, 16).
  3. Final Answer:

    (None, 30, 30, 16) -> Option D
  4. Quick Check:

    Conv2D valid padding reduces size by kernel-1 [OK]
Hint: Valid padding shrinks size by kernel_size - 1 [OK]
Common Mistakes:
  • Assuming output size equals input size without padding
  • Confusing number of channels with number of filters
  • Ignoring batch size dimension (None)
4. Identify the error in this TensorFlow input layer code:
input_layer = tf.keras.layers.Input(shape=28, 28, 1)
medium
A. The shape argument should be a single tuple, not separate values
B. The input layer must specify batch size explicitly
C. The channel dimension should be first, not last
D. Input layers cannot have 3D shapes

Solution

  1. Step 1: Check the syntax of shape argument

    The shape parameter must be a single tuple, e.g., (28, 28, 1), not separate arguments.
  2. Step 2: Verify other options

    Batch size is optional and not required here. Channel last is standard. Input layers can have 3D shapes for images.
  3. Final Answer:

    The shape argument should be a single tuple, not separate values -> Option A
  4. Quick Check:

    Shape must be tuple like (28, 28, 1) [OK]
Hint: Use parentheses to group shape dimensions as a tuple [OK]
Common Mistakes:
  • Passing shape dimensions as separate arguments
  • Forcing batch size in input shape
  • Misplacing channel dimension
5. You want to build a model that accepts variable-length sequences of 10 features each. Which input shape specification is correct for the first layer?
hard
A. tf.keras.layers.Input(shape=(10,))
B. tf.keras.layers.Input(shape=(10, None))
C. tf.keras.layers.Input(shape=(None, 10))
D. tf.keras.layers.Input(shape=(None,))

Solution

  1. Step 1: Understand variable-length sequences

    Variable-length means the sequence length is unknown, so use None for that dimension.
  2. Step 2: Identify feature dimension position

    Each sequence element has 10 features, so feature dimension is fixed at 10, sequence length is variable.
  3. Step 3: Match shape to (sequence_length, features)

    The correct shape is (None, 10), meaning variable sequence length and fixed 10 features per step.
  4. Final Answer:

    tf.keras.layers.Input(shape=(None, 10)) -> Option C
  5. Quick Check:

    Variable length = None in first dimension [OK]
Hint: Use None for variable dimension, fixed size for features [OK]
Common Mistakes:
  • Swapping sequence length and feature dimensions
  • Using fixed size for variable-length dimension
  • Omitting feature dimension