
上QQ阅读APP看书,第一时间看更新
2.1.4 使用保存的Keras模式对模型进行复用
前面已经说过,对于保存的文件,Keras是将所有的信息都保存在h5文件中,包含所有的模型结构信息和训练过的参数信息。
new_model = tf.keras.models.load_model('./saver/the_save_model.h5')
tf.keras.models.load_model函数从给定的地址中载入h5模型,载入完成后会依据存档自动建立一个新的模型。
模型的复用可直接调用模型predict函数:
new_prediction = new_model.predict(iris_data)
这里直接将iris数据作为预测数据进行输入。全部代码如下所示。
【程序2-3】

计算结果被完整打印出来,如图2.7所示。

图2.7 打印结果