Installation & Environment

Set up a reliable Python environment for TensorFlow 2.19

1 — Prerequisites

(a) Python ≥ 3.9 (b) pip ≥ 23 (c) CUDA 12.x + cuDNN 9 (optional for GPU)

2 — CPU Install


# Terminal
python -m pip install --upgrade pip
python -m pip install tensorflow==2.19.0

Note ▸ TensorFlow 2.19 is the first release compiled against NumPy 2.0 while still supporting NumPy 1.26 for backward compatibility.

3 — GPU Install


# Windows / Linux
python -m pip install tensorflow==2.19.0 --extra-index-url https://pypi.nvidia.com

Ensure nvidia-smi shows the driver ≥ 550 and nvcc --version matches CUDA 12.x.

4 — Verify Setup


import tensorflow as tf
print(tf.__version__)      # 2.19.0
print(tf.config.list_physical_devices("GPU"))

Tensor Fundamentals

Understand tensors, operations, and eager execution

1 — Creating Tensors


import tensorflow as tf
a = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
b = tf.random.normal(shape=(2, 2))

2 — Basic Math


c = tf.add(a, b)           # add
d = tf.matmul(a, b)        # matmul

All ops execute immediately thanks to eager mode (default since TF 2.0). Wrap performance-critical code in @tf.function to stage a graph.

3 — Automatic Gradients


x = tf.Variable(3.0)
with tf.GradientTape() as tape:      # GradientTape
    y = x**2 + 2*x + 1
dy_dx = tape.gradient(y, x)          # 6.0

High-Level API (tf.keras)

Build, train, evaluate, and save neural networks

1 — Sequential Model


from tensorflow.keras import layers, models

model = models.Sequential([
    layers.Input((28, 28, 1)),
    layers.Conv2D(32, 3, activation="relu"),
    layers.MaxPooling2D(),
    layers.Flatten(),
    layers.Dense(10, activation="softmax")
])
model.compile(optimizer="adam",
              loss="sparse_categorical_crossentropy",
              metrics=["accuracy"])

2 — Training Loop


model.fit(train_ds, epochs=5,
          validation_data=val_ds,
          callbacks=[tf.keras.callbacks.TensorBoard("logs")])

3 — Saving / Loading


model.save("cnn.keras")        # Native Keras format
restored = models.load_model("cnn.keras")

The new .keras file (v2.12+) is now the recommended portable format.

Efficient Data Pipelines (tf.data)

Stream and transform large datasets efficiently

1 — Building a Pipeline


files = tf.data.Dataset.list_files("images/*.jpg")
def decode(path):
    img = tf.io.read_file(path)
    img = tf.image.decode_jpeg(img, channels=3)
    return tf.image.resize(img, (224, 224))
ds = files.map(decode, num_parallel_calls=tf.data.AUTOTUNE) \
           .batch(32).prefetch(tf.data.AUTOTUNE)

2 — Best Practices

Cache ↦ Shuffle ↦ Batch ↦ Prefetch; use AUTOTUNE and TFRecord for maximum throughput.

Custom Training Loops

Full control via GradientTape & optimizers

Step-by-Step Example


optimizer = tf.keras.optimizers.Adam(1e-3)
loss_fn   = tf.keras.losses.SparseCategoricalCrossentropy()

for epoch in range(10):
    for x, y in train_ds:
        with tf.GradientTape() as t:
            pred = model(x, training=True)
            loss = loss_fn(y, pred)
        grads = t.gradient(loss, model.trainable_variables)
        optimizer.apply_gradients(zip(grads, model.trainable_variables))

Distributed & Accelerated Training

Scale workloads across GPUs, TPUs, or multiple hosts

1 — Multi-GPU


strategy = tf.distribute.MirroredStrategy()
with strategy.scope():
    model = build_model()       # your builder
    model.compile(...)

2 — TPU Pods (GCP)


resolver = tf.distribute.cluster_resolver.TPUClusterResolver("grpc://...")
tf.config.experimental_connect_to_cluster(resolver)
tf.tpu.experimental.initialize_tpu_system(resolver)

strategy = tf.distribute.TPUStrategy(resolver)

3 — Parameter Server Strategy

Use for heterogeneous clusters where compute and memory resources differ; supports elastic training on-prem or in Kubernetes.

Deployment Options

Serve models on servers, browsers, and mobile devices

1 — TensorFlow Serving

Export a SavedModel and run the TF-Serving Docker image; REST + gRPC endpoints auto-generated.

2 — TensorFlow Lite (Edge)


converter = tf.lite.TFLiteConverter.from_saved_model("cnn.keras")
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()
open("model.tflite","wb").write(tflite_model)

3 — TensorFlow.js (Web)

Convert with tensorflowjs_converter and run directly in WebGL/WebGPU-accelerated browsers.

Debugging & Performance Tools

Profile, debug, and optimise TensorFlow workflows

tf.profiler & TensorBoard Profiler

Capture traces with tf.profiler.experimental.start(); visualise kernels, memory, and device utilisation.

tf.debugging

Enable tf.debugging.set_log_device_placement(True) to trace op placement; use check_numerics to catch NaNs.

Further Resources & Project Ideas

Continue mastering TensorFlow beyond this guide

Official Docs

https://www.tensorflow.org/ – tutorials, API reference, and model garden.

Hands-on Projects (2025 Trends)

1. Vision transformer fine-tuning   2. Multimodal text-image retrieval   3. On-device speech keyword spotting   4. GNNs for social-graph recommendations   5. Diffusion models for image generation