After defining a Keras model's layers, which method must be called to configure it with an optimizer, loss function, and metrics before training can begin?
Select an answer to reveal the explanation.
Short Explanation and Infographic
Once you've stacked your layers into a model, Keras needs to know how you actually want to train it — what optimizer to use for updating weights, what loss function measures how wrong it is, and which metrics to track along the way (like accuracy). That configuration step is model.compile(optimizer=..., loss=..., metrics=...), and it has to happen before you can call fit(), or fit() will complain that the model isn't compiled. So 'model.compile()' is correct. build() is a lower-level method mainly used to explicitly construct a model's internal weight shapes given an input shape, not to configure training. fit() is the actual training step, which needs compile() to have already run. And summary() just prints an architecture overview — it has nothing to do with setting up how training works.
Full explanation below image
Full Explanation
In the standard Keras workflow, model.compile() is the required step between defining a model's architecture and training it, and it configures the model for the training process. Calling model.compile(optimizer=..., loss=..., metrics=[...]) specifies three key things: the optimizer (such as 'adam' or an instance of tf.keras.optimizers.SGD) that determines how weights are updated based on gradients; the loss function (such as 'categorical_crossentropy' or 'mse') that quantifies how far the model's predictions are from the true labels and is the quantity minimized during training; and optionally a list of metrics (such as 'accuracy') to monitor during training and evaluation, which do not affect training itself but are reported for human interpretation. Once compile() has been called, the model is ready to be trained via model.fit(), which will raise an error if called on a model that has not been compiled, since fit() relies on the optimizer and loss function having already been configured.
model.build() is a distinct, lower-level method used to explicitly construct a model's internal weights given a specified input shape, which is sometimes necessary for custom subclassed models or before calling methods like model.summary() on a model whose input shape hasn't yet been inferred from actual data. It configures the model's internal weight tensors based on shape, but it does not set up the optimizer, loss function, or metrics needed for training.
model.fit() is the method that actually performs training, running forward and backward passes across the provided data for a specified number of epochs and updating weights via the configured optimizer. It depends on compile() having already been called; attempting to call fit() before compile() results in an error because the model doesn't yet know what optimizer or loss function to use.
model.summary() simply prints a textual overview of the model's layers, their output shapes, and parameter counts. It is a diagnostic and inspection tool with no effect on and no dependency on the model's training configuration; it can be called before or after compile(), though it typically requires that the model's input shape be known (either via an Input layer or after build() or the first call/predict).
The overall Keras sequence — define layers, compile() to configure training, fit() to train, evaluate() or predict() to assess or use the trained model — is a standard pattern, and compile() is specifically the step that bridges architecture definition and the actual training loop.