From 473504d837e4d28510a67c4b585de806166b1801 Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Fri, 7 Aug 2020 16:49:00 +0200 Subject: [PATCH] Format --- spacy/ml/models/tagger.py | 15 +++++++++++++-- spacy/ml/models/tok2vec.py | 12 +++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/spacy/ml/models/tagger.py b/spacy/ml/models/tagger.py index 78637e8b5..d6bdb283b 100644 --- a/spacy/ml/models/tagger.py +++ b/spacy/ml/models/tagger.py @@ -1,11 +1,22 @@ -from typing import Optional +from typing import Optional, List from thinc.api import zero_init, with_array, Softmax, chain, Model +from thinc.types import Floats2d from ...util import registry +from ..tokens import Doc @registry.architectures.register("spacy.Tagger.v1") -def build_tagger_model(tok2vec: Model, nO: Optional[int] = None) -> Model: +def build_tagger_model( + tok2vec: Model[List[Doc], List[Floats2d]], nO: Optional[int] = None +) -> Model[List[Doc], List[Floats2d]]: + """Build a tagger model, using a provided token-to-vector component. The tagger + model simply adds a linear layer with softmax activation to predict scores + given the token vectors. + + tok2vec (Model[List[Doc], List[Floats2d]]): The token-to-vector subnetwork. + nO (int or None): The number of tags to output. Inferred from the data if None. + """ # TODO: glorot_uniform_init seems to work a bit better than zero_init here?! t2v_width = tok2vec.get_dim("nO") if tok2vec.has_dim("nO") else None output_layer = Softmax(nO, t2v_width, init_W=zero_init) diff --git a/spacy/ml/models/tok2vec.py b/spacy/ml/models/tok2vec.py index a3f8b3c87..5689ce50b 100644 --- a/spacy/ml/models/tok2vec.py +++ b/spacy/ml/models/tok2vec.py @@ -205,7 +205,9 @@ def CharacterEmbed(width: int, rows: int, nM: int, nC: int): @registry.architectures.register("spacy.MaxoutWindowEncoder.v1") -def MaxoutWindowEncoder(width: int, window_size: int, maxout_pieces: int, depth: int) -> Model[List[Floats2d], List[Floats2d]]: +def MaxoutWindowEncoder( + width: int, window_size: int, maxout_pieces: int, depth: int +) -> Model[List[Floats2d], List[Floats2d]]: """Encode context using convolutions with maxout activation, layer normalization and residual connections. @@ -235,7 +237,9 @@ def MaxoutWindowEncoder(width: int, window_size: int, maxout_pieces: int, depth: @registry.architectures.register("spacy.MishWindowEncoder.v1") -def MishWindowEncoder(width: int, window_size: int, depth: int) -> Model[List[Floats2d], List[Floats2d]]: +def MishWindowEncoder( + width: int, window_size: int, depth: int +) -> Model[List[Floats2d], List[Floats2d]]: """Encode context using convolutions with mish activation, layer normalization and residual connections. @@ -256,7 +260,9 @@ def MishWindowEncoder(width: int, window_size: int, depth: int) -> Model[List[Fl @registry.architectures.register("spacy.TorchBiLSTMEncoder.v1") -def BiLSTMEncoder(width: int, depth: int, dropout: float) -> Model[List[Floats2d], List[Floats2d]]: +def BiLSTMEncoder( + width: int, depth: int, dropout: float +) -> Model[List[Floats2d], List[Floats2d]]: """Encode context using bidirectonal LSTM layers. Requires PyTorch. width (int): The input and output width. These are required to be the same,