优化定制的Keras模型

时间:2019-08-29 02:36:37

标签: keras

我有一个经过5个班级训练的keras模型,模型的最后一层看起来像这样

dr_steps = Dropout(0.25)(Dense(128, activation = 'relu')(gap_dr))
out_layer = Dense(5, activation = 'softmax')(dr_steps)
model = Model(inputs = [in_lay], outputs = [out_layer])

我想做的是在8类多标签问题上微调此模型,但是我不确定如何实现这一点,这是我尝试过的

dr_steps = Dropout(0.25)(Dense(128, activation = 'relu')(gap_dr))
out_layer = Dense(t_y.shape[-1], activation = 'softmax')(dr_steps)
model = Model(inputs = [in_lay], outputs = [out_layer])
weights_path = 'weights.best.hdf5'
retina_model.load_weights(weights_path)
model.layers.pop()
output = Dense(8, activation = 'sigmoid')(model.layers[-1].output)
model = Model(inputs = [in_lay], outputs = [output])
loss = 'binary_crossentropy'
model.compile(optimizer = RAdam(), loss = FocalLoss,
                         metrics = ["binary_accuracy",precision, recall,auc])

但这会引发这样的错误

raise ValueError(str(e))
ValueError: Dimension 1 in both shapes must be equal, but are 8 and 5. Shapes are [128,8] and [128,5]. for 'Assign_390' (op: 'Assign') with input shapes: [128,8], [128,5].

任何有关如何微调此模型的建议都将非常有帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

在这里

model = Model(inputs = [in_lay], outputs = [out_layer])
weights_path = 'weights.best.hdf5'

out_layer 应该具有在 weights.best.hdf5 中描述的相同尺寸(5个类)。

因此,t_y.shape[-1]的尺寸应为5,而不是8。