[Learn about machine learning from the Keras] — 5.Model Build operaton process

Czxdas
3 min readSep 21, 2023

--

In the description of model.fit in the previous section, it was mentioned that the model will check whether the build action has been performed on the model and the included layers before training. In fact, you can add a program before model.fit to perform this action, and use model.build to execute it.

from tensorflow.keras.models import Sequential
from tensorflow.keras.datasets import mnist

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype("float32") / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype("float32") / 255

from tensorflow.keras import layers
from tensorflow.keras.models import Model

model = Sequential([
layers.Dense(512, activation="relu"),
layers.Dense(10, activation="softmax")
])

model.build(input_shape=(None, 784))

model.compile(optimizer="rmsprop",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])

model.fit(train_images, train_labels, epochs=5, batch_size=128)

How will model.build work?

(1)
It will first check whether the parameters of model.build are passed into input_shape, which is the input dimension. If not, an exception error will be issued and input_shape will be passed in.

(2)
Execute the keras.engine.sequential._build_graph_network_for_inferred_shape function, pass in the input_shape dimension to keras.engine.input_layer.Input. First create an input layer entity, and then start with this input layer, iterate keras.engine.sequential.layers each time The added layers are connected in series. In this example, keras.engine.base_layer.call will be called during each layer iteration, and then through
keras.engine.base_layer._functional_construction_call
-> keras.engine.base_layer._keras_tensor_symbolic_call
-> keras.engine.base_layer._infer_output_signature
Execute the call of each keras.layers.core.dense.Dense entity, which is the call function implemented by the layer entity. Each layer call will check whether the layer itself has been built, and if not, the build action will be performed. In this example, the layer itself keras.layers.core.dense executes keras.layers.core.dense.build. The build will definitely pass in the received input entity. You must understand the dimensions of the received tensor. Then the keras.engine.base_layer.add_weight function will be used to initialize the kernel (weight) and bias (fault tolerance) attribute settings of keras.layers.core.dense corresponding to the parameter content of the originally declared keras.layers.core.dense.

Then, the inner product of the input and kernel weights is converted by the activation function and bias is added to generate the output layer, which becomes the input layer of the next iteration, and repeats until the last layer. The final output layer will be passed back. Here we are making connections between layers and ensuring that layers are related.

So far, the weights of the model are only initial settings. The keras.engine.sequential.Sequential.built property will also be set to True to indicate that the model has been built. Also mark keras.engine.sequential.Sequential._graph_initialized set to True.

(3)
Pass the initial input layer and the output layer generated by (2) into the keras.engine.functional._init_graph_network function to re-initialize the graph network for model.Sequential, and enter keras.engine through keras.engine.functional._map_graph_network. functional ._build_map executes recursion keras.engine.functional._build_map_helper to find the links of the topology composed by each layer, and then specify keras.engine.sequential.Sequential.input as the input layer entity and keras.engine. sequential .Sequential.output is the output layer entity.
The latest graph network has been set up.

Finally, model.build can be executed before model.fit, and it does not matter whether it is before or after model.compiler, the model can still be successfully constructed without any problem.

--

--

Czxdas
Czxdas

Written by Czxdas

Keep looking for Prajna wisdom

No responses yet