加载训练有素的Keras模型并继续训练

编程入门 行业动态 更新时间:2024-10-24 08:24:31
本文介绍了加载训练有素的Keras模型并继续训练的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道是否有可能保存经过部分训练的Keras模型并在再次加载模型后继续进行训练.

I was wondering if it was possible to save a partly trained Keras model and continue the training after loading the model again.

这样做的原因是,将来我将拥有更多的训练数据,并且我不想再次对整个模型进行训练.

The reason for this is that I will have more training data in the future and I do not want to retrain the whole model again.

我正在使用的功能是:

#Partly train model model.fit(first_training, first_classes, batch_size=32, nb_epoch=20) #Save partly trained model model.save('partly_trained.h5') #Load partly trained model from keras.models import load_model model = load_model('partly_trained.h5') #Continue training model.fit(second_training, second_classes, batch_size=32, nb_epoch=20)

添加了完整的示例

第一个数据集在10个纪元后,最后一个纪元的损失为0.0748,准确度为0.9863.

With the first dataset after 10 epochs the loss of the last epoch will be 0.0748 and the accuracy 0.9863.

保存,删除和重新加载模型后,第二个数据集上训练的模型的损失和准确性分别为0.1711和0.9504.

After saving, deleting and reloading the model the loss and accuracy of the model trained on the second dataset will be 0.1711 and 0.9504 respectively.

这是由新的训练数据还是完全重新训练的模型引起的?

Is this caused by the new training data or by a completely re-trained model?

""" Model by: machinelearningmastery/ """ # load (downloaded if needed) the MNIST dataset import numpy from keras.datasets import mnist from keras.models import Sequential from keras.layers import Dense from keras.utils import np_utils from keras.models import load_model numpy.random.seed(7) def baseline_model(): model = Sequential() model.add(Dense(num_pixels, input_dim=num_pixels, init='normal', activation='relu')) model.add(Dense(num_classes, init='normal', activation='softmax')) modelpile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model if __name__ == '__main__': # load data (X_train, y_train), (X_test, y_test) = mnist.load_data() # flatten 28*28 images to a 784 vector for each image num_pixels = X_train.shape[1] * X_train.shape[2] X_train = X_train.reshape(X_train.shape[0], num_pixels).astype('float32') X_test = X_test.reshape(X_test.shape[0], num_pixels).astype('float32') # normalize inputs from 0-255 to 0-1 X_train = X_train / 255 X_test = X_test / 255 # one hot encode outputs y_train = np_utils.to_categorical(y_train) y_test = np_utils.to_categorical(y_test) num_classes = y_test.shape[1] # build the model model = baseline_model() #Partly train model dataset1_x = X_train[:3000] dataset1_y = y_train[:3000] model.fit(dataset1_x, dataset1_y, nb_epoch=10, batch_size=200, verbose=2) # Final evaluation of the model scores = model.evaluate(X_test, y_test, verbose=0) print("Baseline Error: %.2f%%" % (100-scores[1]*100)) #Save partly trained model model.save('partly_trained.h5') del model #Reload model model = load_model('partly_trained.h5') #Continue training dataset2_x = X_train[3000:] dataset2_y = y_train[3000:] model.fit(dataset2_x, dataset2_y, nb_epoch=10, batch_size=200, verbose=2) scores = model.evaluate(X_test, y_test, verbose=0) print("Baseline Error: %.2f%%" % (100-scores[1]*100))

推荐答案

实际上-model.save保存了根据您的情况重新开始培训所需的所有信息.重新加载模型可能会破坏的唯一事情是优化器状态.要进行检查,请尝试save并重新加载模型,并根据训练数据对其进行训练.

Actually - model.save saves all information need for restarting training in your case. The only thing which could be spoiled by reloading model is your optimizer state. To check that - try to save and reload model and train it on training data.

更多推荐

加载训练有素的Keras模型并继续训练

本文发布于:2023-11-10 09:50:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575054.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:训练有素   模型   加载   Keras

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!