文章目录:
一.循环神经网络
1.RNN原理
2.RNN应用
二.Keras编写RNN
1.代码实现
2.完整代码
3.运行结果
三.绘制图形
四.总结
https://github.com/eastmountyxz/
AI-for-TensorFlow
https://github.com/eastmountyxz/
AI-for-Keras
学Python近十年,认识了很多大佬和朋友,感恩。作者的本意是帮助更多初学者入门,因此在github开源了所有代码,也在公众号同步更新。深知自己很菜,得拼命努力前行,编程也没有什么捷径,干就对了。希望未来能更透彻学习和撰写文章,也能在读博几年里学会真正的独立科研。同时非常感谢参考文献中的大佬们的文章和分享。
- https://blog.csdn.net/eastmount
第二步,导入扩展包。
import numpy as npfrom keras.datasets import mnistfrom keras.utils import np_utilsfrom keras.models import Sequentialfrom keras.layers import SimpleRNN, Activation, Densefrom keras.optimizers import Adam
第三步,定义参数。
TIME_STEPS = 28 # 时间点数据 每次读取1行共28次 same as the height of the imageINPUT_SIZE = 28 # 每行读取28个像素点 same as the width of the imageBATCH_SIZE = 50 # 每个批次训练50张图片BATCH_INDEX = 0OUTPUT_SIZE = 10 # 每张图片输出分类矩阵CELL_SIZE = 50 # RNN中隐藏单元LR = 0.001 # 学习率
第四步,载入MNIST数据及预处理。
# 下载MNIST数据# training X shape (60000, 28x28), Y shape (60000, )# test X shape (10000, 28x28), Y shape (10000, )(X_train, y_train), (X_test, y_test) = mnist.load_data()# 数据预处理# 参数-1表示样例的个数 28*28表示像素长度和宽度X_train = X_train.reshape(-1, 28, 28) / 255 # normalizeX_test = X_test.reshape(-1, 28, 28) / 255 # normalize# 将类向量转化为类矩阵 数字 5 转换为 0 0 0 0 0 1 0 0 0 0 矩阵y_train = np_utils.to_categorical(y_train, num_classes=10)y_test = np_utils.to_categorical(y_test, num_classes=10)
model = Sequential()model.add(SimpleRNN(batch_input_shape = (None, TIME_STEPS, INPUT_SIZE),output_dim = CELL_SIZE,unroll=True,))model.add(Dense(OUTPUT_SIZE))model.add(Activation('softmax'))
# optimizeradam = Adam(LR)# We add metrics to get more results you want to see# 激活神经网络model.compile(optimizer=adam, # 加速神经网络loss='categorical_crossentropy', # 损失函数metrics=['accuracy']) # 计算误差或准确率
for step in range(4001):# 分批截取数据 BATCH_INDEX初始值为0 BATCH_SIZE为50 取28个步长和28个INPUT_SIZE# data shape = (batch_num, steps, inputs/outputs)X_batch = X_train[BATCH_INDEX: BATCH_INDEX+BATCH_SIZE, :, :]Y_batch = y_train[BATCH_INDEX: BATCH_INDEX+BATCH_SIZE, : ]# 计算误差cost = model.train_on_batch(X_batch, Y_batch)# 累加参数BATCH_INDEX += BATCH_SIZE# 如果BATCH_INDEX累加大于总体的个数 则重新赋值0开始分批计算BATCH_INDEX = 0 if BATCH_INDEX >= X_train.shape[0] else BATCH_INDEX# 每隔500步输出if step % 500 == 0:# 评价算法accuracy = model.evaluate(y_test,batch_size=y_test.shape[0],verbose=False): ', cost, 'test accuracy: ', accuracy)
# -*- coding: utf-8 -*-"""Created on Fri Feb 23 18:43:06 2020@author: xiuzhang Eastmount CSDNWuhan fighting!"""import numpy as npfrom keras.datasets import mnistfrom keras.utils import np_utilsfrom keras.models import Sequentialfrom keras.layers import SimpleRNN, Activation, Densefrom keras.optimizers import Adam#------------------------------定义参数------------------------------TIME_STEPS = 28 # 时间点数据 每次读取1行共28次 same as the height of the imageINPUT_SIZE = 28 # 每行读取28个像素点 same as the width of the imageBATCH_SIZE = 50 # 每个批次训练50张图片BATCH_INDEX = 0OUTPUT_SIZE = 10 # 每张图片输出分类矩阵CELL_SIZE = 50 # RNN中隐藏单元LR = 0.001 # 学习率#---------------------------载入数据及预处理---------------------------# 下载MNIST数据# training X shape (60000, 28x28), Y shape (60000, )# test X shape (10000, 28x28), Y shape (10000, )(X_train, y_train), (X_test, y_test) = mnist.load_data()# 数据预处理# 参数-1表示样例的个数 28*28表示像素长度和宽度X_train = X_train.reshape(-1, 28, 28) / 255 # normalizeX_test = X_test.reshape(-1, 28, 28) / 255 # normalize# 将类向量转化为类矩阵 数字 5 转换为 0 0 0 0 0 1 0 0 0 0 矩阵y_train = np_utils.to_categorical(y_train, num_classes=10)y_test = np_utils.to_categorical(y_test, num_classes=10)#---------------------------创建RNN神经网络---------------------------# 创建RNN模型model = Sequential()# RNN cellmodel.add(SimpleRNN(# 设置输入batch形状 批次数量50 时间点28 每行读取像素28个# for batch_input_shape, if using tensorflow as the backend, we have to put None for the batch_size.# Otherwise, model.evaluate() will get error.batch_input_shape = (None, TIME_STEPS, INPUT_SIZE),# RNN输出给后一层的结果为50output_dim = CELL_SIZE,unroll=True,))# output layermodel.add(Dense(OUTPUT_SIZE)) # 全连接层 输出对应10分类model.add(Activation('softmax')) # 激励函数 tanh#---------------------------神经网络优化器---------------------------# optimizeradam = Adam(LR)# We add metrics to get more results you want to see# 激活神经网络model.compile(optimizer=adam, # 加速神经网络loss='categorical_crossentropy', # 损失函数metrics=['accuracy']) # 计算误差或准确率#--------------------------------训练和预测------------------------------for step in range(4001):# 分批截取数据 BATCH_INDEX初始值为0 BATCH_SIZE为50 取28个步长和28个INPUT_SIZE# data shape = (batch_num, steps, inputs/outputs)X_batch = X_train[BATCH_INDEX: BATCH_INDEX+BATCH_SIZE, :, :]Y_batch = y_train[BATCH_INDEX: BATCH_INDEX+BATCH_SIZE, : ]# 计算误差cost = model.train_on_batch(X_batch, Y_batch)# 累加参数BATCH_INDEX += BATCH_SIZE# 如果BATCH_INDEX累加大于总体的个数 则重新赋值0开始分批计算BATCH_INDEX = 0 if BATCH_INDEX >= X_train.shape[0] else BATCH_INDEX# 每隔500步输出if step % 500 == 0:# 评价算法cost, accuracy = model.evaluate(X_test, y_test,batch_size=y_test.shape[0],verbose=False)print('test cost: ', cost, 'test accuracy: ', accuracy)
test cost: 2.3657307624816895 test accuracy: 0.07580000162124634test cost: 0.5747528076171875 test accuracy: 0.840399980545044test cost: 0.4435984492301941 test accuracy: 0.863099992275238test cost: 0.3612927794456482 test accuracy: 0.897599995136261test cost: 0.30560624599456787 test accuracy: 0.9138000011444092test cost: 0.3092554211616516 test accuracy: 0.9089999794960022test cost: 0.2737627327442169 test accuracy: 0.9168999791145325test cost: 0.22912506759166718 test accuracy: 0.9351000189781189test cost: 0.23802728950977325 test accuracy: 0.9323999881744385
# -*- coding: utf-8 -*-# -*- coding: utf-8 -*-"""Created on Fri Feb 23 18:43:06 2020@author: xiuzhang Eastmount CSDNWuhan fighting!"""import numpy as npfrom keras.datasets import mnistfrom keras.utils import np_utilsfrom keras.models import Sequentialfrom keras.layers import SimpleRNN, Activation, Densefrom keras.optimizers import Adam#------------------------------定义参数------------------------------TIME_STEPS = 28 # 时间点数据 每次读取1行共28次 same as the height of the imageINPUT_SIZE = 28 # 每行读取28个像素点 same as the width of the imageBATCH_SIZE = 50 # 每个批次训练50张图片BATCH_INDEX = 0OUTPUT_SIZE = 10 # 每张图片输出分类矩阵CELL_SIZE = 50 # RNN中隐藏单元LR = 0.001 # 学习率#---------------------------载入数据及预处理---------------------------# 下载MNIST数据# training X shape (60000, 28x28), Y shape (60000, )# test X shape (10000, 28x28), Y shape (10000, )(X_train, y_train), (X_test, y_test) = mnist.load_data()# 数据预处理# 参数-1表示样例的个数 28*28表示像素长度和宽度X_train = X_train.reshape(-1, 28, 28) / 255 # normalizeX_test = X_test.reshape(-1, 28, 28) / 255 # normalize# 将类向量转化为类矩阵 数字 5 转换为 0 0 0 0 0 1 0 0 0 0 矩阵y_train = np_utils.to_categorical(y_train, num_classes=10)y_test = np_utils.to_categorical(y_test, num_classes=10)#---------------------------创建RNN神经网络---------------------------# 创建RNN模型model = Sequential()# RNN cellmodel.add(SimpleRNN(# 设置输入batch形状 批次数量50 时间点28 每行读取像素28个# for batch_input_shape, if using tensorflow as the backend, we have to put None for the batch_size.# Otherwise, model.evaluate() will get error.batch_input_shape = (None, TIME_STEPS, INPUT_SIZE),# RNN输出给后一层的结果为50output_dim = CELL_SIZE,unroll=True,))# output layermodel.add(Dense(OUTPUT_SIZE)) # 全连接层 输出对应10分类model.add(Activation('softmax')) # 激励函数 tanh#---------------------------神经网络优化器---------------------------# optimizeradam = Adam(LR)# We add metrics to get more results you want to see# 激活神经网络model.compile(optimizer=adam, # 加速神经网络loss='categorical_crossentropy', # 损失函数metrics=['accuracy']) # 计算误差或准确率#--------------------------------训练和预测------------------------------cost_list = []acc_list = []step_list = []for step in range(4001):# 分批截取数据 BATCH_INDEX初始值为0 BATCH_SIZE为50 取28个步长和28个INPUT_SIZE# data shape = (batch_num, steps, inputs/outputs)X_batch = X_train[BATCH_INDEX: BATCH_INDEX+BATCH_SIZE, :, :]Y_batch = y_train[BATCH_INDEX: BATCH_INDEX+BATCH_SIZE, : ]# 计算误差cost = model.train_on_batch(X_batch, Y_batch)# 累加参数BATCH_INDEX += BATCH_SIZE# 如果BATCH_INDEX累加大于总体的个数 则重新赋值0开始分批计算BATCH_INDEX = 0 if BATCH_INDEX >= X_train.shape[0] else BATCH_INDEX# 每隔200步输出if step % 200 == 0:# 评价算法cost, accuracy = model.evaluate(X_test, y_test,batch_size=y_test.shape[0],verbose=False)# 写入列表cost_list.append(cost)acc_list.append(accuracy)step_list.append(step)print('test cost: ', cost, 'test accuracy: ', accuracy)#--------------------------------绘制相关曲线------------------------------import matplotlib.pyplot as pltfrom mpl_toolkits.axes_grid1 import host_subplot# 绘制曲线图host = host_subplot(111)plt.subplots_adjust(right=0.8) # ajust the right boundary of the plot windowpar1 = host.twinx()# 设置类标host.set_xlabel("Iterations")host.set_ylabel("Loss")par1.set_ylabel("Accuracy")# 绘制曲线p1, = host.plot(step_list, cost_list, "bo-", linewidth=2, markersize=12, label="cost")p2, = par1.plot(step_list, acc_list, "gs-", linewidth=2, markersize=12, label="accuracy")# 设置颜色host.axis["left"].label.set_color(p1.get_color())par1.axis["right"].label.set_color(p2.get_color())# 绘图plt.legend(loc="upper left")plt.title("Keras for RNN - Eastmount CSDN")plt.draw()plt.show()
转眼,工资已经停了三年,贷款还欠了79万多,像样的论文一篇没有,娃娃又要读书,项目还得抓紧,唉,异地读博真不容易,压力山大。本来还想毕业去北上广奋斗两年赚点钱,现在寒冬将至,更难了,只希望能顺利毕业。
今天把本就不多的公积金也提出来了,家里少有的余粮。话说现在数字化服务真方便,感叹于国家的强大和贵州的大数据发展,各种办事办证都好快,点赞。回家几天陪伴女神和小珞,虽然没怎么学习,只有半夜零散的时间看书,但感觉做了好多家事,大事小事都有,也算是成长,人生感恩有你们。又要回去拼搏了,时间真紧张,似乎争分夺秒都不够,再次感谢武大、彭老师、小伙伴、朋友、家人和自己,再难也要充满干劲,咬牙前行!
PS:回来途中,看到比我还小的外卖小哥不小心翻车,上前扶他起来,一顿关心后,我似乎更应该努力了。每一代都有每一代的奋斗史,每个人都有每个人的责任和生活,fighting~
——2022.08.26 感于贵阳
十九.Keras搭建循环神经网络分类案例及RNN原理详解
天行健,君子以自强不息。
地势坤,君子以厚德载物。
[1] 神经网络和机器学习基础入门分享 - 作者的文章
[2] 斯坦福机器学习视频NG教授: https://class.coursera.org/ml/class/index
[3] 书籍《游戏开发中的人工智能》、《游戏编程中的人工智能技术》
[4] 网易云莫烦老师视频(强推 我付费支持老师一波)
[5] [Python人工智能] 八.卷积神经网络CNN原理详解及TensorFlow编写CNN
[6] 机器学习实战—MNIST手写体数字识别 - RunningSucks
[7] https://github.com/siucaan/CNN_MNIST
[8] https://study.163.com/course/courseLearn.htm?courseId=1003340023
[9] https://github.com/MorvanZhou/tutorials/blob/master/
kerasTUT/7-RNN_Classifier_example.py
[10] 来自谷歌大脑工程师的RNN系列教程 | RNN的基本介绍 - CNET科技行者
[11] 深度学习文本分类实战报告:CNN,RNN&HAN - AI研习社
[12] 深度学习导论 - 读李宏毅《1天搞懂深度学习》 - 慢慢的燃烧