7
votes

I would like to design a neural network for a multi-task deep learning task. Within the Keras API we can either use the "Sequential" or "Functional" approach to build such a neural network. Underneath I provide the code I used to build a network using both approaches to build a network with two outputs:

Sequential

seq_model = Sequential()
seq_model.add(LSTM(32, input_shape=(10,2)))
seq_model.add(Dense(8))
seq_model.add(Dense(2))
seq_model.summary()

Functional

input1 = Input(shape=(10,2))
lay1 = LSTM(32, input_shape=(10,2))(input1)
lay2 = Dense(8)(lay1)
out1 = Dense(1)(lay2)
out2 = Dense(1)(lay2)
func_model = Model(inputs=input1, outputs=[out1, out2])
func_model.summary()

When I look at both the summary outputs for the models, each of them contains identical number of trainable params:

Sequential and Functional .summary()

Up until now, this looks fine - however I start doubting myself when I plot both models (using keras.utils.plot_model) which results in the followings graphs: Sequential and Functional plot_model()

Personally I do not know how to interpret these. When using a multi-task learning approach, I want all neurons (in my case 8) of the layer before the output-layer to connect to both output neurons. For me this clearly shows in the Functional API (where I have two Dense(1) instances), but this is not very clear from the Sequential API. Nevertheless, the amount of trainable params is identical; suggesting that also the Sequential API the last layer is fully connected to both neurons in the Dense output layer.

Could anybody explain to me the differences between those two examples, or are those fully identical and result in the same neural network architecture? Also, which one would be preferred in this case?

Thank you a lot in advance.

2

2 Answers

8
votes

The difference between Sequential and functional keras API:

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 allows you to create models that have a lot more flexibility as you can easily define models where layers connect to more than just the previous and next layers. In fact, you can connect layers to (literally) any other layer. As a result, creating complex networks such as siamese networks and residual networks become possible.

To answer your question:

No these APIs are not the same and the number of layers is normal that are the same number. Which one to use? It depends on the use you want to make of this network. What are you doing the training for? What do you want the output to be?

I recommend this link to make the most of the concept.

Sequential Models & Functional Models

I hope I helped you understand better.

2
votes

Both models are (in theory) equivalent, as the two output nodes do not have any interaction between them.

It is just that the required outputs have a different shape

[(batch_size,2)]

vs

[(batch_size,),(batch_size,)]

and thus, the loss will be different.

The total loss is averaged for the sequential model in this example, whereas it is summed up for the functional model with two outputs (at least with a default loss such as MSE).

Of course, you can also adapt the functional model to be exactly equivalent to the sequential model:

out1 = Dense(2)(lay2)
#out2 = Dense(1)(lay2)
func_model = Model(inputs=input1, outputs=out1)

Maybe you will also need some activations after the Dense layers.