The Keras functional API is the way to go for defining complex models, such as multi-output models, directed acyclic graphs, or models with shared layers.

This guide assumes that you are already familiar with the Sequential model.

Let’s start with something simple.

First example: a densely-connected network

The Sequential model is probably a better choice to implement such a network, but it helps to start with something really simple.

To use the functional API, build your input and output layers and then pass them to the model() function. This model can be trained just like Keras sequential models.

Note that Keras objects are modified in place which is why it’s not necessary for model to be assigned back to after it is compiled.

All models are callable, just like layers

With the functional API, it is easy to reuse trained models: you can treat any model as if it were a layer. Note that you aren’t just reusing the architecture of the model, you are also reusing its weights.

This can allow, for instance, to quickly create models that can process sequences of inputs. You could turn an image classification model into a video classification model, in just one line:

Multi-input and multi-output models

Here’s a good use case for the functional API: models with multiple inputs and outputs. The functional API makes it easy to manipulate a large number of intertwined datastreams.

Let’s consider the following model. We seek to predict how many retweets and likes a news headline will receive on Twitter. The main input to the model will be the headline itself, as a sequence of words, but to spice things up, our model will also have an auxiliary input, receiving extra data such as the time of day when the headline was posted, etc.

The model will also be supervised via two loss functions. Using the main loss function earlier in a model is a good regularization mechanism for deep models.

Here’s what our model looks like:

multi-input-multi-output-graph

Let’s implement it with the functional API.

The main input will receive the headline, as a sequence of integers (each integer encodes a word). The integers will be between 1 and 10,000 (a vocabulary of 10,000 words) and the sequences will be 100 words long.

We’ll include an

library(keras)

main_input <- layer_input(shape = c(100), dtype = 'int32', name = 'main_input')

lstm_out <- main_input %>% 
  layer_embedding(input_dim = 10000, output_dim = 512, input_length = 100) %>% 
  layer_lstm(units = 32)

Here we insert the auxiliary loss, allowing the LSTM and Embedding layer to be trained smoothly even though the main loss will be much higher in the model:

auxiliary_output <- lstm_out %>% 
  layer_dense(units = 1, activation = 'sigmoid', name = 'aux_output')

At this point, we feed into the model our auxiliary input data by concatenating it with the LSTM output, stacking a deep densely-connected network on top and adding the main logistic regression layer

auxiliary_input <- layer_input(shape = c(5), name = 'aux_input')

main_output <- layer_concatenate(c(lstm_out, auxiliary_input)) %>%  
  layer_dense(units = 64, activation = 'relu') %>% 
  layer_dense(units = 64, activation = 'relu') %>% 
  layer_dense(units = 64, activation = 'relu') %>% 
  layer_dense(units = 1, activation = 'sigmoid', name = 'main_output')

This defines a model with two inputs and two outputs:

Model
__________________________________________________________________________________________
Layer (type)                 Output Shape        Param #    Connected to                  
==========================================================================================
main_input (InputLayer)      (None, 100)         0                                        
__________________________________________________________________________________________
embedding_1 (Embedding)      (None, 100, 512)    5120000                                  
__________________________________________________________________________________________
lstm_1 (LSTM)                (None, 32)          69760                                    
__________________________________________________________________________________________
aux_input (InputLayer)       (None, 5)           0                                        
__________________________________________________________________________________________
concatenate_1 (Concatenate)  (None, 37)          0                                        
__________________________________________________________________________________________
dense_1 (Dense)              (None, 64)          2432                                     
__________________________________________________________________________________________
dense_2 (Dense)              (None, 64)          4160                                     
__________________________________________________________________________________________
dense_3 (Dense)              (None, 64)          4160                                     
__________________________________________________________________________________________
main_output (Dense)          (None, 1)           65                                       
__________________________________________________________________________________________
aux_output (Dense)           (None, 1)           33                                       
==========================================================================================
Total params: 5,200,610
Trainable params: 5,200,610
Non-trainable params: 0
__________________________________________________________________________________________

We compile the model and assign a weight of 0.2 to the auxiliary loss. To specify different loss_weights or loss for each different output, you can use a list or a dictionary. Here we pass a single loss as the loss argument, so the same loss will be used on all outputs.

We can train the model by passing it lists of input arrays and target arrays:

Since our inputs and outputs are named (we passed them a “name” argument), We could also have compiled the model via:

Shared layers

Another good use for the functional API are models that use shared layers. Let’s take a look at shared layers.

Let’s consider a dataset of tweets. We want to build a model that can tell whether two tweets are from the same person or not (this can allow us to compare users by the similarity of their tweets, for instance).

One way to achieve this is to build a model that encodes two tweets into two vectors, concatenates the vectors and then adds a logistic regression; this outputs a probability that the two tweets share the same author. The model would then be trained on positive tweet pairs and negative tweet pairs.

Because the problem is symmetric, the mechanism that encodes the first tweet should be reused (weights and all) to encode the second tweet. Here we use a shared LSTM layer to encode the tweets.

Let’s build this with the functional API. We will take as input for a tweet a binary matrix of shape (280, 256), i.e. a sequence of 280 vectors of size 256, where each dimension in the 256-dimensional vector encodes the presence/absence of a character (out of an alphabet of 256 frequent characters).

library(keras)

tweet_a <- layer_input(shape = c(280, 256))
tweet_b <- layer_input(shape = c(280, 256))

To share a layer across different inputs, simply instantiate the layer once, then call it on as many inputs as you want:

The concept of layer “node”

Whenever you are calling a layer on some input, you are creating a new tensor (the output of the layer), and you are adding a “node” to the layer, linking the input tensor to the output tensor. When you are calling the same layer multiple times, that layer owns multiple nodes indexed as 1, 2, 2…

You can obtain the output tensor of a layer via layer$output, or its output shape via layer$output_shape. But what if a layer is connected to multiple inputs?

As long as a layer is only connected to one input, there is no confusion, and $output will return the one output of the layer:

Not so if the layer has multiple inputs:

AttributeError: Layer lstm_4 has multiple inbound nodes, hence the notion of "layer output" is ill-defined. Use `get_output_at(node_index)` instead.

Okay then. The following works:

get_output_at(lstm, 1)
get_output_at(lstm, 2)

Simple enough, right?

The same is true for the properties input_shape and output_shape: as long as the layer has only one node, or as long as all nodes have the same input/output shape, then the notion of “layer output/input shape” is well defined, and that one shape will be returned by layer$output_shape/layer$input_shape. But if, for instance, you apply the same layer_conv_2d() layer to an input of shape (32, 32, 3), and then to an input of shape (64, 64, 3), the layer will have multiple input/output shapes, and you will have to fetch them by specifying the index of the node they belong to:

More examples

Code examples are still the best way to get started, so here are a few more.

Inception module

For more information about the Inception architecture, see Going Deeper with Convolutions.

library(keras)

input_img <- layer_input(shape = c(256, 256, 3))

tower_1 <- input_img %>% 
  layer_conv_2d(filters = 64, kernel_size = c(1, 1), padding='same', activation='relu') %>% 
  layer_conv_2d(filters = 64, kernel_size = c(3, 3), padding='same', activation='relu')

tower_2 <- input_img %>% 
  layer_conv_2d(filters = 64, kernel_size = c(1, 1), padding='same', activation='relu') %>% 
  layer_conv_2d(filters = 64, kernel_size = c(5, 5), padding='same', activation='relu')

tower_3 <- input_img %>% 
  layer_max_pooling_2d(pool_size = c(3, 3), strides = c(1, 1), padding = 'same') %>% 
  layer_conv_2d(filters = 64, kernel_size = c(1, 1), padding='same', activation='relu')

output <- layer_concatenate(c(tower_1, tower_2, tower_3), axis = 1)

Residual connection on a convolution layer

For more information about residual networks, see Deep Residual Learning for Image Recognition.

Shared vision model

This model reuses the same image-processing module on two inputs, to classify whether two MNIST digits are the same digit or different digits.

Visual question answering model

This model can select the correct one-word answer when asked a natural-language question about a picture.

It works by encoding the question into a vector, encoding the image into a vector, concatenating the two, and training on top a logistic regression over some vocabulary of potential answers.

# First, let's define a vision model using a Sequential model.
# This model will encode an image into a vector.
vision_model <- keras_model_sequential() 
vision_model %>% 
  layer_conv_2d(filters = 64, kernel_size = c(3, 3), activation = 'relu', padding = 'same',
                input_shape = c(224, 224, 3)) %>% 
  layer_conv_2d(filters = 64, kernel_size = c(3, 3), activation = 'relu') %>% 
  layer_max_pooling_2d(pool_size = c(2, 2)) %>% 
  layer_conv_2d(filters = 128, kernel_size = c(3, 3), activation = 'relu', padding = 'same') %>% 
  layer_conv_2d(filters = 128, kernel_size = c(3, 3), activation = 'relu') %>% 
  layer_max_pooling_2d(pool_size = c(2, 2)) %>% 
  layer_conv_2d(filters = 256, kernel_size = c(3, 3), activation = 'relu', padding = 'same') %>% 
  layer_conv_2d(filters = 256, kernel_size = c(3, 3), activation = 'relu') %>% 
  layer_conv_2d(filters = 256, kernel_size = c(3, 3), activation = 'relu') %>% 
  layer_max_pooling_2d(pool_size = c(2, 2)) %>% 
  layer_flatten()

# Now let's get a tensor with the output of our vision model:
image_input <- layer_input(shape = c(224, 224, 3))
encoded_image <- image_input %>% vision_model

# Next, let's define a language model to encode the question into a vector.
# Each question will be at most 100 word long,
# and we will index words as integers from 1 to 9999.
question_input <- layer_input(shape = c(100), dtype = 'int32')
encoded_question <- question_input %>% 
  layer_embedding(input_dim = 10000, output_dim = 256, input_length = 100) %>% 
  layer_lstm(units = 256)

# Let's concatenate the question vector and the image vector then
# train a logistic regression over 1000 words on top
output <- layer_concatenate(c(encoded_question, encoded_image)) %>% 
  layer_dense(units = 1000, activation='softmax')

# This is our final model:
vqa_model <- keras_model(inputs = c(image_input, question_input), outputs = output)

Video question answering model

Now that we have trained our image QA model, we can quickly turn it into a video QA model. With appropriate training, you will be able to show it a short video (e.g. 100-frame human action) and ask a natural language question about the video (e.g. “what sport is the boy playing?” -> “football”).