mirror of
https://github.com/explosion/spaCy.git
synced 2025-06-29 17:33:10 +03:00
Fix tensorizer
This commit is contained in:
parent
0bf14082a4
commit
2527ba68e5
|
@ -532,7 +532,7 @@ def build_text_classifier(nr_class, width=64, **cfg):
|
||||||
vectors = trained_vectors
|
vectors = trained_vectors
|
||||||
vectors_width = width
|
vectors_width = width
|
||||||
static_vectors = None
|
static_vectors = None
|
||||||
cnn_model = (
|
tok2vec = (
|
||||||
vectors
|
vectors
|
||||||
>> with_flatten(
|
>> with_flatten(
|
||||||
LN(Maxout(width, vectors_width))
|
LN(Maxout(width, vectors_width))
|
||||||
|
@ -540,6 +540,9 @@ def build_text_classifier(nr_class, width=64, **cfg):
|
||||||
(ExtractWindow(nW=1) >> LN(Maxout(width, width*3)))
|
(ExtractWindow(nW=1) >> LN(Maxout(width, width*3)))
|
||||||
) ** depth, pad=depth
|
) ** depth, pad=depth
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
cnn_model = (
|
||||||
|
tok2vec
|
||||||
>> flatten_add_lengths
|
>> flatten_add_lengths
|
||||||
>> ParametricAttention(width)
|
>> ParametricAttention(width)
|
||||||
>> Pooling(sum_pool)
|
>> Pooling(sum_pool)
|
||||||
|
@ -556,6 +559,7 @@ def build_text_classifier(nr_class, width=64, **cfg):
|
||||||
>> zero_init(Affine(nr_class, nr_class*2, drop_factor=0.0))
|
>> zero_init(Affine(nr_class, nr_class*2, drop_factor=0.0))
|
||||||
>> logistic
|
>> logistic
|
||||||
)
|
)
|
||||||
|
model.tok2vec = tok2vec
|
||||||
model.nO = nr_class
|
model.nO = nr_class
|
||||||
model.lsuv = False
|
model.lsuv = False
|
||||||
return model
|
return model
|
||||||
|
|
|
@ -434,7 +434,7 @@ class Tensorizer(Pipe):
|
||||||
name = 'tensorizer'
|
name = 'tensorizer'
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def Model(cls, output_size=300, input_size=384, **cfg):
|
def Model(cls, output_size=300, input_size=128, **cfg):
|
||||||
"""Create a new statistical model for the class.
|
"""Create a new statistical model for the class.
|
||||||
|
|
||||||
width (int): Output size of the model.
|
width (int): Output size of the model.
|
||||||
|
@ -442,11 +442,7 @@ class Tensorizer(Pipe):
|
||||||
**cfg: Config parameters.
|
**cfg: Config parameters.
|
||||||
RETURNS (Model): A `thinc.neural.Model` or similar instance.
|
RETURNS (Model): A `thinc.neural.Model` or similar instance.
|
||||||
"""
|
"""
|
||||||
model = chain(
|
return zero_init(Affine(output_size, input_size))
|
||||||
SELU(output_size, input_size),
|
|
||||||
SELU(output_size, output_size),
|
|
||||||
zero_init(Affine(output_size, output_size)))
|
|
||||||
return model
|
|
||||||
|
|
||||||
def __init__(self, vocab, model=True, **cfg):
|
def __init__(self, vocab, model=True, **cfg):
|
||||||
"""Construct a new statistical model. Weights are not allocated on
|
"""Construct a new statistical model. Weights are not allocated on
|
||||||
|
@ -562,12 +558,11 @@ class Tensorizer(Pipe):
|
||||||
gold_tuples (iterable): Gold-standard training data.
|
gold_tuples (iterable): Gold-standard training data.
|
||||||
pipeline (list): The pipeline the model is part of.
|
pipeline (list): The pipeline the model is part of.
|
||||||
"""
|
"""
|
||||||
for name, model in pipeline:
|
if pipeline is not None:
|
||||||
if getattr(model, 'tok2vec', None):
|
for name, model in pipeline:
|
||||||
self.input_models.append(model.tok2vec)
|
if getattr(model, 'tok2vec', None):
|
||||||
|
self.input_models.append(model.tok2vec)
|
||||||
if self.model is True:
|
if self.model is True:
|
||||||
self.cfg['input_size'] = 384
|
|
||||||
self.cfg['output_size'] = 300
|
|
||||||
self.model = self.Model(**self.cfg)
|
self.model = self.Model(**self.cfg)
|
||||||
link_vectors_to_models(self.vocab)
|
link_vectors_to_models(self.vocab)
|
||||||
if sgd is None:
|
if sgd is None:
|
||||||
|
@ -1061,6 +1056,14 @@ class TextCategorizer(Pipe):
|
||||||
def Model(cls, nr_class, **cfg):
|
def Model(cls, nr_class, **cfg):
|
||||||
return build_text_classifier(nr_class, **cfg)
|
return build_text_classifier(nr_class, **cfg)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def tok2vec(self):
|
||||||
|
if self.model in (None, True, False):
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return chain(self.model.tok2vec, flatten)
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, vocab, model=True, **cfg):
|
def __init__(self, vocab, model=True, **cfg):
|
||||||
self.vocab = vocab
|
self.vocab = vocab
|
||||||
self.model = model
|
self.model = model
|
||||||
|
|
Loading…
Reference in New Issue
Block a user