Feature-wise scaling and shifting FiLM layer in Keras
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Feature-wise Linear Modulation, often called FiLM, applies per-feature scaling and shifting conditioned on another input. In Keras, this is useful for multi-modal models where one branch modulates another branch. A correct implementation must align tensor shapes and keep conditioning networks stable.
Core Sections
FiLM Concept in One Equation
FiLM transforms feature tensor x using learned parameters gamma and beta.
FiLM(x) = gamma * x + beta
Gamma and beta are produced by a conditioning network, often from text, metadata, or task context.
Build a Simple FiLM Layer in Keras
Create a custom layer that receives feature tensor and conditioning vector.
This version targets two-dimensional feature tensors of shape batch by feature.
Integrate FiLM into a Multi-input Model
This pattern is a good baseline for conditional prediction tasks.
Handle Shape Alignment Carefully
If feature tensors are rank three or higher, reshape gamma and beta for broadcasting.
Shape mismatches are the most common FiLM implementation bug.
Stabilize Training
Large unconstrained gamma values can destabilize optimization. Common tricks include initializing gamma near one, using regularization, and limiting conditioning branch depth.
You can also apply normalization before FiLM to keep modulation magnitudes predictable.
Evaluate Modulation Impact
Run ablation experiments with and without FiLM to verify benefit. If performance does not improve, conditioning signal may be weak or merged at wrong network depth.
Sequence and Vision FiLM Variants
FiLM is not limited to flat features. For sequence models, conditioning can modulate each time step channel-wise. For vision models, conditioning can modulate feature maps channel by channel.
Shape management is critical when moving from dense to spatial features.
Regularization and Monitoring
Because FiLM introduces extra capacity, overfitting can increase on small datasets. Add dropout or weight decay in conditioning branches, and monitor validation performance closely.
Track gamma and beta statistics during training. Extremely large magnitudes often indicate unstable conditioning behavior and may require lower learning rates or normalization.
Careful experiment tracking is essential so FiLM gains are distinguished from unrelated training changes such as optimizer or data preprocessing adjustments.
When FiLM is applied in deeper layers, verify gradient flow with simple diagnostics to ensure conditioning signal remains effective throughout the network depth.
Consistent experiment logging helps teams iterate on FiLM designs with confidence.
Common Pitfalls
- Forgetting to align gamma and beta shapes with feature tensor dimensions.
- Using overly deep conditioning branches and causing unstable training.
- Applying FiLM at arbitrary layers without ablation validation.
- Ignoring initialization and letting gamma explode early.
- Passing unnormalized conditioning features and adding noisy modulation.
Summary
- FiLM applies feature-wise scaling and shifting conditioned on context.
- Keras implementation requires careful tensor shape management.
- Multi-input models are natural places to use FiLM.
- Stabilize gamma and beta learning with sensible initialization and normalization.
- Validate FiLM value through controlled ablation experiments.

