如何在keras python中保存模型?

时间:2017-11-19 14:14:32

标签: deep-learning keras

Model description:
cnn1=Sequential()
cnn1.add(Conv2D(128,(2,300), activation = 'relu',input_shape = (maxLenofSent,300,1)))

cnn1.add(MaxPooling2D(1,3))

cnn1.add(Flatten())
cnn1.add(Dense(100, activation = 'relu'))

cnn2=Sequential()
cnn2.add(Conv2D(128,(2,300), activation = 'relu',input_shape = (maxLenofSent,300,1)))
cnn2.add(MaxPooling2D(1,3))
cnn2.add(Flatten())
cnn2.add(Dense(100, activation = 'relu'))


classifier2=Sequential()
classifier2.add(Merge([cnn1,cnn2], mode='concat'))
classifier2.add(Dense(70,activation='sigmoid'))
classifier2.add(Dropout(0.2))
classifier2.add(Dense(2,activation='tanh'))
sgd = SGD(lr = 0.01, momentum = 0.9, decay=1e-2, nesterov = False)
classifier2.compile(loss = 'categorical_crossentropy', optimizer = sgd, metrics = ['accuracy'])

如何保存完整模型,以便以后可以用于测试。两个cnn的输出转到ann并分类。

2 个答案:

答案 0 :(得分:0)

这里有如何保存模型:

model_json = model.to_json()
with open("<path.json>", "w") as json_file:
    json_file.write(model_json)
model.save_weights("<path.hdf5>", overwrite=True)

如果您想在每个纪元保存模型和权重,请尝试搜索回调。

答案 1 :(得分:0)

在保存模型之前,您需要使用classifier2.fit()

进行训练

https://keras.io/models/sequential/#fit

保存模型使用classifier2.save('filename.hdf5')