Keras models are used to represent the actual neural network model. There are two modes to create models in Keras that are very simple to use and implement. These two modes are Sequential API and Functional API that you can use to create models in Keras.
Sequential API:
In this API, you can simply arrange different kinds of layers in Keras in sequential order. Most artificial neural networks have layers are in sequential order and it flows from one layer to another layer in that sequential order.
To create an ANN model, you need to call Sequential() API with the specified code below:
from keras.models import Sequential
model = Sequential()
Now, you can add a layer by simply creating a layer with the help of Keras layer API with the content below:
from keras.models import Sequential
model = Sequential()
inpt_lyr = Dense(32, input_shape=(8,)) model.add(inpt_lyr)
hdn_lyr = Dense(64, activation='relu'); model.add(hdn_lyr)
otpt_lyr = Dense(8)
model.add(otpt_lyr)
Now that we have created one input layer, hidden layer, and output layer. You might need to create a method to access the model with the code below:
Let’s modify the model.layers as list:
>>> lyrs = model.layers
>>> lyrs
[
<keras.layers.core.Dense object at 0x000001A7B828B6A0>,
<keras.layers.core.Dense object at 0x000002A7B828B6A8>
<keras.layers.core.Dense object at 0x000002A7B828B6A8>
]
Now, we’re going to modify some files below:
model.inputs:
>>> inpt = model.inputs
>>> inpt
[<tf.Tensor 'dense_13_input:0' shape=(?, 6) dtype=float32>]
models.outputs:
>>> otpts = model.outputs
>>> otpts
<tf.Tensor 'dense_15/BiasAdd:0' shape=(?, 8) dtype=float32>]
The model.get_weights file will return all weights as NumPy arrays. and model.set_weights(weight_numpy_array) will set the weights of the model.
Functional API: It is an alternative approach for creating more complex models than sequential models. There are various input and output layers in a functional model that you can implement. To create a functional model, you need to import the input layer with the help of the model shown below:
>>> from keras.layers import Input
>>> dta = Input(shape=(8,9))
>>> from keras.layers import Dense
Next, you need to add a Dense layer for the input with the help of the code below:
>>> layer = Dense(2) (dta)
>>> print(layer)
Tensor(“dense1/add:0”,shape = (?, 3,3), data_type = float32)
The model can be defined with the help of the module below:
from keras.models import Model
mdl = Model(inputs = dta, outputs = lyr)
The final code and configurations that you might need to do in your code are shown below:
from keras.layers import Input
from keras.models import Model
from keras.layers import Dense
dta = Input(shape=(2,3))
lyr = Dense(2)(data) model =
Model(inputs=dta,outputs=lyr) model.summary()
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 3, 3) 0
________________________________________________________________
dense_1 (Dense) (None, 3, 3) 9
=============================================================
Total parameters: 9
Trainable parameters: 9
Non-trainable params: 0