TF之Transformer:基于tensorflow和Keras框架(特征编码+Tokenizer处理文本+保存模型)针对UCI新闻数据集利用Transformer算法实现新闻文本多分类案例
生活随笔
收集整理的這篇文章主要介紹了
TF之Transformer:基于tensorflow和Keras框架(特征编码+Tokenizer处理文本+保存模型)针对UCI新闻数据集利用Transformer算法实现新闻文本多分类案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
TF之Transformer:基于tensorflow和Keras框架(特征編碼+Tokenizer處理文本+保存模型)針對UCI新聞數據集利用Transformer算法實現新聞文本多分類案例
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 422419 entries, 0 to 422418
Data columns (total 8 columns):# Column Non-Null Count Dtype
--- ------ -------------- ----- 0 ID 422419 non-null int64 1 TITLE 422419 non-null object2 URL 422419 non-null object3 PUBLISHER 422417 non-null object4 CATEGORY 422419 non-null object5 STORY 422419 non-null object6 HOSTNAME 422419 non-null object7 TIMESTAMP 422419 non-null int64
dtypes: int64(2), object(6)
memory usage: 25.8+ MB
目錄
基于tensorflow和Keras框架(特征編碼+Tokenizer處理文本數據+保存模型)針對UCI新聞數據集利用Transformer算法實現新聞文本多分類案例
# 1、定義數據集
# 2、數據預處理
# 2.1、【類別型】特征編碼化
# 3、模型訓練與推理
# 3.1、切分數據集
# 3.2、文本數據再處理
# 配置Tokenizer
# 3.3、模型創建與編譯
# 定義超參數
# 創建Transformer模型
# 編譯模型
# 3.4、模型訓練
# 3.5、評估模型
# T1、模型保存ckpt
# T2、模型保存h5
# 4、模型推理
# T1、加載ckpt的最新的模型參數
# T2、加載h5模型
相關文章
TF之Transformer:基于tensorflow和Keras框架(特征編碼+Tokenizer處理文本數據+保存模型)針對UCI新聞數據集利用Transformer算法實現新聞文本多分類案例
TF之Transformer:基于tensorflow和Keras框架(特征編碼+Tokenizer處理文本數據+保存模型)針對UCI新聞數據集利用Transformer算法實現新聞文本多分類案例實現代碼
基于tensorflow和Keras框架(特征編碼+Tokenizer處理文本數據+保存模型)針對UCI新聞數據集利用Transformer算法實現新聞文本多分類案例
# 1、定義數據集
| ID | TITLE | URL | PUBLISHER | CATEGORY | STORY | HOSTNAME | TIMESTAMP |
| 1 | Fed official says weak data caused by weather, should not slow taper | http://www.latimes.com/business/money/la-fi-mo-federal-reserve-plosser-stimulus-economy-20140310,0,1312750.story\?track=rss | Los Angeles Times | b | ddUyU0VZz0BRneMioxUPQVP6sIxvM | www.latimes.com | 1.39447E+12 |
| 2 | Fed's Charles Plosser sees high bar for change in pace of tapering | http://www.livemint.com/Politics/H2EvwJSK2VE6OF7iK1g3PP/Feds-Charles-Plosser-sees-high-bar-for-change-in-pace-of-ta.html | Livemint | b | ddUyU0VZz0BRneMioxUPQVP6sIxvM | www.livemint.com | 1.39447E+12 |
| 3 | US open: Stocks fall after Fed official hints at accelerated tapering | http://www.ifamagazine.com/news/us-open-stocks-fall-after-fed-official-hints-at-accelerated-tapering-294436 | IFA Magazine | b | ddUyU0VZz0BRneMioxUPQVP6sIxvM | www.ifamagazine.com | 1.39447E+12 |
| 4 | Fed risks falling 'behind the curve', Charles Plosser says | http://www.ifamagazine.com/news/fed-risks-falling-behind-the-curve-charles-plosser-says-294430 | IFA Magazine | b | ddUyU0VZz0BRneMioxUPQVP6sIxvM | www.ifamagazine.com | 1.39447E+12 |
| 5 | Fed's Plosser: Nasty Weather Has Curbed Job Growth | http://www.moneynews.com/Economy/federal-reserve-charles-plosser-weather-job-growth/2014/03/10/id/557011 | Moneynews | b | ddUyU0VZz0BRneMioxUPQVP6sIxvM | www.moneynews.com | 1.39447E+12 |
| 6 | Plosser: Fed May Have to Accelerate Tapering Pace | http://www.nasdaq.com/article/plosser-fed-may-have-to-accelerate-tapering-pace-20140310-00371 | NASDAQ | b | ddUyU0VZz0BRneMioxUPQVP6sIxvM | www.nasdaq.com | 1.39447E+12 |
| 7 | Fed's Plosser: Taper pace may be too slow | http://www.marketwatch.com/story/feds-plosser-taper-pace-may-be-too-slow-2014-03-10\?reflink=MW_news_stmp | MarketWatch | b | ddUyU0VZz0BRneMioxUPQVP6sIxvM | www.marketwatch.com | 1.39447E+12 |
| 8 | Fed's Plosser expects US unemployment to fall to 6.2% by the end of 2014 | http://www.fxstreet.com/news/forex-news/article.aspx\?storyid=23285020-b1b5-47ed-a8c4-96124bb91a39 | FXstreet.com | b | ddUyU0VZz0BRneMioxUPQVP6sIxvM | www.fxstreet.com | 1.39447E+12 |
| 9 | US jobs growth last month hit by weather:Fed President Charles Plosser | http://economictimes.indiatimes.com/news/international/business/us-jobs-growth-last-month-hit-by-weatherfed-president-charles-plosser/articleshow/31788000.cms | Economic Times | b | ddUyU0VZz0BRneMioxUPQVP6sIxvM | economictimes.indiatimes.com | 1.39447E+12 |
| 10 | ECB unlikely to end sterilisation of SMP purchases - traders | http://www.iii.co.uk/news-opinion/reuters/news/152615 | Interactive Investor | b | dPhGU51DcrolUIMxbRm0InaHGA2XM | www.iii.co.uk | 1.39447E+12 |
# 2、數據預處理
# 2.1、【類別型】特征編碼化
num_classes 4 ['b' 't' 'e' 'm'] <class 'pandas.core.frame.DataFrame'> RangeIndex: 422419 entries, 0 to 422418 Data columns (total 9 columns):# Column Non-Null Count Dtype --- ------ -------------- ----- 0 ID 422419 non-null int64 1 TITLE 422419 non-null object2 URL 422419 non-null object3 PUBLISHER 422417 non-null object4 CATEGORY 422419 non-null object5 STORY 422419 non-null object6 HOSTNAME 422419 non-null object7 TIMESTAMP 422419 non-null int64 8 category_id 422419 non-null int64 dtypes: int64(3), object(6) memory usage: 29.0+ MB# 3、模型訓練與推理
# 3.1、切分數據集
# 3.2、文本數據再處理
# 配置Tokenizer
# 3.3、模型創建與編譯
# 定義超參數
# 創建Transformer模型
# 編譯模型
# 3.4、模型訓練
Epoch 1/5 10561/10561 [==============================] - 10770s 1s/step - loss: 0.3124 - accuracy: 0.8936 - val_loss: 0.2171 - val_accuracy: 0.9253 Epoch 2/5 10561/10561 [==============================] - 10724s 1s/step - loss: 0.1968 - accuracy: 0.9323 - val_loss: 0.1921 - val_accuracy: 0.9334 Epoch 3/53490/10561 [========>.....................] - ETA: 1:48:29 - loss: 0.1757 - accuracy: 0.9388Epoch 1/3 10561/10561 [==============================] - ETA: 0s - loss: 0.3350 - accuracy: 0.8837# 3.5、評估模型
# T1、模型保存ckpt
# T2、模型保存h5
# 4、模型推理
# T1、加載ckpt的最新的模型參數
# 將輸入文本轉換為數字序列并進行預測 T1、加載ckpt的最新的模型參數-------------------------------------- 1/1 [==============================] - 0s 441ms/step Apple announces new iPhone 13 ('t', 0.98686767) 1/1 [==============================] - 0s 24ms/step Bitcoin reaches all-time high ('b', 0.8688579)# T2、加載h5模型
# 將輸入文本轉換為數字序列并進行預測 T2、加載h5模型-------------------------------------- 1/1 [==============================] - 0s 498ms/step Apple is expected to launch a new iPhone in September ('e', 0.87868917) 1/1 [==============================] - 0s 16ms/step Apple announces new iPhone 13 ('e', 0.93488216) 1/1 [==============================] - 0s 17ms/step Bitcoin reaches all-time high ('e', 0.93678874)總結
以上是生活随笔為你收集整理的TF之Transformer:基于tensorflow和Keras框架(特征编码+Tokenizer处理文本+保存模型)针对UCI新闻数据集利用Transformer算法实现新闻文本多分类案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决ios微信浏览器时间不兼容的问题
- 下一篇: overflow 的各种用法