spaCy icon indicating copy to clipboard operation
spaCy copied to clipboard

Cycle found when reading CoNLL file

Open petterhh opened this issue 10 years ago • 14 comments

I am trying to train the dependency parser in spaCy for Norwegian using the Norwegian Dependency Treebank (NDT - http://www.lrec-conf.org/proceedings/lrec2014/pdf/303_Paper.pdf). However, when trying to train the model, I'm getting a "Cycle found" error, e.g. (printing the words, heads and deps for a sentence where the error occurs):

['Prinsesse', 'Märtha', 'Louise', 'og', 'Ari', 'Behn', 'har', 'igjen', 'valgt', 'et', 'trendy', 'navn', ',', 'fastslår', 'navneforsker', 'Ivar', 'Utne', 'ved', 'Universitetet', 'i', 'Bergen', '.'] [2, 7, 2, 5, 2, 5, 0, 9, 7, 12, 12, 9, 7, 7, 16, 14, 16, 16, 18, 19, 19, 7] ['APP', 'SUBJ', 'FLAT', 'KONJ', 'KOORD', 'FLAT', 'FINV', 'ADV', 'INFV', 'DET', 'ATR', 'DOBJ', 'IK', 'PAR', 'APP', 'SUBJ', 'FLAT', 'ATR', 'PUTFYLL', 'FLAT', 'FLAT', 'IP'] Traceback (most recent call last): File "spacy_nor.py", line 98, in main() File "spacy_nor.py", line 88, in main parser = train_parser(nlp, train_data, left_labels, right_labels) File "spacy_nor.py", line 25, in train_parser gold = GoldParse(doc, heads=heads, deps=deps) File "spacy/gold.pyx", line 276, in spacy.gold.GoldParse.init (spacy/gold.cpp:10564) Exception: Cycle found: {1, 12, 9, 7}

Note that ROOT is not a dependency relation in NDT - instead, they use FINV (finite verb) and FRAG (fragment). I am following the tutorial at https://github.com/explosion/spaCy/blob/master/examples/training/train_parser.py with a separate function for translating the CoNLL file to the format in spaCy. Is there something particular I have to change about the indices? It seems that spaCy sentences (Docs) start at index 0, but subtracting 1 from all CoNLL indices, or replacing the root index (0) with the index of the root word itself (or -1), does not resolve this issue.

Does spaCy require me to use the ROOT relation? It would make much more sense if it instead simply uses the labels in the training data, which is the case for all other parsers I have employed on NDT (including the Malt Parser, Mate, Turbo and RBG).

petterhh avatar Oct 24 '16 16:10 petterhh

You should use the ROOT relation, yes, because there are some places where that's hard coded. I'm sorry that this is a bit inconvenient.

That's not the problem here though. The spacy.syntax.nonproj.contains_cycle function just takes a list of head indices. I'm getting a cycle when I give it your indices. In your sentence, it seems like 7 depends on 9, which depends on 12, which depends on 7 --- a cycle.

honnibal avatar Oct 24 '16 16:10 honnibal

Sentences in CoNLL format start at index 1, not index 0 (See http://universaldependencies.org/format.html) - 0 is simply an "abstract" root node. These differing indexing approaches is what I'm thinking might be the issue. It is not 7 that depends on 9, but rather 8 that depends on 9. How should I change the indexing to go about this issue?

Note: as I said in the first post, subtracting 1 from all CoNLL indices, or replacing the root index (0) with the index of the root word itself (or -1) does not resolve the issue.

Thanks for the quick reponse!

petterhh avatar Oct 24 '16 17:10 petterhh

The head index should point to the head word in a zero-indexed array. The root word is interpreted as being its own head (so, having a self-cycle).

Is ['Prinsesse', 'Märtha', 'Louise'] a named entity? I would expect this to have the structure [2, 2, x], where x>2. Something looks really wrong with your dependencies.

honnibal avatar Oct 24 '16 17:10 honnibal

Haha, that's correct, kinda - it's Norwegian for "Princess Märtha Louise" (cf. "President Barack Obama") - "Märtha" being her first name, with "Princess" and "Louise" as dependencies from that. "Louise" has "Märtha" as its head with a so-called FLAT dependency relation, used for multi-word names.

petterhh avatar Oct 24 '16 17:10 petterhh

Right, so there's your problem — your heads are 1-indexed. If "Märtha" is the head of that constituent, then you want the head of Princess to be 1.

honnibal avatar Oct 24 '16 17:10 honnibal

Exactly - and as I said, I tried to fix that by subtracting 1 from all head indices, to no avail. What is strange is that it works for some sentences, but not for others - surely, it should work for all sentences if it works for some? They are all annotated with the same annotation guidelines.

petterhh avatar Oct 24 '16 17:10 petterhh

Are you doing the head translation before you fix the off-by-one? If so, the head will be off-by-one...Creating potentially (but not necessarily) a cycle

honnibal avatar Oct 24 '16 17:10 honnibal

Wow, of course, how did I miss that... That's embarassing.

Unfortunaltely, I am now met by a new error:

['Hvis', 'han', 'ikke', 'hadde', 'tenkt', 'slik', ',', 'ville', 'familien', 'forblitt', 'salmakere', ',', 'og', 'forsvunnet', '.'] [3, 3, 4, 9, 3, 4, 3, 7, 7, 7, 9, 9, 13, 9, 7] ['SBU', 'SUBJ', 'ADV', 'ADV', 'INFV', 'ADV', 'IK', 'ROOT', 'SUBJ', 'INFV', 'SPRED', 'IK', 'KONJ', 'KOORD', 'IP'] Traceback (most recent call last): File "spacy_nor.py", line 98, in main() File "spacy_nor.py", line 88, in main parser = train_parser(nlp, train_data, left_labels, right_labels) File "spacy_nor.py", line 26, in train_parser loss += parser.update(doc, gold) File "spacy/syntax/parser.pyx", line 208, in spacy.syntax.parser.Parser.update (spacy/syntax/parser.cpp:7907) File "spacy/syntax/arc_eager.pyx", line 445, in spacy.syntax.arc_eager.ArcEager.set_costs (spacy/syntax/arc_eager.cpp:8436) AssertionError

I see that the assertion is for n_gold >= 1, but that doesn't tell me much.

petterhh avatar Oct 24 '16 18:10 petterhh

Pretty sure I've made exactly that error more than once. Probably something like 4 times.

The transition system only supports projective trees. Non-projective parses are supported by using Nivre's pseudo-projective transformation. Pass make_projective=True to GoldParse. You'll have to deprojectivize the tree after parsing. You could add a step to the pipeline — the required function is at spacy.syntax.nonproj.PseudoProjectivity.deprojectivize

I'll fix the error message — the training API still has these rough edges.

honnibal avatar Oct 24 '16 18:10 honnibal

Hm. You probably want to transform the whole training corpus — otherwise you won't have the correct set of labels to give to the parser before training. See the bin/parser/train_ud.py script — but I think some of its details are currently out of date for 1.0 =/

honnibal avatar Oct 24 '16 18:10 honnibal

Oh, I see, that's too bad. At least I got it up and running now, and I'll continue working on it tomorrow. Thanks for all the great help and for developing spaCy, which continues to impress me!

petterhh avatar Oct 24 '16 18:10 petterhh

No problem!

honnibal avatar Oct 25 '16 00:10 honnibal

And I'm back for more questions!

I didn't quite get what I needed to do before training - are you talking about the step where I collect left_labels andright_labels? I can't see much about transforming in the bin/parser/train_ud.pyscript either.

I now have

for words, heads, deps in train_data:
    doc = Doc(vocab, words=words)
    gold = GoldParse(doc, heads=heads, deps=deps, make_projective=True)
    loss += parser.update(doc, gold)

and to evaluate after training:

for tokens, heads, deps in test_data:
    doc = Doc(vocab, words=tokens)
    parser(doc)
    nonproj.PseudoProjectivity.deprojectivize(doc)
    for token in doc:
        print(token.text, token.dep_, token.head)

The results I'm getting are completely off, so I'm guessing I'm missing a point about the deprojectivization?

petterhh avatar Oct 25 '16 09:10 petterhh

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 09 '18 07:05 lock[bot]