Keras构建RNN网络训练如何实现
Admin 2022-09-16 群英技术资讯 424 次浏览
SimpleRNN用于在Keras中构建普通的简单RNN层,在使用前需要import。
from keras.layers import SimpleRNN
在实际使用时,需要用到几个参数。
model.add( SimpleRNN( batch_input_shape = (BATCH_SIZE,TIME_STEPS,INPUT_SIZE), output_dim = CELL_SIZE, ) )
其中,batch_input_shape代表RNN输入数据的shape,shape的内容分别是每一次训练使用的BATCH,TIME_STEPS表示这个RNN按顺序输入的时间点的数量,INPUT_SIZE表示每一个时间点的输入数据大小。
CELL_SIZE代表训练每一个时间点的神经元数量。
与之前的训练CNN网络和普通分类网络不同,RNN网络在建立时就规定了batch_input_shape,所以训练的时候也需要一定量一定量的传入训练数据。
model.train_on_batch在使用前需要对数据进行处理。获取指定BATCH大小的训练集。
X_batch = X_train[index_start:index_start + BATCH_SIZE,:,:] Y_batch = Y_train[index_start:index_start + BATCH_SIZE,:] index_start += BATCH_SIZE
具体训练过程如下:
for i in range(500): X_batch = X_train[index_start:index_start + BATCH_SIZE,:,:] Y_batch = Y_train[index_start:index_start + BATCH_SIZE,:] index_start += BATCH_SIZE cost = model.train_on_batch(X_batch,Y_batch) if index_start >= X_train.shape[0]: index_start = 0 if i%100 == 0: ## acc cost,accuracy = model.evaluate(X_test,Y_test,batch_size=50) ## W,b = model.layers[0].get_weights() print("accuracy:",accuracy) x = X_test[1].reshape(1,28,28)
这是一个RNN神经网络的例子,用于识别手写体。
import numpy as np from keras.models import Sequential from keras.layers import SimpleRNN,Activation,Dense ## 全连接层 from keras.datasets import mnist from keras.utils import np_utils from keras.optimizers import Adam TIME_STEPS = 28 INPUT_SIZE = 28 BATCH_SIZE = 50 index_start = 0 OUTPUT_SIZE = 10 CELL_SIZE = 75 LR = 1e-3 (X_train,Y_train),(X_test,Y_test) = mnist.load_data() X_train = X_train.reshape(-1,28,28)/255 X_test = X_test.reshape(-1,28,28)/255 Y_train = np_utils.to_categorical(Y_train,num_classes= 10) Y_test = np_utils.to_categorical(Y_test,num_classes= 10) model = Sequential() # conv1 model.add( SimpleRNN( batch_input_shape = (BATCH_SIZE,TIME_STEPS,INPUT_SIZE), output_dim = CELL_SIZE, ) ) model.add(Dense(OUTPUT_SIZE)) model.add(Activation("softmax")) adam = Adam(LR) ## compile model.compile(loss = 'categorical_crossentropy',optimizer = adam,metrics = ['accuracy']) ## tarin for i in range(500): X_batch = X_train[index_start:index_start + BATCH_SIZE,:,:] Y_batch = Y_train[index_start:index_start + BATCH_SIZE,:] index_start += BATCH_SIZE cost = model.train_on_batch(X_batch,Y_batch) if index_start >= X_train.shape[0]: index_start = 0 if i%100 == 0: ## acc cost,accuracy = model.evaluate(X_test,Y_test,batch_size=50) ## W,b = model.layers[0].get_weights() print("accuracy:",accuracy)
实验结果为:
10000/10000 [==============================] - 1s 147us/step accuracy: 0.09329999938607215 ………………………… 10000/10000 [==============================] - 1s 112us/step accuracy: 0.9395000022649765 10000/10000 [==============================] - 1s 109us/step accuracy: 0.9422999995946885 10000/10000 [==============================] - 1s 114us/step accuracy: 0.9534000000357628 10000/10000 [==============================] - 1s 112us/step accuracy: 0.9566000008583069 10000/10000 [==============================] - 1s 113us/step accuracy: 0.950799999833107 10000/10000 [==============================] - 1s 116us/step 10000/10000 [==============================] - 1s 112us/step accuracy: 0.9474999988079071 10000/10000 [==============================] - 1s 111us/step accuracy: 0.9515000003576278 10000/10000 [==============================] - 1s 114us/step accuracy: 0.9288999977707862 10000/10000 [==============================] - 1s 115us/step accuracy: 0.9487999993562698
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
django分页器怎么样使用?django自带的分页器是比较实用的,但是很多新手可能不太了解django分页器用法,下面小编就给大家简单的介绍一下django是实现分页以及分页器的用法,感兴趣的朋友就继续往下看吧。
一般在命令行输入pip install wordcloud 总会显示安装失败,所以下面这篇文章主要给大家介绍了关于Python3.9用pip安装wordcloud库失败的解决过程,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
这篇文章主要为大家介绍了Python实现两种多分类混淆矩阵,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
先说1双引号与3个双引号的区别,双引号所表示的字符串通常要写成一行如:s1 = "hello,world"如果要写成多行,那么就要使用 (“连行符”
这篇文章给大家分享的是python怎样获取数组元素的下标的方法。下文给大家介绍了三种实现方法,小编觉得挺实用的,因此分享给大家做个参考,感兴趣的朋友接下来一起跟随小编看看吧。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008