Tensorflow/Keras -在一个循环中使用相同的层名构建多个模型



我想在for循环中多次构建相同的模型:

### 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()

我得到下面的结果。

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     
_________________________________________________________________
..........
_________________________________________________________________

我想把'conv2d'作为我的第一个conv2d图层名称,'conv2d_1'作为我的第二个conv2d图层名称。

是否有一种方法可以在我所有的模型中获得相同的层名/层参考id ?

这是使用所有keras.layers.Layer子节点继承的name关键字参数的好机会:

model = Sequential([InputLayer(input_shape=(28,28,1), name="my_input"),Conv2D(32, 3, name="my_conv"),...])

最新更新