在Keras中,可以使用load_model
函數(shù)加載單個模型,但如果要加載多個模型,可以使用model_from_json
和model_from_yaml
函數(shù)。這兩個函數(shù)可以分別從JSON文件或YAML文件中加載模型結(jié)構(gòu),并使用load_weights
函數(shù)加載模型權(quán)重。
以下是加載多個模型的示例代碼:
from keras.models import model_from_json
# 加載模型結(jié)構(gòu)
with open('model1.json', 'r') as json_file:
model1_json = json_file.read()
model1 = model_from_json(model1_json)
# 加載模型權(quán)重
model1.load_weights('model1_weights.h5')
# 加載模型結(jié)構(gòu)
with open('model2.json', 'r') as json_file:
model2_json = json_file.read()
model2 = model_from_json(model2_json)
# 加載模型權(quán)重
model2.load_weights('model2_weights.h5')
這樣就可以加載多個模型,并且分別加載它們的結(jié)構(gòu)和權(quán)重。請確保在加載模型之前,已經(jīng)將模型結(jié)構(gòu)和權(quán)重保存到JSON文件和HDF5文件中。