The Functional API – Keras The Keras Functional API provides more flexibility in creating models compared to keras.sequential. The overall structure for creating models using the functional API is as follows.
First create an input node and define the shape of the input. For example, if the input consists of 784 dimensional vectors. Then, we would define the input node as: inputs = keras.Input(shape=(784,)). This is the shape of one sample in the dataset. The batchsize is not to be included.
Then, the first layer in the graph is created that takes in this input shape as follows: dense = layers.Dense(64, activation = “relu”), after we have created a layer, then we pass it the input and to give an output, x x = dense(inputs). Note, 64 is the number of units (neurons) in the Dense layer.
Then, we simply add more layers as needed. Let’s add another dense layer with 64 units that takes the input (x) from the previous layer and gives (y) as an output: y = layers.Dense(64, activation = “relu”)(x). Lets’ add a final layer as follows: outputs = layers.Dense(10)(x).
Finally, we can create a model that aggregates all the layers as follows: model = model.keras.Model(inputs=inputs, outputs=outputs, name = “myFirstModel”)
Print the model summary: model.summary( )
We can also plot the model and display the inputs and output shapes for each layer in the graph. See keras tutorial for the commands.
Training: This is same as the Sequential models. model.fit( )
Evaluation: This is same as the Sequential models. model.evaluate ( )
Save the model. model.Save ( )
The saved model can then be loaded later on as. models.load_model ( )