Ядро Jupyter умирает / консоль Spyder останавливается во время обучения пользовательской модели NER в Spacy 2.0.11 - PullRequest
1 голос
/ 12 апреля 2020

Я пытался обучить пользовательскую модель NER на просторах. Первоначально я установил последнюю простую версию, но во время обучения получала следующую ошибку

ValueError: [E103] Попытка установить конфликтующие do c .ents: токен может быть только частью одного сущность, поэтому убедитесь, что настраиваемые сущности не перекрываются.

После этого я установил простую версию spacy==2.0.11 и попытался запустить свой код. Когда у меня около 10 строк данных для обучения, модель работает нормально, и она сохраняется в моей выходной директории. Но когда появляется больше данных (5K строк), которые являются исходными данными обучения, мое ядро ​​jupyter умирает или когда я запускаю в spyder, консоль просто существует !!

Я понимаю, что устаревшая версия spacy не выдает ошибку значения, но все равно бесполезно, поскольку я не могу обучить свою модель.

Пример данных:

CarryBag    09038820815c.txt
Stopperneedle   0903882080f4.txt
Foilbags    09038820819.txt

У меня около 700 таких файлов с данными, которые будут помечены и в каждом файле несколько сущностей нуждаются в маркировке. Код для справки:

import spacy
# import en_core_web_sm
import re
import csv
from spacy.matcher import PhraseMatcher
import plac
from pathlib import Path
import random

#Function to convert PhraseMatcher return value to string indexes 
def str_index_conversion(lbl, doc, matchitem):
    o_one = len(str(doc[0:matchitem[1]]))
    subdoc = doc[matchitem[1]:matchitem[2]]
    o_two = o_one + len(str(subdoc))
    return (o_one, o_two, lbl)

# nlp = spacy.load('en')
nlp = spacy.load('en_core_web_sm')

if 'ner' not in nlp.pipe_names:
    ner = nlp.create_pipe('ner')
    nlp.add_pipe(ner)
else:
    ner = nlp.get_pipe('ner')

ner.add_label('PRODUCT')     

DIR = 'D:/Docs/'
matcher = PhraseMatcher(nlp.vocab)


list_str_index = []
to_train_ents = []
with open(r'D:\ner_dummy_pack.csv', newline='', encoding ='utf-8') as myFile:

    reader = csv.reader(myFile)
    for row in reader:
        try:
            product = row[0].lower()
            #print('K---'+ product)
            filename = row[1]
            file = open(DIR+filename, "r", encoding ='utf-8')
            print(file)
            filecontents = file.read()
            for s in filecontents:
                filecontents = re.sub(r'\s+', ' ', filecontents)
                filecontents = re.sub(r'^https?:\/\/.*[\r\n]*', '', filecontents, flags=re.MULTILINE)
                filecontents = re.sub(r"http\S+", "", filecontents)
                filecontents = re.sub(r"[-\"#/@;:<>?{}*`• ?+=~|$.!‘?“”?,_]", " ", filecontents)
                filecontents = re.sub(r'\d+', '', filecontents)#removing all numbers
                filecontents = re.sub(' +', ' ',filecontents)
                #filecontents = filecontents.encode().decode('unicode-escape')
                filecontents = ''.join([line.lower() for line in filecontents])
                if "," in product:
                    product_patterns = product.split(',')
                    product_patterns = [i.strip() for i in product_patterns]

                    for elem in product_patterns:
                        matcher.add('PRODUCT', None, nlp(elem)) 

                else:
                    matcher.add('PRODUCT', None, nlp(product))                
                print(filecontents)
                doc = nlp(filecontents)
                matches = matcher(doc)
                        #print(matches)
                list_str_index = [str_index_conversion('PRODUCT', doc, x) for x in matches]
                to_train_ents.append((filecontents, dict(entities=list_str_index)))
                break


        except Exception as e:
            print(e)
            pass

to_train_entsfinal=to_train_ents      




def main(model=None, output_dir=None, n_iter=100):
    # nlp.vocab.vectors.name = 'spacy_pretrained_vectors'
    optimizer = nlp.begin_training()
    other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'ner']

    with nlp.disable_pipes(*other_pipes):  # only train NER
        for itn in range(10):
            losses = {}
            random.shuffle(to_train_entsfinal)
            for item in to_train_entsfinal:
                nlp.update([item[0]],
                           [item[1]],
                           sgd=optimizer,
                           drop=0.50,
                           losses=losses)
            print(losses)
            print("OUTTTTT")


    if output_dir is None:
        output_dir = "C:\\Users\\APRIL"


    noutput_dir = Path(output_dir)
    if not noutput_dir.exists():
        noutput_dir.mkdir()

    #nlp.meta['name'] = new_model_name
    nlp.to_disk(output_dir)


    random.shuffle(to_train_entsfinal)

if __name__ == '__main__':
    main()   

Может кто-нибудь помочь мне решить эту проблему. Даже когда я удалил конфликтующие объекты в выборке из 10+ строк, например:

Blister       abc.txt
Blisterpack   abc.txt
Blisters      abc.txt   

, возникает та же проблема и модель не обучается

Предлагаемые изменения:

def main(model=None, output_dir=None, n_iter=100):
    top_memory_precentage_use = 75 # or what ever number you choose

    def handle_memory(ruler):
        if psutil.virtual_memory().percent < top_memory_precentage_use:
            dump_ruler_nonascii(ruler)
            ruler = nlp.begin_training() #or just init the nlp object again
        return ruler

    # This fitted for my use case
    def dump_ruler_nonascii(ruler):
        path = Path(os.path.join(self.data_path, 'config.jsonl'))
        pattern = ruler.patterns
        with open(path, "a", encoding="utf-8") as f:
            for line in pattern:
                f.write(json.dumps(line, ensure_ascii=False) + "\n")
        return ruler
    # nlp.vocab.vectors.name = 'spacy_pretrained_vectors'
    optimizer = nlp.begin_training()
    other_pipes = [pipe for pipe in nlp.pipe_names if pipe != 'ner']

    with nlp.disable_pipes(*other_pipes):  # only train NER
        for itn in range(10):
            losses = {}
            random.shuffle(to_train_entsfinal)
            for item in to_train_entsfinal:
                nlp.update([item[0]],
                           [item[1]],
                           sgd=optimizer,
                           drop=0.50,
                           losses=losses)
            print(losses)
            print("OUTTTTT")


    if output_dir is None:
        output_dir = "C:\\Users\\APRIL"


    noutput_dir = Path(output_dir)
    if not noutput_dir.exists():
        noutput_dir.mkdir()

    #nlp.meta['name'] = new_model_name
    nlp.to_disk(output_dir)


    random.shuffle(to_train_entsfinal)

if __name__ == '__main__':
    main()   

1 Ответ

0 голосов
/ 12 апреля 2020

Трудно сказать вам , почему это происходит, но я могу предоставить вам 2 вспомогательные функции для вашего обучения l oop. что вы можете настроить для вашего использования. В моем случае это были записи шаблонов, и я проверял использование памяти каждую итерацию.

#add the following imports
import psutil
import os


top_memory_precentage_use = 75 # or what ever number you choose

def handle_memory(ruler):
    if psutil.virtual_memory().percent < top_memory_precentage_use:
        dump_ruler_nonascii(ruler)
        ruler = nlp.begin_training() #or just init the nlp object again
    return ruler

# This fitted for my use case
def dump_ruler_nonascii(ruler):
    path = Path(os.path.join(self.data_path, 'config.jsonl'))
    pattern = ruler.patterns
    with open(path, "a", encoding="utf-8") as f:
        for line in pattern:
            f.write(json.dumps(line, ensure_ascii=False) + "\n")
...