# How to Use TensorFlow and Keras in the AI For Beginners Curriculum: A Complete Guide

> Learn AI for Beginners with TensorFlow and Keras. This guide explores the Microsoft curriculum's 12+ notebooks, covering deep learning from basics to advanced computer vision, NLP, and reinforcement learning.

- Repository: [Microsoft/AI-For-Beginners](https://github.com/microsoft/AI-For-Beginners)
- Tags: tutorial
- Published: 2026-08-28

---

**The Microsoft AI For Beginners curriculum teaches deep learning through a standardized TensorFlow 2.17.0 and Keras workflow that progresses from basic `tf.keras` models to advanced computer vision, NLP, and reinforcement learning implementations across 12+ interactive notebooks.**

The Microsoft AI-For-Beginners repository structures its TensorFlow and Keras content across three core learning tracks: Neural Networks, Computer Vision, and Natural Language Processing. Each notebook implements a self-contained, production-oriented pattern that remains consistent whether you are building your first dense layer or fine-tuning BERT for text classification.

## Environment Setup and Installation

Before running any TensorFlow examples, create the conda environment specified in the repository root. The [`environment.yml`](https://github.com/microsoft/AI-For-Beginners/blob/main/environment.yml) file pins **TensorFlow 2.17.0** and includes Keras as the bundled high-level API.

```bash
conda env create -f environment.yml
conda activate ai-for-beginners

```

This installation provides `tensorflow` with GPU support capabilities and the full `tf.keras` module used throughout the curriculum.

## The Six-Step TensorFlow/Keras Workflow

Every notebook in the curriculum follows an identical architectural scaffold. This repetition allows learners to focus on domain concepts—such as convolution or attention mechanisms—while the TensorFlow implementation pattern remains familiar.

### Step 1: Data Ingestion with tf.data

The curriculum emphasizes **tf.data pipelines** for efficient preprocessing. Rather than loading entire datasets into memory, notebooks use `tf.data.Dataset` objects with automatic prefetching and batching.

In `lessons/3-NeuralNetworks/05-Frameworks/IntroKerasTF.ipynb`, the pattern appears as:

```python
train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)) \
            .shuffle(10_000).batch(128).prefetch(tf.data.AUTOTUNE)

```

This approach is replicated in computer vision and NLP lessons to handle large image corpora and text sequences efficiently.

### Step 2: Model Definition Using Keras APIs

Notebooks utilize both the **Sequential API** for simple stacks and the **Functional API** for complex multi-input or branching architectures. The Functional API appears in advanced lessons like `lessons/4-ComputerVision/12-Segmentation/SemanticSegmentationTF.ipynb`, enabling skip connections and custom layer graphs.

A standard model definition uses `tf.keras.Input` as the entry point and chains `tf.keras.layers` operations:

```python
inputs = keras.Input(shape=(28, 28, 1))
x = layers.Conv2D(32, 3, activation='relu')(inputs)
x = layers.MaxPooling2D()(x)
outputs = layers.Dense(10, activation='softmax')(x)
model = keras.Model(inputs, outputs)

```

### Step 3: Compilation with Optimizers and Loss Functions

The `model.compile()` method configures the training objective. The curriculum demonstrates various configurations:

- **Classification**: `keras.losses.SparseCategoricalCrossentropy()` with `keras.optimizers.Adam()`
- **Segmentation**: Custom Dice loss functions in `SemanticSegmentationTF.ipynb`
- **Sequence Modeling**: CRF-compatible loss implementations in `lessons/5-NLP/19-NER/NER-TF.ipynb`

### Step 4: Training with Callbacks

Robust training requires checkpointing and early stopping. The notebooks consistently use `model.fit()` with a `callbacks` list containing `EarlyStopping` and `ModelCheckpoint`:

```python
callbacks = [
    keras.callbacks.EarlyStopping(patience=3, restore_best_weights=True),
    keras.callbacks.ModelCheckpoint("best_model.h5", save_best_only=True),
]
model.fit(train_ds, epochs=20, validation_data=test_ds, callbacks=callbacks)

```

This pattern appears in CNN training, transfer learning fine-tuning, and reinforcement learning policy optimization.

### Step 5: Evaluation and Inference

Final validation uses `model.evaluate()` for metrics and `model.predict()` for inference. The curriculum emphasizes separating test datasets through `tf.data` pipelines to prevent data leakage.

## Architectural Patterns Across the Curriculum

The repository demonstrates specific TensorFlow implementations for distinct AI domains:

| Technique | Implementation Location | Key TensorFlow Components |
|-----------|------------------------|---------------------------|
| **Convolutional Networks** | `lessons/4-ComputerVision/07-ConvNets/ConvNetsTF.ipynb` | `layers.Conv2D`, `layers.MaxPooling2D` |
| **Transfer Learning** | `lessons/4-ComputerVision/08-TransferLearning/TransferLearningTF.ipynb` | `tf.keras.applications`, TensorFlow Hub |
| **Semantic Segmentation** | `lessons/4-ComputerVision/12-Segmentation/SemanticSegmentationTF.ipynb` | Custom loss functions, U-Net architecture |
| **Recurrent Networks** | `lessons/5-NLP/16-RNN/RNNTF.ipynb` | `layers.Embedding`, `layers.LSTM`, `layers.GRU` |
| **Transformers** | `lessons/5-NLP/18-Transformers/TransformersTF.ipynb` | TensorFlow Hub BERT loading |
| **Reinforcement Learning** | `lessons/6-Other/22-DeepRL/CartPole-RL-TF.ipynb` | `tf.GradientTape` for policy gradients |

## Complete End-to-End Code Example

The following script consolidates the curriculum's standard workflow into a single MNIST classifier, mirroring the structure found in `IntroKerasTF.ipynb` and `ConvNetsTF.ipynb`:

```python
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# 1️⃣ Load & preprocess data

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = x_train[..., tf.newaxis] / 255.0
x_test  = x_test[..., tf.newaxis] / 255.0

train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)) \
            .shuffle(10_000).batch(128).prefetch(tf.data.AUTOTUNE)
test_ds  = tf.data.Dataset.from_tensor_slices((x_test, y_test)) \
            .batch(128).prefetch(tf.data.AUTOTUNE)

# 2️⃣ Build model using Functional API

inputs = keras.Input(shape=(28, 28, 1))
x = layers.Conv2D(32, 3, activation='relu')(inputs)
x = layers.MaxPooling2D()(x)
x = layers.Conv2D(64, 3, activation='relu')(x)
x = layers.Flatten()(x)
x = layers.Dense(128, activation='relu')(x)
outputs = layers.Dense(10, activation='softmax')(x)

model = keras.Model(inputs, outputs, name="mnist_cnn")

# 3️⃣ Compile

model.compile(
    loss=keras.losses.SparseCategoricalCrossentropy(),
    optimizer=keras.optimizers.Adam(),
    metrics=["accuracy"],
)

# 4️⃣ Train with callbacks

callbacks = [
    keras.callbacks.EarlyStopping(patience=3, restore_best_weights=True),
    keras.callbacks.ModelCheckpoint("best_mnist_cnn.h5", save_best_only=True),
]
model.fit(train_ds, epochs=20, validation_data=test_ds, callbacks=callbacks)

# 5️⃣ Evaluate & predict

model.evaluate(test_ds)
predictions = model.predict(x_test[:5])

```

## Summary

- The AI For Beginners curriculum standardizes on **TensorFlow 2.17.0** with Keras integrated as the primary modeling API.
- Every notebook implements a **six-step workflow**: environment setup, `tf.data` ingestion, model definition via `tf.keras.layers`, compilation with `model.compile()`, training with callbacks, and evaluation.
- **Advanced implementations** include transfer learning via TensorFlow Hub, semantic segmentation with custom loss functions, and reinforcement learning using `tf.GradientTape`.
- Key files to explore include `IntroKerasTF.ipynb` for fundamentals, `ConvNetsTF.ipynb` for computer vision, and `TransformersTF.ipynb` for modern NLP architectures.

## Frequently Asked Questions

### What version of TensorFlow is required for the AI For Beginners curriculum?

The repository specifies **TensorFlow 2.17.0** in its [`environment.yml`](https://github.com/microsoft/AI-For-Beginners/blob/main/environment.yml) file. This version includes Keras 3.x integrated directly into the `tf.keras` namespace, providing access to both the Sequential and Functional APIs used throughout the notebooks.

### How does the curriculum handle data preprocessing in TensorFlow?

Notebooks use **tf.data pipelines** exclusively. The standard pattern involves `tf.data.Dataset.from_tensor_slices()` followed by `.shuffle()`, `.batch()`, and `.prefetch(tf.data.AUTOTUNE)` to optimize CPU-GPU data transfer. This appears in `IntroKerasTF.ipynb` and scales to large datasets in the computer vision and NLP sections.

### What is the difference between the Sequential and Functional APIs in the provided notebooks?

The **Sequential API** appears in introductory lessons for simple layer stacks, while the **Functional API** (using `keras.Input` and explicit layer calls) is required for complex architectures like U-Net in `SemanticSegmentationTF.ipynb` and multi-input models in the transfer learning lessons. The Functional API enables skip connections and shared layers that Sequential cannot express.

### Can I use GPU acceleration with the TensorFlow examples in this repository?

Yes. The conda environment installs GPU-compatible TensorFlow binaries. When a CUDA-compatible GPU is available, `model.fit()` automatically utilizes GPU acceleration for training loops. The `tf.data` prefetching specifically optimizes the data pipeline to prevent CPU bottlenecks during GPU training sessions.