Day 6 - Using the Keras Functional API
-
The functional API can be used to build nonsequential networks (e.g.
wide & deep neural networks) which can make it possible for
neural networks to learn both deep patterns (using the deep path)
and simple rules (through thee short path).
-
A model created using the functional API has to be compiled,
trained, evaluated and used to make predictions the same way as a
model created using the sequential API.
-
A model can have multiple inputs. A different subset of features can
be passed through different inputs.
-
When calling the
fit
, evaluate
and
predict
methods, a list of matrices has to be passed -
one per input.
-
A model can also have multiple outputs when:
-
The task demands it - predicting the position (regression - x
and y coordinates) and class of an object (classification).
-
The are multiple independent tasks based on the same data.
Multiple outputs is better than training a single neural network
per task because a single neural network can learn features in
the data that are useful across tasks.
-
It is used as a regularization technique - auxiliary outputs can
be added to ensure lower layers are learning something useful on
their own without relying on the rest of the network.
-
When using multiple outputs, each output needs its own loss
function.
-
An instance of a Keras
Model
has to be created
specifying which inputs and outputs to use.
-
Keras will compute all losses and add them up to get the final loss
used for training.
-
It is possible to set different weights for each loss function (e.g.
to give the final output loss more importance than the auxiliary
output loss) using the
loss_weights
parametere.
-
When training the model, labels need to be provided for each of the
outputs.
-
When evaluating the model, the individual losses and the total loss
is returned.
-
The
predict
method returns a prediction for each of the
outputs.