Concepts / The Keras Functional API

The Keras Functional API

Advanced deep learning combines tensor-based data representation, tensor operations, and gradient-based optimization.

  • Programming

Why This API Matters

A neural network can be understood as a route of tensor transformations. Data enters as a tensor, layers transform that tensor, and another tensor leaves the route. The Keras Functional API makes this route explicit: you create an input tensor, pass it through layers, retain the resulting tensors, and use the starting and ending tensors to define a Model.

The broader deep-learning picture has three connected parts. Neural networks represent data with tensors, manipulate tensors with mathematical operations, and improve parameter values through gradient-based optimization. Keras provides a framework-level way to describe neural-network models while running on a backend framework.

From Scalars to Tensors

Tensor dimensionality describes how many axes are needed to organize values. A scalar is a 0D tensor, a vector is a 1D tensor, and a matrix is a 2D tensor. Tensors with more than two dimensions are higher-dimensional tensors. Data batches are also part of this representation discussion, so the useful first question is: which dimensions are needed to represent this data before a network applies operations to it?

RepresentationDimensionalityRole in the tensor map
Scalar0DA single value
Vector1DA one-axis collection of values
Matrix2DA two-axis arrangement of values
Higher-dimensional tensorMore than 2DAn arrangement requiring additional axes

The dimensionality labels identify the number of axes used to organize tensor values.

more axesmore axesmore axesScalar0DVector1DMatrix2DHigher-dimensionaltensorMore than 2D
How does the shape and dimensionality change from a scalar to higher-dimensional data?

Operations on Tensor Data

Once data is represented as tensors, a network applies tensor operations. Important operations named in the source include element-wise operations, broadcasting, tensor dot, and tensor reshaping. Element-wise operations work across corresponding tensor values. Broadcasting allows tensor values to participate in an operation across compatible dimensions. Tensor dot combines tensors through a dot operation, while reshaping changes how tensor values are organized without changing the idea that the data remains tensor data.

element-wise operationcorresponding valuesbroadcastingtensor dotreshapeTensor AbeforeElement-wise resultafterTensor BbeforeBroadcast resultafterTensor dot resultafterReshaped tensorafter
How do common tensor operations change the organization or combination of tensor data?

Optimization as a Changing State

Gradient-based optimization changes the network over repeated steps. The network begins with current parameter values. Derivatives provide information about change, and the gradient organizes that information for tensor operations. Stochastic gradient descent then uses the gradient to produce updated parameter values. The updated state becomes the starting point for another iteration. Backpropagation is the algorithm used for chaining derivatives.

derive change informationorganizeuseproducerepeatCurrent parametersDerivativeschange informationGradientorganized informationStochastic gradientdescentupdate stepUpdated parameters
How do derivatives and stochastic gradient descent move a network through successive parameter states?

This optimization loop is part of the same larger picture as the Functional API. The Functional API describes how tensors move through transformations; optimization describes how the network's parameter state changes as the network is improved.

Keras and Its Backends

Keras occupies a framework role in the deep-learning ecosystem. It can run on top of backend frameworks such as TensorFlow, Theano, or CNTK. This relationship separates the way a model is described from the underlying framework that runs the tensor computations.

can run on top ofcan run on top ofcan run on top ofKerasframework interfaceTensorFlowbackend frameworkTheanobackend frameworkCNTKbackend framework
How does a Keras model definition connect to an underlying backend framework?

Building a Connected Route

The Functional API describes a model as a connected route of tensor transformations. A layer is used as a function: it accepts an input tensor and returns an output tensor. The output tensor can then become the input to another layer.

A Two-Layer Tensor Route

Describe how a Functional API model is assembled from an input tensor, two connected layers, and an output tensor.

Start with the input: Create or identify the input tensor that begins the model route.

Apply the first layer: Use the first layer as a function. It accepts the input tensor and produces a new tensor.

Continue the handoff: Pass the first layer's output tensor into the next layer. That layer produces the tensor saved as the route's output.

Choose the endpoints: Give Keras the original input tensor and the final output tensor when assembling the Model.

The model is defined by the connected transformations between the selected input and output tensors.

input tensorreturnsinput tensorreturnsstarting endpointending endpointInput tensorModelinput and output endpointsLayer 1accepts tensorIntermediate tensorLayer 2returns tensorOutput tensor
How do input tensors move through connected layers to become output tensors, and how are those connections used to define a Keras model?

Checking the Layer Path

When checking whether a Functional API model has been assembled correctly, trace the tensor handoff one step at a time. Begin at the model input. For each layer, identify the tensor entering it and the tensor it returns. Continue until the tensor selected as the model output. This route matters more than the visual order of lines in a file because the Model is assembled from the connected transformations between the chosen endpoints.

enterreturnenterreturntrace fromtrace toInput tensorstart tracingConnected routemodel assembledLayer 1inspect input and outputIntermediate tensorsaved handoffLayer 2inspect input and outputOutput tensorending endpoint
What sequence of layers and tensor transformations does data follow from the model input to the model output?
  • Treating the Functional API as a list of layers whose visual order is enough to define the model.

    The model is formed from connected tensor transformations between the selected input and output tensors.

    Fix: Trace every tensor handoff from the input endpoint to the output endpoint.

  • Selecting an input and output tensor that are not related.

    A disconnected input and output pair causes a RuntimeError.

    Fix: Confirm that each layer receives the tensor produced by the preceding step and that the final tensor is reachable from the input.

  • Forgetting that a layer accepts and returns tensors.

    The Functional API depends on the tensor route created by these layer calls.

    Fix: Record the input tensor and returned output tensor at every layer.

Practice the Route

MEDIUM

A model route begins with an input tensor, passes through three layers, and ends with a tensor produced by the third layer. Explain which two tensors should be supplied to the Model constructor and describe how you would verify that the route is connected.

Hints
  • The Model constructor uses the starting tensor and the ending tensor.
  • Trace the output of each layer into the input of the next layer.
  • A disconnected pair of endpoints is invalid.
EASY

Classify each representation by dimensionality: a single value, a one-axis collection, a two-axis arrangement, and a tensor requiring more than two axes. Then name one tensor operation from the source that could manipulate tensor data.

Hints
  • The dimensionality sequence begins at 0D.
  • The named operations include element-wise operations, broadcasting, tensor dot, and reshaping.

Key Takeaways

  1. Deep learning connects tensor-based data representation, tensor operations, and gradient-based optimization.
  2. Scalars are 0D tensors, vectors are 1D, matrices are 2D, and higher-dimensional tensors require more than two axes.
  3. Derivatives provide change information, stochastic gradient descent updates parameters, and the process repeats iteratively.
  4. Keras can run on top of backend frameworks such as TensorFlow, Theano, and CNTK.
  5. The Functional API connects input tensors, layers, intermediate tensors, and output tensors; a Model is assembled from related input and output endpoints.
  6. To check a model, trace the tensor path through every layer and confirm that the selected endpoints are connected.

Key Takeaways

  • The Functional API represents a model as a connected route of tensor transformations.
  • A layer acts as a function that accepts an input tensor and returns an output tensor.
  • The Model uses a related input tensor and output tensor to capture the connected route.
  • Tracing each tensor handoff is the most reliable way to check whether the intended model route was assembled.
  • Tensor representation, tensor operations, and gradient-based optimization form the wider conceptual map around Keras.