LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 1323 min read

Deep Learning Frameworks (TensorFlow & Keras)

Learn TensorFlow, Google's production-grade deep learning framework, and Keras, its high-level API for building neural networks, with a working Sequential model example.

Introduction

So far every library in this course has worked with classical machine learning: decision trees, boosting, linear models. Deep learning is a different approach that builds layered neural networks capable of learning directly from raw images, audio, and text. TensorFlow, Google's deep learning framework, is one of the two dominant tools for building these networks in Python (the other, PyTorch, is covered in the next lesson).

This lesson introduces TensorFlow and its official high-level API, Keras, then builds and trains a small neural network from scratch.

What You Will Learn
  • What TensorFlow is and where it is used in production.
  • What Keras is and its relationship to TensorFlow today.
  • How to install tensorflow with pip.
  • How to define a Sequential model with Dense layers.
  • How .compile() and .fit() work together to train a network.

What is TensorFlow?

TensorFlow is an open-source deep learning framework developed by Google. It handles the heavy lifting of neural network training: automatic differentiation (computing gradients), GPU/TPU acceleration, and exporting trained models for deployment to servers, mobile devices, and browsers via TensorFlow Lite and TensorFlow.js.

TensorFlow's core operations work with tensors — multi-dimensional arrays similar to NumPy arrays, but able to track gradients and run on a GPU. It is widely used in industry because of its maturity, deployment tooling, and strong production support.

What is Keras?

Keras started as a separate, independent high-level API that could run on top of several backends. Today, Keras is bundled directly into TensorFlow as tf.keras, and it is the recommended, official way to build models in TensorFlow. It provides a much simpler, more readable way to define, train, and evaluate neural networks than working with raw TensorFlow operations.

In practice, when people say 'I built this in Keras,' they almost always mean they used tf.keras — TensorFlow's built-in Keras API — rather than the older standalone keras package.

Installing TensorFlow

Installing the tensorflow package gives you both TensorFlow and its bundled tf.keras API — there is no separate install step needed for Keras.

pip install tensorflow

Example: A Sequential Neural Network

The Sequential model is the simplest way to build a neural network in Keras: you stack layers one after another, in order. The example below builds a small network to classify points into one of two categories based on two numeric features.

import tensorflow as tf
from tensorflow import keras
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
# Generate a toy dataset: two interleaving half-moon shapes
X, y = make_moons(n_samples=500, noise=0.2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Build the model: a stack of fully connected (Dense) layers
model = keras.Sequential([
keras.layers.Dense(16, activation='relu', input_shape=(2,)),
keras.layers.Dense(8, activation='relu'),
keras.layers.Dense(1, activation='sigmoid'),
])
# Configure how the model learns
model.compile(
optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'],
)
# Train the model
history = model.fit(X_train, y_train, epochs=20, batch_size=16, verbose=0)
# Evaluate on unseen test data
loss, accuracy = model.evaluate(X_test, y_test, verbose=0)
print(f"Test accuracy: {accuracy:.2%}")
Output

Click Run to see what this code prints.

Understanding the Training Output

Each piece of this example maps to a distinct step in the deep learning workflow, and the same shape appears in nearly every Keras project you will see.

  • Dense(16, activation='relu') — a fully connected layer of 16 neurons using the ReLU activation function.
  • input_shape=(2,) — tells the first layer to expect input with 2 features per sample.
  • activation='sigmoid' on the final layer — squashes output into a 0-to-1 probability, suited to binary classification.
  • model.compile() — sets the optimizer (how weights update), the loss function (what is being minimized), and metrics to track.
  • model.fit(epochs=20) — runs 20 full passes over the training data, adjusting weights each time.
  • model.evaluate() — measures performance on data the model never trained on.

Common Mistakes

Avoid These Mistakes
  • Mismatching the final layer's activation and loss function — for example, using sigmoid with categorical_crossentropy instead of binary_crossentropy.
  • Forgetting to normalize/scale input features before training, which can slow down or destabilize learning.
  • Training for too many epochs without any validation data, leading to overfitting that goes unnoticed until deployment.

Best Practices

  • Pass validation_data or validation_split to .fit() so you can watch for overfitting during training.
  • Use callbacks like EarlyStopping to automatically stop training once validation performance stops improving.
  • Start with a small network and grow it only if the model is underfitting — deep learning does not always beat classical ML on small, tabular datasets.
  • Save trained models with model.save() so training does not need to be repeated for every prediction.

Frequently Asked Questions

No. Installing tensorflow automatically includes tf.keras, which is the actively maintained, recommended version of Keras today.

No, TensorFlow runs on CPU by default and that is fine for learning and small models. For large networks or datasets, a GPU dramatically speeds up training, and TensorFlow will use one automatically if it detects compatible hardware and drivers.

Reach for TensorFlow when the problem involves unstructured data like images, audio, or raw text, or when you specifically need a neural network architecture. For structured, tabular data, scikit-learn or gradient boosting libraries are often simpler and just as accurate.

Key Takeaways

  • TensorFlow is Google's production-grade deep learning framework, built around tensors and automatic differentiation.
  • Keras (as tf.keras) is TensorFlow's official high-level API for defining and training neural networks.
  • keras.Sequential() stacks layers in order; Dense layers are fully connected layers.
  • model.compile() configures the optimizer, loss, and metrics; model.fit() runs the training loop.
  • TensorFlow shines on unstructured data like images and text; classical ML often wins on small tabular datasets.

Summary

TensorFlow and its built-in Keras API make building and training neural networks approachable with just a few lines of code, while still scaling to massive, production-grade deep learning systems. Next, you will look at PyTorch, the other major deep learning framework, and see how its more flexible, code-first style compares.

Lesson 13 Completed
  • You understand what TensorFlow and Keras are and how they relate.
  • You built, compiled, and trained a Sequential neural network.
  • You are ready to compare this against PyTorch's approach to deep learning.
Next Lesson →

Deep Learning Frameworks (PyTorch)