135-1821-9792

Python实现Keras搭建神经网络训练分类模型的方法-创新互联

这篇文章将为大家详细讲解有关Python实现Keras搭建神经网络训练分类模型的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联公司是一家集网站建设,洪山企业网站建设,洪山品牌网站建设,网站定制,洪山网站建设报价,网络营销,网络优化,洪山网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

注释讲解版:

# Classifier example

import numpy as np
# for reproducibility
np.random.seed(1337)
# from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import RMSprop

# 程序中用到的数据是经典的手写体识别mnist数据集
# download the mnist to the path if it is the first time to be called
# X shape (60,000 28x28), y
# (X_train, y_train), (X_test, y_test) = mnist.load_data()
# 下载minst.npz:
# 链接: https://pan.baidu.com/s/1b2ppKDOdzDJxivgmyOoQsA
# 提取码: y5ir
# 将下载好的minst.npz放到当前目录下
path='./mnist.npz'
f = np.load(path)
X_train, y_train = f['x_train'], f['y_train']
X_test, y_test = f['x_test'], f['y_test']
f.close()

# data pre-processing
# 数据预处理
# normalize
# X shape (60,000 28x28),表示输入数据 X 是个三维的数据
# 可以理解为 60000行数据,每一行是一张28 x 28 的灰度图片
# X_train.reshape(X_train.shape[0], -1)表示:只保留第一维,其余的纬度,不管多少纬度,重新排列为一维
# 参数-1就是不知道行数或者列数多少的情况下使用的参数
# 所以先确定除了参数-1之外的其他参数,然后通过(总参数的计算) / (确定除了参数-1之外的其他参数) = 该位置应该是多少的参数
# 这里用-1是偷懒的做法,等同于 28*28
# reshape后的数据是:共60000行,每一行是784个数据点(feature)
# 输入的 x 变成 60,000*784 的数据,然后除以 255 进行标准化
# 因为每个像素都是在 0 到 255 之间的,标准化之后就变成了 0 到 1 之间
X_train = X_train.reshape(X_train.shape[0], -1) / 255
X_test = X_test.reshape(X_test.shape[0], -1) / 255
# 分类标签编码
# 将y转化为one-hot vector
y_train = np_utils.to_categorical(y_train, num_classes = 10)
y_test = np_utils.to_categorical(y_test, num_classes = 10)

# Another way to build your neural net
# 建立神经网络
# 应用了2层的神经网络,前一层的激活函数用的是relu,后一层的激活函数用的是softmax
#32是输出的维数
model = Sequential([
  Dense(32, input_dim=784),
  Activation('relu'),
  Dense(10),
  Activation('softmax')
])

# Another way to define your optimizer
# 优化函数
# 优化算法用的是RMSprop
rmsprop = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)

# We add metrics to get more results you want to see
# 不自己定义,直接用内置的优化器也行,optimizer='rmsprop'
#激活模型:接下来用 model.compile 激励神经网络
model.compile(
  optimizer=rmsprop,
  loss='categorical_crossentropy',
  metrics=['accuracy']
)

print('Training------------')
# Another way to train the model
# 训练模型
# 上一个程序是用train_on_batch 一批一批的训练 X_train, Y_train
# 默认的返回值是 cost,每100步输出一下结果
# 输出的样式与上一个程序的有所不同,感觉用model.fit()更清晰明了
# 上一个程序是Python实现Keras搭建神经网络训练回归模型:
# https://blog.csdn.net/weixin_45798684/article/details/106503685
model.fit(X_train, y_train, nb_epoch=2, batch_size=32)

print('\nTesting------------')
# Evaluate the model with the metrics we defined earlier
# 测试
loss, accuracy = model.evaluate(X_test, y_test)

print('test loss:', loss)
print('test accuracy:', accuracy)

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章标题:Python实现Keras搭建神经网络训练分类模型的方法-创新互联
URL分享:http://kswsj.com/article/dcjspo.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号