When building and training a Sequential model in Keras, which order of method calls reflects the correct standard workflow?
Select an answer to reveal the explanation.
Short Explanation and Infographic
Keras workflow has a natural order, and it makes total sense once you think about it. First you compile() — this is where you tell the model what optimizer, loss function, and metrics to use, basically configuring it for training. Then you fit() — this actually runs the training loop over your data, updating weights epoch after epoch. Finally you evaluate() — now that training's done, you check how the model performs on data it hasn't been trained on. That compile-fit-evaluate order is correct. You can't fit() before compile() because the model doesn't even know its loss function or optimizer yet — it'll throw an error. And evaluating before fitting makes no sense either, since you'd be scoring an untrained model. Configure first, train second, judge last.
Full explanation below image
Full Explanation
The standard Keras workflow for a Sequential (or Functional) model follows the order: compile(), then fit(), then evaluate(). The compile() step configures the model for training by specifying the optimizer (e.g., Adam, SGD), the loss function (e.g., categorical_crossentropy, mse), and any metrics to track (e.g., accuracy). This step must happen before training because the model needs to know how to compute loss and how to update weights before any training iterations can occur. The fit() method then runs the actual training loop: it iterates over the training data for a specified number of epochs, computing forward passes, loss, backpropagated gradients, and parameter updates via the configured optimizer, optionally tracking validation performance if validation data is supplied. Finally, evaluate() is called after training completes to measure the model's performance — typically loss and any specified metrics — on a held-out test set that was not used during training, giving an unbiased read on generalization.
Calling fit() before compile() is invalid because the model has not yet been configured with a loss function or optimizer; Keras will raise an error since it does not know how to compute gradients or apply updates without this configuration.
Calling evaluate() before fit() is logically premature: it would measure the performance of a model that has undergone no training, using only its initial (often randomly initialized) weights, which is not a meaningful or intended step in the workflow. Evaluation is meant to happen after the model has learned from data, not before.
Calling evaluate() before fit(), even after compile(), suffers from the same problem: compiling only configures the model for training; it does not train it. Evaluating an untrained model at this point provides no useful signal about its learned performance and is not part of any standard Keras workflow.
Memory aid: think of it as 'configure, train, test' — compile sets the rules, fit does the learning, evaluate checks the result. This mirrors the general machine learning workflow of setup, training, and unbiased assessment.