我想为cifar10数据集在keras中训练2个模型。首先是从头开始(model1),其次是微调一个预先训练过的模型(model2)。我使用以下代码来做到这一点:
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
import numpy as np
import os
from keras.models import load_model
#model 1
input_shape = (32, 32, 3)
model1 = Sequential()
model1.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
model1.add(Conv2D(64, (3, 3), activation='relu'))
model1.add(MaxPooling2D(pool_size=(2, 2)))
model1.add(Dropout(0.25))
model1.add(Flatten())
model1.add(Dense(128, activation='relu'))
model1.add(Dropout(0.5))
model1.add(Dense(10, activation='softmax'))
#... training
#model 2
kmodel = load_model('cifar10\\cifar10.h5')
model2=Sequential()
for i in range (len(kmodel.layers)):
model2.add(kmodel.layers[i])我想知道:
模型1中的:
如何在一些中间层之后添加softmax层(model1.add(Dense(10, activation='softmax'))),以便使每个新的softmax层只与前一层连接,而不与下一层连接?
模型2中的:
如何将softmax层添加到中间层(即第2层、第4层、第7层)?(当然,我应该冻结所有的kmodel层,只需要训练新的softmax层)
发布于 2018-08-07 06:16:54
这里的限制是Keras的Sequential()操作符,它只允许线性堆栈层。
为了避免这种情况,我们可以通过一种更直接(但更丑)的方式,如前所述来指定模型。对于您的代码,它应该是这样的:
input_shape = (32, 32, 3)
x = Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape)(inputs)
x = Conv2D(64, kernel_size=(3, 3),activation='relu',input_shape=input_shape)(x)
...
predictions = Dense(10, activation='softmax')(x)然后,您可以简单地将中间层中的预测指定为
input_shape = (32, 32, 3)
x = Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape)(inputs)
x = Conv2D(64, kernel_size=(3, 3),activation='relu',input_shape=input_shape)(x)
...
# do the splitting at some random point of your choice
predictions_intermediary = Dense(10, activation='softmax')(x)
# regular next layer
x = Dense(128, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)遗憾的是,我对Keras还不太熟悉,无法告诉您预培训模型是如何工作的,但我假设您可以类似地定义预培训模型,然后指定可训练的层,就像前面的示例一样。
请注意,您的“共享还是拆分”问题在这里已经过时,因为创建不同的层/操作将自动创建不同的权重矩阵,因此您不必担心在这里有共享权重(如果与softmax输入形状相比,在下一个输入层中有不同的输入维度,则无论如何都不能工作)。
https://stackoverflow.com/questions/51711832
复制相似问题