您好,登錄后才能下訂單哦!
這篇文章主要介紹“python神經(jīng)網(wǎng)絡(luò)怎么使用Keras構(gòu)建RNN”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“python神經(jīng)網(wǎng)絡(luò)怎么使用Keras構(gòu)建RNN”文章能幫助大家解決問(wèn)題。
SimpleRNN用于在Keras中構(gòu)建普通的簡(jiǎn)單RNN層,在使用前需要import。
from keras.layers import SimpleRNN
在實(shí)際使用時(shí),需要用到幾個(gè)參數(shù)。
model.add( SimpleRNN( batch_input_shape = (BATCH_SIZE,TIME_STEPS,INPUT_SIZE), output_dim = CELL_SIZE, ) )
其中,batch_input_shape代表RNN輸入數(shù)據(jù)的shape,shape的內(nèi)容分別是每一次訓(xùn)練使用的BATCH,TIME_STEPS表示這個(gè)RNN按順序輸入的時(shí)間點(diǎn)的數(shù)量,INPUT_SIZE表示每一個(gè)時(shí)間點(diǎn)的輸入數(shù)據(jù)大小。
CELL_SIZE代表訓(xùn)練每一個(gè)時(shí)間點(diǎn)的神經(jīng)元數(shù)量。
與之前的訓(xùn)練CNN網(wǎng)絡(luò)和普通分類(lèi)網(wǎng)絡(luò)不同,RNN網(wǎng)絡(luò)在建立時(shí)就規(guī)定了batch_input_shape,所以訓(xùn)練的時(shí)候也需要一定量一定量的傳入訓(xùn)練數(shù)據(jù)。
model.train_on_batch在使用前需要對(duì)數(shù)據(jù)進(jìn)行處理。獲取指定BATCH大小的訓(xùn)練集。
X_batch = X_train[index_start:index_start + BATCH_SIZE,:,:] Y_batch = Y_train[index_start:index_start + BATCH_SIZE,:] index_start += BATCH_SIZE
具體訓(xùn)練過(guò)程如下:
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)
全部代碼
這是一個(gè)RNN神經(jīng)網(wǎng)絡(luò)的例子,用于識(shí)別手寫(xiě)體。
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)
實(shí)驗(yàn)結(jié)果為:
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
關(guān)于“python神經(jīng)網(wǎng)絡(luò)怎么使用Keras構(gòu)建RNN”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。