我用下面的代码来创建使用VGG16 CNN的模型,但创建模型之后,该模型的输入层从结构消失(参照图像)。
为什么输入层从结构上消失?
vgg16_model = keras.applications.vgg16.VGG16()
model = Sequential([])
for layer in vgg16_model.layers[:-1]:
model.add(layer)
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax', name = 'prediction'))
该模型结构
这仅仅是Keras模型表示的神器使用顺序API时,它有没有实际效果责任:在Input
层是有含蓄的,但它不被认为是适当的层,并没有在model.summary()
露面。它不会出现如果使用功能API。
请看下面的两个相同的机型,采用了两种不同的API编写的:
连续API
from keras.models import Sequential
from keras.layers import Dense # notice that we don't import Input here...
model_seq = Sequential([
Dense(64, input_shape=(784,),activation='relu'),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
model_seq.summary()
# result:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 64) 50240
_________________________________________________________________
dense_2 (Dense) (None, 64) 4160
_________________________________________________________________
dense_3 (Dense) (None, 10) 650
=================================================================
Total params: 55,050
Trainable params: 55,050
Non-trainable params: 0
_________________________________________________________________
功能API
from keras.models import Model
from keras.layers import Input, Dense # explicitly import Input layer
inputs = Input(shape=(784,))
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)
model_func = Model(inputs=inputs, outputs=predictions)
model_func.summary()
# result:
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 784) 0
_________________________________________________________________
dense_1 (Dense) (None, 64) 50240
_________________________________________________________________
dense_2 (Dense) (None, 64) 4160
_________________________________________________________________
dense_3 (Dense) (None, 10) 650
=================================================================
Total params: 55,050
Trainable params: 55,050
Non-trainable params: 0
_________________________________________________________________
这两种模式是相同的;的事实Input
层不model.summary()
明确地显示出来,当连续API使用关于模型的功能并不意味着什么。编辑:丹尼尔·默勒正确地在下面的评论中指出,它甚至不是一个真正的层,什么都不做,除了定义输入形状(注意上面的model_func.summary
其0训练参数)。
换句话说,没有后顾之忧......
此相关的线程可能是有用的,太:Keras Sequential model input layer