自然語言處理的領域目前正在發生巨大變化,近年來已經提出了許多出色的模型,包括Bert,GPT,等。
同時,將圖形神經網絡作為一種精緻的設計不斷地用於自然語言處理領域,例如TextGCN和Tensor-TextGCN。
該工具箱專用於自然語言處理,並希望以最簡單的方式實現模型。
關鍵字:NLP; Gnn
型號:
示例(有關更多詳細信息,請參見測試):
上述所有實驗均在GTX 1080 GPU上測試,並使用存儲器8000MIB進行測試。
2020/5/ - :將項目名稱轉換為FERNLP的NLPGNN。
2020/5/17:嘗試根據BERT注意矩陣將句子轉換為圖形,但失敗了。本節提供了可視化BERT注意矩陣的解決方案。有關更多詳細信息,您可以檢查字典“ bert-gcn”。
2020/5/11:添加文本和文本分類的文本和文本。
2020/5/5:添加杜松子酒,圖形分類的圖形。
2020/4/25:基於消息傳遞方法,添加gan,杜松子酒模型。
2020/4/23:基於消息傳遞方法添加GCN模型。
2020/4/16:目前專注於NLP中的GNN模型,並試圖將一些GNN模型集成到FERNLP中。
2020/4/2:添加GPT2模型,可以使用OpenAI(基礎,中,大)發布的參數。更詳細的參考字典“ tg/en/interactive.py”
2020/3/26:添加Bilstm+分類的注意示例
2020/3/23:添加RADAM優化器。
2020/3/19:添加測試示例“ albert_ner_train.py”“ albert_ner_test.py”
2020/3/16:添加基於BPE方法的訓練子單詞嵌入的模型。訓練有素的嵌入在TextCNN模型中用於改進其改進。有關更多詳細信息,請參見“ tran_bpe_embeding.py”。
2020/3/8:添加測試示例“ run_tucker.py”,用於WN18上的火車塔克。
2020/3/3:添加測試示例“ tran_text_cnn.py”,用於火車文本模型。
2020/3/2:基於BERT的文本分類添加測試示例“ Train_bert_classification.py”。
git clone https://github.com/kyzhouhzau/NLPGNN.git
python setup.py install
python bert_ner_train.py
將火車,有效和測試文件放入“輸入”字典中。
數據格式:“測試 ner input train”中的參考數據
例如“ rank” rankl對對破骨細細作用用用用。用用用用用。用用。 。用用用。 。用用
對於火車中的每一行都包含兩個部分,第一部分是“ rankl rank”對破胞的作用用。 ”是一個句子。第二部分是“ oooo b-解剖學i-解剖學i-解剖e-Anatomy oooo”是句子中每個單詞的標籤。他們倆都使用“ t”來連接。
from nlpgnn . models import bert
bert = bert . BERT () python bert_ner_train.py
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
bert (BERT) multiple 101677056
_________________________________________________________________
dense (Dense) multiple 35374
=================================================================
Total params: 101,712,430
Trainable params: 101,712,430
Non-trainable params: 0
_________________________________________________________________
from nlpgnn . models import bert
from nlpgnn . metrics . crf import CrfLogLikelihood
bert = bert . BERT ()
crf = CrfLogLikelihood () python bert_ner_crf_train.py
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
bert (BERT) multiple 101677056
_________________________________________________________________
dense (Dense) multiple 35374
_________________________________________________________________
crf (CrfLogLikelihood) multiple 2116
=================================================================
Total params: 101,714,546
Trainable params: 101,714,546
Non-trainable params: 0
_________________________________________________________________
from nlpgnn . models import albert
bert = albert . ALBERT () python albert_ner_train.py
large
Model: "albert_ner"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
albert (ALBERT) multiple 11092992
_________________________________________________________________
dense (Dense) multiple 6921
=================================================================
Total params: 11,099,913
Trainable params: 11,099,913
Non-trainable params: 0
_________________________________________________________________
使用默認參數,我們在“中文糖尿病標註數據集”和“ Conll-2003”有效數據上獲得以下結果。
| 模型 | 宏F1 | 宏-P | 宏R | LR | 時代 | 麥克斯倫 | batch_size | 數據 |
|---|---|---|---|---|---|---|---|---|
| Bert+基礎 | 0.7005 | 0.7244 | 0.7031 | 2E-5 | 3 | 128 | 6 | 中文糖尿病標註數據集 |
| BERT+BASE+CRF | 0.7009 | 0.7237 | 0.7041 | 2E-5(BERT),2E-3(CRF) | 3 | 128 | 6 | 中文糖尿病標註數據集 |
| Bert+基礎 | 0.9128 | 0.9208 | 0.9227 | 2E-5 | 5 | 128 | 8 | Conll-2003 |
| 阿爾伯特+基地 | 0.8512 | 0.8678 | 0.8589 | 1E-4 | 8 | 128 | 16 | Conll-2003 |
| 阿爾伯特+大 | 0.8670 | 0.8778 | 0.8731 | 2E-5 | 10 | 128 | 4 | Conll-2003 |
將火車,有效和測試文件放入“輸入”字典中。
數據格式:“ tests cls bert(或Albert) Input”中的參考數據。
例如,“作作上曾經強拳王之,小王之之之王王王克里琴琴科科科談自自己己是否是是否否會會2”
對於火車(測試,有效)中的每一行,第一部分“作為上曾經最拳拳王之王,小小克里琴琴琴談談談己己是是否否否出會復復會會會復复”是句子,第二部分是“ 2”。
from nlpgnn . models import bert
bert = bert . BERT () python train_bert_classification.py
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
bert (BERT) multiple 102267648
_________________________________________________________________
dense (Dense) multiple 11535
=================================================================
Total params: 102,279,183
Trainable params: 102,279,183
Non-trainable params: 0
_________________________________________________________________
from nlpgnn . models import TextCNN
model = TextCNN . TextCNN () python train_text_cnn.py
Use "WordPiece embedding" to Initialize word embedding. Train your embeddings.
python train_bpe_embedding.py
有關更多詳細信息參考文字
使用默認參數,我們在“新聞標題短文本分類”和SST-2有效數據上獲得以下結果。
| 模型 | ACC | LR | 時代 | 麥克斯倫 | batch_size | 數據 |
|---|---|---|---|---|---|---|
| Bert+基礎 | 0.8899 | 1E-5 | 5 | 50 | 32 | 新聞標題短文本分類 |
| Bert+基礎 | 0.9266 | 2E-5 | 3 | 128 | 8 | SST-2 |
| 阿爾伯特+基地 | 0.9186 | 1E-5 | 3 | 128 | 16 | SST-2 |
| 阿爾伯特+大 | 0.9461 | 1E-6 | 3 | 128 | 4 | SST-2 |
| Bilstm+注意 | 0.8269 | 0.01 | 3 | 128 | 64 | SST-2 |
| textcnn | 0.8233 | 0.01 | 3 | 128 | 64 | SST-2 |
from nlpgnn . models import gpt2
bert = gpt2 . GPT2 () python interactive.py
Model: "gen_gp_t2" base
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
gpt2 (GPT2) multiple 124439808
=================================================================
Total params: 124,439,808
Trainable params: 124,439,808
Non-trainable params: 0
_________________________________________________________________
例子:
Input >>> Stocks continue to fall this week
Output >>> as stocks fall for the second consecutive week as investors flee for safe havens.
"The market is off the charts," said John Schmieding, senior vice president, market strategy at RBC Capital Markets.
"We don't know what the Fed's intent is on, what direction it's going in. We don't know where they plan to go.
We don't know what direction they're going to move into."
張板可以幫助您可視化損失並評估指標:
USEAGE:
tensorboard --port 6006 --logdir="./tensorboard"


與本文相同的數據拆分和參數設置
| 模型 | 科拉 | PubMed | Citeseer |
|---|---|---|---|
| GCN | 81.80 | 79.50 | 71.20 |
| 甘 | 83.00 | 79.00 | 72.30 |
| 蓋 | 82.40 | 79.60 | 71.70 |
| 模型 | Mutag | 蛋白質 | NCI1 |
|---|---|---|---|
| 杜松子酒 | 87.62±8.76 # | 73.05±1.85 # | 73.13±5.57 # |
| 圖形 | 86.06±8.26 | 75.11±2.87 | 76.91±3.45 |
注意:#符號表示當前結果小於紙張結果。在本文中,作者使用此方法評估模型。這種方法很昂貴。所以我在這裡沒有這樣做。
| 模型 | R8 | R52 |
|---|---|---|
| 文字 | 96.68±0.42 | 92.80±0.32 |
| TextGCN2019 | 97.108±0.243 | 92.512±0.249 |
1,對於英語任務,您需要設置參數“ cased”(在fennlp.datas.checkpoint.checkpoint.loadcheckpoint中),以與您的預處理輸入數據一致,以確保令牌可以正確區分案例。
2,當您使用Bert或Albert時,需要以下參數:
param.maxlen
param.label_size
param.batch_size
如果您不知道Label_size的計數,則腳本將在第一次運行火車代碼時告訴您。
3,學習率和batch_size將確定模型收斂,請參見鏈接以獲取更多詳細信息。
4,如果您不熟悉Bert和Albert中的優化器,那沒關係。您需要記住的最重要的事情是參數“ Learning_rate”和“ Decay_steps”(在Fennlp.optimizers.optim.adamwarmup中)很重要。您可以將“學習率”設置為相對較小的值,讓“ decay_steps”等於樣本*epoch/batch_size或更高。
5,如果發現代碼運行較慢,則可以嘗試使用 @ tf.Function並設置適當的模型寫作和評估頻率。
6,其他任何其他問題,您都可以通過“ [email protected]”來限制我或提出有關問題的問題。
[1] BERT:深層雙向變壓器的預訓練以理解語言
[2]阿爾伯特:一個精簡的語言表示學習的精簡版
[3]語言模型是無監督的多任務學習者
[4]量子化學的神經信息傳遞
[5]與圖形卷積網絡的半監督分類
[6]圖形注意網絡
[7]圖形神經網絡有多強大?
[8]圖:歸納表示在大圖上學習
[9]擴散改善圖形學習
[10]基準圖形神經網絡
[11]用於文本分類的文本級別圖形神經網絡
[12]用於文本分類的圖形卷積網絡
[13]用於文本分類的張量圖卷積網絡
[14]對半監督學習的圖形卷積網絡的更深入見解