-
学习中心
[
博客
为创始人和工程经理提供关于扩展、管理和产品开发的见解。
社区帖子
阅读编程教程,分享你的知识,并一起成为更好的开发者。
](www.codementor.io/community/t…)
热门话题
写一篇文章
注册
寻找开发者和导师社区帖子博客注册登录
[
](www.codementor.io/projects?re…)
关注
让我解决一些问题
如何在keras中训练后保存/恢复模型?
发布日期:2021年8月24日
大家好,我的名字是Alex Polymath。
这是另一篇关于神经网络基础知识的文章--保存和加载训练好的NN(神经网络)的权重
你可以运行google colab或者使用你的电脑。
概述
在本教程中,我们将使用kaggle的mnist数据集。
- 首先,我们将为训练准备数据
- 训练神经网络
- 保存它
- 加载它
- 在测试数据上测试它
1.从kaggle下载数据。
会有2个文件
- train.csv.zip
- test.csv.zip
我不知道为什么,但是测试文件没有任何意义,
,因为那里没有标签。
如果你使用google colab
把train.csv.zip文件拖到文件中。
!unzip train.csv.zip
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import keras
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
编制数据
train_df = pd.read_csv('/content/train.csv') #might be in other place
train_labels = train_df['label'] #We need Y values - labels
train_labels = train_labels.to_numpy() # nothing smart just convert to numpy array
del train_df['label'] # remove label from original dataframe to use it as X
train_data = train_df.to_numpy()
# we can't use values 1,2,3,4,4,5 for Y
# instead we should use smth like [1,0,0,0,0,0], [0,1,0,0,0,0], ...
y = LabelBinarizer().fit_transform(train_labels)
#Split train and test data
X_train, X_test, y_train, y_test = train_test_split(train_data, y, test_size=0.1)
- 训练神经网络
2.编译神经网络
# Define sequential model
model = keras.Sequential()
# Define the first layer
model.add(keras.layers.Dense(128, activation="relu", input_shape=(784,)))
model.add(keras.layers.Dense(128, activation="relu", input_shape=(128,)))
model.add(keras.layers.Dense(128, activation="relu", input_shape=(128,)))
model.add(keras.layers.Dense(128, activation="relu", input_shape=(128,)))
# Add activation function to classifier
model.add(keras.layers.Dense(10, activation='softmax'))
# Finish the modecl compilation
model.compile('adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Complete the model fit operation
# callbacks=[PlotLossesKeras()] - this is a single magic line of code which draw #live chart
model.fit(train_data, y, epochs=10, validation_data=(X_test, y_test), callbacks=[PlotLossesKeras()], verbose=0)
3.保存权重
model.save('/content/mynn')
4.加载权重
test_model = keras.models.load_model('/content/mynn')
5. 在测试数据上测试它
test_df = pd.read_csv('/content/mnist_test.csv') #might be in other place
test_labels = test_df['label'] #We need Y values - labels
test_labels = test_labels.to_numpy() # nothing smart just convert to numpy array
del test_df['label'] # remove label from original dataframe to use it as X
test_data = test_df.to_numpy()
让我们把一些随机项目可视化
img = test_data[3].reshape(28,28)
plt.imshow(img)
//we make a trick with np array to wrap single item into array
// because predict method predicts many values at once
y_proba = test_model.predict(np.array([test_data[3]]))
y_classes = y_proba.argmax(axis=-1)
y_classes
报告
喜欢这个帖子吗?如果对你有帮助,请给Alex Polymath一个赞。
分享
让我解决一些问题
大家好,我叫Alex!我是一个全栈开发者。最近我创建了基于GPT2微调神经网络的服务textporn.io。audio-data.cc的创造者 - 语音 - 到 - 文本服务clip.photos的创造者 - ...
关注
发现并阅读Alex Polymath的更多文章
开始
喜欢这个帖子吗?
给亚历克斯留下一个喜欢和评论
成为第一个分享您意见的人
提交
显示更多回复