DEV Community

Anurag Verma
Anurag Verma

Posted on

Sequential API keras tensorflow

The Sequential API is a way to create a neural network model in Keras, a popular deep learning library. It allows you to build a model layer by layer, in a linear fashion, by specifying the input layer and the output layer, and then adding any number of hidden layers in between.

Here's an example of how you can use the Sequential API to create a simple fully-connected network with two hidden layers:

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

# Create a Sequential model
model = Sequential()

# Add a hidden layer with 64 units and ReLU activation
model.add(Dense(64, activation='relu', input_shape=(input_shape,)))

# Add another hidden layer with 64 units and ReLU activation
model.add(Dense(64, activation='relu'))

# Add the output layer with a single unit and sigmoid activation
model.add(Dense(1, activation='sigmoid'))

Enter fullscreen mode Exit fullscreen mode

In this example, the input shape is specified in the first hidden layer. The model has two hidden layers with 64 units each and ReLU activation, and an output layer with a single unit and sigmoid activation.

You can then compile and fit the model using the compile and fit methods:

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit(x_train, y_train, epochs=10, batch_size=32)

Enter fullscreen mode Exit fullscreen mode

This will train the model for 10 epochs on the training data x_train and y_train, using the Adam optimizer and binary cross-entropy loss. The model's accuracy will be tracked during training.

Top comments (0)