स्पासी नेर मॉडल के लिए मेरा इनपुट डेटा BILUO
टैगिंग स्कीम में है और मैं इसे कुछ आवश्यकता के हिस्से के रूप में उपयोग करना चाहता हूं। जब मैं बिना मिनीबैच के मॉडल को प्रशिक्षित करने का प्रयास करता हूं, तो यह ठीक काम करता है (टिप्पणी की गई भाग)। लेकिन मैं यह समझने में असमर्थ हूं कि मॉडल की सटीकता बढ़ाने के लिए यहां मिनीबैच और गोल्डपार्स का उपयोग कैसे किया जाए। क्या मेरी उम्मीदें यहां मान्य हैं क्योंकि मुझे इस तरह के संयोजन के साथ एक भी उदाहरण नहीं मिला? इसके अलावा, मैंने पहले ही मॉडल को स्टार्ट, एंड, लेबल फॉर्मेट के दृष्टिकोण से प्रशिक्षित किया है। कृपया इस खंड का पता लगाने में मेरी मदद करें। मेरा कोड नीचे जैसा है,
import spacy
from spacy.gold import offsets_from_biluo_tags
from spacy.gold import biluo_tags_from_offsets
import random
from spacy.util import minibatch, compounding
from os import path
from tqdm import tqdm
def train_spacy(data, iterations, model=None):
TRAIN_DATA = data
print(f"downloads = {model}")
if model is not None and path.exists(model):
print(f"training existing model")
nlp = spacy.load(model)
print("Model is Loaded '%s'" % model)
else:
print(f"Creating new model")
nlp = spacy.blank('en') # create blank Language class
if 'ner' not in nlp.pipe_names:
ner = nlp.create_pipe('ner')
nlp.add_pipe(ner, last=True)
else:
ner = nlp.get_pipe('ner')
# Based on template, get labels and save those for further training
LABEL = ["Name", "ORG"]
for i in LABEL:
# print(i)
ner.add_label(i)
# get names of other pipes to disable them during training
other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'ner']
with nlp.disable_pipes(*other_pipes): # only train NER
if model is None:
optimizer = nlp.begin_training()
else:
optimizer = nlp.entity.create_optimizer()
tags = dict()
for itn in range(iterations):
print("Starting iteration " + str(itn))
random.shuffle(TRAIN_DATA)
losses = {}
# for text, annotations in tqdm(TRAIN_DATA):
# print(f"text={text}, an={annotations}")
# tags['entities'] = offsets_from_biluo_tags(nlp(text), annotations)
# print(f"a={tags}")
# nlp.update([text], # batch of texts
# [tags], # batch of annotations
# drop=0.5, # dropout - make it harder to memorise data
# sgd=optimizer, # callable to update weights
# losses=losses)
# print(losses)
batches = minibatch(TRAIN_DATA, size=compounding(4.0, 16.0, 1.001))
# type 2 with mini batch
for batch in batches:
texts, annotations = zip(*batch)
print(texts)
tags = {'entities': annotations}
nlp.update(
texts, # batch of texts
[tags], # batch of annotations
drop=0.4, # dropout - make it harder to memorise data
losses=losses,
sgd=optimizer
)
print(losses)
return nlp
data_biluo = [
('I am Shah Khan, I work in MS Co', ['O', 'O', 'B-Name', 'L-Name', 'O', 'O', 'O', 'B-ORG', 'L-ORG']),
('I am Tom Tomb, I work in Telecom Networks', ['O', 'O', 'B-Name', 'L-Name', 'O', 'O', 'O', 'B-ORG', 'L-ORG'])
]
model = train_spacy(data_biluo, 10)
model.to_disk('./Vectors/')
1 उत्तर
आपको अपने मिनीबैच के साथ 2 समस्याएं हैं:
tags
ऑफसेट के साथ ner टैग्स का पुनरावर्तनीय होना चाहिए- आपका
data_biluo
वाक्यों के बीच में,
के लिए जिम्मेदार नहीं है।
जैसे ही आप उनके लिए सही करते हैं, आप जाने के लिए ठीक हैं:
import spacy
from spacy.gold import offsets_from_biluo_tags, GoldParse
from spacy.util import minibatch, compounding
import random
from tqdm import tqdm
def train_spacy(data, iterations, model=None):
TRAIN_DATA = data
print(f"downloads = {model}")
if model is not None and path.exists(model):
print(f"training existing model")
nlp = spacy.load(model)
print("Model is Loaded '%s'" % model)
else:
print(f"Creating new model")
nlp = spacy.blank('en') # create blank Language class
if 'ner' not in nlp.pipe_names:
ner = nlp.create_pipe('ner')
nlp.add_pipe(ner, last=True)
else:
ner = nlp.get_pipe('ner')
# Based on template, get labels and save those for further training
LABEL = ["Name", "ORG"]
for i in LABEL:
# print(i)
ner.add_label(i)
# get names of other pipes to disable them during training
other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'ner']
with nlp.disable_pipes(*other_pipes): # only train NER
if model is None:
optimizer = nlp.begin_training()
else:
optimizer = nlp.entity.create_optimizer()
tags = dict()
for itn in range(iterations):
print("Starting iteration " + str(itn))
random.shuffle(TRAIN_DATA)
losses = {}
batches = minibatch(TRAIN_DATA, size=compounding(4.0, 16.0, 1.001))
# type 2 with mini batch
for batch in batches:
texts, _ = zip(*batch)
golds = [GoldParse(nlp.make_doc(t),entities = a) for t,a in batch]
nlp.update(
texts, # batch of texts
golds, # batch of annotations
drop=0.4, # dropout - make it harder to memorise data
losses=losses,
sgd=optimizer
)
print(losses)
return nlp
data_biluo = [
('I am Shah Khan, I work in MS Co', ['O', 'O', 'B-Name', 'L-Name', 'O', 'O', 'O', 'O', 'B-ORG', 'L-ORG']),
('I am Tom Tomb, I work in Telecom Networks', ['O', 'O', 'B-Name', 'L-Name', 'O', 'O', 'O', 'O', 'B-ORG', 'L-ORG'])
]
model = train_spacy(data_biluo, 10)
Starting iteration 0
{'ner': 17.999998331069946}
Starting iteration 1
{'ner': 16.6766300201416}
Starting iteration 2
{'ner': 16.997647166252136}
Starting iteration 3
{'ner': 16.486496448516846}
Starting iteration 4
{'ner': 15.695325374603271}
Starting iteration 5
{'ner': 14.312554001808167}
Starting iteration 6
{'ner': 12.099276185035706}
Starting iteration 7
{'ner': 11.473928153514862}
Starting iteration 8
{'ner': 8.814643770456314}
Starting iteration 9
{'ner': 7.233813941478729}
संबंधित सवाल
नए सवाल
python
पायथन एक बहु-प्रतिमान है, गतिशील रूप से टाइप किया हुआ, बहुउद्देशीय प्रोग्रामिंग भाषा है। यह एक साफ और एक समान वाक्यविन्यास सीखने, समझने और उपयोग करने के लिए त्वरित होने के लिए डिज़ाइन किया गया है। कृपया ध्यान दें कि अजगर 2 आधिकारिक तौर पर 01-01-2020 के समर्थन से बाहर है। फिर भी, संस्करण-विशिष्ट पायथन सवालों के लिए, [अजगर -२.०] या [अजगर -३.x] टैग जोड़ें। पायथन वेरिएंट (जैसे, ज्योथन, PyPy) या लाइब्रेरी (उदा।, पांडस और न्यूमपी) का उपयोग करते समय, कृपया इसे टैग में शामिल करें।