[英]Tensorflow/Keras - Building multiple models in a loop with the same layer names
I want to build the same model multiple times in a for loop:我想在 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()
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.我想将“conv2d”作为我的第一个 Conv2D 图层名称,将“conv2d_1”作为我的第二个 Conv2D 图层名称。
Is there a way I can get the same layer name / layer reference id in all my models?有没有办法可以在我的所有模型中获得相同的图层名称/图层参考 ID?
这是使用所有keras.layers.Layer
子级继承的name
关键字参数的好机会:
model = Sequential([InputLayer(input_shape=(28,28,1), name="my_input"),Conv2D(32, 3, name="my_conv"),...])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.