TensorFlow: Deployment
Saving & Loading Models
# Save in Keras format (recommended for TF/Keras)
model.save('model.keras')
model = keras.models.load_model('model.keras')
# SavedModel format (for TF Serving, TFLite conversion)
model.export('saved_model/')
# or: tf.saved_model.save(model, 'saved_model/')
model = tf.saved_model.load('saved_model/')
# Save/load weights only (fine-tuning, checkpointing)
model.save_weights('weights.h5')
model.load_weights('weights.h5')
# HDF5 format (legacy)
model.save('model.h5')
model = keras.models.load_model('model.h5')TF Serving (Production API)
# Serve a SavedModel via REST/gRPC
docker run -p 8501:8501 \
--mount type=bind,source=/path/to/saved_model,target=/models/my_model \
-e MODEL_NAME=my_model \
tensorflow/serving
# REST prediction request
curl -X POST http://localhost:8501/v1/models/my_model:predict \
-H "Content-Type: application/json" \
-d '{"instances": [[1.0, 2.0, 3.0, ...]]}'TFLite — Mobile & Edge
# Convert to TFLite
converter = tf.lite.TFLiteConverter.from_saved_model('saved_model/')
# Optional: quantize for smaller/faster model
converter.optimizations = [tf.lite.Optimize.DEFAULT] # dynamic range quant
# Full integer quantization (fastest on CPU/microcontrollers):
converter.representative_dataset = representative_dataset_gen
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
# Run inference with TFLite interpreter
interpreter = tf.lite.Interpreter(model_path='model.tflite')
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output = interpreter.get_tensor(output_details[0]['index'])TensorFlow.js
// Run TF models in the browser or Node.js
import * as tf from '@tensorflow/tfjs'
import '@tensorflow/tfjs-backend-webgl' // GPU in browser
// Load a converted model
const model = await tf.loadLayersModel('/model/model.json')
// Predict
const input = tf.tensor2d([[1, 2, 3, 4]])
const prediction = model.predict(input)
const result = prediction.dataSync()
// Convert from Python SavedModel:
# pip install tensorflowjs
# tensorflowjs_converter --input_format=keras model.keras ./web_modelTensorBoard
# Log metrics, histograms, images during training
tensorboard_cb = keras.callbacks.TensorBoard(
log_dir='logs/',
histogram_freq=1, # log weight histograms every epoch
write_images=True, # log model weights as images
)
model.fit(..., callbacks=[tensorboard_cb])
# Launch TensorBoard
# tensorboard --logdir logs/
# Open: http://localhost:6006Scalars tab: loss and metrics over epochs
Histograms: weight distributions — detect vanishing/exploding gradients
Images: visualize input images or feature maps
Projector: visualize high-dimensional embeddings with t-SNE/PCA
Profile: per-step timing — identify bottlenecks in data pipeline or GPU utilization
Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free