SoFunction
Updated on 2024-11-15

Difference between Sequential and Functional Models in Keras and Explanation

Difference between Sequential and Functional models

Sequential model

There is only one input and output, and the network is a linear stack of layers

This can be done by sending a request to theSequentialThe model passes a list of layers to construct the model:

from  import Sequential
from  import Dense, Activation
The first layer of #Sequential needs to accept a parameter about the input data shape, and the subsequent layers can automatically derive the shape of the intermediate data.
model = Sequential()
(Dense(32, input_shape=(784,)))
(Activation('relu'))
(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])
 
# Generate dummy data
import numpy as np
data = ((1000, 100))
labels = (2, size=(1000, 1))
 
# Train the model, iterating on the data in batches of 32 samples
(data, labels, epochs=10, batch_size=32)
# For a single-input model with 10 classes (categorical classification):
 
 
model = Sequential()
(Dense(32, activation='relu', input_dim=100))
(Dense(10, activation='softmax'))
(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
 
# Generate dummy data
import numpy as np
data = ((1000, 100))
labels = (10, size=(1000, 1))
 
# Convert labels to categorical one-hot encoding
one_hot_labels = .to_categorical(labels, num_classes=10)
 
# Train the model, iterating on the data in batches of 32 samples
(data, one_hot_labels, epochs=10, batch_size=32)

Functional model

Distinction:

1. The layer object accepts a tensor as a parameter and returns a tensor.

2. a frame whose inputs are tensors and whose outputs are also tensors is a model that is modeled by theModelDefinition.

from  import Sequential, Model
from keras import layers
from keras import Input
"""
# Sequential model implementation
seq_model = Sequential()
seq_model.add((32, activation='relu', input_shape=(64,)))
seq_model.add((32, activation='relu'))
seq_model.add((10, activation='softmax'))
"""
# Corresponding functional model implementations
input_tensor = Input(shape=(64,))
x = (32, activation='relu')(input_tensor)
x = (32, activation='relu')(x)
output_tensor = (10, activation='softmax')(x)
model = Model(input_tensor, output_tensor)
(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
(data, labels)  # starts training
()  # View Model

3. All models are callable, just like layers

This approach allows you to quickly create models that can handle sequential signals, and you can quickly turn a model that classifies images into a model that classifies videos with a single line of code:

from  import TimeDistributed
 
# Input tensor for sequences of 20 timesteps,
# each containing a 784-dimensional vector
input_sequences = Input(shape=(20, 784))
 
# This applies our previous model to every timestep in the input sequences.
# the output of the previous model was a 10-way softmax,
# so the output of the layer below will be a sequence of 20 vectors of size 10.
processed_sequences = TimeDistributed(model)(input_sequences)

4. Constructing models with multiple inputs or multiple outputs

from  import Model
from keras import layers
from keras import Input
text_vocabulary_size = 10000
question_vocabulary_size = 10000
answer_vocabulary_size = 500
# The text input is a sequence of integers of variable length. Note that you can optionally name the input
text_input = Input(shape=(None,), dtype='int32', name='text') 
embedded_text = (
text_vocabulary_size, 64)(text_input)
encoded_text = (32)(embedded_text)
question_input = Input(shape=(None,), dtype='int32', name='question')
embedded_question = (
question_vocabulary_size, 32)(question_input)
encoded_question = (16)(embedded_question)
# Linking coded questions to text
concatenated = ([encoded_text, encoded_question],
axis=-1)
answer = (answer_vocabulary_size,
activation='softmax')(concatenated)
# Specify two inputs and outputs when the model is instantiated
model = Model([text_input, question_input], answer)
(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['acc'])
 
 
import numpy as np
num_samples = 1000
max_length = 100
text = (1, text_vocabulary_size, 
                        size=(num_samples, max_length))
question = (1, question_vocabulary_size,
size=(num_samples, max_length))
answers = (answer_vocabulary_size, size=(num_samples))
answers = .to_categorical(answers, answer_vocabulary_size)
([text, question], answers, epochs=10, batch_size=128)
# Fit using a dictionary composed of inputs (only after naming the inputs) Fit using a list composed of inputs
({'text': text, 'question': question}, answers,
          epochs=10, batch_size=128)
 

from keras import layers
from keras import Input
from  import Model
vocabulary_size = 50000
num_income_groups = 10
posts_input = Input(shape=(None,), dtype='int32', name='posts')
embedded_posts = (256, vocabulary_size)(posts_input)
x = layers.Conv1D(128, 5, activation='relu')(embedded_posts)
x = layers.MaxPooling1D(5)(x)
x = layers.Conv1D(256, 5, activation='relu')(x)
x = layers.Conv1D(256, 5, activation='relu')(x)
x = layers.MaxPooling1D(5)(x)
x = layers.Conv1D(256, 5, activation='relu')(x)
x = layers.Conv1D(256, 5, activation='relu')(x)
x = layers.GlobalMaxPooling1D()(x)
x = (128, activation='relu')(x)
# Note that the output layers all have names
age_prediction = (1, name='age')(x)
income_prediction = (num_income_groups,
activation='softmax',
name='income')(x)
gender_prediction = (1, activation='sigmoid', name='gender')(x)
model = Model(posts_input,
[age_prediction, income_prediction, gender_prediction])
 
 
# Develop different loss functions
(optimizer='rmsprop',
loss=['mse', 'categorical_crossentropy', 'binary_crossentropy'])
# Equivalent to the above writeup (this writeup can only be used if the output layer has a name)
(optimizer='rmsprop',
loss={'age': 'mse',
'income': 'categorical_crossentropy',
'gender': 'binary_crossentropy'})
 
 
# Assume that age_targets, income_targets, and gender_targets are Numpy arrays.
(posts, [age_targets, income_targets, gender_targets],
epochs=10, batch_size=64)
# Equivalent
(posts, {'age': age_targets,
'income': income_targets,
'gender': gender_targets},
epochs=10, batch_size=64)
 
 
due to,Mean square error of age regression task(MSE)The value of the loss is usually in the 3~5 control,And the cross-entropy loss values used for gender categorization tasks may be as low as 0.1。under these circumstances,To balance the contribution of different losses,We can let the weights of the cross-entropy loss take 10,(indicates contrast) MSE The weight of the loss is taken 0.5
(optimizer='rmsprop',
loss=['mse', 'categorical_crossentropy', 'binary_crossentropy'],
loss_weights=[0.25, 1., 10.])
 
# Equivalent
(optimizer='rmsprop',
loss={'age': 'mse',
'income': 'categorical_crossentropy',
'gender': 'binary_crossentropy'},
loss_weights={'age': 0.25,
'income': 1.,
'gender': 10.})

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.