如何分割在keras中训练的模型?

omjgkv6w  于 2023-04-30  发布在  其他
关注(0)|答案(3)|浏览(174)

我训练了一个有4个隐藏层和2个密集层的模型,并保存了该模型。
现在,我想加载这个模型并将其拆分为两个模型,一个有一个隐藏层,另一个只有密集层。
我用下面的方法分割了模型与隐藏层

model = load_model ("model.hdf5")
HL_model = Model(inputs=model.input, outputs=model.layers[7].output)

这里的模型是加载模型,在第7层是我的最后一个隐藏层。我试着把密集的东西分开
DL_model = Model(inputs=model.layers[8].input, outputs=model.layers[-1].output)
我犯了错误

TypeError: Input layers to a `Model` must be `InputLayer` objects.

拆分后,HL_model的输出将成为DL_model的输入。
谁能帮我创建一个模型与密集层?
PS:我也试过下面的代码

from keras.layers import Input 
inputs = Input(shape=(9, 9, 32), tensor=model_1.layers[8].input)
model_3 = Model(inputs=inputs, outputs=model_1.layers[-1].output)

得到的错误为

RuntimeError: Graph disconnected: cannot obtain value for tensor Tensor("conv2d_1_input:0", shape=(?, 144, 144, 3), dtype=float32) at layer "conv2d_1_input". The following previous layers were accessed without issue: []

这里**(144,144,3)中的输入图像尺寸为型号**。

falq053o

falq053o1#

你需要先指定一个新的Input层,然后在其上堆叠其余的层:

DL_input = Input(model.layers[8].input_shape[1:])
DL_model = DL_input
for layer in model.layers[8:]:
    DL_model = layer(DL_model)
DL_model = Model(inputs=DL_input, outputs=DL_model)
slhcrj9b

slhcrj9b2#

更一般一点。可以使用以下函数拆分模型

from keras.layers import Input
from keras.models import Model
def get_bottom_top_model(model, layer_name):
    layer = model.get_layer(layer_name)
    bottom_input = Input(model.input_shape[1:])
    bottom_output = bottom_input
    top_input = Input(layer.output_shape[1:])
    top_output = top_input

    bottom = True
    for layer in model.layers:
        if bottom:
            bottom_output = layer(bottom_output)
        else:
            top_output = layer(top_output)
        if layer.name == layer_name:
            bottom = False

    bottom_model = Model(bottom_input, bottom_output)
    top_model = Model(top_input, top_output)

    return bottom_model, top_model
bottom_model, top_model = get_bottom_top_model(model, "dense_1")

Layer_name只是要分割的图层的名称。

368yc8dk

368yc8dk3#

对于顺序keras模型,我们可以执行以下操作:
假设,这是我的模型:

hidden_units = 256
dropout = 0.45
model = Sequential()
model.add(Dense(hidden_units, activation='relu', input_dim=784))
model.add(Dropout(dropout))
model.add(Dense(hidden_units, activation='relu'))
model.add(Dropout(dropout))
model.add(Dense(hidden_units, activation='relu'))
model.add(Dropout(dropout))
model.add(Dense(10, activation='softmax'))
model.summary()

我想在第三层分割它(i。即,密度_9)

Model: "sequential_2"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense_8 (Dense)             (None, 256)               200960    
                                                                 
 dropout_6 (Dropout)         (None, 256)               0         
                                                                 
 dense_9 (Dense)             (None, 256)               65792     
                                                                 
 dropout_7 (Dropout)         (None, 256)               0         
                                                                 
 dense_10 (Dense)            (None, 256)               65792     
                                                                 
 dropout_8 (Dropout)         (None, 256)               0         
                                                                 
 dense_11 (Dense)            (None, 10)                2570      
                                                                 
=================================================================
Total params: 335,114
Trainable params: 335,114
Non-trainable params: 0
_________________________________________________________________

模型的尾部,i。即,接受用户输入的模型:

model_tail = keras.Sequential(model.layers[:3])

而头部只是由第三层之后的层组成。但是,我们需要向其添加一个输入层。这个输入层需要消耗尾部模型的输出。(也在另一个answer中提到)

input_tail =  keras.layers.Input(model.layers[3].input_shape[1:])
model_head = keras.Sequential([input_tail] + model.layers[3:])

我们使用它如下:

tail_pred = model_tail.predict(some_input)
head_pred = model_head.predict(tail_pred)
head_pred.argmax(axis=-1) # assuming a simple classifier

相关问题