The Keras Functional API
Keras provides a framework-level way to describe neural-network models while running on a backend framework.
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?
| Representation | Dimensionality | Role in the tensor map |
|---|---|---|
| Scalar | 0D | A single value |
| Vector | 1D | A one-axis collection of values |
| Matrix | 2D | A two-axis arrangement of values |
| Higher-dimensional tensor | More than 2D | An arrangement requiring additional axes |
The dimensionality labels identify the number of axes used to organize tensor values.
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.
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.
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.
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.
- 1Start with the input
Create or identify the input tensor that begins the model route.
- 2Apply the first layer
Use the first layer as a function. It accepts the input tensor and produces a new tensor.
- 3Continue the handoff
Pass the first layer's output tensor into the next layer. That layer produces the tensor saved as the route's output.
- 4Choose 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.
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.
Common mistake Treating the Functional API as a list of layers whose visual order is enough to define the model.
What it looks likeA learner focuses on which layer statements appear first instead of checking which tensor is passed into each layer.
Why it failsThe model is formed from connected tensor transformations between the selected input and output tensors.
Do this insteadTrace every tensor handoff from the input endpoint to the output endpoint.
Common mistake Selecting an input and output tensor that are not related.
What it looks likeThe chosen endpoints do not belong to one connected route.
Why it failsA disconnected input and output pair causes a
RuntimeError.Do this insteadConfirm that each layer receives the tensor produced by the preceding step and that the final tensor is reachable from the input.
Common mistake Forgetting that a layer accepts and returns tensors.
What it looks likeA learner treats a layer as an isolated object rather than as a transformation from one tensor to another.
Why it failsThe Functional API depends on the tensor route created by these layer calls.
Do this insteadRecord the input tensor and returned output tensor at every layer.
Practice the Route
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.
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
- 1Deep learning connects tensor-based data representation, tensor operations, and gradient-based optimization.
- 2Scalars are 0D tensors, vectors are 1D, matrices are 2D, and higher-dimensional tensors require more than two axes.
- 3Derivatives provide change information, stochastic gradient descent updates parameters, and the process repeats iteratively.
- 4Keras can run on top of backend frameworks such as TensorFlow, Theano, and CNTK.
- 5The Functional API connects input tensors, layers, intermediate tensors, and output tensors; a Model is assembled from related input and output endpoints.
- 6To check a model, trace the tensor path through every layer and confirm that the selected endpoints are connected.
At a glance
- 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.