mirror of
https://github.com/explosion/spaCy.git
synced 2024-12-27 02:16:32 +03:00
e796aab4b3
* implement textcat resizing for TextCatCNN * resizing textcat in-place * simplify code * ensure predictions for old textcat labels remain the same after resizing (WIP) * fix for softmax * store softmax as attr * fix ensemble weight copy and cleanup * restructure slightly * adjust documentation, update tests and quickstart templates to use latest versions * extend unit test slightly * revert unnecessary edits * fix typo * ensemble architecture won't be resizable for now * use resizable layer (WIP) * revert using resizable layer * resizable container while avoid shape inference trouble * cleanup * ensure model continues training after resizing * use fill_b parameter * use fill_defaults * resize_layer callback * format * bump thinc to 8.0.4 * bump spacy-legacy to 3.0.6
49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
from thinc.api import Model, noop
|
|
from .parser_model import ParserStepModel
|
|
|
|
|
|
def TransitionModel(
|
|
tok2vec, lower, upper, resize_output, dropout=0.2, unseen_classes=set()
|
|
):
|
|
"""Set up a stepwise transition-based model"""
|
|
if upper is None:
|
|
has_upper = False
|
|
upper = noop()
|
|
else:
|
|
has_upper = True
|
|
# don't define nO for this object, because we can't dynamically change it
|
|
return Model(
|
|
name="parser_model",
|
|
forward=forward,
|
|
dims={"nI": tok2vec.maybe_get_dim("nI")},
|
|
layers=[tok2vec, lower, upper],
|
|
refs={"tok2vec": tok2vec, "lower": lower, "upper": upper},
|
|
init=init,
|
|
attrs={
|
|
"has_upper": has_upper,
|
|
"unseen_classes": set(unseen_classes),
|
|
"resize_output": resize_output,
|
|
},
|
|
)
|
|
|
|
|
|
def forward(model, X, is_train):
|
|
step_model = ParserStepModel(
|
|
X,
|
|
model.layers,
|
|
unseen_classes=model.attrs["unseen_classes"],
|
|
train=is_train,
|
|
has_upper=model.attrs["has_upper"],
|
|
)
|
|
|
|
return step_model, step_model.finish_steps
|
|
|
|
|
|
def init(model, X=None, Y=None):
|
|
model.get_ref("tok2vec").initialize(X=X)
|
|
lower = model.get_ref("lower")
|
|
lower.initialize()
|
|
if model.attrs["has_upper"]:
|
|
statevecs = model.ops.alloc2f(2, lower.get_dim("nO"))
|
|
model.get_ref("upper").initialize(X=statevecs)
|