为什么在删除和重新创建模型后,keras中的图层编号不会从1开始?

时间:2018-06-13 10:29:11

标签: python keras

我是keras的新手,我正在尝试创建一个CNN模型。我创建了一个顺序模型如下 -

model = models.Sequential()
model.add(layers.Conv2D(32, (5, 5), activation='relu', input_shape=(150, 150, 3)))
model.add(layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
print(model.summary())

我得到如下摘要 -

  
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 146, 146, 32)      2432      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 73, 73, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 71, 71, 64)        18496     
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 35, 35, 64)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 33, 33, 64)        36928     
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 16, 16, 64)        0         
=================================================================
Total params: 57,856
Trainable params: 57,856
Non-trainable params: 0
_________________________________________________________________

在此之后,我使用del model删除模型并再次使用上面的代码创建它,我得到如下摘要 -

  
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_4 (Conv2D)            (None, 146, 146, 32)      2432      
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 73, 73, 32)        0         
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 71, 71, 64)        18496     
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 35, 35, 64)        0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 33, 33, 64)        36928     
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 16, 16, 64)        0         
=================================================================
Total params: 57,856
Trainable params: 57,856
Non-trainable params: 0
_________________________________________________________________

那么,为什么此摘要显示来自 conv2d_4 的图层编号,它应该来自 conv2d_1

即使我创建了另一个模型 -

model_2 = models.Sequential()
model_2.add(layers.Conv2D(32, (5, 5), activation='relu', input_shape=(150, 150, 3)))
model_2.add(layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model_2.add(layers.Conv2D(64, (3, 3), activation='relu'))
model_2.add(layers.MaxPooling2D((2, 2)))
model_2.add(layers.Conv2D(64, (3, 3), activation='relu'))
model_2.add(layers.MaxPooling2D((2, 2)))
print(model_2.summary())

我在前一个模型的最后一个图层编号后开始编号 -

  
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_7 (Conv2D)            (None, 146, 146, 32)      2432      
_________________________________________________________________
max_pooling2d_7 (MaxPooling2 (None, 73, 73, 32)        0         
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 71, 71, 64)        18496     
_________________________________________________________________
max_pooling2d_8 (MaxPooling2 (None, 35, 35, 64)        0         
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 33, 33, 64)        36928     
_________________________________________________________________
max_pooling2d_9 (MaxPooling2 (None, 16, 16, 64)        0         
=================================================================
Total params: 57,856
Trainable params: 57,856
Non-trainable params: 0
_________________________________________________________________

1 个答案:

答案 0 :(得分:0)

这取决于您使用的后端:

你可以看到(https://github.com/keras-team/keras/blob/master/keras/engine/base_layer.py#L132):

name = _to_snake_case(prefix) + '_' + str(K.get_uid(prefix))

并且Keras没有重置del model上的uid。当使用张量流后端时,Keras会在clear_session()上重置uid。