层重量形状(474、60)与提供的重量形状(474、120)不兼容

时间:2018-12-09 21:18:49

标签: python numpy keras neural-network keras-layer

我有一个网络,其规格如下:

Layer (type)                 Output Shape              Param #
input_38 (InputLayer)        (None, 474)               0
dense_149 (Dense)            (None, 120)               57000
dense_150 (Dense)            (None, 120)               14520
dropout_38 (Dropout)         (None, 120)               0
dense_151 (Dense)            (None, 120)               14520
dense_152 (Dense)            (None, 1)                 121

现在,我正在训练模型并使用以下命令保存前2个密集层的权重:

reshaped_weights1 = model.layers[1].get_weights()
reshaped_weights2 = model.layers[2].get_weights()

,并尝试在具有与上述相同结构的另一个模型(微调)中使用这些权重,但要使用一部分数据来训练第一个模型。该模型如下所示:

def createModelHelper1(dropoutRate = 0.0, numNeurons=40, optimizer = 'adam', numNeuronsFirstTwo=40):
    inputLayer = Input(shape=(data.shape[1],))
    denseLayer1 = Dense(numNeuronsFirstTwo, kernel_regularizer=l2(0.001))(inputLayer)
    denseLayer2 = Dense(numNeuronsFirstTwo, kernel_regularizer=l2(0.001))(denseLayer1)
    dropoutLayer = Dropout(dropoutRate)(denseLayer2)
    denseLayer3 = Dense(numNeurons, kernel_regularizer=l2(0.001))(dropoutLayer)
    outputLayer = Dense(1, activation='sigmoid')(denseLayer3)

    model = Model(input=inputLayer, output=outputLayer)
    model.layers[1].set_weights(reshaped_weights1) #ERROR
    model.layers[1].trainable = False #freezing the layer

    model.layers[2].set_weights(reshaped_weights2) 
    model.layers[2].trainable = False #freezing the layer

    model.compile(loss='binary_crossentropy', optimizer=optimizer)
    return model

我收到错误提示:

The weight shape (474,60) not compatible with provided weight shape (474, 120).

我在这里做错了什么?两个模型的结构完全相同吗? 提前非常感谢!

0 个答案:

没有答案