Understanding Keras
Technical Mastery: Building, Training, and Introspection
Keras is designed for **Progressive Disclosure of Complexity**. In this chapter, we master the technical specifics: from building models layer-by-layer to orchestrating complex multi-sensor systems through the Functional API.
We'll look at how weights are initialized, how to visualize model architecture, and how to "extract" features from existing models for new applications.
Incremental Building and Introspection
A **Sequential** model can be built incrementally using the `.add()` method. Until you call `.build()` or pass an input shape to the first layer, the model's weights aren't initialized because Keras doesn't know the input dimension yet. We use `.summary()` to follow how the output shape changes as we add layers.
Multi-Modal Systems with the Functional API
Complex tasks like Autonomous Driving require **Multiple Inputs** (Camera + LiDAR) and **Multiple Outputs** (Steering + Speed). The Functional API uses **Symbolic Tensors** to build an acyclic graph of layers. We can train these multi-modal models by passing data via dictionaries to the `fit()` method.
Feature Extraction and Reuse
A model isn't just a black box; it's a hierarchy of features. **Feature Extraction** involves accessing intermediate layers of a pre-trained model to reuse those patterns for a new task. For example, a model trained to detect lung nodes can have its intermediate "texture" features extracted to help detect other anomalies.
Practice Questions
Question 1
Which Keras API is most suitable for a model with multiple inputs (e.g., Image + Tabular data)?
Question 2
When would you prefer a Custom Training Loop (GradientTape) over model.fit()?