In a Keras workflow, after a model has been built and compiled, what does calling model.fit(x_train, y_train) do?
Select an answer to reveal the explanation.
Short Explanation and Infographic
Once you've built your model and run compile() to set the optimizer, loss, and metrics, fit() is where the actual learning happens. Feed it your training data and labels, tell it how many epochs to run, and behind the scenes Keras runs forward passes, computes the loss, backpropagates, and updates weights with the optimizer you configured — batch after batch, epoch after epoch. That's why 'trains the model on the provided data' is the answer. It's not saving anything to disk — that's what save() or a ModelCheckpoint callback does. It's also not just predicting without learning; that's predict(), which explicitly does NOT touch the weights. And it has nothing to do with converting to a mobile format — that's a separate export step using something like TensorFlow Lite.
Full explanation below image
Full Explanation
model.fit() is the Keras API method that actually trains a compiled model on provided data. Once model.compile() has configured the optimizer, loss function, and metrics, fit(x_train, y_train, epochs=..., batch_size=..., validation_data=...) runs the full training loop internally: it splits the training data into mini-batches, and for each batch performs a forward pass to generate predictions, computes the loss by comparing predictions to the provided labels, calculates gradients via backpropagation, and updates the model's weights using the configured optimizer. This process repeats for the number of epochs specified, and if validation_data or a validation_split is supplied, fit() also evaluates the model on that held-out data at the end of each epoch and reports both training and validation metrics, which is useful for monitoring overfitting during training.
Saving the compiled model architecture (and optionally its weights) to disk is instead handled by methods such as model.save() or model.save_weights(), or by a ModelCheckpoint callback passed into fit() to save automatically during training; this is a distinct concern from the training process itself, though it can be triggered as a side effect via callbacks.
Generating predictions without updating weights is the job of model.predict(x), which runs only forward passes over the input data and returns outputs, explicitly not computing gradients or touching any parameters — this is used for inference after training is complete, or to inspect intermediate outputs.
Converting a model into a format suitable for mobile or edge deployment is a separate export step, typically performed with a conversion tool such as the TensorFlow Lite Converter, which takes a trained Keras/TensorFlow model and produces a smaller, optimized file format (.tflite) for on-device inference; this happens after training is finished and is unrelated to what fit() does.
Understanding the distinct roles of compile() (configuration), fit() (training), predict() (inference), and save()/export tooling (persistence and deployment) is fundamental to correctly sequencing a Keras workflow.