2017-05-13 22:55:01 +00:00
|
|
|
# cython: infer_types=True
|
2017-05-15 19:46:08 +00:00
|
|
|
# cython: cdivision=True
|
|
|
|
# cython: boundscheck=False
|
2017-05-13 22:55:01 +00:00
|
|
|
# coding: utf-8
|
|
|
|
from __future__ import unicode_literals, print_function
|
|
|
|
|
2017-10-27 17:45:57 +00:00
|
|
|
from collections import OrderedDict
|
2017-05-13 22:55:01 +00:00
|
|
|
import ujson
|
2017-09-26 11:44:56 +00:00
|
|
|
import json
|
2017-10-18 19:45:01 +00:00
|
|
|
import numpy
|
2017-05-13 22:55:01 +00:00
|
|
|
cimport cython.parallel
|
|
|
|
import cytoolz
|
|
|
|
import numpy.random
|
|
|
|
cimport numpy as np
|
2017-10-27 17:45:57 +00:00
|
|
|
from cpython.ref cimport PyObject, Py_XDECREF
|
2017-10-24 10:40:47 +00:00
|
|
|
from cpython.exc cimport PyErr_CheckSignals, PyErr_SetFromErrno
|
2017-10-27 17:45:57 +00:00
|
|
|
from libc.math cimport exp
|
|
|
|
from libcpp.vector cimport vector
|
2017-11-14 23:51:42 +00:00
|
|
|
from libc.string cimport memset, memcpy
|
2017-10-27 17:45:57 +00:00
|
|
|
from libc.stdlib cimport calloc, free
|
|
|
|
from cymem.cymem cimport Pool
|
|
|
|
from thinc.typedefs cimport weight_t, class_t, hash_t
|
2017-07-20 13:02:55 +00:00
|
|
|
from thinc.extra.search cimport Beam
|
2017-10-27 17:45:57 +00:00
|
|
|
from thinc.api import chain, clone
|
|
|
|
from thinc.v2v import Model, Maxout, Affine
|
2017-10-03 18:07:17 +00:00
|
|
|
from thinc.misc import LayerNorm
|
2017-10-27 17:45:57 +00:00
|
|
|
from thinc.neural.ops import CupyOps
|
2017-05-23 09:23:29 +00:00
|
|
|
from thinc.neural.util import get_array_module
|
2017-10-28 11:16:06 +00:00
|
|
|
from thinc.linalg cimport Vec, VecVec
|
2018-03-27 17:23:02 +00:00
|
|
|
from thinc cimport openblas
|
|
|
|
|
2018-05-15 20:17:29 +00:00
|
|
|
from ._parser_model cimport resize_activations, predict_states, arg_max_if_valid
|
|
|
|
from ._parser_model cimport WeightsC, ActivationsC, SizesC, cpu_log_loss
|
|
|
|
from ._parser_model cimport get_c_weights, get_c_sizes
|
|
|
|
from ._parser_model import ParserModel
|
2017-10-28 11:16:06 +00:00
|
|
|
from .._ml import zero_init, PrecomputableAffine, Tok2Vec, flatten
|
2017-11-06 13:26:26 +00:00
|
|
|
from .._ml import link_vectors_to_models, create_default_optimizer
|
2017-10-09 01:35:40 +00:00
|
|
|
from ..compat import json_dumps, copy_array
|
2017-05-13 22:55:01 +00:00
|
|
|
from ..tokens.doc cimport Doc
|
|
|
|
from ..gold cimport GoldParse
|
2018-04-03 13:50:31 +00:00
|
|
|
from ..errors import Errors, TempErrors
|
2017-10-27 17:45:57 +00:00
|
|
|
from .. import util
|
|
|
|
from .stateclass cimport StateClass
|
|
|
|
from ._state cimport StateC
|
|
|
|
from .transition_system cimport Transition
|
2018-05-15 20:17:29 +00:00
|
|
|
from . cimport _beam_utils
|
|
|
|
from . import _beam_utils
|
|
|
|
from . import nonproj
|
2017-11-14 01:11:40 +00:00
|
|
|
|
|
|
|
|
2017-05-13 22:55:01 +00:00
|
|
|
cdef class Parser:
|
|
|
|
"""
|
|
|
|
Base class of the DependencyParser and EntityRecognizer.
|
|
|
|
"""
|
|
|
|
@classmethod
|
2017-10-06 18:17:31 +00:00
|
|
|
def Model(cls, nr_class, **cfg):
|
2017-10-11 07:43:48 +00:00
|
|
|
depth = util.env_opt('parser_hidden_depth', cfg.get('hidden_depth', 1))
|
2017-10-18 22:42:34 +00:00
|
|
|
if depth != 1:
|
2018-04-03 13:50:31 +00:00
|
|
|
raise ValueError(TempErrors.T004.format(value=depth))
|
2017-10-27 17:45:57 +00:00
|
|
|
parser_maxout_pieces = util.env_opt('parser_maxout_pieces',
|
|
|
|
cfg.get('maxout_pieces', 2))
|
|
|
|
token_vector_width = util.env_opt('token_vector_width',
|
2017-10-28 11:16:06 +00:00
|
|
|
cfg.get('token_vector_width', 128))
|
2018-03-26 15:22:18 +00:00
|
|
|
hidden_width = util.env_opt('hidden_width', cfg.get('hidden_width', 128))
|
|
|
|
embed_size = util.env_opt('embed_size', cfg.get('embed_size', 5000))
|
2018-03-28 15:35:07 +00:00
|
|
|
pretrained_vectors = cfg.get('pretrained_vectors', None)
|
2017-09-21 12:59:48 +00:00
|
|
|
tok2vec = Tok2Vec(token_vector_width, embed_size,
|
2018-03-28 15:35:07 +00:00
|
|
|
pretrained_vectors=pretrained_vectors)
|
2017-09-21 12:59:48 +00:00
|
|
|
tok2vec = chain(tok2vec, flatten)
|
2017-10-20 14:24:16 +00:00
|
|
|
lower = PrecomputableAffine(hidden_width,
|
|
|
|
nF=cls.nr_feature, nI=token_vector_width,
|
|
|
|
nP=parser_maxout_pieces)
|
2017-10-20 01:07:17 +00:00
|
|
|
lower.nP = parser_maxout_pieces
|
2017-05-13 22:55:01 +00:00
|
|
|
|
2017-05-15 19:46:08 +00:00
|
|
|
with Model.use_device('cpu'):
|
2018-05-15 20:17:29 +00:00
|
|
|
upper = zero_init(Affine(nr_class, hidden_width, drop_factor=0.0))
|
2017-10-05 01:06:05 +00:00
|
|
|
|
2017-05-29 08:14:20 +00:00
|
|
|
cfg = {
|
|
|
|
'nr_class': nr_class,
|
2017-10-06 18:50:52 +00:00
|
|
|
'hidden_depth': depth,
|
2017-05-29 08:14:20 +00:00
|
|
|
'token_vector_width': token_vector_width,
|
|
|
|
'hidden_width': hidden_width,
|
2017-10-06 00:38:13 +00:00
|
|
|
'maxout_pieces': parser_maxout_pieces,
|
2018-03-28 15:35:07 +00:00
|
|
|
'pretrained_vectors': pretrained_vectors,
|
2017-05-29 08:14:20 +00:00
|
|
|
}
|
2018-05-15 20:17:29 +00:00
|
|
|
return ParserModel(tok2vec, lower, upper), cfg
|
2017-05-15 19:46:08 +00:00
|
|
|
|
2018-05-15 20:17:29 +00:00
|
|
|
name = 'base_parser'
|
2017-11-06 13:26:26 +00:00
|
|
|
|
2017-05-16 14:17:30 +00:00
|
|
|
def __init__(self, Vocab vocab, moves=True, model=True, **cfg):
|
2017-10-27 12:39:30 +00:00
|
|
|
"""Create a Parser.
|
|
|
|
|
|
|
|
vocab (Vocab): The vocabulary object. Must be shared with documents
|
|
|
|
to be processed. The value is set to the `.vocab` attribute.
|
|
|
|
moves (TransitionSystem): Defines how the parse-state is created,
|
|
|
|
updated and evaluated. The value is set to the .moves attribute
|
|
|
|
unless True (default), in which case a new instance is created with
|
|
|
|
`Parser.Moves()`.
|
|
|
|
model (object): Defines how the parse-state is created, updated and
|
2018-03-28 14:02:59 +00:00
|
|
|
evaluated. The value is set to the .model attribute. If set to True
|
|
|
|
(default), a new instance will be created with `Parser.Model()`
|
|
|
|
in parser.begin_training(), parser.from_disk() or parser.from_bytes().
|
2017-10-27 12:39:30 +00:00
|
|
|
**cfg: Arbitrary configuration parameters. Set to the `.cfg` attribute
|
2017-05-13 22:55:01 +00:00
|
|
|
"""
|
|
|
|
self.vocab = vocab
|
2017-05-16 14:17:30 +00:00
|
|
|
if moves is True:
|
2018-03-27 17:23:02 +00:00
|
|
|
self.moves = self.TransitionSystem(self.vocab.strings)
|
2017-05-16 14:17:30 +00:00
|
|
|
else:
|
|
|
|
self.moves = moves
|
2017-08-18 20:38:59 +00:00
|
|
|
if 'beam_width' not in cfg:
|
|
|
|
cfg['beam_width'] = util.env_opt('beam_width', 1)
|
|
|
|
if 'beam_density' not in cfg:
|
|
|
|
cfg['beam_density'] = util.env_opt('beam_density', 0.0)
|
2017-09-22 14:38:22 +00:00
|
|
|
cfg.setdefault('cnn_maxout_pieces', 3)
|
2017-05-13 22:55:01 +00:00
|
|
|
self.cfg = cfg
|
2017-05-16 09:21:59 +00:00
|
|
|
self.model = model
|
2017-09-26 10:42:52 +00:00
|
|
|
self._multitasks = []
|
2017-05-13 22:55:01 +00:00
|
|
|
|
|
|
|
def __reduce__(self):
|
2017-05-17 10:04:50 +00:00
|
|
|
return (Parser, (self.vocab, self.moves, self.model), None, None)
|
2017-05-13 22:55:01 +00:00
|
|
|
|
2018-05-15 20:17:29 +00:00
|
|
|
@property
|
|
|
|
def move_names(self):
|
|
|
|
names = []
|
|
|
|
for i in range(self.moves.n_moves):
|
|
|
|
name = self.moves.move_name(self.moves.c[i].move, self.moves.c[i].label)
|
|
|
|
names.append(name)
|
|
|
|
return names
|
|
|
|
|
|
|
|
nr_feature = 8
|
|
|
|
|
|
|
|
@property
|
|
|
|
def labels(self):
|
|
|
|
class_names = [self.moves.get_class_name(i) for i in range(self.moves.n_moves)]
|
|
|
|
return class_names
|
|
|
|
|
|
|
|
@property
|
|
|
|
def tok2vec(self):
|
|
|
|
'''Return the embedding and convolutional layer of the model.'''
|
|
|
|
return None if self.model in (None, True, False) else self.model.tok2vec
|
|
|
|
|
|
|
|
@property
|
|
|
|
def postprocesses(self):
|
|
|
|
# Available for subclasses, e.g. to deprojectivize
|
|
|
|
return []
|
|
|
|
|
|
|
|
def add_label(self, label):
|
|
|
|
resized = False
|
|
|
|
for action in self.moves.action_types:
|
|
|
|
added = self.moves.add_action(action, label)
|
|
|
|
if added:
|
|
|
|
resized = True
|
|
|
|
if self.model not in (True, False, None) and resized:
|
|
|
|
self.model.resize_output(self.moves.n_moves)
|
|
|
|
|
|
|
|
def add_multitask_objective(self, target):
|
|
|
|
# Defined in subclasses, to avoid circular import
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def init_multitask_objectives(self, get_gold_tuples, pipeline, **cfg):
|
|
|
|
'''Setup models for secondary objectives, to benefit from multi-task
|
|
|
|
learning. This method is intended to be overridden by subclasses.
|
|
|
|
|
|
|
|
For instance, the dependency parser can benefit from sharing
|
|
|
|
an input representation with a label prediction model. These auxiliary
|
|
|
|
models are discarded after training.
|
|
|
|
'''
|
|
|
|
pass
|
|
|
|
|
|
|
|
def preprocess_gold(self, docs_golds):
|
|
|
|
for doc, gold in docs_golds:
|
|
|
|
yield doc, gold
|
|
|
|
|
|
|
|
def use_params(self, params):
|
|
|
|
# Can't decorate cdef class :(. Workaround.
|
|
|
|
with self.model.use_params(params):
|
|
|
|
yield
|
|
|
|
|
|
|
|
def __call__(self, Doc doc, beam_width=None):
|
2017-10-27 12:39:30 +00:00
|
|
|
"""Apply the parser or entity recognizer, setting the annotations onto
|
|
|
|
the `Doc` object.
|
2017-05-13 22:55:01 +00:00
|
|
|
|
2017-10-27 12:39:30 +00:00
|
|
|
doc (Doc): The document to be processed.
|
2017-05-13 22:55:01 +00:00
|
|
|
"""
|
2017-07-20 13:02:55 +00:00
|
|
|
if beam_width is None:
|
|
|
|
beam_width = self.cfg.get('beam_width', 1)
|
2018-05-15 20:17:29 +00:00
|
|
|
beam_density = self.cfg.get('beam_density', 0.)
|
|
|
|
states = self.predict([doc], beam_width=beam_width,
|
|
|
|
beam_density=beam_density)
|
|
|
|
self.set_annotations([doc], states, tensors=None)
|
|
|
|
return doc
|
2017-07-20 13:02:55 +00:00
|
|
|
|
2018-05-15 20:17:29 +00:00
|
|
|
def pipe(self, docs, int batch_size=256, int n_threads=2, beam_width=None):
|
2017-10-27 12:39:30 +00:00
|
|
|
"""Process a stream of documents.
|
|
|
|
|
|
|
|
stream: The sequence of documents to process.
|
|
|
|
batch_size (int): Number of documents to accumulate into a working set.
|
|
|
|
n_threads (int): The number of threads with which to work on the buffer
|
|
|
|
in parallel.
|
|
|
|
YIELDS (Doc): Documents, in order.
|
2017-05-13 22:55:01 +00:00
|
|
|
"""
|
2017-08-18 20:38:59 +00:00
|
|
|
if beam_width is None:
|
|
|
|
beam_width = self.cfg.get('beam_width', 1)
|
2018-05-15 20:17:29 +00:00
|
|
|
beam_density = self.cfg.get('beam_density', 0.)
|
2017-05-15 19:46:08 +00:00
|
|
|
cdef Doc doc
|
2017-10-18 19:45:01 +00:00
|
|
|
for batch in cytoolz.partition_all(batch_size, docs):
|
2017-11-14 01:11:40 +00:00
|
|
|
batch_in_order = list(batch)
|
|
|
|
by_length = sorted(batch_in_order, key=lambda doc: len(doc))
|
2017-10-18 22:25:21 +00:00
|
|
|
for subbatch in cytoolz.partition_all(8, by_length):
|
2017-10-18 19:45:01 +00:00
|
|
|
subbatch = list(subbatch)
|
2018-05-15 20:17:29 +00:00
|
|
|
parse_states = self.predict(subbatch, beam_width=beam_width,
|
|
|
|
beam_density=beam_density)
|
2017-11-14 01:11:40 +00:00
|
|
|
self.set_annotations(subbatch, parse_states, tensors=None)
|
|
|
|
for doc in batch_in_order:
|
|
|
|
yield doc
|
2017-05-13 22:55:01 +00:00
|
|
|
|
2018-05-15 20:17:29 +00:00
|
|
|
def predict(self, docs, beam_width=1, beam_density=0.0, drop=0.):
|
2017-05-22 22:58:12 +00:00
|
|
|
if isinstance(docs, Doc):
|
|
|
|
docs = [docs]
|
2018-05-15 20:17:29 +00:00
|
|
|
if beam_width < 2:
|
|
|
|
return self.greedy_parse(docs, drop=drop)
|
|
|
|
else:
|
|
|
|
return self.beam_parse(docs, beam_width=beam_width,
|
|
|
|
beam_density=beam_density, drop=drop)
|
2017-05-22 22:58:12 +00:00
|
|
|
|
2018-05-15 20:17:29 +00:00
|
|
|
def greedy_parse(self, docs, drop=0.):
|
|
|
|
cdef vector[StateC*] states
|
|
|
|
cdef StateClass state
|
|
|
|
model = self.model(docs)
|
|
|
|
batch = self.moves.init_batch(docs)
|
|
|
|
weights = get_c_weights(model)
|
|
|
|
for state in batch:
|
|
|
|
if not state.is_final():
|
|
|
|
states.push_back(state.c)
|
|
|
|
sizes = get_c_sizes(model, states.size())
|
2017-10-18 22:25:21 +00:00
|
|
|
with nogil:
|
2018-05-15 20:17:29 +00:00
|
|
|
self._parseC(&states[0],
|
|
|
|
weights, sizes)
|
|
|
|
return batch
|
|
|
|
|
|
|
|
def beam_parse(self, docs, int beam_width, float drop=0., beam_density=0.):
|
2017-07-20 13:02:55 +00:00
|
|
|
cdef Beam beam
|
|
|
|
cdef Doc doc
|
2017-11-14 23:51:42 +00:00
|
|
|
cdef np.ndarray token_ids
|
2018-05-15 20:17:29 +00:00
|
|
|
model = self.model(docs)
|
|
|
|
beams = self.moves.init_beams(docs, beam_width, beam_density=beam_density)
|
2017-11-14 23:51:42 +00:00
|
|
|
token_ids = numpy.zeros((len(docs) * beam_width, self.nr_feature),
|
|
|
|
dtype='i', order='C')
|
|
|
|
cdef int* c_ids
|
|
|
|
cdef int nr_feature = self.nr_feature
|
|
|
|
cdef int n_states
|
2018-05-15 20:17:29 +00:00
|
|
|
model = self.model(docs)
|
|
|
|
todo = [beam for beam in beams if not beam.is_done]
|
2017-11-14 23:51:42 +00:00
|
|
|
while todo:
|
|
|
|
token_ids.fill(-1)
|
|
|
|
c_ids = <int*>token_ids.data
|
|
|
|
n_states = 0
|
|
|
|
for beam in todo:
|
2017-07-20 13:02:55 +00:00
|
|
|
for i in range(beam.size):
|
2017-11-14 23:51:42 +00:00
|
|
|
state = <StateC*>beam.at(i)
|
2017-08-18 20:23:03 +00:00
|
|
|
# This way we avoid having to score finalized states
|
|
|
|
# We do have to take care to keep indexes aligned, though
|
2017-11-14 23:51:42 +00:00
|
|
|
if not state.is_final():
|
|
|
|
state.set_context_tokens(c_ids, nr_feature)
|
|
|
|
c_ids += nr_feature
|
|
|
|
n_states += 1
|
|
|
|
if n_states == 0:
|
|
|
|
break
|
2018-05-15 20:17:29 +00:00
|
|
|
vectors = model.state2vec(token_ids[:n_states])
|
|
|
|
scores = model.vec2scores(vectors)
|
|
|
|
todo = self.transition_beams(todo, scores)
|
|
|
|
return beams
|
|
|
|
|
|
|
|
cdef void _parseC(self, StateC** states,
|
|
|
|
WeightsC weights, SizesC sizes) nogil:
|
|
|
|
cdef int i, j
|
|
|
|
cdef vector[StateC*] unfinished
|
|
|
|
cdef ActivationsC activations
|
|
|
|
memset(&activations, 0, sizeof(activations))
|
|
|
|
while sizes.states >= 1:
|
|
|
|
predict_states(&activations,
|
|
|
|
states, &weights, sizes)
|
|
|
|
# Validate actions, argmax, take action.
|
|
|
|
self.c_transition_batch(states,
|
|
|
|
activations.scores, sizes.classes, sizes.states)
|
|
|
|
for i in range(sizes.states):
|
|
|
|
if not states[i].is_final():
|
|
|
|
unfinished.push_back(states[i])
|
|
|
|
for i in range(unfinished.size()):
|
|
|
|
states[i] = unfinished[i]
|
|
|
|
sizes.states = unfinished.size()
|
|
|
|
unfinished.clear()
|
|
|
|
|
|
|
|
def set_annotations(self, docs, states_or_beams, tensors=None):
|
|
|
|
cdef StateClass state
|
|
|
|
cdef Beam beam
|
|
|
|
cdef Doc doc
|
|
|
|
states = []
|
|
|
|
beams = []
|
|
|
|
for state_or_beam in states_or_beams:
|
|
|
|
if isinstance(state_or_beam, StateClass):
|
|
|
|
states.append(state_or_beam)
|
|
|
|
else:
|
|
|
|
beam = state_or_beam
|
|
|
|
state = StateClass.borrow(<StateC*>beam.at(0))
|
|
|
|
states.append(state)
|
|
|
|
beams.append(beam)
|
|
|
|
for i, (state, doc) in enumerate(zip(states, docs)):
|
|
|
|
self.moves.finalize_state(state.c)
|
|
|
|
for j in range(doc.length):
|
|
|
|
doc.c[j] = state.c._sent[j]
|
|
|
|
self.moves.finalize_doc(doc)
|
|
|
|
for hook in self.postprocesses:
|
|
|
|
for doc in docs:
|
|
|
|
hook(doc)
|
|
|
|
for beam in beams:
|
|
|
|
_beam_utils.cleanup_beam(beam)
|
|
|
|
|
|
|
|
def transition_states(self, states, float[:, ::1] scores):
|
|
|
|
cdef StateClass state
|
|
|
|
cdef float* c_scores = &scores[0, 0]
|
|
|
|
cdef vector[StateC*] c_states
|
|
|
|
for state in states:
|
|
|
|
c_states.push_back(state.c)
|
|
|
|
self.c_transition_batch(&c_states[0], c_scores, scores.shape[1], scores.shape[0])
|
|
|
|
return [state for state in states if not state.c.is_final()]
|
|
|
|
|
|
|
|
cdef void c_transition_batch(self, StateC** states, const float* scores,
|
|
|
|
int nr_class, int batch_size) nogil:
|
|
|
|
cdef int[500] is_valid # TODO: Unhack
|
|
|
|
cdef int i, guess
|
|
|
|
cdef Transition action
|
|
|
|
for i in range(batch_size):
|
|
|
|
self.moves.set_valid(is_valid, states[i])
|
|
|
|
guess = arg_max_if_valid(&scores[i*nr_class], is_valid, nr_class)
|
|
|
|
action = self.moves.c[guess]
|
|
|
|
action.do(states[i], action.label)
|
|
|
|
states[i].push_hist(guess)
|
2017-07-20 13:02:55 +00:00
|
|
|
|
2018-05-15 20:17:29 +00:00
|
|
|
def transition_beams(self, beams, float[:, ::1] scores):
|
|
|
|
cdef Beam beam
|
|
|
|
cdef float* c_scores = &scores[0, 0]
|
|
|
|
for beam in beams:
|
|
|
|
for i in range(beam.size):
|
|
|
|
state = <StateC*>beam.at(i)
|
|
|
|
if not state.is_final():
|
|
|
|
self.moves.set_valid(beam.is_valid[i], state)
|
|
|
|
memcpy(beam.scores[i], c_scores, scores.shape[1] * sizeof(float))
|
|
|
|
c_scores += scores.shape[1]
|
|
|
|
beam.advance(_beam_utils.transition_state, NULL, <void*>self.moves.c)
|
|
|
|
beam.check_done(_beam_utils.check_final_state, NULL)
|
|
|
|
return [b for b in beams if not b.is_done]
|
|
|
|
|
2017-09-21 12:59:48 +00:00
|
|
|
def update(self, docs, golds, drop=0., sgd=None, losses=None):
|
2018-05-15 20:17:29 +00:00
|
|
|
if isinstance(docs, Doc) and isinstance(golds, GoldParse):
|
|
|
|
docs = [docs]
|
|
|
|
golds = [golds]
|
2018-04-03 13:50:31 +00:00
|
|
|
if len(docs) != len(golds):
|
|
|
|
raise ValueError(Errors.E077.format(value='update', n_docs=len(docs),
|
|
|
|
n_golds=len(golds)))
|
2018-05-15 20:17:29 +00:00
|
|
|
if losses is None:
|
|
|
|
losses = {}
|
|
|
|
losses.setdefault(self.name, 0.)
|
2018-04-03 20:02:56 +00:00
|
|
|
# The probability we use beam update, instead of falling back to
|
|
|
|
# a greedy update
|
2018-05-15 20:17:29 +00:00
|
|
|
beam_update_prob = self.cfg.get('beam_update_prob', 1.0)
|
|
|
|
if self.cfg.get('beam_width', 1) >= 2 and numpy.random.random() < beam_update_prob:
|
|
|
|
return self.update_beam(docs, golds, self.cfg.get('beam_width', 1),
|
|
|
|
drop=drop, sgd=sgd, losses=losses,
|
|
|
|
beam_density=self.cfg.get('beam_density', 0.0))
|
2018-03-27 17:23:02 +00:00
|
|
|
# Chop sequences into lengths of this many transitions, to make the
|
|
|
|
# batch uniform length.
|
|
|
|
cut_gold = numpy.random.choice(range(20, 100))
|
|
|
|
states, golds, max_steps = self._init_gold_batch(docs, golds, max_length=cut_gold)
|
2018-05-15 20:17:29 +00:00
|
|
|
states_golds = [(s, g) for (s, g) in zip(states, golds)
|
|
|
|
if not s.is_final() and g is not None]
|
2017-05-23 08:06:53 +00:00
|
|
|
|
2018-05-15 20:17:29 +00:00
|
|
|
# Prepare the stepwise model, and get the callback for finishing the batch
|
|
|
|
model, finish_update = self.model.begin_update(docs, drop=drop)
|
|
|
|
for _ in range(max_steps):
|
|
|
|
if not states_golds:
|
2017-05-25 16:18:59 +00:00
|
|
|
break
|
2018-05-15 20:17:29 +00:00
|
|
|
states, golds = zip(*states_golds)
|
|
|
|
scores, backprop = model.begin_update(states, drop=drop)
|
|
|
|
d_scores = self.get_batch_loss(states, golds, scores, losses)
|
|
|
|
backprop(d_scores, sgd=sgd)
|
|
|
|
# Follow the predicted action
|
|
|
|
self.transition_states(states, scores)
|
|
|
|
states_golds = [eg for eg in states_golds if not eg[0].is_final()]
|
|
|
|
# Do the backprop
|
|
|
|
finish_update(golds, sgd=sgd)
|
|
|
|
return losses
|
|
|
|
|
|
|
|
def update_beam(self, docs, golds, width, drop=0., sgd=None, losses=None,
|
|
|
|
beam_density=0.0):
|
2017-08-12 22:15:16 +00:00
|
|
|
lengths = [len(d) for d in docs]
|
2017-08-13 23:02:05 +00:00
|
|
|
states = self.moves.init_batch(docs)
|
|
|
|
for gold in golds:
|
|
|
|
self.moves.preprocess_gold(gold)
|
2018-05-15 20:17:29 +00:00
|
|
|
model, finish_update = self.model.begin_update(docs, drop=drop)
|
2017-11-13 17:18:26 +00:00
|
|
|
states_d_scores, backprops, beams = _beam_utils.update_beam(
|
2018-05-15 20:17:29 +00:00
|
|
|
self.moves, self.nr_feature, 10000, states, golds, model.state2vec,
|
|
|
|
model.vec2scores, width, drop=drop, losses=losses,
|
|
|
|
beam_density=beam_density)
|
2017-08-12 19:47:45 +00:00
|
|
|
for i, d_scores in enumerate(states_d_scores):
|
2018-05-15 20:17:29 +00:00
|
|
|
losses[self.name] += (d_scores**2).sum()
|
2017-08-12 19:47:45 +00:00
|
|
|
ids, bp_vectors, bp_scores = backprops[i]
|
|
|
|
d_vector = bp_scores(d_scores, sgd=sgd)
|
2018-05-15 20:17:29 +00:00
|
|
|
if isinstance(model.ops, CupyOps) \
|
|
|
|
and not isinstance(ids, model.state2vec.ops.xp.ndarray):
|
|
|
|
model.backprops.append((
|
|
|
|
util.get_async(model.cuda_stream, ids),
|
|
|
|
util.get_async(model.cuda_stream, d_vector),
|
2017-08-12 22:15:16 +00:00
|
|
|
bp_vectors))
|
|
|
|
else:
|
2018-05-15 20:17:29 +00:00
|
|
|
model.backprops.append((ids, d_vector, bp_vectors))
|
|
|
|
model.make_updates(sgd)
|
2017-11-13 17:18:26 +00:00
|
|
|
cdef Beam beam
|
|
|
|
for beam in beams:
|
2018-05-15 20:17:29 +00:00
|
|
|
_beam_utils.cleanup_beam(beam)
|
|
|
|
|
2018-03-27 17:23:02 +00:00
|
|
|
def _init_gold_batch(self, whole_docs, whole_golds, min_length=5, max_length=500):
|
2017-05-25 16:18:59 +00:00
|
|
|
"""Make a square batch, of length equal to the shortest doc. A long
|
|
|
|
doc will get multiple states. Let's say we have a doc of length 2*N,
|
|
|
|
where N is the shortest doc. We'll make two states, one representing
|
|
|
|
long_doc[:N], and another representing long_doc[N:]."""
|
2017-05-26 16:31:23 +00:00
|
|
|
cdef:
|
|
|
|
StateClass state
|
|
|
|
Transition action
|
|
|
|
whole_states = self.moves.init_batch(whole_docs)
|
2018-03-27 17:23:02 +00:00
|
|
|
max_length = max(min_length, min(max_length, min([len(doc) for doc in whole_docs])))
|
2017-05-27 22:59:00 +00:00
|
|
|
max_moves = 0
|
2017-05-25 16:18:59 +00:00
|
|
|
states = []
|
2017-05-26 16:31:23 +00:00
|
|
|
golds = []
|
|
|
|
for doc, state, gold in zip(whole_docs, whole_states, whole_golds):
|
2017-05-25 16:18:59 +00:00
|
|
|
gold = self.moves.preprocess_gold(gold)
|
2017-05-26 16:31:23 +00:00
|
|
|
if gold is None:
|
|
|
|
continue
|
|
|
|
oracle_actions = self.moves.get_oracle_sequence(doc, gold)
|
|
|
|
start = 0
|
2017-05-25 16:18:59 +00:00
|
|
|
while start < len(doc):
|
2017-05-26 16:31:23 +00:00
|
|
|
state = state.copy()
|
2017-05-27 22:59:00 +00:00
|
|
|
n_moves = 0
|
2017-05-25 16:18:59 +00:00
|
|
|
while state.B(0) < start and not state.is_final():
|
2017-05-26 16:31:23 +00:00
|
|
|
action = self.moves.c[oracle_actions.pop(0)]
|
|
|
|
action.do(state.c, action.label)
|
2017-10-06 11:08:50 +00:00
|
|
|
state.c.push_hist(action.clas)
|
2017-05-27 22:59:00 +00:00
|
|
|
n_moves += 1
|
2017-05-26 16:31:23 +00:00
|
|
|
has_gold = self.moves.has_gold(gold, start=start,
|
|
|
|
end=start+max_length)
|
|
|
|
if not state.is_final() and has_gold:
|
2017-05-25 16:18:59 +00:00
|
|
|
states.append(state)
|
2017-05-26 16:31:23 +00:00
|
|
|
golds.append(gold)
|
2017-05-27 22:59:00 +00:00
|
|
|
max_moves = max(max_moves, n_moves)
|
2017-05-26 16:31:23 +00:00
|
|
|
start += min(max_length, len(doc)-start)
|
2017-05-27 22:59:00 +00:00
|
|
|
max_moves = max(max_moves, len(oracle_actions))
|
|
|
|
return states, golds, max_moves
|
2017-05-25 16:18:59 +00:00
|
|
|
|
2018-05-15 20:17:29 +00:00
|
|
|
def get_batch_loss(self, states, golds, float[:, ::1] scores, losses):
|
2017-05-15 19:46:08 +00:00
|
|
|
cdef StateClass state
|
|
|
|
cdef GoldParse gold
|
|
|
|
cdef Pool mem = Pool()
|
|
|
|
cdef int i
|
|
|
|
is_valid = <int*>mem.alloc(self.moves.n_moves, sizeof(int))
|
|
|
|
costs = <float*>mem.alloc(self.moves.n_moves, sizeof(float))
|
|
|
|
cdef np.ndarray d_scores = numpy.zeros((len(states), self.moves.n_moves),
|
|
|
|
dtype='f', order='C')
|
|
|
|
c_d_scores = <float*>d_scores.data
|
|
|
|
for i, (state, gold) in enumerate(zip(states, golds)):
|
|
|
|
memset(is_valid, 0, self.moves.n_moves * sizeof(int))
|
|
|
|
memset(costs, 0, self.moves.n_moves * sizeof(float))
|
|
|
|
self.moves.set_costs(is_valid, costs, state, gold)
|
|
|
|
cpu_log_loss(c_d_scores,
|
|
|
|
costs, is_valid, &scores[i, 0], d_scores.shape[1])
|
|
|
|
c_d_scores += d_scores.shape[1]
|
2018-05-15 20:17:29 +00:00
|
|
|
if losses is not None:
|
|
|
|
losses.setdefault(self.name, 0.)
|
|
|
|
losses[self.name] += (d_scores**2).sum()
|
2017-05-15 19:46:08 +00:00
|
|
|
return d_scores
|
2018-05-15 20:17:29 +00:00
|
|
|
|
|
|
|
def create_optimizer(self):
|
|
|
|
return create_default_optimizer(self.model.ops,
|
|
|
|
**self.cfg.get('optimizer', {}))
|
|
|
|
|
2018-03-27 09:39:59 +00:00
|
|
|
def begin_training(self, get_gold_tuples, pipeline=None, sgd=None, **cfg):
|
2017-05-16 09:21:59 +00:00
|
|
|
if 'model' in cfg:
|
|
|
|
self.model = cfg['model']
|
2018-03-27 19:08:41 +00:00
|
|
|
if not hasattr(get_gold_tuples, '__call__'):
|
|
|
|
gold_tuples = get_gold_tuples
|
|
|
|
get_gold_tuples = lambda: gold_tuples
|
Improve label management in parser and NER (#2108)
This patch does a few smallish things that tighten up the training workflow a little, and allow memory use during training to be reduced by letting the GoldCorpus stream data properly.
Previously, the parser and entity recognizer read and saved labels as lists, with extra labels noted separately. Lists were used becaue ordering is very important, to ensure that the label-to-class mapping is stable.
We now manage labels as nested dictionaries, first keyed by the action, and then keyed by the label. Values are frequencies. The trick is, how do we save new labels? We need to make sure we iterate over these in the same order they're added. Otherwise, we'll get different class IDs, and the model's predictions won't make sense.
To allow stable sorting, we map the new labels to negative values. If we have two new labels, they'll be noted as having "frequency" -1 and -2. The next new label will then have "frequency" -3. When we sort by (frequency, label), we then get a stable sort.
Storing frequencies then allows us to make the next nice improvement. Previously we had to iterate over the whole training set, to pre-process it for the deprojectivisation. This led to storing the whole training set in memory. This was most of the required memory during training.
To prevent this, we now store the frequencies as we stream in the data, and deprojectivize as we go. Once we've built the frequencies, we can then apply a frequency cut-off when we decide how many classes to make.
Finally, to allow proper data streaming, we also have to have some way of shuffling the iterator. This is awkward if the training files have multiple documents in them. To solve this, the GoldCorpus class now writes the training data to disk in msgpack files, one per document. We can then shuffle the data by shuffling the paths.
This is a squash merge, as I made a lot of very small commits. Individual commit messages below.
* Simplify label management for TransitionSystem and its subclasses
* Fix serialization for new label handling format in parser
* Simplify and improve GoldCorpus class. Reduce memory use, write to temp dir
* Set actions in transition system
* Require thinc 6.11.1.dev4
* Fix error in parser init
* Add unicode declaration
* Fix unicode declaration
* Update textcat test
* Try to get model training on less memory
* Print json loc for now
* Try rapidjson to reduce memory use
* Remove rapidjson requirement
* Try rapidjson for reduced mem usage
* Handle None heads when projectivising
* Stream json docs
* Fix train script
* Handle projectivity in GoldParse
* Fix projectivity handling
* Add minibatch_by_words util from ud_train
* Minibatch by number of words in spacy.cli.train
* Move minibatch_by_words util to spacy.util
* Fix label handling
* More hacking at label management in parser
* Fix encoding in msgpack serialization in GoldParse
* Adjust batch sizes in parser training
* Fix minibatch_by_words
* Add merge_subtokens function to pipeline.pyx
* Register merge_subtokens factory
* Restore use of msgpack tmp directory
* Use minibatch-by-words in train
* Handle retokenization in scorer
* Change back-off approach for missing labels. Use 'dep' label
* Update NER for new label management
* Set NER tags for over-segmented words
* Fix label alignment in gold
* Fix label back-off for infrequent labels
* Fix int type in labels dict key
* Fix int type in labels dict key
* Update feature definition for 8 feature set
* Update ud-train script for new label stuff
* Fix json streamer
* Print the line number if conll eval fails
* Update children and sentence boundaries after deprojectivisation
* Export set_children_from_heads from doc.pxd
* Render parses during UD training
* Remove print statement
* Require thinc 6.11.1.dev6. Try adding wheel as install_requires
* Set different dev version, to flush pip cache
* Update thinc version
* Update GoldCorpus docs
* Remove print statements
* Fix formatting and links [ci skip]
2018-03-19 01:58:08 +00:00
|
|
|
cfg.setdefault('min_action_freq', 30)
|
2018-03-27 09:39:59 +00:00
|
|
|
actions = self.moves.get_actions(gold_parses=get_gold_tuples(),
|
Improve label management in parser and NER (#2108)
This patch does a few smallish things that tighten up the training workflow a little, and allow memory use during training to be reduced by letting the GoldCorpus stream data properly.
Previously, the parser and entity recognizer read and saved labels as lists, with extra labels noted separately. Lists were used becaue ordering is very important, to ensure that the label-to-class mapping is stable.
We now manage labels as nested dictionaries, first keyed by the action, and then keyed by the label. Values are frequencies. The trick is, how do we save new labels? We need to make sure we iterate over these in the same order they're added. Otherwise, we'll get different class IDs, and the model's predictions won't make sense.
To allow stable sorting, we map the new labels to negative values. If we have two new labels, they'll be noted as having "frequency" -1 and -2. The next new label will then have "frequency" -3. When we sort by (frequency, label), we then get a stable sort.
Storing frequencies then allows us to make the next nice improvement. Previously we had to iterate over the whole training set, to pre-process it for the deprojectivisation. This led to storing the whole training set in memory. This was most of the required memory during training.
To prevent this, we now store the frequencies as we stream in the data, and deprojectivize as we go. Once we've built the frequencies, we can then apply a frequency cut-off when we decide how many classes to make.
Finally, to allow proper data streaming, we also have to have some way of shuffling the iterator. This is awkward if the training files have multiple documents in them. To solve this, the GoldCorpus class now writes the training data to disk in msgpack files, one per document. We can then shuffle the data by shuffling the paths.
This is a squash merge, as I made a lot of very small commits. Individual commit messages below.
* Simplify label management for TransitionSystem and its subclasses
* Fix serialization for new label handling format in parser
* Simplify and improve GoldCorpus class. Reduce memory use, write to temp dir
* Set actions in transition system
* Require thinc 6.11.1.dev4
* Fix error in parser init
* Add unicode declaration
* Fix unicode declaration
* Update textcat test
* Try to get model training on less memory
* Print json loc for now
* Try rapidjson to reduce memory use
* Remove rapidjson requirement
* Try rapidjson for reduced mem usage
* Handle None heads when projectivising
* Stream json docs
* Fix train script
* Handle projectivity in GoldParse
* Fix projectivity handling
* Add minibatch_by_words util from ud_train
* Minibatch by number of words in spacy.cli.train
* Move minibatch_by_words util to spacy.util
* Fix label handling
* More hacking at label management in parser
* Fix encoding in msgpack serialization in GoldParse
* Adjust batch sizes in parser training
* Fix minibatch_by_words
* Add merge_subtokens function to pipeline.pyx
* Register merge_subtokens factory
* Restore use of msgpack tmp directory
* Use minibatch-by-words in train
* Handle retokenization in scorer
* Change back-off approach for missing labels. Use 'dep' label
* Update NER for new label management
* Set NER tags for over-segmented words
* Fix label alignment in gold
* Fix label back-off for infrequent labels
* Fix int type in labels dict key
* Fix int type in labels dict key
* Update feature definition for 8 feature set
* Update ud-train script for new label stuff
* Fix json streamer
* Print the line number if conll eval fails
* Update children and sentence boundaries after deprojectivisation
* Export set_children_from_heads from doc.pxd
* Render parses during UD training
* Remove print statement
* Require thinc 6.11.1.dev6. Try adding wheel as install_requires
* Set different dev version, to flush pip cache
* Update thinc version
* Update GoldCorpus docs
* Remove print statements
* Fix formatting and links [ci skip]
2018-03-19 01:58:08 +00:00
|
|
|
min_freq=cfg.get('min_action_freq', 30))
|
|
|
|
self.moves.initialize_actions(actions)
|
2018-02-02 01:32:40 +00:00
|
|
|
cfg.setdefault('token_vector_width', 128)
|
2017-05-16 09:21:59 +00:00
|
|
|
if self.model is True:
|
2017-05-29 08:14:20 +00:00
|
|
|
self.model, cfg = self.Model(self.moves.n_moves, **cfg)
|
2017-11-06 13:26:26 +00:00
|
|
|
if sgd is None:
|
|
|
|
sgd = self.create_optimizer()
|
2018-05-15 20:17:29 +00:00
|
|
|
self.model.begin_training(
|
|
|
|
self.model.ops.allocate((5, cfg['token_vector_width'])))
|
2018-02-15 22:50:21 +00:00
|
|
|
if pipeline is not None:
|
2018-03-27 09:39:59 +00:00
|
|
|
self.init_multitask_objectives(get_gold_tuples, pipeline, sgd=sgd, **cfg)
|
2017-09-22 14:38:22 +00:00
|
|
|
link_vectors_to_models(self.vocab)
|
2018-02-02 01:32:40 +00:00
|
|
|
else:
|
|
|
|
if sgd is None:
|
|
|
|
sgd = self.create_optimizer()
|
2018-05-15 20:17:29 +00:00
|
|
|
self.model.begin_training(
|
|
|
|
self.model.ops.allocate((5, cfg['token_vector_width'])))
|
2018-02-02 01:32:40 +00:00
|
|
|
self.cfg.update(cfg)
|
2017-11-06 13:26:26 +00:00
|
|
|
return sgd
|
2018-01-21 18:37:02 +00:00
|
|
|
|
2017-05-29 09:45:45 +00:00
|
|
|
def to_disk(self, path, **exclude):
|
|
|
|
serializers = {
|
2018-05-15 20:17:29 +00:00
|
|
|
'model': lambda p: self.model.to_disk(p),
|
2017-05-29 09:45:45 +00:00
|
|
|
'vocab': lambda p: self.vocab.to_disk(p),
|
|
|
|
'moves': lambda p: self.moves.to_disk(p, strings=False),
|
2017-05-31 11:42:39 +00:00
|
|
|
'cfg': lambda p: p.open('w').write(json_dumps(self.cfg))
|
2017-05-29 09:45:45 +00:00
|
|
|
}
|
|
|
|
util.to_disk(path, serializers, exclude)
|
|
|
|
|
|
|
|
def from_disk(self, path, **exclude):
|
|
|
|
deserializers = {
|
|
|
|
'vocab': lambda p: self.vocab.from_disk(p),
|
|
|
|
'moves': lambda p: self.moves.from_disk(p, strings=False),
|
2018-02-13 19:44:33 +00:00
|
|
|
'cfg': lambda p: self.cfg.update(util.read_json(p)),
|
2017-05-29 09:45:45 +00:00
|
|
|
'model': lambda p: None
|
|
|
|
}
|
|
|
|
util.from_disk(path, deserializers, exclude)
|
|
|
|
if 'model' not in exclude:
|
|
|
|
path = util.ensure_path(path)
|
|
|
|
if self.model is True:
|
2017-05-29 11:38:20 +00:00
|
|
|
self.model, cfg = self.Model(**self.cfg)
|
2017-05-31 11:42:39 +00:00
|
|
|
else:
|
|
|
|
cfg = {}
|
2018-05-15 20:17:29 +00:00
|
|
|
with (path / 'model').open('rb') as file_:
|
2017-05-31 11:42:39 +00:00
|
|
|
bytes_data = file_.read()
|
2018-05-15 20:17:29 +00:00
|
|
|
self.model.from_bytes(bytes_data)
|
2017-05-29 11:38:20 +00:00
|
|
|
self.cfg.update(cfg)
|
2017-05-29 09:45:45 +00:00
|
|
|
return self
|
2017-05-17 10:04:50 +00:00
|
|
|
|
2017-05-29 08:14:20 +00:00
|
|
|
def to_bytes(self, **exclude):
|
2017-06-02 19:07:56 +00:00
|
|
|
serializers = OrderedDict((
|
2018-05-15 20:17:29 +00:00
|
|
|
('model', lambda: self.model.to_bytes()),
|
2017-06-02 19:07:56 +00:00
|
|
|
('vocab', lambda: self.vocab.to_bytes()),
|
|
|
|
('moves', lambda: self.moves.to_bytes(strings=False)),
|
2017-09-26 11:44:56 +00:00
|
|
|
('cfg', lambda: json.dumps(self.cfg, indent=2, sort_keys=True))
|
2017-06-02 19:07:56 +00:00
|
|
|
))
|
2017-05-29 09:45:45 +00:00
|
|
|
return util.to_bytes(serializers, exclude)
|
2017-05-29 08:14:20 +00:00
|
|
|
|
|
|
|
def from_bytes(self, bytes_data, **exclude):
|
2017-06-02 19:07:56 +00:00
|
|
|
deserializers = OrderedDict((
|
|
|
|
('vocab', lambda b: self.vocab.from_bytes(b)),
|
|
|
|
('moves', lambda b: self.moves.from_bytes(b, strings=False)),
|
2017-09-26 11:44:56 +00:00
|
|
|
('cfg', lambda b: self.cfg.update(json.loads(b))),
|
2018-05-15 20:17:29 +00:00
|
|
|
('model', lambda b: None)
|
2017-06-02 19:07:56 +00:00
|
|
|
))
|
2017-05-29 09:45:45 +00:00
|
|
|
msg = util.from_bytes(bytes_data, deserializers, exclude)
|
2017-05-29 08:14:20 +00:00
|
|
|
if 'model' not in exclude:
|
2018-03-28 14:02:59 +00:00
|
|
|
# TODO: Remove this once we don't have to handle previous models
|
2018-04-10 20:19:05 +00:00
|
|
|
if self.cfg.get('pretrained_dims') and 'pretrained_vectors' not in self.cfg:
|
2018-03-28 14:02:59 +00:00
|
|
|
self.cfg['pretrained_vectors'] = self.vocab.vectors.name
|
2017-05-29 08:14:20 +00:00
|
|
|
if self.model is True:
|
2017-09-22 14:38:22 +00:00
|
|
|
self.model, cfg = self.Model(**self.cfg)
|
2017-05-29 13:40:45 +00:00
|
|
|
else:
|
|
|
|
cfg = {}
|
2018-05-15 20:17:29 +00:00
|
|
|
if 'model' in msg:
|
|
|
|
self.model.from_bytes(msg['model'])
|
2017-05-29 11:38:20 +00:00
|
|
|
self.cfg.update(cfg)
|
2017-05-29 08:14:20 +00:00
|
|
|
return self
|