spaCy/spacy/syntax
Matthew Honnibal 827b5af697 Update draft of parser neural network model
Model is good, but code is messy. Currently requires Chainer, which may cause the build to fail on machines without a GPU.

Outline of the model:

We first predict context-sensitive vectors for each word in the input:

(embed_lower | embed_prefix | embed_suffix | embed_shape)
>> Maxout(token_width)
>> convolution ** 4

This convolutional layer is shared between the tagger and the parser. This prevents the parser from needing tag features.
To boost the representation, we make a "super tag" with POS, morphology and dependency label. The tagger predicts this
by adding a softmax layer onto the convolutional layer --- so, we're teaching the convolutional layer to give us a
representation that's one affine transform from this informative lexical information. This is obviously good for the
parser (which backprops to the convolutions too).

The parser model makes a state vector by concatenating the vector representations for its context tokens. Current
results suggest few context tokens works well. Maybe this is a bug.

The current context tokens:

* S0, S1, S2: Top three words on the stack
* B0, B1: First two words of the buffer
* S0L1, S0L2: Leftmost and second leftmost children of S0
* S0R1, S0R2: Rightmost and second rightmost children of S0
* S1L1, S1L2, S1R2, S1R, B0L1, B0L2: Likewise for S1 and B0

This makes the state vector quite long: 13*T, where T is the token vector width (128 is working well). Fortunately,
there's a way to structure the computation to save some expense (and make it more GPU friendly).

The parser typically visits 2*N states for a sentence of length N (although it may visit more, if it back-tracks
with a non-monotonic transition). A naive implementation would require 2*N (B, 13*T) @ (13*T, H) matrix multiplications
for a batch of size B. We can instead perform one (B*N, T) @ (T, 13*H) multiplication, to pre-compute the hidden
weights for each positional feature wrt the words in the batch. (Note that our token vectors come from the CNN
-- so we can't play this trick over the vocabulary. That's how Stanford's NN parser works --- and why its model
is so big.)

This pre-computation strategy allows a nice compromise between GPU-friendliness and implementation simplicity.
The CNN and the wide lower layer are computed on the GPU, and then the precomputed hidden weights are moved
to the CPU, before we start the transition-based parsing process. This makes a lot of things much easier.
We don't have to worry about variable-length batch sizes, and we don't have to implement the dynamic oracle
in CUDA to train.

Currently the parser's loss function is multilabel log loss, as the dynamic oracle allows multiple states to
be 0 cost. This is defined as:

(exp(score) / Z) - (exp(score) / gZ)

Where gZ is the sum of the scores assigned to gold classes. I'm very interested in regressing on the cost directly,
but so far this isn't working well.

Machinery is in place for beam-search, which has been working well for the linear model. Beam search should benefit
greatly from the pre-computation trick.
2017-05-12 16:09:15 -05:00
..
__init__.pxd * Work on greedy parser 2014-12-16 22:46:55 +11:00
__init__.py * Work on greedy parser 2014-12-16 22:46:55 +11:00
_parse_features.pxd * Add a parse_batch method to Parser, that releases the GIL around a batch of documents. 2016-02-01 08:34:55 +01:00
_parse_features.pyx Tidy up and fix formatting and imports 2017-04-15 13:05:15 +02:00
_state.pxd Fix setting of empty lexeme in initial parse state 2017-03-15 09:26:53 -05:00
_state.pyx * Continue proxying. Some problem currently 2016-02-01 02:22:21 +01:00
arc_eager.pxd * Update for thinc 5.0, including changing cost from int to weight_t, and updating the tagger and parser 2016-01-30 14:31:36 +01:00
arc_eager.pyx Tidy up and fix formatting and imports 2017-04-15 13:05:15 +02:00
beam_parser.pxd Data running through, likely errors in model 2017-05-06 14:22:20 +02:00
beam_parser.pyx Data running through, likely errors in model 2017-05-06 14:22:20 +02:00
iterators.pxd * Refactor noun chunk iterators, so that they're simple functions. Install the iterator when the Doc is created, but allow users to write to the noun_chunk_iterator attribute. The iterator functions accept an object and yield (int start, int end, int label) triples. 2016-05-02 14:25:10 +02:00
iterators.pyx Ensure noun chunks can't be nested. Closes #955 2017-04-23 17:56:39 +02:00
ner.pxd * Remove import of _state module 2015-06-23 17:25:08 +02:00
ner.pyx Tidy up and fix formatting and imports 2017-04-15 13:05:15 +02:00
nonproj.pxd integrated pseudo-projective parsing into parser 2016-03-01 10:09:08 +01:00
nonproj.pyx Tidy up and fix formatting and imports 2017-04-15 13:05:15 +02:00
parser.pxd Get pre-computed version working 2017-05-08 00:38:35 +02:00
parser.pyx Update draft of parser neural network model 2017-05-12 16:09:15 -05:00
stateclass.pxd Data running through, likely errors in model 2017-05-06 14:22:20 +02:00
stateclass.pyx Update draft of parser neural network model 2017-05-12 16:09:15 -05:00
transition_system.pxd Allow beam parsing for NER 2017-03-11 11:12:01 -06:00
transition_system.pyx Remove print statement 2017-04-15 16:11:43 +02:00