简体   繁体   English

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

[英]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.

相关问题 Keras/Tensorflow:在同一个 GPU 上循环或使用 Process 训练多个模型 - Keras/Tensorflow: Train multiple models on the same GPU in a loop or using Process 在 Keras 中合并多个模型(tensorflow) - Merge multiple Models in Keras (tensorflow) 尽管我使用相同的层模块构建了完全相同的模型,但 Tensorflow 和 Keras 显示出一些不同的结果 - Tensorflow and Keras show a little different result even though I build exactly same models using same layer modules 将新层添加到 tensorflow.python.keras.models.ZA559B87068921EEC784086CE5485 - Add a new layer to a tensorflow.python.keras.models.Model 带有 Keras 和 Tensorflow 的自定义层 - Custom layer with Keras and Tensorflow Keras:在不同模型中使用同一层(共享权重) - Keras: Use the same layer in different models (share weights) 如何在 TensorFlow 中将“for loop”的迭代器范围作为 keras 输入层传递? - How to pass iterator range for `for loop` as keras input layer in TensorFlow? 如何在 Tensorflow 2.x Keras 自定义层中使用多个输入? - How to use multiple inputs in Tensorflow 2.x Keras Custom Layer? 同时在不同的 GPU 上训练多个 keras/tensorflow 模型 - Train multiple keras/tensorflow models on different GPUs simultaneously 在keras层中包裹张量流函数 - Wrap tensorflow function in keras layer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM