Bird
Raised Fist0
TensorFlowml~20 mins

Why Keras simplifies model building in TensorFlow - Experiment to Prove It

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
Experiment - Why Keras simplifies model building
Problem:You want to build a neural network model to classify handwritten digits using the MNIST dataset. Currently, you are writing long and complex code with low readability and many manual steps.
Current Metrics:Training accuracy: 95%, Validation accuracy: 93%, Training loss: 0.15, Validation loss: 0.18
Issue:The code is hard to write and understand, making it difficult to experiment and improve the model quickly.
Your Task
Simplify the model building process using Keras Sequential API to make the code shorter, clearer, and easier to modify, while maintaining similar accuracy and loss.
Use the Keras Sequential API only
Keep the model architecture similar (two hidden layers)
Use the same dataset (MNIST)
Maintain training for 5 epochs
Hint 1
Hint 2
Hint 3
Hint 4
Solution
TensorFlow
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten

# Load MNIST dataset
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()

# Normalize pixel values
X_train, X_test = X_train / 255.0, X_test / 255.0

# Build model using Keras Sequential API
model = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(128, activation='relu'),
    Dense(64, activation='relu'),
    Dense(10, activation='softmax')
])

# Compile model
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Train model
history = model.fit(X_train, y_train, epochs=5, validation_split=0.2)

# Evaluate on test data
test_loss, test_acc = model.evaluate(X_test, y_test)
Replaced manual layer creation and training loops with Keras Sequential API
Used Flatten layer to convert 2D images to 1D vectors automatically
Used Dense layers with activation functions in a simple list format
Compiled model with optimizer, loss, and metrics in one step
Used model.fit with validation_split to handle validation automatically
Results Interpretation

Before: Complex code with manual steps, training accuracy 95%, validation accuracy 93%, loss around 0.15-0.18.

After: Simple, readable code using Keras Sequential API, training accuracy improved to ~97%, validation accuracy ~96%, loss reduced to ~0.08-0.10.

Keras simplifies building neural networks by providing a clear, concise API that handles many details automatically. This makes your code easier to write, read, and modify, while achieving good model performance.
Bonus Experiment
Try adding a Dropout layer after the first Dense layer to reduce overfitting and observe the effect on validation accuracy.
💡 Hint
Use keras.layers.Dropout with a rate like 0.2 between Dense layers and retrain the model.

Practice

(1/5)
1. Why does Keras simplify building neural networks compared to using raw TensorFlow?
easy
A. Because it requires writing complex low-level code
B. Because it provides a clear, simple way to define layers and train models
C. Because it only works with small datasets
D. Because it does not support training models

Solution

  1. Step 1: Understand Keras design goal

    Keras is designed to make neural network building easy by providing simple building blocks like layers.
  2. Step 2: Compare with raw TensorFlow

    Raw TensorFlow requires more detailed code for defining models and training, which can be complex for beginners.
  3. Final Answer:

    Because it provides a clear, simple way to define layers and train models -> Option B
  4. Quick Check:

    Keras simplifies model building = A [OK]
Hint: Keras = simple layers + easy training steps [OK]
Common Mistakes:
  • Thinking Keras needs complex code
  • Believing Keras only works for small data
  • Assuming Keras cannot train models
2. Which of the following is the correct way to start building a model in Keras?
easy
A. model = keras.Sequential()
B. model = keras.compile()
C. model = keras.fit()
D. model = keras.evaluate()

Solution

  1. Step 1: Identify model creation method

    In Keras, you create a model by initializing a Sequential or Functional model, commonly with keras.Sequential().
  2. Step 2: Understand other methods

    compile(), fit(), and evaluate() are methods called on the model after creation, not for building it.
  3. Final Answer:

    model = keras.Sequential() -> Option A
  4. Quick Check:

    Start model with Sequential() = B [OK]
Hint: Build model with Sequential(), compile and fit later [OK]
Common Mistakes:
  • Using compile() to create model
  • Calling fit() before model creation
  • Confusing evaluate() with model building
3. What will be the output shape of the model after running this code?
import tensorflow as tf
model = tf.keras.Sequential([
  tf.keras.layers.Dense(10, input_shape=(5,)),
  tf.keras.layers.Dense(3)
])
model.summary()
medium
A. Output shape: (None, 3)
B. Output shape: (None, 10)
C. Output shape: (5, 3)
D. Output shape: (10, 3)

Solution

  1. Step 1: Analyze model layers

    The first Dense layer outputs 10 units; the second Dense layer outputs 3 units.
  2. Step 2: Determine final output shape

    The model output shape matches the last layer's units, so (None, 3), where None is batch size.
  3. Final Answer:

    Output shape: (None, 3) -> Option A
  4. Quick Check:

    Last layer units = output shape = A [OK]
Hint: Output shape matches last layer units [OK]
Common Mistakes:
  • Confusing input shape with output shape
  • Ignoring last layer's units
  • Thinking batch size is fixed
4. Identify the error in this Keras model code:
import tensorflow as tf
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(8))
model.compile(optimizer='adam', loss='mse')
model.fit(x_train, y_train, epochs=5)
medium
A. fit() missing batch_size argument
B. compile() called before adding layers
C. Missing input shape in first Dense layer
D. Using 'mse' loss is invalid

Solution

  1. Step 1: Check model layer definition

    The first Dense layer lacks input_shape, which is required for the first layer in Sequential models.
  2. Step 2: Verify other steps

    compile() is correctly called after adding layers; batch_size is optional; 'mse' is a valid loss.
  3. Final Answer:

    Missing input shape in first Dense layer -> Option C
  4. Quick Check:

    First layer needs input shape = C [OK]
Hint: First layer must have input_shape defined [OK]
Common Mistakes:
  • Assuming batch_size is mandatory in fit()
  • Thinking compile() order is wrong
  • Believing 'mse' is invalid loss
5. You want to build a Keras model that classifies images into 4 categories. Which sequence of steps correctly uses Keras to build, compile, and train this model?
hard
A. Define layers without input shape, fit model, then compile
B. Compile model first, then define layers, then fit with data
C. Fit model first, then define layers, then compile
D. Define layers with input shape, compile with optimizer and loss, then fit with data

Solution

  1. Step 1: Build model with layers including input shape

    First, define the model layers specifying input shape so Keras knows input size.
  2. Step 2: Compile model with optimizer and loss

    Next, compile the model to set optimizer and loss function before training.
  3. Step 3: Train model with fit()

    Finally, train the model using fit() with training data and epochs.
  4. Final Answer:

    Define layers with input shape, compile with optimizer and loss, then fit with data -> Option D
  5. Quick Check:

    Build -> Compile -> Train = D [OK]
Hint: Build layers, compile, then fit to train [OK]
Common Mistakes:
  • Compiling before building layers
  • Fitting before compiling
  • Skipping input shape in first layer