Я тренировал свою модель NER на каком-то тексте и пытался найти в нем города с настраиваемыми объектами.
Пример:-
('paragraph Designated Offices Party A New York Party B Delaware paragraph pricing source calculation Market Value shall generally accepted pricing source reasonably agreed parties paragraph Spot rate Spot Rate specified paragraph reasonably agreed parties',
{'entities': [(37, 41, 'DesignatedBankLoc'),(54, 62, 'CounterpartyBankLoc')]})
Я ищу здесь 2 сущности DesignatedBankLoc
и CounterpartyBankLoc
. Также для отдельного текста может быть несколько сущностей.
в настоящее время я тренируюсь на 60 строках данных следующим образом:
import spacy
import random
def train_spacy(data,iterations):
TRAIN_DATA = data
nlp = spacy.blank('en') # create blank Language class
# create the built-in pipeline components and add them to the pipeline
# nlp.create_pipe works for built-ins that are registered with spaCy
if 'ner' not in nlp.pipe_names:
ner = nlp.create_pipe('ner')
nlp.add_pipe(ner, last=True)
# add labels
for _, annotations in TRAIN_DATA:
for ent in annotations.get('entities'):
# print (ent[2])
ner.add_label(ent[2])
# 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
optimizer = nlp.begin_training()
for itn in range(iterations):
print("Statring iteration " + str(itn))
random.shuffle(TRAIN_DATA)
losses = {}
for text, annotations in TRAIN_DATA:
nlp.update(
[text], # batch of texts
[annotations], # batch of annotations
drop=0.5, # dropout - make it harder to memorise data
sgd=optimizer, # callable to update weights
losses=losses)
print(losses)
return nlp
prdnlp = train_spacy(TRAIN_DATA, 100)
Моя проблема: -
Модель предсказывает правильно, если ввод отличается / тот же образец текста содержит обученные города. Модель не прогнозирует ни одну из сущностей, даже если одинаковый / другой шаблон текста, но разные города, что никогда не встречается в наборе обучающих данных.
Пожалуйста, подскажите, почему это происходит, пожалуйста, дайте мне понять концепцию, как это происходит?