Introduction

Within TensorFlow, Keras — accessed as tf.keras — is the official high-level API for building and training neural networks, sitting directly on top of the tensors and tensor operations covered in the previous two topics. Rather than manually wiring together matrix multiplications, weight initialization, and gradient updates by hand, tf.keras provides ready-made building blocks — layers, models, optimizers, and loss functions — that let you define a neural network in just a few lines of readable code.

Since TensorFlow 2.0, tf.keras has been the officially recommended and default way to build models in TensorFlow, meaning the vast majority of real-world TensorFlow code you'll encounter is written using this API rather than raw tensor operations directly.

Why Does Keras Matter Within TensorFlow?

Keras helps to:

  • Provide a high-level, readable API for defining neural networks without manual tensor manipulation
  • Dramatically reduce the amount of boilerplate code needed to build and train a model
  • Offer multiple model-building approaches (Sequential, Functional, Subclassing) suited to different needs
  • Bundle together layers, optimizers, loss functions, and training loops into a cohesive workflow
  • Serve as the officially recommended, default interface for building models in TensorFlow 2.x
  • Bridge the gap between low-level tensor operations and practical, real-world model development

Where Keras Fits Within the TensorFlow Stack

Whiteboard
Whiteboard diagram
Everything Keras does under the hood is ultimately built from
the tensors and tensor operations covered in the previous two
topics — a Dense layer, for example, is really just performing
a matrix multiplication (tf.matmul) between inputs and weights,
adding a bias, and applying an activation function, all wrapped
up in a clean, reusable interface.

A Minimal Keras Example

Core Building Blocks of tf.keras

1. Layers

Pre-built, reusable components (Dense, Conv2D, LSTM, etc.) that each perform a specific transformation on their input, and can be stacked together to build a complete network.

2. Models

A container that organizes layers together into a complete, trainable neural network, providing methods like .fit(), .evaluate(), and .predict().

3. Optimizers

Algorithms (like Adam or SGD, as covered in the earlier Optimization section) responsible for updating a model's weights based on computed gradients during training.

4. Loss Functions

Built-in implementations of common loss functions (like binary cross-entropy or MSE, as covered in the Loss Functions topic) used to measure prediction error during training.

The Three Ways to Build a Model in Keras (Preview)

ApproachDescriptionBest For
Sequential APIA simple, linear stack of layersStraightforward, single-input/output models
Functional APIA flexible graph of layers, supporting multiple inputs/outputsMore complex architectures
Model SubclassingA fully custom Python class defining the modelMaximum flexibility for research or custom logic

(Each of these three approaches is covered in full depth in its own dedicated topic next in this section.)

The Standard Keras Workflow

Whiteboard
Whiteboard diagram
This define → compile → fit → evaluate → predict pattern is
consistent across virtually all Keras workflows, regardless of
which of the three model-building approaches is used, making it
a reliable mental model for understanding any Keras-based code.

tf.keras vs Standalone Keras 3

As covered in the earlier general Keras topic, standalone Keras 3 supports multiple backends (TensorFlow, PyTorch, JAX). Within the specific context of this TensorFlow-focused section, tf.keras refers to Keras as it's bundled directly inside TensorFlow itself.

Aspecttf.keras (This Section's Focus)Standalone Keras 3
BackendTensorFlow onlyTensorFlow, PyTorch, or JAX (configurable)
InstallationIncluded automatically with TensorFlowInstalled separately via pip install keras
Import Stylefrom tensorflow import kerasimport keras (with backend set via environment variable)
Typical Use CaseStandard TensorFlow-based projectsProjects needing backend flexibility

tf.keras (High-Level) vs Raw Tensor Operations (Low-Level)

Aspecttf.kerasRaw Tensor Operations
Abstraction LevelHigh — layers and models handle details internallyLow — every computation must be manually defined
Code VerbosityMinimal — a few lines define a full modelExtensive — weight initialization, forward pass, etc. all manual
FlexibilityGood, especially with Functional API/SubclassingMaximum — full control over every computation
Best ForMost standard model-building tasksCustom research, novel operations, deep customization

Key Properties of Keras Within TensorFlow

  • tf.keras is TensorFlow's official, bundled high-level API for building and training neural networks.
  • It's built directly on top of the tensors and tensor operations covered in the previous topics.
  • Core building blocks include Layers, Models, Optimizers, and Loss Functions.
  • Keras offers three distinct model-building approaches — Sequential, Functional, and Subclassing — for different levels of flexibility.
  • The define → compile → fit → evaluate → predict workflow is consistent across nearly all Keras-based projects.

Where Is tf.keras Used?

FieldApplication
Computer VisionBuilding CNNs for image classification and detection
Natural Language ProcessingBuilding RNN/Transformer-based models for text tasks
Tabular Data ModelingBuilding standard feedforward networks for structured data
Research PrototypingRapidly testing new model architectures
Production Model TrainingStandard workflows for training and deploying TensorFlow models

Advantages

  • Dramatically reduces the code and complexity needed to build and train neural networks
  • Provides a consistent, well-documented workflow across many different model types
  • Offers multiple model-building approaches to match different complexity needs
  • Deeply integrated with the rest of the TensorFlow ecosystem (TensorBoard, TF Serving, TFLite, etc.)
  • Officially maintained and recommended as the default TensorFlow model-building interface

Limitations

  • High-level abstraction can obscure some lower-level implementation details from newcomers
  • Highly customized, cutting-edge architectures may still require dropping to raw tensor operations or Subclassing
  • Some advanced customization requires understanding the underlying tensor operations anyway
  • Debugging issues can sometimes require looking past the high-level API to the underlying computation
  • Choosing between Sequential, Functional, and Subclassing requires understanding their respective tradeoffs

Real-World Examples

Applicationtf.keras Use
Image Classification AppsSequential or Functional API models using Conv2D layers
Sales Forecasting ToolsSequential models with Dense/LSTM layers for time series
Custom Research ArchitecturesModel Subclassing for novel, non-standard designs
Multi-Input Recommendation SystemsFunctional API handling multiple distinct input types
Educational TutorialsSequential API as the simplest entry point for learning deep learning

Best Practices

  • Start with the Sequential API for simple, linear models before moving to more complex approaches.
  • Understand that every Keras layer is ultimately performing the tensor operations covered in the prior topic.
  • Use the compile → fit → evaluate → predict pattern as a reliable mental model across projects.
  • Reserve Model Subclassing for genuinely custom logic that Sequential or Functional can't express cleanly.
  • Take advantage of tf.keras's tight integration with the broader TensorFlow ecosystem (TensorBoard, callbacks, etc.).

Interview Tip

A common interview question is:

"What is tf.keras, and how does it relate to the lower-level tensor operations in TensorFlow?"

A strong answer is:

tf.keras is TensorFlow's official high-level API for building and training neural networks, providing pre-built layers, models, optimizers, and loss functions so you don't need to manually define every tensor operation involved in a network's forward and backward pass. Under the hood, though, every Keras layer is ultimately built from the same tensor operations covered earlier — for example, a Dense layer is really just a matrix multiplication between inputs and weights, plus a bias addition and an activation function, all wrapped in a clean, reusable interface — meaning Keras trades some low-level control for dramatically simpler, more maintainable code.

Explicitly connecting Keras back to the underlying tensor operations makes your answer stronger.

Conclusion

tf.keras provides the high-level, officially recommended interface for building neural networks in TensorFlow, translating the low-level tensors and tensor operations from the previous topics into clean, reusable layers, models, and training workflows. With this overview in place, the next topics dive into each of the three specific ways to build a model in Keras — starting with the Sequential API, the simplest and most straightforward approach.