您好,登錄后才能下訂單哦!
這篇文章主要介紹“Tensorflow2.4如何搭建單層和多層Bi-LSTM模型”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“Tensorflow2.4如何搭建單層和多層Bi-LSTM模型”文章能幫助大家解決問題。
本文使用 cpu 版本的 TensorFlow 2.4 ,分別搭建單層 Bi-LSTM 模型和多層 Bi-LSTM 模型完成文本分類任務。
確保使用 numpy == 1.19.0 左右的版本,否則在調用 TextVectorization 的時候可能會報 NotImplementedError 。
(1)我們本文用到的數據是電影的影評數據,每個樣本包含了一個對電影的評論文本和一個情感標簽,1 表示積極評論,0 表示負面評論,也就是說這是一份二分類的數據。
(2)我們通過 TensorFlow 內置的函數,可以從網絡上直接下載 imdb_reviews 數據到本地的磁盤,并取出訓練數據和測試數據。
(3)通過使用 tf.data.Dataset 相關的處理函數,我們將訓練數據和測試數據分別進行混洗,并且設置每個 batch 大小都是 64 ,每個樣本都是 (text, label) 的形式。如下我們取了任意一個 batch 中的前兩個影評文本和情感標簽。
import numpy as np import tensorflow_datasets as tfds import tensorflow as tf import matplotlib.pyplot as plt tfds.disable_progress_bar() BUFFER_SIZE = 10000 BATCH_SIZE = 64 dataset, info = tfds.load('imdb_reviews', with_info=True, as_supervised=True) train_dataset, test_dataset = dataset['train'], dataset['test'] train_dataset = train_dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE) test_dataset = test_dataset.batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE) for example, label in train_dataset.take(1): print('text: ', example.numpy()[:2]) print('label: ', label.numpy()[:2])
部分樣本顯示:
text: [
b"First of all, I have to say I have worked for blockbuster and have seen quite a few movies to the point its tough for me to find something I haven't seen. Taking this into account, I want everyone to know that this movie was by far the worst film ever made, it made me pine for Gigli, My Boss's Daughter, and any other piece of junk you've ever seen. BeLyt must be out of his mind, I've only found one person who liked it and even they couldn't tell me what the movie was about. If you are able to decipher this movie and are able to tell me what it was about you have to either be the writer or a fortune teller because there's any other way a person could figure this crap out.<br /><br />FOR THE LOVE OF G-D STAY AWAY!"
b"Just got out and cannot believe what a brilliant documentary this is. Rarely do you walk out of a movie theater in such awe and amazement. Lately movies have become so over hyped that the thrill of discovering something truly special and unique rarely happens. Amores Perros did this to me when it first came out and this movie is doing to me now. I didn't know a thing about this before going into it and what a surprise. If you hear the concept you might get the feeling that this is one of those touchy movies about an amazing triumph covered with over the top music and trying to have us fully convinced of what a great story it is telling but then not letting us in. Fortunetly this is not that movie. The people tell the story! This does such a good job of capturing every moment of their involvement while we enter their world and feel every second with them. There is so much beyond the climb that makes everything they go through so much more tense. Touching the Void was also a great doc about mountain climbing and showing the intensity in an engaging way but this film is much more of a human story. I just saw it today but I will go and say that this is one of the best documentaries I have ever seen."
]
label: [0 1]
(1)想要在模型中訓練這些數據,必須將這些文本中的 token 都轉換成機器可以識別的整數,最簡單的方法就是使用 TextVectorization 來制作一個編碼器 encoder,這里只將出現次數最多的 1000 個 token 當做詞表,另外規定每個影評處理之后只能保留最長 200 的長度,如果超過則會被截斷,如果不足則用填充字符對應的整數 0 補齊。
(2)這里展現出來了某個樣本的經過整數映射止之后的結果,可以看到影評對應的整數數組長度為 200 。
MAX_SEQ_LENGTH = 200 VOCAB_SIZE = 1000 encoder = tf.keras.layers.experimental.preprocessing.TextVectorization(max_tokens=VOCAB_SIZE, output_sequence_length=MAX_SEQ_LENGTH) encoder.adapt(train_dataset.map(lambda text, label: text)) vocab = np.array(encoder.get_vocabulary()) encoded_example = encoder(example)[:1].numpy() print(encoded_example) print(label[:1])
隨機選取一個樣本進行證書映射結果:
[[ 86 5 32 10 26 6 130 10 26 926 16 1 3 26 108 176 4 164
93 6 2 215 30 1 16 70 6 160 140 10 731 108 647 11 78 1
10 178 305 6 118 12 11 18 14 33 234 2 240 20 122 91 9 91
70 1 16 1 56 1 580 3 99 81 408 5 1 825 122 108 1 217
28 46 5 25 349 195 61 249 29 409 37 405 9 3 54 35 404 360
70 49 2 18 14 43 45 23 24 491 6 1 11 18 3 24 491 6
360 70 49 9 14 43 23 26 6 352 28 2 762 42 4 1 1 80
213 99 81 97 4 409 96 811 11 638 1 13 16 2 116 5 1 766
242 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0]]
tf.Tensor([0], shape=(1,), dtype=int64)
(1) 第一層是我們剛才定義好的 encoder ,將輸入的文本進行整數的映射。
(2)第二層是 Embedding 層,我們這里設置了每個詞的詞嵌入維度為 32 維。
(3)第三層是 Bi-LSTM 層,這里我們設置了每個 LSTM 單元的輸出維度為 16 維。
(4)第四層是一個輸出 8 維向量的全連接層,并且使用的 relu 激活函數。
(5)第五層是 Dropout ,設置神經元丟棄率為 0.5 ,主要是為了防止過擬合。
(6)第六層是一個輸出 1 維向量的全連接層,也就是輸出層,表示的是該樣本的 logit 。
model = tf.keras.Sequential([ encoder, tf.keras.layers.Embedding( input_dim=len(encoder.get_vocabulary()), output_dim=32, mask_zero=True), tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(16)), tf.keras.layers.Dense(8, activation='relu'), tf.keras.layers.Dropout(0.5), tf.keras.layers.Dense(1) ])
(7)在沒有經過訓練的模型上對文本進行預測,如果輸出小于 0 則為消極評論,如果大于 0 則為積極評論,我們可以看出這條評論本來應該是積極評論,但是卻輸出的 logit 卻是負數,即錯誤預測成了消極的。
sample_text = ('The movie was cool. The animation and the graphics were out of this world. I would recommend this movie.') model.predict(np.array([sample_text]))
預測結果為:
array([[-0.01437075]], dtype=float32)
array([[-0.01437075]], dtype=float32)
(8)我們使用 BinaryCrossentropy 作為損失函數,需要注意的是如果模型輸出結果給到 BinaryCrossentropy 的是一個 logit 值(值域范圍 [-∞, +∞] ),則應該設置 from_logits=True 。如果模型輸出結果給到 BinaryCrossentropy 的是一個概率值 probability (值域范圍 [0, 1] ),則應該設置為 from_logits=False 。
(9)我們使用 Adam 作為優化器,并且設置學習率為 1e-3 。
(10)我們使用準確率 accuracy 作為評估指標。
(11)使用訓練數據訓練 10 個 epoch,同時每經過一個 epoch 使用驗證數據對模型進行評估。
model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True), optimizer=tf.keras.optimizers.Adam(1e-3), metrics=['accuracy']) history = model.fit(train_dataset, epochs=10, validation_data=test_dataset, validation_steps=30)
訓練過程如下:
Epoch 1/10 391/391 [==============================] - 30s 65ms/step - loss: 0.6461 - accuracy: 0.5090 - val_loss: 0.4443 - val_accuracy: 0.8245 Epoch 2/10 391/391 [==============================] - 23s 58ms/step - loss: 0.4594 - accuracy: 0.6596 - val_loss: 0.3843 - val_accuracy: 0.8396 ... Epoch 10/10 391/391 [==============================] - 22s 57ms/step - loss: 0.3450 - accuracy: 0.8681 - val_loss: 0.3920 - val_accuracy: 0.8417
(12)訓練結束之后使用測試數據對模型進行測試,準確率可以達到 0.8319 。如果經過超參數的調整和足夠的訓練時間,效果會更好。
model.evaluate(test_dataset)
結果為:
391/391 [==============================] - 6s 15ms/step - loss: 0.3964 - accuracy: 0.8319
(13)使用訓練好的模型對影評進行分類預測,可以看出可以正確得識別文本的情感取向。因為負數表示的就是影評為負面情緒的。
sample_text = ('The movie was not cool. The animation and the graphics were bad. I would not recommend this movie.') model.predict(np.array([sample_text]))
結果為:
array([[-1.6402857]], dtype=float32)
(1)我們上面只是搭建了一層的 Bi-LSTM ,這里我們搭建了兩層的 Bi-LSTM 模型,也就是在第二層 Bidirectional 之后又加了一層 Bidirectional ,這樣可以使我們的模型更加有效。我們使用的損失函數、優化器、評估指標都和上面一樣。
model = tf.keras.Sequential([ encoder, tf.keras.layers.Embedding(len(encoder.get_vocabulary()), 32, mask_zero=True), tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32, return_sequences=True)), tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(16)), tf.keras.layers.Dense(8, activation='relu'), tf.keras.layers.Dropout(0.5), tf.keras.layers.Dense(1) ]) model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True), optimizer=tf.keras.optimizers.Adam(1e-3), metrics=['accuracy']) history = model.fit(train_dataset, epochs=10, validation_data=test_dataset, validation_steps=30)
訓練過程如下:
Epoch 1/10 391/391 [==============================] - 59s 124ms/step - loss: 0.6170 - accuracy: 0.5770 - val_loss: 0.3931 - val_accuracy: 0.8135 Epoch 2/10 391/391 [==============================] - 45s 114ms/step - loss: 0.4264 - accuracy: 0.7544 - val_loss: 0.3737 - val_accuracy: 0.8380 ... Epoch 10/10 391/391 [==============================] - 45s 114ms/step - loss: 0.3138 - accuracy: 0.8849 - val_loss: 0.4069 - val_accuracy: 0.8323
(2)訓練結束之后使用測試數據對模型進行測試,準確率可以達到 0.8217 。如果經過超參數的調整和足夠的訓練時間,效果會更好。
model.evaluate(test_dataset)
結果為:
391/391 [==============================] - 14s 35ms/step - loss: 0.4021 - accuracy: 0.8217
(3)使用訓練好的模型對影評進行分類預測,可以看出可以正確得識別文本的情感取向。因為正數表示的就是影評為積極情緒的。
sample_text = ('The movie was good. The animation and the graphics were very good. you should love movie.') model.predict(np.array([sample_text]))
結果為:
array([[3.571126]], dtype=float32)
關于“Tensorflow2.4如何搭建單層和多層Bi-LSTM模型”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。