DEV Community

Anurag Verma
Anurag Verma

Posted on

3

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay