网页
综合
更新时间 2026-11-14 05:30:26
The Functional API
自动蜘蛛
keras.io
正文约 5622 字
Author: fchollet Date created: 2019/03/01 Last modified: 2023/06/25 Description: Complete guide to the functional API.
View in Colab • GitHub source
The Keras functional API is a way to create models that are more flexible than the keras.Sequential API. The functional API can handle models with non-linear topology, shared layers, and even multiple inputs or outputs.
The main idea is that a deep learning model is usually a directed acyclic graph (DAG) of layers. So the functional API is a way to build graphs of layers.
Consider the following model:
This is a basic graph with three layers. To build this model using the functional API, start by creating an input node:
The shape of the data is set as a 784-dimensional vector. The batch size is always omitted since only the shape of each sample is specified.
If, for example, you have an image input with a shape of (32, 32, 3), you would use:
The inputs that is returned contains information about the shape and dtype of the input data that you feed to your model. Here's the shape:
You create a new node in the graph of layers by calling a layer on this inputs object:
The "layer call" action is like drawing an arrow from "inputs" to this layer you created. You're "passing" the inputs to the dense layer, and you get x as the output.
Let's add a few more layers to the graph of layers:
At this point, you can create a Model by specifying its inputs and outputs in the graph of layers:
Let's check out what the model summary looks like:
You can also plot the model as a graph:
And, optionally, display the input and output shapes of each layer in the plotted graph:
This figure and the code are almost identical. In the code version, the connection arrows are replaced by the call operation.
A "graph of layers" is an intuitive mental image for a deep learning model, and the functional API is a way to create models that closely mirrors this.
Training, evaluation, and inference work exactly in the same way for models built using the functional API as for Sequential models.
The Model class offers a built-in training loop (the fit() method) and a built-in evaluation loop (the evaluate() method). Note that you can easily customize these loops to implement your own training routines. See also the guides on customizing what happens in fit():
Here, load the MNIST image data, reshape it into vectors, fit the model on the data (while monitoring performance on a validation split), then evaluate the model on the test data:
For further reading, see the training and evaluation guide.
Saving the model and serialization work the same way for models built using the functional API as they do for Sequential models. The standard way to save a functional model is to call model.save() to save the entire model as a single file. You can later recreate the same model from this file, even if the code that built the model is no longer available.
This saved file includes the: - model architecture - model weight values (that were learned during training) - model training config, if any (as passed to compile()) - optimizer and its state, if any (to restart training where you left off)
For details, read the model serialization & saving guide.
In the functional API, models are created by specifying their inputs and outputs in a graph of layers. That means that a single graph of layers can be used to generate multiple models.
In the example below, you use the same stack of layers to instantiate two models: an encoder model that turns image inputs into 16-dimensional vectors, and an end-to-end autoencoder model for training.
Here, the decoding architecture is strictly symmetrical to the encoding architecture, so the output shape is the same as the input shape (28, 28, 1).
The reverse of a Conv2D layer is a Conv2DTranspose layer, and the reverse of a MaxPooling2D layer is an UpSampling2D layer.
You can treat any model as if it were a layer by invoking it on an Input or on the output of another layer. By calling a model you aren't just reusing the architecture of the model, you're also reusing its weights.
To see this in action, here's a different take on the autoencoder example that creates an encoder model, a decoder model, and chains them in two calls to obtain the autoencoder model:
As you can see, the model can be nested: a model can contain sub-models (since a model is just like a layer). A common use case for model nesting is ensembling. For example, here's how to ensemble a set of models into a single model that averages their predictions:
The functional API makes it easy to manipulate multiple inputs and outputs. This cannot be handled with the Sequential API.
For example, if you're building a system for ranking customer issue tickets by priority and routing them to the correct department, then the model will have three inputs:
This model will have two outputs:
You can build this model in a few lines with the functional API:
When compiling this model, you can assign different losses to each output. You can even assign different weights to each loss – to modulate their contribution to the total training loss.
Since the output layers have different names, you could also specify the losses and loss weights with the corresponding layer names:
Train the model by passing lists of NumPy arrays of inputs and targets:
When calling fit with a Dataset object, it should yield either a tuple of lists like ([title_data, body_data, tags_data], [priority_targets, dept_targets]) or a tuple of dictionaries like ({'title': title_data, 'body': body_data, 'tags': tags_data}, {'priority': priority_targets, 'department': dept_targets}).
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。