Small fixes to as_example (#4957)

* label in span not writable anymore

* Revert "label in span not writable anymore"

This reverts commit ab442338c8.

* fixing yield - remove redundant list
This commit is contained in:
Sofie Van Landeghem 2020-02-03 13:02:12 +01:00 committed by GitHub
parent 71b93f33bb
commit cabd60fa1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 25 deletions

View File

@ -534,7 +534,9 @@ class Language(object):
if not hasattr(proc, "rehearse"): if not hasattr(proc, "rehearse"):
continue continue
grads = {} grads = {}
proc.rehearse(examples, sgd=get_grads, losses=losses, **config.get(name, {})) proc.rehearse(
examples, sgd=get_grads, losses=losses, **config.get(name, {})
)
for key, (W, dW) in grads.items(): for key, (W, dW) in grads.items():
sgd(W, dW, key=key) sgd(W, dW, key=key)
return losses return losses
@ -590,10 +592,7 @@ class Language(object):
kwargs = component_cfg.get(name, {}) kwargs = component_cfg.get(name, {})
kwargs.update(cfg) kwargs.update(cfg)
proc.begin_training( proc.begin_training(
get_examples, get_examples, pipeline=self.pipeline, sgd=self._optimizer, **kwargs
pipeline=self.pipeline,
sgd=self._optimizer,
**kwargs
) )
self._link_components() self._link_components()
return self._optimizer return self._optimizer
@ -701,7 +700,7 @@ class Language(object):
cleanup=False, cleanup=False,
component_cfg=None, component_cfg=None,
n_process=1, n_process=1,
as_example=False as_example=False,
): ):
"""Process texts as a stream, and yield `Doc` objects in order. """Process texts as a stream, and yield `Doc` objects in order.
@ -737,7 +736,7 @@ class Language(object):
disable=disable, disable=disable,
n_process=n_process, n_process=n_process,
component_cfg=component_cfg, component_cfg=component_cfg,
as_example=False # TODO: shouldn't this be as_example=as_example ? as_example=as_example,
) )
for doc, context in zip(docs, contexts): for doc, context in zip(docs, contexts):
yield (doc, context) yield (doc, context)

View File

@ -108,11 +108,9 @@ class Pipe(object):
self.set_annotations(docs, predictions) self.set_annotations(docs, predictions)
if as_example: if as_example:
annotated_examples = []
for ex, doc in zip(examples, docs): for ex, doc in zip(examples, docs):
ex.doc = doc ex.doc = doc
annotated_examples.append(ex) yield ex
yield from annotated_examples
else: else:
yield from docs yield from docs
@ -329,11 +327,9 @@ class Tensorizer(Pipe):
self.set_annotations(docs, tensors) self.set_annotations(docs, tensors)
if as_example: if as_example:
annotated_examples = []
for ex, doc in zip(examples, docs): for ex, doc in zip(examples, docs):
ex.doc = doc ex.doc = doc
annotated_examples.append(ex) yield ex
yield from annotated_examples
else: else:
yield from docs yield from docs
@ -464,11 +460,9 @@ class Tagger(Pipe):
self.set_annotations(docs, tag_ids) self.set_annotations(docs, tag_ids)
if as_example: if as_example:
annotated_examples = []
for ex, doc in zip(examples, docs): for ex, doc in zip(examples, docs):
ex.doc = doc ex.doc = doc
annotated_examples.append(ex) yield ex
yield from annotated_examples
else: else:
yield from docs yield from docs
@ -1256,11 +1250,9 @@ class TextCategorizer(Pipe):
self.set_annotations(docs, scores, tensors=tensors) self.set_annotations(docs, scores, tensors=tensors)
if as_example: if as_example:
annotated_examples = []
for ex, doc in zip(examples, docs): for ex, doc in zip(examples, docs):
ex.doc = doc ex.doc = doc
annotated_examples.append(ex) yield ex
yield from annotated_examples
else: else:
yield from docs yield from docs
@ -1616,11 +1608,9 @@ class EntityLinker(Pipe):
self.set_annotations(docs, kb_ids, tensors=tensors) self.set_annotations(docs, kb_ids, tensors=tensors)
if as_example: if as_example:
annotated_examples = []
for ex, doc in zip(examples, docs): for ex, doc in zip(examples, docs):
ex.doc = doc ex.doc = doc
annotated_examples.append(ex) yield ex
yield from annotated_examples
else: else:
yield from docs yield from docs
@ -1834,11 +1824,9 @@ class Sentencizer(Pipe):
else: else:
self.set_annotations(docs, predictions) self.set_annotations(docs, predictions)
if as_example: if as_example:
annotated_examples = []
for ex, doc in zip(examples, docs): for ex, doc in zip(examples, docs):
ex.doc = doc ex.doc = doc
annotated_examples.append(ex) yield ex
yield from annotated_examples
else: else:
yield from docs yield from docs