2020-02-27 17:42:27 +00:00
|
|
|
from pydantic import StrictInt
|
2020-05-18 20:23:33 +00:00
|
|
|
from thinc.api import Model, chain, list2array, Linear, zero_init, use_ops, with_array
|
2020-02-27 17:42:27 +00:00
|
|
|
|
2020-02-28 10:57:41 +00:00
|
|
|
from ...util import registry
|
2020-03-29 17:40:36 +00:00
|
|
|
from .._precomputable_affine import PrecomputableAffine
|
2020-05-18 20:23:33 +00:00
|
|
|
from ..tb_framework import TransitionModel
|
2020-02-28 10:57:41 +00:00
|
|
|
|
2020-02-27 17:42:27 +00:00
|
|
|
|
|
|
|
@registry.architectures.register("spacy.TransitionBasedParser.v1")
|
|
|
|
def build_tb_parser_model(
|
|
|
|
tok2vec: Model,
|
|
|
|
nr_feature_tokens: StrictInt,
|
|
|
|
hidden_width: StrictInt,
|
|
|
|
maxout_pieces: StrictInt,
|
2020-05-18 20:23:33 +00:00
|
|
|
use_upper=True,
|
2020-02-27 17:42:27 +00:00
|
|
|
nO=None,
|
|
|
|
):
|
|
|
|
token_vector_width = tok2vec.get_dim("nO")
|
2020-05-18 20:23:33 +00:00
|
|
|
tok2vec = chain(
|
|
|
|
tok2vec,
|
|
|
|
with_array(Linear(hidden_width, token_vector_width)),
|
|
|
|
list2array(),
|
|
|
|
)
|
|
|
|
tok2vec.set_dim("nO", hidden_width)
|
2020-02-27 17:42:27 +00:00
|
|
|
|
|
|
|
lower = PrecomputableAffine(
|
2020-05-18 20:23:33 +00:00
|
|
|
nO=hidden_width if use_upper else nO,
|
2020-02-27 17:42:27 +00:00
|
|
|
nF=nr_feature_tokens,
|
|
|
|
nI=tok2vec.get_dim("nO"),
|
2020-05-18 20:23:33 +00:00
|
|
|
nP=maxout_pieces
|
2020-02-27 17:42:27 +00:00
|
|
|
)
|
2020-05-18 20:23:33 +00:00
|
|
|
if use_upper:
|
|
|
|
with use_ops("numpy"):
|
|
|
|
# Initialize weights at zero, as it's a classification layer.
|
|
|
|
upper = Linear(nO=nO, init_W=zero_init)
|
|
|
|
else:
|
|
|
|
upper = None
|
|
|
|
return TransitionModel(tok2vec, lower, upper)
|