spaCy icon indicating copy to clipboard operation
spaCy copied to clipboard

BILOU tag issue for training in spacy 2.x

Open irfan-zoefit opened this issue 9 years ago • 3 comments

Data is in BILOU format

('Thousands of demonstrators have marched through London to protest the war in Iraq and demand the withdrawal of British troops from that country .', {'entities': [(0, 9, 'O'), (10, 12, 'O'), (13, 26, 'O'), (27, 31, 'O'), (32, 39, 'O'), (40, 47, 'O'), (48, 54, 'B-geo'), (22, 24, 'O'), (58, 65, 'O'), (66, 69, 'O'), (70, 73, 'O'), (74, 76, 'O'), (77, 81, 'B-geo'), (5, 8, 'O'), (86, 92, 'O'), (66, 69, 'O'), (97, 107, 'O'), (10, 12, 'O'), (111, 118, 'B-org'), (119, 125, 'O'), (126, 130, 'O'), (131, 135, 'O'), (136, 143, 'O')]})

Using train_new_entity_type.py for training. During training getting following error

Created blank 'en' model
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1599, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1026, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Users/anjums/PycharmProjects/untitled1/trainer.py", line 148, in <module>
    plac.call(main)
  File "/Library/Python/2.7/site-packages/plac_core.py", line 328, in call
    cmd, result = parser.consume(arglist)
  File "/Library/Python/2.7/site-packages/plac_core.py", line 207, in consume
    return cmd, self.func(*(args + varargs + extraopts), **kwargs)
  File "/Users/anjums/PycharmProjects/untitled1/trainer.py", line 120, in main
    losses=losses)
  File "/Library/Python/2.7/site-packages/spacy/language.py", line 407, in update
    proc.update(docs, golds, drop=drop, sgd=get_grads, losses=losses)
  File "nn_parser.pyx", line 538, in spacy.syntax.nn_parser.Parser.update
  File "nn_parser.pyx", line 652, in spacy.syntax.nn_parser.Parser._init_gold_batch
  File "ner.pyx", line 119, in spacy.syntax.ner.BiluoPushDown.preprocess_gold
  File "ner.pyx", line 178, in spacy.syntax.ner.BiluoPushDown.lookup_transition
KeyError: u'U-O'

Your Environment

  • Operating System: macOS X
  • Python Version Used: 2.7.x
  • spaCy Version Used: 2.x
  • Environment Information:

irfan-zoefit avatar Nov 22 '17 11:11 irfan-zoefit

First, you can simply describe only entities with their spans, there is no need to describe all tokens if they are no matter: [(48, 54, 'GEO'), (77, 81, 'GEO'), (111, 118, 'ORG')]. Then, when you are changing TRAIN_DATA in train_new_entity_type.py, you should make sure you've specify new label here (if you adding more than one new label you should fix script here to add both of them). Hopes, this will help.

GreenRiverRUS avatar Nov 25 '17 21:11 GreenRiverRUS

This is interesting – you should be able to also provide BILUO tags as entities instead of the offsets. There are two possible explanations here: 1) There's a bug somewhere in spaCy. 2) There's some inconsistency in your tags, i.e. a mismatched tag.

The easiest way to find out is to use the gold.tags_to_entities function to convert your BILUO tags to offsets, and see if this works and matches the entities you want to label. Keep in mind that this function will return the token offsets, not the character offsets:

from spacy.gold import tags_to_entities
offsets = tags_to_entities(entities)

Btw, also just noticed that this helper function currently isn't documented (we currently only include the function that converts offsets to BILUO tags). There should probably also be a twin to the biluo_tags_from_offsets function that takes the Doc as well and gives you character offsets.

ines avatar Nov 26 '17 15:11 ines

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar May 08 '18 05:05 lock[bot]