I want to build the same model multiple times in a for loop:
### Building the model ###
def build_model():
# create
model = Sequential([
InputLayer(input_shape = (28, 28, 1)),
Conv2D(32, (3, 3)),
Activation('relu'),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3)),
Activation('relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(num_classes),
Activation('softmax')
])
# compile
model.compile(
loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = [ 'accuracy' ]
)
# return
return model
### Fit 100 models ###
for i in range(2):
model = build_model()
model.summary()
I get the results below.
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 26, 26, 32) 320
_________________________________________________________________
activation (Activation) (None, 26, 26, 32) 0
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 13, 13, 32) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 11, 11, 64) 18496
_________________________________________________________________
..........
_________________________________________________________________
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_2 (Conv2D) (None, 26, 26, 32) 320
_________________________________________________________________
activation_3 (Activation) (None, 26, 26, 32) 0
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 13, 13, 32) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 11, 11, 64) 18496
_________________________________________________________________
..........
_________________________________________________________________
I would like to have 'conv2d' as my first Conv2D layer name and 'conv2d_1' as my second Conv2D layer name.
Is there a way I can get the same layer name / layer reference id in all my models?
CodePudding user response:
This is a good opportunity to use the name
keyword argument inherited by all keras.layers.Layer
children:
model = Sequential([InputLayer(input_shape=(28,28,1), name="my_input"),Conv2D(32, 3, name="my_conv"),...])