Bird
Raised Fist0
TensorFlowml~10 mins

Dense (fully connected) layers 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 create a dense layer with 10 units.

TensorFlow
layer = tf.keras.layers.Dense([1])
Drag options to blanks, or click blank then click option'
A0
BNone
C10
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or None will cause errors or no neurons to be created.
Choosing a smaller number like 5 does not meet the requirement.
2fill in blank
medium

Complete the code to add a ReLU activation to the dense layer.

TensorFlow
layer = tf.keras.layers.Dense(10, activation=[1])
Drag options to blanks, or click blank then click option'
A'relu'
B'sigmoid'
C'softmax'
D'tanh'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sigmoid' or 'softmax' activates differently and are usually for output layers.
Forgetting quotes around the activation name causes syntax errors.
3fill in blank
hard

Fix the error in the code by completing the missing argument for input shape.

TensorFlow
model = tf.keras.Sequential([
    tf.keras.layers.Dense(10, activation='relu', input_shape=[1])
])
Drag options to blanks, or click blank then click option'
A(10,)
B{10}
C[10]
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using an integer alone causes errors because shape must be a tuple.
Using list or set brackets is invalid for input_shape.
4fill in blank
hard

Fill both blanks to create a model with two dense layers: first with 64 units and ReLU activation, second with 10 units and softmax activation.

TensorFlow
model = tf.keras.Sequential([
    tf.keras.layers.Dense([1], activation='relu'),
    tf.keras.layers.Dense([2], activation='softmax')
])
Drag options to blanks, or click blank then click option'
A64
B10
C32
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the number of units between layers.
Using incorrect numbers like 32 or 5 which do not match the task.
5fill in blank
hard

Fill all three blanks to create a dense layer with 128 units, 'tanh' activation, and use it as the first layer with input shape (20,).

TensorFlow
model = tf.keras.Sequential([
    tf.keras.layers.Dense([1], activation=[2], input_shape=[3])
])
Drag options to blanks, or click blank then click option'
A128
B'tanh'
C(20,)
D'relu'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong activation like 'relu' instead of 'tanh'.
Not using a tuple for input_shape.
Using wrong number of units.

Practice

(1/5)
1. What does a Dense (fully connected) layer do in a neural network?
easy
A. Does not connect any neurons, only passes data through
B. Connects every input neuron to every output neuron with weights
C. Connects neurons randomly without weights
D. Only connects input neurons to output neurons with zero weights

Solution

  1. Step 1: Understand the role of Dense layers

    A Dense layer connects each input neuron to every output neuron using weights and biases to learn patterns.
  2. Step 2: Compare options with Dense layer behavior

    Only Connects every input neuron to every output neuron with weights correctly describes this full connection with weights; others are incorrect or incomplete.
  3. Final Answer:

    Connects every input neuron to every output neuron with weights -> Option B
  4. Quick Check:

    Dense layer = full weighted connections [OK]
Hint: Dense means all inputs connect to all outputs [OK]
Common Mistakes:
  • Thinking Dense layers connect neurons randomly
  • Believing Dense layers have zero weights
  • Assuming Dense layers do not connect neurons
2. Which of the following is the correct way to add a Dense layer with 10 neurons and ReLU activation in TensorFlow?
easy
A. tf.keras.layers.Dense(10, activation='relu')
B. tf.keras.DenseLayer(10, activation='relu')
C. tf.layers.Dense(activation='relu', units=10)
D. tf.keras.layers.Dense(activation='relu', neurons=10)

Solution

  1. Step 1: Recall TensorFlow Dense layer syntax

    The correct syntax is tf.keras.layers.Dense(units, activation='function').
  2. Step 2: Match options to correct syntax

    tf.keras.layers.Dense(10, activation='relu') matches this exactly. Others have wrong class names or parameter names.
  3. Final Answer:

    tf.keras.layers.Dense(10, activation='relu') -> Option A
  4. Quick Check:

    Correct Dense syntax = tf.keras.layers.Dense(10, activation='relu') [OK]
Hint: Use tf.keras.layers.Dense(units, activation) [OK]
Common Mistakes:
  • Using wrong class name like DenseLayer
  • Swapping parameter names (neurons vs units)
  • Placing activation before units
3. What will be the output shape of this model?
model = tf.keras.Sequential([
  tf.keras.layers.Dense(5, input_shape=(3,)),
  tf.keras.layers.Dense(2)
])
output = model(tf.constant([[1.0, 2.0, 3.0]]))
print(output.shape)
medium
A. (3, 2)
B. (1, 5)
C. (1, 2)
D. (3, 5)

Solution

  1. Step 1: Analyze model layers and input shape

    Input shape is (3,), first Dense outputs 5 units, second Dense outputs 2 units.
  2. Step 2: Determine output shape after second Dense

    Batch size is 1 (one input), final output shape is (1, 2).
  3. Final Answer:

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

    Output shape = (batch_size, last layer units) = (1, 2) [OK]
Hint: Output shape = (batch, last Dense units) [OK]
Common Mistakes:
  • Confusing input shape with output shape
  • Mixing up units of first and second Dense layers
  • Ignoring batch dimension
4. Identify the error in this code snippet:
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(10, input_shape=(4,)))
model.add(tf.keras.layers.Dense(5, activation='relu'))
model.add(tf.keras.layers.Dense(1))
model.compile(optimizer='adam', loss='mse')
model.fit(x_train, y_train, epochs=5)
medium
A. Loss function 'mse' is invalid
B. Input shape should be specified in the first layer only
C. Missing activation in the first Dense layer
D. No error, code is correct

Solution

  1. Step 1: Check Dense layer usage and input shape

    Input shape is correctly specified in the first Dense layer only.
  2. Step 2: Verify loss function and activation usage

    Loss 'mse' is valid for regression; activation in second layer is fine; first layer activation is optional.
  3. Final Answer:

    No error, code is correct -> Option D
  4. Quick Check:

    Code syntax and usage are correct [OK]
Hint: Input shape only in first layer; 'mse' is valid loss [OK]
Common Mistakes:
  • Thinking activation is mandatory in every Dense layer
  • Specifying input_shape in multiple layers
  • Believing 'mse' is invalid loss
5. You want to build a model to classify images into 3 categories. Which Dense layer setup is best for the output layer?
hard
A. Dense(3, activation='softmax')
B. Dense(1, activation='sigmoid')
C. Dense(3, activation='relu')
D. Dense(3)

Solution

  1. Step 1: Understand classification output needs

    For 3 categories, output layer should have 3 units, one per class.
  2. Step 2: Choose activation for multi-class classification

    Softmax activation outputs probabilities summing to 1, ideal for multi-class.
  3. Step 3: Evaluate options

    Dense(3, activation='softmax') uses 3 units with softmax, perfect for 3-class classification; others are unsuitable.
  4. Final Answer:

    Dense(3, activation='softmax') -> Option A
  5. Quick Check:

    Multi-class output = units=classes + softmax [OK]
Hint: Use softmax with units = number of classes [OK]
Common Mistakes:
  • Using sigmoid for multi-class output
  • Omitting activation in output layer
  • Using relu activation for output