[Learn about machine learning from the Keras] — 10.Model.fit vs Sequence.fit

Czxdas
2 min readSep 22, 2023

Because the previous article briefly described the differences between creating an instance of the keras.engine.training.Model class and the keras.engine.Sequential class when creating a model instance, here we will continue to explain the differences in how to do model fit. . For the operation of keras.engine.Sequential’s fit, please refer to the previous article.

from tensorflow.keras.layers import Input, Dense 
from tensorflow.keras.models import Model

inputs = Input(shape=(784,))
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)

model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(data, labels)

The operation of keras.engine.training.Model.fit() is mainly to run a loop consisting of the number of steps after each epoch and batch_size are set. Each time the keras.engine.training.Model.train_step training function is run, . This is the same as keras.engine.Sequential.

The difference lies in the execution of the keras.engine.training.Model.train_step training function.

The basic execution sequence in keras.engine.training.Model.train_step is keras.engine.training.Model.call -> keras.engine.base_layer.call (each layer performs pre-call inspection and pre-training processing).

In this example, the model’s keras.engine.functional.call will then be executed and will go directly to keras.engine.functional._run_internal_graph. Because the keras.engine.functional._init_graph_network has been executed to build the model before when creating the entity, the layer associations have been searched out recursively and stored in the built-in list, so keras.engine.functional._run_internal_graph is This data will be called out in order to call the respective call functions of each node layer. In fact, the respective call functions are the calls of the parent class of each layer, keras.engine.base_layer.call. After executing the fetching settings and doing corresponding processing, they actually go to the keras.layers.core.dense.call function, that is The call function implemented by the entity of the layer. The call function of the layer entity will actually perform the inner product of the input tensor and its own weight (kernel), plus the bias tensor variable, and finally convert the output tensor through the specified activation function. The output tensor is then converted into the input tensor of the next layer, and the above actions are repeated until the output of the last layer is generated.

The subsequent execution content is the same as keras.engine.Sequential, calculating the loss value and update weight.
Therefore, this section can be connected with the previous section. To sum up, the model ensures that after the keras.engine.sequential._build_graph_network_for_inferred_shape function and the keras.engine.functional._init_graph_network function are executed, it means that the build has been completed and you can continue to follow each layer. settings to calculate the output tensor.

--

--