Defining a sequential model

In a sequential model, we stack each layer, one above another:

from keras.models import Sequential
from keras.layers import Dense

First, let's define our model as a Sequential() model, as follows:

model = Sequential()

Now, define the first layer, as shown in the following code:

model.add(Dense(13, input_dim=7, activation='relu'))

In the preceding code, Dense implies a fully connected layer, input_dim implies the dimension of our input, and activation specifies the activation function that we use. We can stack up as many layers as we want, one above another.

Define the next layer with the relu activation, as follows:

model.add(Dense(7, activation='relu'))

Define the output layer with the sigmoid activations:

model.add(Dense(1, activation='sigmoid'))

The final code block of the sequential model is shown as follows. As you can see, the Keras code is much simpler than the TensorFlow code:

model = Sequential()
model.add(Dense(13, input_dim=7, activation='relu'))
model.add(Dense(7, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset