gpt4 book ai didi

python - 加载经过训练的 Keras 模型并继续训练

转载 作者:IT老高 更新时间:2023-10-28 21:41:55 28 4
gpt4 key购买 nike

我想知道是否可以保存部分训练的 Keras 模型并在再次加载模型后继续训练。

这样做的原因是我以后会有更多的训练数据,我不想再重新训练整个模型。

我正在使用的功能是:

#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)

编辑 1:添加了完整的工作示例

第一个数据集在 10 个 epoch 之后,最后一个 epoch 的损失将为 0.0748,准确度为 0.9863。

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

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

"""
Model by: http://machinelearningmastery.com/
"""
# 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'))
model.compile(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))

编辑 2:tensorflow.keras 备注

对于 tensorflow.keras,将参数 nb_epochs 更改为模型拟合中的 epochs。导入和basemodel函数是:

import numpy
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import load_model


numpy.random.seed(7)

def baseline_model():
model = Sequential()
model.add(Dense(num_pixels, input_dim=num_pixels, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model

最佳答案

实际上 - model.save 在您的案例中保存了重新开始训练所需的所有信息。唯一可能被重新加载模型破坏的是您的优化器状态。要检查这一点 - 尝试 save 并重新加载模型并在训练数据上对其进行训练。

关于python - 加载经过训练的 Keras 模型并继续训练,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42666046/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com