简体   繁体   中英

How can I save/overwrite my TensorFlow/Keras model ONLY when validation accuracy improves?

There is a lot already out there about saving models, but I'm struggling to work out how I can save my model only when it improves upon val_accuracy . My model looks like this:

model = keras.Sequential([
    keras.layers.Embedding(numberOfWords,
                           embedding_vector_length, input_length=1000),
    keras.layers.LSTM(128),
    keras.layers.Dropout(0.3),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dropout(0.3),
    keras.layers.Dense(64, activation='relu'),
    keras.layers.Dropout(0.3),
    keras.layers.Dense(1, activation='sigmoid')
])

model.compile(optimizer=tf.keras.optimizers.Adam(lr=1e-3, decay=1e-5), loss='binary_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=200, batch_size=32,
          validation_data=(x_test, y_test))

During training, I want to save the model after the first epoch. Then, after every epoch, if val_accuracy has been improved upon, I want to overwrite the old model with the new one.

How do I do this?

you just have to define a Callback-List and enter it into the model.fit declaration: Keras_fit In this example its just safing the best weigths , so its actually overwriting the old ones, and safes it into an hdf5 format. Hope that solved your problem :)

from keras.callbacks import ModelCheckpoint

filepath="weights.best.hdf5"

checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1,save_best_only=True, mode='max')
callbacks_list = [checkpoint]


model.fit(x_train, y_train, epochs=200,,callbacks=callbacks_list, batch_size=32,
          validation_data=(x_test, y_test))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM