Skip to content Skip to sidebar Skip to footer

Keras' Sequential Vs Functional Api For Multi-task Learning Neural Network

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

Solution 1:

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.

Solution 2:

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.

Solution 3:

Both networks are functionally equivalent. Dense layers are fully connected by definition, which is considered to be the most basic and simple design that can be assumed for "normal" neural networks not otherwise specified. The exact learned parameters and behavior may vary slightly based on the implementation. The graph presented is ambiguous only because it does not show the connection of the neurons (which may number in the millions), but rather provides a symbolic representation of the connectivity with its name (Dense), in this case indicating a fully connected layer.

I expect that the sequential model (or equivalent functional model using one dense layer with two neurons as the output) would be faster because it can use a simplified optimization path, but I have not tested this and I have no knowledge of the compile time optimizations performed by Tensorflow.

Post a Comment for "Keras' Sequential Vs Functional Api For Multi-task Learning Neural Network"