保存加载keras模型

网友投稿 534 2022-11-16

保存加载keras模型

保存加载keras模型

保存加载keras模型

# load libraries 保存和加载 Keras 模型import numpy as npfrom keras.datasets import imdbfrom keras.preprocessing.text import Tokenizerfrom keras import modelsfrom keras import layersfrom keras.models import load_model# set random seed 设置随机种子np.random.seed(0)# set the number of features we want 我们想要的特征数量number_of_features = 1000# load data and target vector from movie review data# 加载数据和分类标签(train_Data, train_target), (test_data, test_target) = imdb.load_data(num_words=number_of_features)# convert movie review data to a one-hot encoded feature matrix特# 将影评数据转化为one-hot编码的征矩阵tokenizer = Tokenizer(num_words=number_of_features)train_features = tokenizer.sequences_to_matrix(train_data, mode="binary")test_features = tokenizer.sequences_to_matrix(test_data, mode="binary")# start neural network 启动神经网络network = models.Sequential()# add fully connected layer with ReLU activation function 添加使用relu激活函数的全连接层network.add(layers.Dense(units=16, activation="relu", input_shape=(number_of_features,)))# 添加使用 sigmoid 激活函数的全连接层# add fully connected layer with a sigmoid activation functionnetwork.add(layers.Dense(units=1, activation="sigmoid"))# compile neural network 编译神经网络network.compile(loss="binary_crossentropy", #交叉熵 optimizer="rmsprop", #优化器 metrics=["accuracy"]) #准确率作为性能指标# train neural networkhistory = network.fit(train_features, train_target, epochs=3, verbose=0, batch_size=100, #每个批次观察值数量 validation_data=(test_features, test_target)) #测试数据# save neural network 保存模型network.save("model.h5")We can then load the model either in another application or for additional training 加载模型# load neural network 加载模型network = load_model("model.h5")DiscussionUnlike scikit-learn, Keras does not recommend you save models using pickle. Instead, models are saved as an HDF5 file. The HDF5 file contains everything you need to not only load the model to make predicitons (i.e., achitecture and trained parameters), but also to restart training (i.e. loss and optimizer settings and the current state)

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Hadoop环境配置之hive环境配置详解
下一篇:保存和加载scikit-learn模型
相关文章

 发表评论

暂时没有评论,来抢沙发吧~