Python Keras构建RNN:SimpleRNN与train_on_batch实战详解
本文详解如何在Python中使用Keras构建循环神经网络(RNN)。重点解析SimpleRNN层的batch_input_shape、time_steps等关键参数,并演示如何利用model.train_on_batch进行分批训练。结合MNIST手写数字识别案例,展示完整代码实现与准确率评估过程,帮助开发者掌握RNN基础训练流程。
在Keras框架中构建循环神经网络(RNN)时,掌握核心函数与配置至关重要。
1、SimpleRNN
SimpleRNN是Keras中用于构建基础循环神经网络层的模块,使用前需确保已正确导入该模块。
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输入数据的形状,具体包含三个维度: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 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
以上为Python神经网络使用Keras构建RNN训练的详细内容,如需了解更多Keras构建RNN训练的相关资料,请持续关注本站其他技术文章。
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









