ModelRefs / TensorFlow & Keras — Tutorial
TensorFlow & Keras — Tutorial
Build, train, and export models with Keras — the high-level API that powers production ML at scale. Covers Keras vs PyTorch: when to choose which.
Overview
Build, train, and export models with Keras — the high-level API that powers production ML at scale
Level: Intermediate. Estimated reading time: 40 minutes.
Keras vs PyTorch: when to choose which
Both Keras (via TensorFlow) and PyTorch are mature, production-grade frameworks. The practical differences:
Keras strengths: faster prototyping with Sequential/Functional APIs, built-in support for SavedModel and TF Serving deployment, tighter integration with Google Cloud and TPUs, and model.fit() handling training loops, callbacks, and metrics automatically.
PyTorch strengths: more Pythonic, easier to debug (eager execution is default since TF 2.0 too), dominant in research (most papers release PyTorch code), and native support for dynamic graphs.
For production ML pipelines at companies already on GCP: TensorFlow/Keras. For research and experimentation: PyTorch. For learning: both are fine — the concepts transfer directly.
The three Keras APIs
Sequential API: stack layers linearly. Simplest — use when your model is a straight pipeline with one input and one output.
Functional API: build a directed acyclic graph of layers. Use when you have multiple inputs, multiple outputs, or skip connections (ResNets, attention networks).
Subclassing API: define a class inheriting from keras.Model and implement call(). Maximum flexibility — equivalent to PyTorch nn.Module. Use for non-standard architectures.
The training workflow is the same for all three: model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(X_train, y_train, epochs=10, validation_split=0.1, callbacks=[...]) model.evaluate(X_test, y_test)
Callbacks and saving models
Keras callbacks execute code at specific points in training. The essential callbacks:
ModelCheckpoint: save the best model weights during training. EarlyStopping: stop when validation loss plateaus. ReduceLROnPlateau: halve the learning rate when validation loss stagnates. TensorBoard: visualise loss curves, histograms, and embeddings in a browser.
Saving: model.save("model.keras") saves the full model (architecture + weights + compile config). model.save_weights("weights.h5") saves only weights. Load with keras.models.load_model("model.keras").
For deployment: tf.saved_model.save() exports to TF Serving format. keras.export.export_savedmodel() for mobile/edge via TFLite.
Continue your research
Use these connected ModelRefs sections to compare alternatives, inspect implementation paths, and review the evidence and governance boundaries relevant to TensorFlow & Keras — Tutorial.