1. 程式人生 > >How to Use the Keras Functional API for Deep Learning

How to Use the Keras Functional API for Deep Learning

The Keras Python library makes creating deep learning models fast and easy.

The sequential API allows you to create models layer-by-layer for most problems. It is limited in that it does not allow you to create models that share layers or have multiple inputs or outputs.

The functional API in Keras is an alternate way of creating models that offers a lot more flexibility, including creating more complex models.

In this tutorial, you will discover how to use the more flexible functional API in Keras to define deep learning models.

After completing this tutorial, you will know:

  • The difference between the Sequential and Functional APIs.
  • How to define simple Multilayer Perceptron, Convolutional Neural Network, and Recurrent Neural Network models using the functional API.
  • How to define more complex models with shared layers and multiple inputs and outputs.

Let’s get started.

  • Update Nov/2017: Update note about hanging dimension for input layers only affecting 1D input, thanks Joe.
  • Updated Nov/2018: Added missing flatten layer for CNN, thanks Konstantin.
  • Update Nov/2018: Added description of the functional API Python syntax.

Tutorial Overview

This tutorial is divided into 7 parts; they are:

  1. Keras Sequential Models
  2. Keras Functional Models
  3. Standard Network Models
  4. Shared Layers Model
  5. Multiple Input and Output Models
  6. Best Practices
  7. NEW: Note on the Functional API Python Syntax

1. Keras Sequential Models

As a review, Keras provides a Sequential model API.

This is a way of creating deep learning models where an instance of the Sequential class is created and model layers are created and added to it.

For example, the layers can be defined and passed to the Sequential as an array:

123 from keras.models import Sequentialfrom keras.layers import Densemodel=Sequential([Dense(2,input_dim=1),Dense(1)])

Layers can also be added piecewise:

12345 from keras.models import Sequentialfrom keras.layers import Densemodel=Sequential()model.add(Dense(2,input_dim=1))model.add(Dense(1))

The Sequential model API is great for developing deep learning models in most situations, but it also has some limitations.

For example, it is not straightforward to define models that may have multiple different input sources, produce multiple output destinations or models that re-use layers.

2. Keras Functional Models

The Keras functional API provides a more flexible way for defining models.

It specifically allows you to define multiple input or output models as well as models that share layers. More than that, it allows you to define ad hoc acyclic network graphs.

Models are defined by creating instances of layers and connecting them directly to each other in pairs, then defining a Model that specifies the layers to act as the input and output to the model.

Let’s look at the three unique aspects of Keras functional API in turn:

1. Defining Input

Unlike the Sequential model, you must create and define a standalone Input layer that specifies the shape of input data.

The input layer takes a shape argument that is a tuple that indicates the dimensionality of the input data.

When input data is one-dimensional, such as for a multilayer Perceptron, the shape must explicitly leave room for the shape of the mini-batch size used when splitting the data when training the network. Therefore, the shape tuple is always defined with a hanging last dimension when the input is one-dimensional (2,), for example:

12 from keras.layers import Inputvisible=Input(shape=(2,))

2. Connecting Layers

The layers in the model are connected pairwise.

This is done by specifying where the input comes from when defining each new layer. A bracket notation is used, such that after the layer is created, the layer from which the input to the current layer comes from is specified.

Let’s make this clear with a short example. We can create the input layer as above, then create a hidden layer as a Dense that receives input only from the input layer.

1234 from keras.layers import Inputfrom keras.layers import Densevisible=Input(shape=(2,))hidden=Dense(2)(visible)

Note the (visible) after the creation of the Dense layer that connects the input layer output as the input to the dense hidden layer.

It is this way of connecting layers piece by piece that gives the functional API its flexibility. For example, you can see how easy it would be to start defining ad hoc graphs of layers.

3. Creating the Model

After creating all of your model layers and connecting them together, you must define the model.

As with the Sequential API, the model is the thing you can summarize, fit, evaluate, and use to make predictions.

Keras provides a Model class that you can use to create a model from your created layers. It requires that you only specify the input and output layers. For example:

123456 from keras.models import Modelfrom keras.layers import Inputfrom keras.layers import Densevisible=Input(shape=(2,))hidden=Dense(2)(visible)model=Model(inputs=visible,outputs=hidden)

Now that we know all of the key pieces of the Keras functional API, let’s work through defining a suite of different models and build up some practice with it.

Each example is executable and prints the structure and creates a diagram of the graph. I recommend doing this for your own models to make it clear what exactly you have defined.

My hope is that these examples provide templates for you when you want to define your own models using the functional API in the future.

3. Standard Network Models

When getting started with the functional API, it is a good idea to see how some standard neural network models are defined.

In this section, we will look at defining a simple multilayer Perceptron, convolutional neural network, and recurrent neural network.

These examples will provide a foundation for understanding the more elaborate examples later.

Multilayer Perceptron

In this section, we define a multilayer Perceptron model for binary classification.

The model has 10 inputs, 3 hidden layers with 10, 20, and 10 neurons, and an output layer with 1 output. Rectified linear activation functions are used in each hidden layer and a sigmoid activation function is used in the output layer, for binary classification.

123456789101112131415 # Multilayer Perceptronfrom keras.utils import plot_modelfrom keras.models import Modelfrom keras.layers import Inputfrom keras.layers import Densevisible=Input(shape=(10,))hidden1=Dense(10,activation='relu')(visible)hidden2=Dense(20,activation='relu')(hidden1)hidden3=Dense(10,activation='relu')(hidden2)output=Dense(1,activation='sigmoid')(hidden3)model=Model(inputs=visible,outputs=output)# summarize layersprint(model.summary())# plot graphplot_model(model,to_file='multilayer_perceptron_graph.png')

Running the example prints the structure of the network.

1234567891011121314151617 _________________________________________________________________Layer (type)                 Output Shape              Param #=================================================================input_1 (InputLayer)         (None, 10)                0_________________________________________________________________dense_1 (Dense)              (None, 10)                110_________________________________________________________________dense_2 (Dense)              (None, 20)                220_________________________________________________________________dense_3 (Dense)              (None, 10)                210_________________________________________________________________dense_4 (Dense)              (None, 1)                 11=================================================================Total params: 551Trainable params: 551Non-trainable params: 0_________________________________________________________________

A plot of the model graph is also created and saved to file.

Multilayer Perceptron Network Graph

Multilayer Perceptron Network Graph

Convolutional Neural Network

In this section, we will define a convolutional neural network for image classification.

The model receives black and white 64×64 images as input, then has a sequence of two convolutional and pooling layers as feature extractors, followed by a fully connected layer to interpret the features and an output layer with a sigmoid activation for two-class predictions.

123456789101112131415161718192021 # Convolutional Neural Networkfrom keras.utils import plot_modelfrom keras.models import Modelfrom keras.layers import Inputfrom keras.layers import Densefrom keras.layers import Flattenfrom keras.layers.convolutional import Conv2Dfrom keras.layers.pooling import MaxPooling2Dvisible=Input(shape=(64,64,1))conv1=Conv2D(32,kernel_size=4,activation='relu')(visible)pool1=MaxPooling2D(pool_size=(2,2))(conv1)conv2=Conv2D(16,kernel_size=4,activation='relu')(pool1)pool2=MaxPooling2D(pool_size=(2,2))(conv2)flat=Flatten()(pool2)hidden1=Dense(10,activation='relu')(flat)output=Dense(1,activation='sigmoid')(hidden1)model=Model(inputs=visible,outputs=output)# summarize layersprint(model.summary())# plot graphplot_model(model,to_file='convolutional_neural_network.png')

Running the example summarizes the model layers.

1234567891011121314151617181920212223 _________________________________________________________________Layer (type)                 Output Shape              Param #   =================================================================input_1 (InputLayer)         (None, 64, 64, 1)         0         _________________________________________________________________conv2d_1 (Conv2D)            (None, 61, 61, 32)        544       _________________________________________________________________max_pooling2d_1 (MaxPooling2 (None, 30, 30, 32)        0         _________________________________________________________________conv2d_2 (Conv2D)            (None, 27, 27, 16)        8208      _________________________________________________________________max_pooling2d_2 (MaxPooling2 (None, 13, 13, 16)        0         _________________________________________________________________flatten_1 (Flatten)          (None, 2704)              0         _________________________________________________________________dense_1 (Dense)              (None, 10)                27050     _________________________________________________________________dense_2 (Dense)              (None, 1)                 11        =================================================================Total params: 35,813Trainable params: 35,813Non-trainable params: 0_________________________________________________________________

A plot of the model graph is also created and saved to file.

Convolutional Neural Network Graph

Convolutional Neural Network Graph

Recurrent Neural Network

In this section, we will define a long short-term memory recurrent neural network for sequence classification.

The model expects 100 time steps of one feature as input. The model has a single LSTM hidden layer to extract features from the sequence, followed by a fully connected layer to interpret the LSTM output, followed by an output layer for making binary predictions.

123456789101112131415 # Recurrent Neural Networkfrom keras.utils import plot_modelfrom keras.models import Modelfrom keras.layers import Inputfrom keras.layers import Densefrom keras.layers.recurrent import LSTMvisible=Input(shape=(100,1))hidden1=LSTM(10)(visible)hidden2=Dense(10,activation='relu')(hidden1)output=Dense(1,activation