Keras構(gòu)建RNN網(wǎng)絡(luò)訓(xùn)練如何實現(xiàn)
Admin 2022-09-16 群英技術(shù)資訊 795 次瀏覽
這篇文章主要介紹了“Keras構(gòu)建RNN網(wǎng)絡(luò)訓(xùn)練如何實現(xiàn)”相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Keras構(gòu)建RNN網(wǎng)絡(luò)訓(xùn)練如何實現(xiàn)文章都會有所收獲,下面我們一起來看看吧。SimpleRNN用于在Keras中構(gòu)建普通的簡單RNN層,在使用前需要import。
from keras.layers import SimpleRNN
在實際使用時,需要用到幾個參數(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表示這個RNN按順序輸入的時間點的數(shù)量,INPUT_SIZE表示每一個時間點的輸入數(shù)據(jù)大小。
CELL_SIZE代表訓(xùn)練每一個時間點的神經(jīng)元數(shù)量。
與之前的訓(xùn)練CNN網(wǎng)絡(luò)和普通分類網(wǎng)絡(luò)不同,RNN網(wǎng)絡(luò)在建立時就規(guī)定了batch_input_shape,所以訓(xùn)練的時候也需要一定量一定量的傳入訓(xùn)練數(shù)據(jù)。
model.train_on_batch在使用前需要對數(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)練過程如下:
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神經(jīng)網(wǎng)絡(luò)的例子,用于識別手寫體。
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)
實驗結(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
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:[email protected]進(jìn)行舉報,并提供相關(guān)證據(jù),查實之后,將立刻刪除涉嫌侵權(quán)內(nèi)容。
猜你喜歡
這篇文章主要介紹了???????Python?入門學(xué)習(xí)之函數(shù)式編程,?Python?中的函數(shù)式編程技術(shù)進(jìn)行了簡單的入門介紹,下文詳細(xì)內(nèi)容需要的小伙伴可以參考一下
怎么用Python做中文詞云?一些朋友比較好奇中文詞云的實現(xiàn),因此這篇文章就主要給大家介紹Python實現(xiàn)中文詞云的方法,下文的示例代碼對大家了解Python實現(xiàn)詞云的方法有一定的幫助,感興趣的朋友接下來一起跟隨小編來學(xué)習(xí)一下吧。
本文主要介紹了使用python?opencv對畸變圖像進(jìn)行矯正的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
這篇文章主要為大家介紹了Python的變量,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
今天帶大家學(xué)習(xí)怎么用python實現(xiàn)一個無界面的小型圖書管理系統(tǒng),文中有非常詳細(xì)的圖文解說及代碼示例,對正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下
推薦內(nèi)容
相關(guān)標(biāo)簽
成為群英會員,開啟智能安全云計算之旅
立即注冊關(guān)注或聯(lián)系群英網(wǎng)絡(luò)
7x24小時售前:400-678-4567
7x24小時售后:0668-2555666
24小時QQ客服
群英微信公眾號
CNNIC域名投訴舉報處理平臺
服務(wù)電話:010-58813000
服務(wù)郵箱:[email protected]
投訴與建議:0668-2555555
Copyright ? QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版權(quán)所有
增值電信經(jīng)營許可證 : B1.B2-20140078 ICP核準(zhǔn)(ICP備案)粵ICP備09006778號 域名注冊商資質(zhì) 粵 D3.1-20240008