这篇文章主要介绍“python神经网络怎么使用Keras构建RNN”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“python神经网络怎么使用Keras构建RNN”文章能帮助大家解决问题。
Keras中构建RNN的重要函数
1、SimpleRNN
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代表训练每一个时间点的神经元数量。
2、model.train_on_batch
与之前的训练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 npfrom keras.models import Sequentialfrom keras.layers import SimpleRNN,Activation,Dense ## 全连接层from keras.datasets import mnistfrom keras.utils import np_utilsfrom keras.optimizers import AdamTIME_STEPS = 28INPUT_SIZE = 28BATCH_SIZE = 50index_start = 0OUTPUT_SIZE = 10CELL_SIZE = 75LR = 1e-3(X_train,Y_train),(X_test,Y_test) = mnist.load_data() X_train = X_train.reshape(-1,28,28)/255X_test = X_test.reshape(-1,28,28)/255Y_train = np_utils.to_categorical(Y_train,num_classes= 10)Y_test = np_utils.to_categorical(Y_test,num_classes= 10)model = Sequential()# conv1model.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)## compilemodel.compile(loss = 'categorical_crossentropy',optimizer = adam,metrics = ['accuracy'])## tarinfor 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/stepaccuracy: 0.09329999938607215…………………………10000/10000 [==============================] - 1s 112us/stepaccuracy: 0.939500002264976510000/10000 [==============================] - 1s 109us/stepaccuracy: 0.942299999594688510000/10000 [==============================] - 1s 114us/stepaccuracy: 0.953400000035762810000/10000 [==============================] - 1s 112us/stepaccuracy: 0.956600000858306910000/10000 [==============================] - 1s 113us/stepaccuracy: 0.95079999983310710000/10000 [==============================] - 1s 116us/step10000/10000 [==============================] - 1s 112us/stepaccuracy: 0.947499998807907110000/10000 [==============================] - 1s 111us/stepaccuracy: 0.951500000357627810000/10000 [==============================] - 1s 114us/stepaccuracy: 0.928899997770786210000/10000 [==============================] - 1s 115us/stepaccuracy: 0.9487999993562698
关于“python神经网络怎么使用Keras构建RNN”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网行业资讯频道,小编每天都会为大家更新不同的知识点。