最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Tensorflow 2.4 搭建單層和多層 Bi-LSTM 模型

 更新時(shí)間:2023年01月06日 17:02:23   作者:我是王大你是誰  
這篇文章主要為大家介紹了Tensorflow 2.4 搭建單層 Bi-LSTM 模型和多層 Bi-LSTM 模型的實(shí)現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

前言

本文使用 cpu 版本的 TensorFlow 2.4 ,分別搭建單層 Bi-LSTM 模型和多層 Bi-LSTM 模型完成文本分類任務(wù)。

確保使用 numpy == 1.19.0 左右的版本,否則在調(diào)用 TextVectorization 的時(shí)候可能會報(bào) NotImplementedError 。

實(shí)現(xiàn)過程

1. 獲取數(shù)據(jù)

(1)我們本文用到的數(shù)據(jù)是電影的影評數(shù)據(jù),每個(gè)樣本包含了一個(gè)對電影的評論文本和一個(gè)情感標(biāo)簽,1 表示積極評論,0 表示負(fù)面評論,也就是說這是一份二分類的數(shù)據(jù)。

(2)我們通過 TensorFlow 內(nèi)置的函數(shù),可以從網(wǎng)絡(luò)上直接下載 imdb_reviews 數(shù)據(jù)到本地的磁盤,并取出訓(xùn)練數(shù)據(jù)和測試數(shù)據(jù)。

(3)通過使用 tf.data.Dataset 相關(guān)的處理函數(shù),我們將訓(xùn)練數(shù)據(jù)和測試數(shù)據(jù)分別進(jìn)行混洗,并且設(shè)置每個(gè) batch 大小都是 64 ,每個(gè)樣本都是 (text, label) 的形式。如下我們?nèi)×巳我庖粋€(gè) batch 中的前兩個(gè)影評文本和情感標(biāo)簽。

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]

2. 處理數(shù)據(jù)

(1)想要在模型中訓(xùn)練這些數(shù)據(jù),必須將這些文本中的 token 都轉(zhuǎn)換成機(jī)器可以識別的整數(shù),最簡單的方法就是使用 TextVectorization 來制作一個(gè)編碼器 encoder,這里只將出現(xiàn)次數(shù)最多的 1000 個(gè) token 當(dāng)做詞表,另外規(guī)定每個(gè)影評處理之后只能保留最長 200 的長度,如果超過則會被截?cái)?,如果不足則用填充字符對應(yīng)的整數(shù) 0 補(bǔ)齊。

(2)這里展現(xiàn)出來了某個(gè)樣本的經(jīng)過整數(shù)映射止之后的結(jié)果,可以看到影評對應(yīng)的整數(shù)數(shù)組長度為 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])

隨機(jī)選取一個(gè)樣本進(jìn)行證書映射結(jié)果:

[[ 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)

3. 單層 Bi-LSTM 模型

(1) 第一層是我們剛才定義好的 encoder ,將輸入的文本進(jìn)行整數(shù)的映射。

(2)第二層是 Embedding 層,我們這里設(shè)置了每個(gè)詞的詞嵌入維度為 32 維。

(3)第三層是 Bi-LSTM 層,這里我們設(shè)置了每個(gè) LSTM 單元的輸出維度為 16 維。

(4)第四層是一個(gè)輸出 8 維向量的全連接層,并且使用的 relu 激活函數(shù)。

(5)第五層是 Dropout ,設(shè)置神經(jīng)元丟棄率為 0.5 ,主要是為了防止過擬合。

(6)第六層是一個(gè)輸出 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)在沒有經(jīng)過訓(xùn)練的模型上對文本進(jìn)行預(yù)測,如果輸出小于 0 則為消極評論,如果大于 0 則為積極評論,我們可以看出這條評論本來應(yīng)該是積極評論,但是卻輸出的 logit 卻是負(fù)數(shù),即錯誤預(yù)測成了消極的。

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]))

預(yù)測結(jié)果為:

array([[-0.01437075]], dtype=float32)

array([[-0.01437075]], dtype=float32)

(8)我們使用 BinaryCrossentropy 作為損失函數(shù),需要注意的是如果模型輸出結(jié)果給到 BinaryCrossentropy 的是一個(gè) logit 值(值域范圍 [-∞, +∞] ),則應(yīng)該設(shè)置 from_logits=True 。如果模型輸出結(jié)果給到 BinaryCrossentropy 的是一個(gè)概率值 probability (值域范圍 [0, 1] ),則應(yīng)該設(shè)置為 from_logits=False 。

(9)我們使用 Adam 作為優(yōu)化器,并且設(shè)置學(xué)習(xí)率為 1e-3 。

(10)我們使用準(zhǔn)確率 accuracy 作為評估指標(biāo)。

(11)使用訓(xùn)練數(shù)據(jù)訓(xùn)練 10 個(gè) epoch,同時(shí)每經(jīng)過一個(gè) epoch 使用驗(yàn)證數(shù)據(jù)對模型進(jìn)行評估。

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)

訓(xùn)練過程如下:

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)訓(xùn)練結(jié)束之后使用測試數(shù)據(jù)對模型進(jìn)行測試,準(zhǔn)確率可以達(dá)到 0.8319 。如果經(jīng)過超參數(shù)的調(diào)整和足夠的訓(xùn)練時(shí)間,效果會更好。

model.evaluate(test_dataset)

結(jié)果為:

391/391 [==============================] - 6s 15ms/step - loss: 0.3964 - accuracy: 0.8319

(13)使用訓(xùn)練好的模型對影評進(jìn)行分類預(yù)測,可以看出可以正確得識別文本的情感取向。因?yàn)樨?fù)數(shù)表示的就是影評為負(fù)面情緒的。

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]))

結(jié)果為:

array([[-1.6402857]], dtype=float32)

4. 多層 Bi-LSTM 模型

(1)我們上面只是搭建了一層的 Bi-LSTM ,這里我們搭建了兩層的 Bi-LSTM 模型,也就是在第二層 Bidirectional 之后又加了一層 Bidirectional ,這樣可以使我們的模型更加有效。我們使用的損失函數(shù)、優(yōu)化器、評估指標(biāo)都和上面一樣。

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)

訓(xùn)練過程如下:

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)訓(xùn)練結(jié)束之后使用測試數(shù)據(jù)對模型進(jìn)行測試,準(zhǔn)確率可以達(dá)到 0.8217 。如果經(jīng)過超參數(shù)的調(diào)整和足夠的訓(xùn)練時(shí)間,效果會更好。

model.evaluate(test_dataset)

結(jié)果為:

391/391 [==============================] - 14s 35ms/step - loss: 0.4021 - accuracy: 0.8217

(3)使用訓(xùn)練好的模型對影評進(jìn)行分類預(yù)測,可以看出可以正確得識別文本的情感取向。因?yàn)檎龜?shù)表示的就是影評為積極情緒的。

sample_text = ('The movie was good. The animation and the graphics were very good. you should love movie.')
model.predict(np.array([sample_text]))

結(jié)果為:

array([[3.571126]], dtype=float32)

以上就是Tensorflow 2.4 搭建單層和多層 Bi-LSTM 模型的詳細(xì)內(nèi)容,更多關(guān)于Tensorflow搭建Bi-LSTM模型的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • pytest實(shí)現(xiàn)多種調(diào)用方式

    pytest實(shí)現(xiàn)多種調(diào)用方式

    pytest是一個(gè)非常成熟的全功能的Python測試框架,本文主要介紹了pytest多種調(diào)用方式,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • numpy如何處理Nan和inf

    numpy如何處理Nan和inf

    numpy是一個(gè)強(qiáng)大的科學(xué)計(jì)算庫,在處理數(shù)據(jù)時(shí)經(jīng)常會遇到NaN(不是數(shù)字)和Inf(無窮大)的情況,排序可以幫助我們查看這些特殊值的分布,使用np.isnan()函數(shù)可以生成一個(gè)布爾數(shù)組,用于判斷哪些是NaN值,而np.isinf()函數(shù)則用于檢測Inf值
    2024-09-09
  • Python 異步之生成器示例詳解

    Python 異步之生成器示例詳解

    這篇文章主要為大家介紹了Python 異步之生成器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • python 文件下載之?dāng)帱c(diǎn)續(xù)傳的實(shí)現(xiàn)

    python 文件下載之?dāng)帱c(diǎn)續(xù)傳的實(shí)現(xiàn)

    用python進(jìn)行文件下載的時(shí)候,一旦出現(xiàn)網(wǎng)絡(luò)波動問題,導(dǎo)致文件下載到一半。如果將下載不完全的文件刪掉,那么又需要從頭開始,如果連續(xù)網(wǎng)絡(luò)波動,是不是要頭禿了。本文提供斷點(diǎn)續(xù)傳下載工具方法,希望可以幫助到你
    2021-11-11
  • django session完成狀態(tài)保持的方法

    django session完成狀態(tài)保持的方法

    這篇文章主要介紹了django session完成狀態(tài)保持的方法,使用登錄頁面演示session的狀態(tài)保持功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-11-11
  • Python OpenCV實(shí)戰(zhàn)之與機(jī)器學(xué)習(xí)的碰撞

    Python OpenCV實(shí)戰(zhàn)之與機(jī)器學(xué)習(xí)的碰撞

    機(jī)器學(xué)習(xí)是人工智能的子集,為計(jì)算機(jī)以及其它具有計(jì)算能力的系統(tǒng)提供自動預(yù)測或決策的能力。本文主要介紹了OpenCV 提供的常見機(jī)器學(xué)習(xí)算法和技術(shù),用于解決計(jì)算機(jī)視覺項(xiàng)目中的實(shí)際問題,需要的朋友可以參考一下
    2021-12-12
  • Python使用WebSocket和SSE實(shí)現(xiàn)HTTP服務(wù)器消息推送方式

    Python使用WebSocket和SSE實(shí)現(xiàn)HTTP服務(wù)器消息推送方式

    本文介紹了兩種實(shí)時(shí)數(shù)據(jù)獲取的技術(shù):WebSocket和SSE,WebSocket是全雙工通信協(xié)議,支持雙向通信,但需要專門定義數(shù)據(jù)協(xié)議,SSE是一種單工通信技術(shù),基于HTTP的流式數(shù)據(jù)傳輸,客戶端開發(fā)簡單,但只能單工通信
    2024-11-11
  • pyinstaller?pathex參數(shù)引發(fā)打包no?module?name異常

    pyinstaller?pathex參數(shù)引發(fā)打包no?module?name異常

    這篇文章主要為大家介紹了一個(gè)關(guān)于pyinstaller的?pathex?參數(shù)所引發(fā)的打包執(zhí)行報(bào)no?module?name的異常錯誤解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Python處理mysql特殊字符的問題

    Python處理mysql特殊字符的問題

    今天小編就為大家分享一篇Python處理mysql特殊字符的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • python GUI庫圖形界面開發(fā)之PyQt5窗口背景與不規(guī)則窗口實(shí)例

    python GUI庫圖形界面開發(fā)之PyQt5窗口背景與不規(guī)則窗口實(shí)例

    這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5窗口背景與不規(guī)則窗口實(shí)例,需要的朋友可以參考下
    2020-02-02

最新評論

山东省| 嘉峪关市| 成安县| 平武县| 梁平县| 宣武区| 仙游县| 麟游县| 新兴县| 永新县| 平山县| 山东| 嘉兴市| 宣武区| 南阳市| 酉阳| 望都县| 开化县| 囊谦县| 沽源县| 泰宁县| 洪雅县| 吴堡县| 平泉县| 武清区| 土默特左旗| 乌什县| 浙江省| 获嘉县| 兰州市| 平阳县| 龙井市| 保靖县| 都兰县| 隆德县| 临汾市| 北辰区| 青铜峡市| 姚安县| 开平市| 甘洛县|