2017-12-01 5 views
0

내가 예제 코드를 실행하려고 '빈'에는 속성이 없습니다 : 웹 사이트 https://spacy.io/usage/training#textcatAttributeError : 모듈 '적응'는

#!/usr/bin/env python 
# coding: utf8 
"""Example of training spaCy's named entity recognizer, starting off with an 
existing model or a blank model. 

For more details, see the documentation: 
* Training: https://spacy.io/usage/training 
* NER: https://spacy.io/usage/linguistic-features#named-entities 

Compatible with: spaCy v2.0.0+ 
""" 
from __future__ import unicode_literals, print_function 

import plac 
import random 
from pathlib import Path 
import spacy 


# training data 
TRAIN_DATA = [ 
    ('Who is Shaka Khan?', { 
     'entities': [(7, 17, 'PERSON')] 
    }), 
    ('I like London and Berlin.', { 
     'entities': [(7, 13, 'LOC'), (18, 24, 'LOC')] 
    }) 
] 


@plac.annotations(
    model=("Model name. Defaults to blank 'en' model.", "option", "m", str), 
    output_dir=("Optional output directory", "option", "o", Path), 
    n_iter=("Number of training iterations", "option", "n", int)) 
def main(model=None, output_dir=None, n_iter=100): 
    """Load the model, set up the pipeline and train the entity recognizer.""" 
    if model is not None: 
     nlp = spacy.load(model) # load existing spaCy model 
     print("Loaded model '%s'" % model) 
    else: 
     nlp = spacy.blank('en') # create blank Language class 
     print("Created blank 'en' model") 

    # 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) 
    # otherwise, get it so we can add labels 
    else: 
     ner = nlp.get_pipe('ner') 

    # add labels 
    for _, annotations in TRAIN_DATA: 
     for ent in annotations.get('entities'): 
      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(n_iter): 
      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) 

    # test the trained model 
    for text, _ in TRAIN_DATA: 
     doc = nlp(text) 
     print('Entities', [(ent.text, ent.label_) for ent in doc.ents]) 
     print('Tokens', [(t.text, t.ent_type_, t.ent_iob) for t in doc]) 

    # save model to output directory 
    if output_dir is not None: 
     output_dir = Path(output_dir) 
     if not output_dir.exists(): 
      output_dir.mkdir() 
     nlp.to_disk(output_dir) 
     print("Saved model to", output_dir) 

     # test the saved model 
     print("Loading from", output_dir) 
     nlp2 = spacy.load(output_dir) 
     for text, _ in TRAIN_DATA: 
      doc = nlp2(text) 
      print('Entities', [(ent.text, ent.label_) for ent in doc.ents]) 
      print('Tokens', [(t.text, t.ent_type_, t.ent_iob) for t in doc]) 


if __name__ == '__main__': 
    plac.call(main) 

    # Expected output: 
    # Entities [('Shaka Khan', 'PERSON')] 
    # Tokens [('Who', '', 2), ('is', '', 2), ('Shaka', 'PERSON', 3), 
    # ('Khan', 'PERSON', 1), ('?', '', 2)] 
    # Entities [('London', 'LOC'), ('Berlin', 'LOC')] 
    # Tokens [('I', '', 2), ('like', '', 2), ('London', 'LOC', 3), 
    # ('and', '', 2), ('Berlin', 'LOC', 3), ('.', '', 2)] 

에서 을하지만 다음과 같은 오류를 얻을 : AttributeError를 속성 'blank'

Traceback (most recent call last): 
    File "trainNer.py", line 98, in <module> 
    plac.call(main) 
    File "C:\Users\M63C755\AppData\Local\Continuum\anaconda3\lib\site-packages\plac_core.py", line 328, in call 
    cmd, result = parser.consume(arglist) 
    File "C:\Users\M63C755\AppData\Local\Continuum\anaconda3\lib\site-packages\plac_core.py", line 207, in consume 
    return cmd, self.func(*(args + varargs + extraopts), **kwargs) 
    File "trainNer.py", line 41, in main 
    nlp = spacy.blank('en') # create blank Language class 
AttributeError: module 'spacy' has no attribute 'blank' 

spacy 웹 사이트는별로 알려주지 않습니다. blank는 예제에만있는 파일 중 하나에 나타나지 않습니다. 방금 spacy 버전 1.9.0을 다운로드했습니다. 그래서, 하나는 비어 있다고 가정합니다.

또한 코드의 내용을 변경하지 않았으며 처음 시도했을뿐입니다. 나는 무엇이 비어 있다고 생각하는지 완전히 확신하지 못한다. 마치 대안 모델을 시작해야하는 것 같습니다. 그러나, 나는이 대체 모델의 형식을 모른다. 나는 빈 속성을 직접 추가 할 생각 이었지만, 공백 속성 리턴 타입을 모른다.

누구든지이 문제를 해결하는 방법을 알고 있습니까?

답변

1

버전 v2 이상으로 업데이트하면이 문제가 해결됩니다.

관련 문제