【问题标题】:How to save model in keras python?如何在keras python中保存模型?
【发布时间】:2018-05-02 20:02:31
【问题描述】:
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进行分类。

【问题讨论】:

标签: deep-learning keras


【解决方案1】:

在保存模型之前,您需要使用classifier2.fit()对其进行训练

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

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

【讨论】:

    【解决方案2】:

    这里如何保存模型:

    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)
    

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

    【讨论】:

      猜你喜欢
      • 2022-07-25
      • 2020-06-20
      • 2018-11-13
      • 2018-12-14
      • 2020-05-09
      • 2018-06-25
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多