A Dense layer connects every input to every output. It helps the model learn patterns by mixing all input information.
Dense (fully connected) layers in TensorFlow
Start learning this pattern below
Jump into concepts and practice - no test required
tf.keras.layers.Dense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform')
units is the number of neurons in the layer.
activation is the function that adds non-linearity, like 'relu' or 'softmax'.
tf.keras.layers.Dense(10)tf.keras.layers.Dense(5, activation='relu')
tf.keras.layers.Dense(3, activation='softmax')
This code creates a simple neural network with one Dense layer. It takes 3 features as input and outputs 2 numbers per sample. The ReLU activation makes sure outputs are not negative. We print the output for 4 input samples.
import tensorflow as tf import numpy as np # Create sample input data: 4 samples, each with 3 features input_data = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [10.0, 11.0, 12.0]], dtype=np.float32) # Define a simple model with one Dense layer of 2 neurons and ReLU activation model = tf.keras.Sequential([ tf.keras.layers.Dense(2, activation='relu', input_shape=(3,)) ]) # Run the model on input data to get predictions output = model(input_data) # Print the output values print(output.numpy())
The Dense layer automatically adds a bias term unless you set use_bias=False.
Weights and biases are learned during training to improve model predictions.
Activation functions help the model learn complex patterns beyond simple linear combinations.
Dense layers connect every input to every output neuron.
They learn weights and biases to transform data.
Activation functions add non-linearity to help learn complex patterns.
Practice
Solution
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.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.Final Answer:
Connects every input neuron to every output neuron with weights -> Option BQuick Check:
Dense layer = full weighted connections [OK]
- Thinking Dense layers connect neurons randomly
- Believing Dense layers have zero weights
- Assuming Dense layers do not connect neurons
Solution
Step 1: Recall TensorFlow Dense layer syntax
The correct syntax is tf.keras.layers.Dense(units, activation='function').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.Final Answer:
tf.keras.layers.Dense(10, activation='relu') -> Option AQuick Check:
Correct Dense syntax = tf.keras.layers.Dense(10, activation='relu') [OK]
- Using wrong class name like DenseLayer
- Swapping parameter names (neurons vs units)
- Placing activation before units
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)
Solution
Step 1: Analyze model layers and input shape
Input shape is (3,), first Dense outputs 5 units, second Dense outputs 2 units.Step 2: Determine output shape after second Dense
Batch size is 1 (one input), final output shape is (1, 2).Final Answer:
(1, 2) -> Option CQuick Check:
Output shape = (batch_size, last layer units) = (1, 2) [OK]
- Confusing input shape with output shape
- Mixing up units of first and second Dense layers
- Ignoring batch dimension
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)
Solution
Step 1: Check Dense layer usage and input shape
Input shape is correctly specified in the first Dense layer only.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.Final Answer:
No error, code is correct -> Option DQuick Check:
Code syntax and usage are correct [OK]
- Thinking activation is mandatory in every Dense layer
- Specifying input_shape in multiple layers
- Believing 'mse' is invalid loss
Solution
Step 1: Understand classification output needs
For 3 categories, output layer should have 3 units, one per class.Step 2: Choose activation for multi-class classification
Softmax activation outputs probabilities summing to 1, ideal for multi-class.Step 3: Evaluate options
Dense(3, activation='softmax') uses 3 units with softmax, perfect for 3-class classification; others are unsuitable.Final Answer:
Dense(3, activation='softmax') -> Option AQuick Check:
Multi-class output = units=classes + softmax [OK]
- Using sigmoid for multi-class output
- Omitting activation in output layer
- Using relu activation for output
