[Learn about machine learning from the Keras] — 6. Valid usage time of Model’s Add
When describing the Sequence construction instance in the previous article, after passing in the layer object array as the constructor parameter, Sequence will automatically add the objects in the layer object array to keras.engine.sequential.Sequential.layers and keras.engine.sequential one by one. Sequential._self_tracked_trackables in two list collections.
If I additionally use keras.engine.sequential.Sequential.Add to join, when can I click to add it?
Because we know from the previous model fit article that if the model of the keras.engine.sequential.Sequential instance does not execute model build, the initial model build action will be performed in model fit, that is, the keras.engine.sequential._build_graph_network_for_inferred_shape function will be executed. keras.engine.functional._init_graph_network function to ensure that the layers of the model are constructed one by one. So what is certain is that if you have not done a model build, at least fit will detect it and do it again.
But if the model build function is executed before model fit, when will the execution of keras.engine.sequential.Sequential.Add be effective?
In fact, it can be effectively added at any time before model fit (of course, the creation of the Sequential entity must be completed). Of course, it must be no problem to add it before model build. Is there any problem after model build? Yes.
The reason is that in the final stage of keras.engine.sequential.Sequential.Add, if it is found that the model has been built, keras.engine.sequential.Sequential._graph_initialized is set to True because keras.engine.functional._init_graph_network has been executed, which means The link of the topology formed by each previous layer has been found. Just update the last output layer to the newly added layer object and execute keras.engine.functional._init_graph_network(inputs, outputs) again. Construct a new topology link and reset keras.engine.sequential._self_tracked_trackables and keras.engine.training.Model.layers.
Examples are as follows:
from tensorflow import keras
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.models import Sequential
from tensorflow.keras import layers
model = Sequential([
layers.Dense(512, activation="relu")
])
model.compile(optimizer="rmsprop",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
model.build(input_shape=(None, 784))
model.add(layers.Dense(10, activation="softmax"))
model.add(layers.Dense(20, activation="relu"))
print(model.layers[0])
print(model.layers[1])
print(model.layers[2])
print(model._self_tracked_trackables[0])
print(model._self_tracked_trackables[1])
print(model._self_tracked_trackables[2])
print(model._self_tracked_trackables[3])
model.summary()