Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Keras to construct RNN in python Neural Network

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use Keras to build RNN in python neural network". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use Keras to build RNN in python neural network" can help you solve the problem.

The important function of constructing RNN in Keras 1, SimpleRNN

SimpleRNN is used to build a normal simple RNN layer in Keras, which requires import before using it.

From keras.layers import SimpleRNN

In actual use, several parameters are needed.

Model.add (SimpleRNN (batch_input_shape = (BATCH_SIZE,TIME_STEPS,INPUT_SIZE), output_dim = CELL_SIZE,)

Where batch_input_shape represents the shape,shape of RNN input data, respectively, the BATCH,TIME_STEPS used for each training represents the number of time points sequentially entered by the RNN, and INPUT_SIZE represents the input data size for each time point.

CELL_SIZE represents the number of neurons trained at each time point.

2 、 model.train_on_batch

Different from the previous training CNN network and ordinary classification network, RNN network defines batch_input_shape when it is established, so a certain amount of training data is needed during training.

Model.train_on_batch needs to process the data before using it. Gets the training set of the specified BATCH size.

X_batch = X _ start:index_start [index _ start:index_start + BATCH_SIZE,:,:] Y_batch = Y _ indexes [index _ start:index_start + BATCH_SIZE,:] index_start + = BATCH_SIZE

The specific training process is as follows:

For i in range [start:index_start + BATCH_SIZE,:,:] Y_batch = YYZ [start:index_start + BATCH_SIZE,:] index_start + = BATCH_SIZE cost = model.train_on_batch [X_train.shape [0]: index_start = 0 if i0 = 0: # # acc cost Accuracy = model.evaluate (X-ray test weights batchwriting sizeplate 50) # # W _ weights b = model.layers [0] .get _ weights () print ("accuracy:", accuracy) x = X_test [1] .reshape (1m 28m 28)

All codes

This is an example of a RNN neural network that is used to recognize handwriting.

Import numpy as npfrom keras.models import Sequentialfrom keras.layers import SimpleRNN,Activation,Dense # # full connection layer 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 Y_test) = mnist.load_data () X_train = X_train.reshape (- 1pc28) / 255X_test = X_test.reshape (- 1pc28) / 255Y_train = np_utils.to_categorical Y_test = np_utils.to_categorical (Yantist) model () # 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 = Xindex [index _ start:index_start + BATCH_SIZE,:,:] Y_batch = Yindex [index _ start:index_start + BATCH_SIZE :] index_start + = BATCH_SIZE cost = model.train_on_batch if index_start > = X_train.shape [0]: index_start = 0 if i0 = 0: # # acc cost,accuracy = model.evaluate (model.layers [0] .get _ weights () print ("accuracy:", accuracy)

The experimental results are as follows:

10000U10000 [=]-1s 147us/stepaccuracy: 0.09329999938607215... 10000 109us/stepaccuracy 10000 [=]-1s 112us/stepaccuracy: 0.939500002264976510000 112us/stepaccuracy: 0.942299999594688510000 111us/stepaccuracy: 0.953400000035762810000 113us/stepaccuracy: 0.956600000858306910000 113us/stepaccuracy: 0.95079998331010000 112us/stepaccuracy: 0.9447499880790710000Universe 10000 [=]-1s 111us/stepaccuracy: 0.951500003527810000 = =]-1s 114us/stepaccuracy: 0.9288999977707862000Universe 10000 [=]-1s 115us/stepaccuracy: 0.9487999993562698 about "how to use Keras to build RNN in python neural network". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report