深度學習 實驗七 循環神經網絡

深度學習 實驗七 循環神經網絡

一、問題描述

之前見過的所以神經網絡(比如全連接網絡和卷積神經網絡)都有一個主要特點,那就是它們都沒有記憶。它們單獨處理每個輸入,在輸入和輸入之間沒有保存任何狀態。對於這樣的網絡,要想處理數據點的序列或者時間序列,你需要向網絡同時展示整個序列,即將序列轉換成單個數據點。例如,在實驗九詞嵌入中就是這麼做的:將全部電影評論轉換爲一個大向量,然後一次性處理。

與此相反,當人在閱讀這個句子時,是一個詞一個詞地閱讀(或者說,眼睛一次掃視一次掃視地閱讀),同時會記住之前的內容,這讓你能夠動態理解這個句子所傳達的含義,以漸進的方式處理信息,同時保存一個關於所處理的內部模型,這就是循環神經網絡,它廣泛應用於自然語言處理中,本實驗會介紹簡單的循環神經網絡(SimpleRNN)來處理文本數據,然後改進網絡使用LSTM神經網絡對比效果。

二、設計簡要描述

1. 簡單循環神經網絡

1.1 循環網絡簡介

瞭解循環網絡的應用基礎知識。

1.2 Keras中的循環層

實現一個簡單的RNN層

1.3 數據處理

使用IMDB電影評論的數據,引入一個sequence模塊把我們的數據格式化輸入到神經網絡中。

1.4 搭建自己RNN神經網絡

按照如下步驟搭建自己的神經網絡並應用上面數據擬合模型

1)從kaeras導入全連接層模塊

2)定義一個序列模型

3) 添加一個Embedding層,參數是(max_features,32)

4)添加一個SimpleRNN層,輸出維度32

5)添加一個全連接層,輸出維度1,激活函數‘sigmoid’

6)編譯模型,參數分別是‘rmsprop’,’binary_crossentropy’,[‘acc’]

7)擬合模型,epochs=10.batch_size=128,validation_split=0.2,結果返回給history

1.5 繪製結果

繪製訓練損失和準確率率結果。

1.6 模型評估

利用測試數據對模型進行評估。

2. LSTM循環神經網絡

2.1 SimpleRNN的缺陷

瞭解SimpleRNN存在梯度消失的情況,可能導致後續模型無法繼續訓練。

2.2 LSTM分析

2.3 搭建自己的LSTM神經網絡

類似1.4節按照以下步驟搭建自己的LSTM神經網絡。

1)從kaeras導入LSTM模塊

2)定義一個序列模型

3)添加一個Embedding層,參數是(max_features,32)

4)添加一個LSTM層,輸出維度32

5)添加一個全連接層,輸出維度1,激活函數‘sigmoid’

6) 編譯模型,參數分別是‘rmsprop’,’binary_crossentropy’,[‘acc’]

7)擬合模型,epochs=10.batch_size=128,validation_split=0.2,結果返回給history

2.4 繪製結果

參考1.5節繪製損失值和準確率的結果圖。

2.5 模型評估

利用測試數據對模型進行評估。

三、程序清單

# test7_循環神經網絡
# 1. 簡單循環神經網絡

# 1.1 循環網絡簡介

# 1.2 Keras中的循環層
# 實現一個簡單的RNN層
from keras.models import Sequential
from keras.layers import Embedding, SimpleRNN

model = Sequential()
model.add(Embedding(10000, 32))
model.add(SimpleRNN(32))
model.summary()

# 1.3 數據處理
from keras.datasets import imdb
from keras.preprocessing import sequence
max_features = 10000 # 作爲特徵的單詞個數
maxlen = 500 
batch_size = 32
# 加載數據
print('Loading data...')
(input_train, y_train), (input_test, y_test) = imdb.load_data(path='F:\Desktop\data\imdb.npz', num_words=max_features)
print(len(input_train), 'train sequences')
print(len(input_test), 'test sequences')

print('Pad sequences (samples x time)')
input_train = sequence.pad_sequences(input_train, maxlen=maxlen)
input_test = sequence.pad_sequences(input_test, maxlen=maxlen)
print('input_train shape:', input_train.shape)
print('input_test shape:', input_test.shape)

# 1.4 搭建自己RNN神經網絡
# 按照如下步驟搭建自己的神經網絡並應用上面數據擬合模型
# 1)從kaeras導入全連接層模塊
# 2)定義一個序列模型
# 3) 添加一個Embedding層,參數是(max_features,32)
# 4)添加一個SimpleRNN層,輸出維度32
# 5)添加一個全連接層,輸出維度1,激活函數‘sigmoid’
# 6)編譯模型,參數分別是‘rmsprop’,’binary_crossentropy’,[‘acc’]
# 7)擬合模型,epochs=10.batch_size=128,validation_split=0.2,結果返回給history
from keras.layers import Dense, Activation

model = Sequential()
model.add(Embedding(max_features, 32))
model.add(SimpleRNN(32))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
history = model.fit(input_train, y_train, epochs=10, batch_size=128, validation_split=0.2)

# 1.5 繪製結果
# 能夠通過以下代碼繪製訓練損失和準確率率結果
import matplotlib.pyplot as plt

acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(len(acc))

plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()

plt.figure()

plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()

plt.show()

# 1.6 RNN 模型評估
result = model.evaluate(input_test, y_test, verbose = 1)
print('Test loss:', result[0])
print('Test accuracy:', result[1])

# 2 LSTM循環神經網絡

# 2.1 SimpleRNN的缺陷
# 2.2 LSTM分析

# 2.3 搭建自己的LSTM神經網絡
# 類似1.4節完成以下步驟
# 1)從kaeras導入LSTM模塊
# 2)定義一個序列模型
# 3)添加一個Embedding層,參數是(max_features,32)
# 4)添加一個LSTM層,輸出維度32
# 5)添加一個全連接層,輸出維度1,激活函數‘sigmoid’
# 6) 編譯模型,參數分別是‘rmsprop’,’binary_crossentropy’,[‘acc’]
# 7)擬合模型,epochs=10.batch_size=128,validation_split=0.2,結果返回給history
from keras.layers import LSTM

model2 = Sequential()
model2.add(Embedding(max_features, 32))
model2.add(LSTM(32))
model2.add(Dense(1, activation='sigmoid'))

model2.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
history2 = model2.fit(input_train, y_train, epochs=10, batch_size=128, validation_split=0.2)

# 2.4 繪製結果
# 參考1.5節繪製損失值和準確率的結果圖
import matplotlib.pyplot as plt

acc2 = history2.history['acc']
val_acc2 = history2.history['val_acc']
loss2 = history2.history['loss']
val_loss2 = history2.history['val_loss']

epochs2 = range(len(acc2))

plt.plot(epochs2, acc2, 'bo', label='Training acc')
plt.plot(epochs2, val_acc2, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()

plt.figure()

plt.plot(epochs2, loss2, 'bo', label='Training loss')
plt.plot(epochs2, val_loss2, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()

plt.show(# 2.5 LSTM 模型評估
result2 = model2.evaluate(input_test, y_test, verbose = 1)
print('Test loss:', result2[0])
print('Test accuracy:', result2[1])
)
plt.title('Training and validation loss')
plt.legend()

plt.show(# 2.5 LSTM 模型評估
result2 = model2.evaluate(input_test, y_test, verbose = 1)
print('Test loss:', result2[0])
print('Test accuracy:', result2[1])
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章