The Sequential model API helps you build a simple stack of layers for machine learning models easily. It lets you create models step-by-step, like stacking blocks.
Sequential model API in TensorFlow
Start learning this pattern below
Jump into concepts and practice - no test required
from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense model = Sequential([ Dense(units=64, activation='relu', input_shape=(input_size,)), Dense(units=10, activation='softmax') ])
The layers are added in the order you list them inside the Sequential constructor.
The first layer needs to know the shape of the input data using input_shape.
add() method instead of passing a list.model = Sequential() model.add(Dense(32, activation='relu', input_shape=(100,))) model.add(Dense(1, activation='sigmoid'))
model = Sequential([
Dense(128, activation='relu', input_shape=(50,)),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])This example builds a simple Sequential model with two layers to classify data into 3 classes. It trains on 5 samples and then predicts probabilities for 2 new samples.
import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense # Create a simple model for classifying 3 classes model = Sequential([ Dense(16, activation='relu', input_shape=(4,)), Dense(3, activation='softmax') ]) # Compile the model with loss and optimizer model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # Sample data: 5 samples, 4 features each import numpy as np x_train = np.array([[5.1, 3.5, 1.4, 0.2], [6.2, 3.4, 5.4, 2.3], [5.9, 3.0, 4.2, 1.5], [6.0, 2.2, 4.0, 1.0], [5.5, 2.3, 4.0, 1.3]]) # Labels for 3 classes y_train = np.array([0, 2, 1, 1, 1]) # Train the model for 5 epochs history = model.fit(x_train, y_train, epochs=5, verbose=0) # Predict class probabilities for new data x_test = np.array([[5.0, 3.6, 1.4, 0.2], [6.7, 3.1, 4.7, 1.5]]) predictions = model.predict(x_test) print('Predictions:') print(predictions) print('Training accuracy after 5 epochs:', history.history['accuracy'][-1])
Sequential models are best for simple, linear stacks of layers.
For models with multiple inputs or outputs, use the Functional API instead.
Always specify input_shape in the first layer to tell the model what data to expect.
The Sequential API lets you build models by stacking layers in order.
It is easy to use and great for beginners and simple problems.
Remember to compile the model before training to set loss and optimizer.
Practice
Sequential model API in TensorFlow?Solution
Step 1: Understand the Sequential API purpose
The Sequential API is designed to build models by stacking layers one after another in a simple linear fashion.Step 2: Compare options with the API's function
Options B, C, and D describe other functionalities not related to the Sequential API's main purpose.Final Answer:
To build a model by stacking layers in a linear order -> Option DQuick Check:
Sequential API = linear stacking of layers [OK]
- Confusing Sequential with Functional API for complex models
- Thinking Sequential handles data preprocessing
- Assuming Sequential is for visualization
Solution
Step 1: Recall correct Sequential model creation syntax
The Sequential model can be created by passing a list of layers inside the constructor, e.g., Sequential([Dense(10)]).Step 2: Check each option's syntax validity
model = Sequential([Dense(10)]) uses the correct list syntax. model = Sequential(Dense(10)) misses the list brackets. model = Sequential().add(Dense(10)) is valid usage but requires assignment to a variable to keep the model reference. model = Sequential.add(Dense(10)) incorrectly calls add() on the class, not an instance.Final Answer:
model = Sequential([Dense(10)]) -> Option BQuick Check:
Sequential needs list of layers in constructor [OK]
- Omitting brackets around layers list
- Calling add() on class instead of instance
- Chaining add() without assignment
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
model = Sequential([
Dense(5, input_shape=(10,)),
Dense(3)
])
print(model.output_shape)Solution
Step 1: Understand input and output shapes in Sequential
The input shape is (10,), so the first Dense layer outputs (None, 5). The second Dense layer outputs (None, 3) because it has 3 units.Step 2: Identify final output shape
The model's output shape is the output of the last layer, which is (None, 3). None means batch size is flexible.Final Answer:
(None, 3) -> Option CQuick Check:
Last Dense units = output shape [OK]
- Confusing input shape with output shape
- Using batch size instead of None
- Mixing layer output shapes
from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense model = Sequential() model.add(Dense(10, input_shape=(5,))) model.add(Dense(1)) model.compile(optimizer='adam', loss='mse') model.fit(x_train, y_train, epochs=5)
Solution
Step 1: Check code for missing definitions
The code usesx_trainandy_trainin model.fit() but they are not defined anywhere, causing a runtime error.Step 2: Verify other parts
Optimizer 'adam' and loss 'mse' are valid strings. add() method is valid for Sequential instances. Imports are sufficient.Final Answer:
x_train and y_train are not defined -> Option AQuick Check:
Undefined training data causes error [OK]
- Assuming loss 'mse' is invalid
- Thinking add() method is not allowed
- Ignoring missing data variables
Solution
Step 1: Understand classification output requirements
For 3 classes, the final layer should have 3 units with softmax activation to output class probabilities.Step 2: Match appropriate loss function
For one-hot encoded labels, 'categorical_crossentropy' is the correct loss function to use with softmax output.Final Answer:
Dense(3, activation='softmax') with loss='categorical_crossentropy' -> Option AQuick Check:
Softmax + categorical_crossentropy = multi-class classification [OK]
- Using sigmoid for multi-class output
- Using mean squared error for classification
- Missing activation in final layer
