TensorFlow Reference
Free reference guide: TensorFlow Reference
About TensorFlow Reference
The TensorFlow Reference is a searchable cheat sheet covering the TensorFlow and Keras ecosystem for deep learning and machine learning. It includes tensor creation and manipulation (tf.constant, tf.Variable, tf.reshape, tf.concat), Keras model building with both Sequential and Functional APIs, and the full model lifecycle from compile() and fit() through evaluate() and predict().
This reference organizes over 45 entries across eight categories: Tensors, Keras, Layers, Optimizers, Data, Training, Deploy, and CLI. The Layers section covers Dense, Conv2D, LSTM, BatchNormalization, Dropout, Embedding, and MaxPooling2D. The Optimizers section includes Adam and SGD configurations, loss functions, learning rate schedulers with ExponentialDecay, and EarlyStopping callbacks for preventing overfitting.
Beyond basic model building, this cheat sheet addresses advanced workflows including custom training loops with GradientTape, graph-mode execution with @tf.function, transfer learning using MobileNetV2 with frozen base layers, tf.data.Dataset pipelines with map/shuffle/batch/prefetch, image augmentation with ImageDataGenerator, and deployment paths covering model.save(), TFLite conversion, TensorFlow Serving via Docker, and ONNX export using tf2onnx.
Key Features
- Tensor operations: tf.constant, tf.Variable, tf.zeros, tf.ones, tf.random.normal, tf.cast, tf.reshape, and tf.concat
- Keras model APIs: Sequential for linear stacks and Functional API for complex architectures with shared layers
- Layer reference: Dense, Conv2D, LSTM, BatchNormalization, Dropout, Embedding, and MaxPooling2D with parameters
- Optimizer and loss configuration: Adam, SGD with momentum, ExponentialDecay scheduler, and EarlyStopping callback
- Data pipeline patterns: tf.data.Dataset with shuffle/batch/prefetch, tfds.load(), and image_dataset_from_directory
- Custom training with GradientTape and @tf.function decorator for graph-mode performance optimization
- Transfer learning example using MobileNetV2 with frozen backbone and custom classification head
- Deployment workflows: model.save(), SavedModel loading, TFLite conversion, TensorFlow Serving, and ONNX export
Frequently Asked Questions
What is the difference between Sequential and Functional API in Keras?
Sequential is a linear stack of layers suitable for simple models where each layer has exactly one input and one output. The Functional API supports complex architectures including multi-input/multi-output models, shared layers, and residual connections. Use tf.keras.Input() to define input tensors and chain layers by calling them as functions to build the computation graph.
How do I create a custom training loop with GradientTape?
Wrap your forward pass inside a with tf.GradientTape() as tape: block. Compute predictions and loss within this context, then call tape.gradient(loss, model.trainable_variables) to get gradients. Finally, apply gradients using optimizer.apply_gradients(zip(grads, model.trainable_variables)). This gives you full control over the training step, useful for GANs, reinforcement learning, or custom loss computation.
When should I use @tf.function?
@tf.function converts a Python function into a TensorFlow graph for faster execution. Use it on training steps and inference functions that will be called repeatedly. The first call traces the function and compiles a graph; subsequent calls skip Python overhead. Avoid using it on functions with Python side effects or dynamic control flow that changes between calls.
How do I set up transfer learning with a pre-trained model?
Load a pre-trained model like MobileNetV2 with weights="imagenet" and include_top=False to remove the classification head. Set base.trainable = False to freeze all layers. Add your own GlobalAveragePooling2D and Dense layers on top. Train with a low learning rate, then optionally unfreeze the last few layers of the base model for fine-tuning.
What is the recommended way to build data pipelines?
Use tf.data.Dataset.from_tensor_slices() for in-memory data or tf.keras.utils.image_dataset_from_directory() for image folders. Chain transformations: .shuffle(buffer_size) for randomization, .map(preprocess_fn) for transformations, .batch(32) for batching, and .prefetch(tf.data.AUTOTUNE) to overlap data loading with training. This is significantly faster than feeding NumPy arrays directly.
How do I prevent overfitting during training?
Use multiple strategies: add Dropout layers between Dense layers, apply BatchNormalization for training stability, use EarlyStopping callback with monitor="val_loss" and restore_best_weights=True, apply ImageDataGenerator for data augmentation, and use a validation_split in model.fit(). The ModelCheckpoint callback saves the best model based on validation metrics.
How do I convert a model to TFLite for mobile deployment?
Use tf.lite.TFLiteConverter.from_saved_model("saved_model/") to create a converter, then call converter.convert() to produce the TFLite flatbuffer. Write the result to a .tflite file. For further optimization, enable quantization with converter.optimizations = [tf.lite.Optimize.DEFAULT] to reduce model size and improve inference speed on mobile and edge devices.
How do I check if TensorFlow is using my GPU?
Call tf.config.list_physical_devices("GPU") to list available GPUs. If the list is empty, TensorFlow is running on CPU only. Ensure you have installed tensorflow[and-cuda] (or the appropriate GPU package), have compatible NVIDIA drivers, CUDA toolkit, and cuDNN installed. Use tf.debugging tools and TensorBoard for monitoring GPU utilization during training.