Bilstm+CRF模型的張量實現,用於序列標記任務。
Sequential labeling是一種典型的方法,建模NLP中的序列預測任務。常見的順序標記任務包括,例如,
以命名實體識別(NER)任務為示例:
Stanford University located at California .
B-ORG I-ORG O O B-LOC O在這裡,將提取兩個實體, Stanford University和California 。具體而言,文本中的每個token都用相應的label標記。例如,{ token :斯坦福大學, label : b-org }。給定令牌序列,序列標記模型旨在預測標籤序列。
Lample等人提出的BiLSTM+CRF ,2016年,是迄今為止用於順序標記任務的最古典和穩定的神經模型。 
配置所有設置
train / test / interactive_predict / api_service ]BIO / BIESO ]PER | LOC | ORG ]記錄所有內容
Web App Demo可輕鬆演示
面向對象:bilstm_crf,數據集,configer,utils
用清晰的結構模塊化,易於DIY。
在手冊中查看更多。
下載倉庫直接使用。
git clone https://github.com/scofield7419/sequence-labeling-BiLSTM-CRF.git
pip install -r requirements.txt
將Bilstm-CRF軟件包作為模塊安裝。
pip install BiLSTM-CRF
用法:
from BiLSTM-CRF.engines.BiLSTM_CRFs import BiLSTM_CRFs as BC
from BiLSTM-CRF.engines.DataManager import DataManager
from BiLSTM-CRF.engines.Configer import Configer
from BiLSTM-CRF.engines.utils import get_logger
...
config_file = r'/home/projects/system.config'
configs = Configer(config_file)
logger = get_logger(configs.log_dir)
configs.show_data_summary(logger) # optional
dataManager = DataManager(configs, logger)
model = BC(configs, logger, dataManager)
###### mode == 'train':
model.train()
###### mode == 'test':
model.test()
###### mode == 'single predicting':
sentence_tokens, entities, entities_type, entities_index = model.predict_single(sentence)
if configs.label_level == 1:
print("nExtracted entities:n %snn" % ("n".join(entities)))
elif configs.label_level == 2:
print("nExtracted entities:n %snn" % ("n".join([a + "t(%s)" % b for a, b in zip(entities, entities_type)])))
###### mode == 'api service webapp':
cmd_new = r'cd demo_webapp; python manage.py runserver %s:%s' % (configs.ip, configs.port)
res = os.system(cmd_new)
open `ip:port` in your browser.
├── main.py
├── system.config
├── HandBook.md
├── README.md
│
├── checkpoints
│ ├── BILSTM-CRFs-datasets1
│ │ ├── checkpoint
│ │ └── ...
│ └── ...
├── data
│ ├── example_datasets1
│ │ ├── logs
│ │ ├── vocabs
│ │ ├── test.csv
│ │ ├── train.csv
│ │ └── dev.csv
│ └── ...
├── demo_webapp
│ ├── demo_webapp
│ ├── interface
│ └── manage.py
├── engines
│ ├── BiLSTM_CRFs.py
│ ├── Configer.py
│ ├── DataManager.py
│ └── utils.py
└── tools
├── calcu_measure_testout.py
└── statis.py
折疊
engines折疊中,提供核心功能PY。data-subfold折疊中,放置了數據集。checkpoints-subfold折疊中,存儲模型檢查點。demo_webapp折疊中,我們可以在Web中演示系統,並提供API。tools折疊中,提供了一些離線效應。文件
main.py是系統的輸入python文件。system.config是所有系統設置的配置文件。HandBook.md提供了一些用法說明。BiLSTM_CRFs.py是主要模型。Configer.py解析system.config 。DataManager.py管理數據集和調度。utils.py在飛行工具上提供。 在以下步驟下:
system.config中編寫您的配置文件。
main.py 
main.py main.py
main.py

包括火車集,測試集,DEV集在內的數據集對於整體使用情況是必需的。但是,您是否只想訓練模型脫機,只需要火車集。訓練後,您可以使用保存的模型檢查點文件進行推斷。如果您想進行測試,應該
對於trainset , testset , devset ,常見格式如下:
(Token) (Label)
for O
the O
lattice B_TAS
QCD I_TAS
computation I_TAS
of I_TAS
nucleon–nucleon I_TAS
low-energy I_TAS
interactions E_TAS
. O
It O
consists O
in O
simulating B_PRO
...
(Token) (Label)
马 B-LOC
来 I-LOC
西 I-LOC
亚 I-LOC
副 O
总 O
理 O
。 O
他 O
兼 O
任 O
财 B-ORG
政 I-ORG
部 I-ORG
长 O
...
注意:
testset只能使用Token行存在。在測試過程中,模型將基於test.csv輸出預測的實體。輸出文件包括兩個: test.out , test.entity.out (可選)。
test.out
與輸入test.csv相同的形成。
test.entity.out
Sentence
entity1 (Type)
entity2 (Type)
entity3 (Type)
...

如果您想將該項目適應自己的特定序列標籤任務,則可能需要以下提示。
下載回購資源。
標籤方案(最重要)
B_PER', i_loc'模型:將模型體系結構修改為您想要的模型架構,以BiLSTM_CRFs.py 。
數據集:在正確的編輯中適應您的數據集。
訓練
有關更多使用詳細信息,請參考手冊
歡迎您發出任何錯誤。