mirror of
https://github.com/explosion/spaCy.git
synced 2024-11-13 05:07:03 +03:00
Remove dead code from spacy._ml
This commit is contained in:
parent
b9616419e1
commit
f6fef30adc
71
spacy/_ml.py
71
spacy/_ml.py
|
@ -348,58 +348,12 @@ def reapply(layer, n_times):
|
||||||
return wrap(reapply_fwd, layer)
|
return wrap(reapply_fwd, layer)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def asarray(ops, dtype):
|
def asarray(ops, dtype):
|
||||||
def forward(X, drop=0.):
|
def forward(X, drop=0.):
|
||||||
return ops.asarray(X, dtype=dtype), None
|
return ops.asarray(X, dtype=dtype), None
|
||||||
return layerize(forward)
|
return layerize(forward)
|
||||||
|
|
||||||
|
|
||||||
def foreach(layer):
|
|
||||||
def forward(Xs, drop=0.):
|
|
||||||
results = []
|
|
||||||
backprops = []
|
|
||||||
for X in Xs:
|
|
||||||
result, bp = layer.begin_update(X, drop=drop)
|
|
||||||
results.append(result)
|
|
||||||
backprops.append(bp)
|
|
||||||
def backward(d_results, sgd=None):
|
|
||||||
dXs = []
|
|
||||||
for d_result, backprop in zip(d_results, backprops):
|
|
||||||
dXs.append(backprop(d_result, sgd))
|
|
||||||
return dXs
|
|
||||||
return results, backward
|
|
||||||
model = layerize(forward)
|
|
||||||
model._layers.append(layer)
|
|
||||||
return model
|
|
||||||
|
|
||||||
|
|
||||||
def rebatch(size, layer):
|
|
||||||
ops = layer.ops
|
|
||||||
def forward(X, drop=0.):
|
|
||||||
if X.shape[0] < size:
|
|
||||||
return layer.begin_update(X)
|
|
||||||
parts = _divide_array(X, size)
|
|
||||||
results, bp_results = zip(*[layer.begin_update(p, drop=drop)
|
|
||||||
for p in parts])
|
|
||||||
y = ops.flatten(results)
|
|
||||||
def backward(dy, sgd=None):
|
|
||||||
d_parts = [bp(y, sgd=sgd) for bp, y in
|
|
||||||
zip(bp_results, _divide_array(dy, size))]
|
|
||||||
try:
|
|
||||||
dX = ops.flatten(d_parts)
|
|
||||||
except TypeError:
|
|
||||||
dX = None
|
|
||||||
except ValueError:
|
|
||||||
dX = None
|
|
||||||
return dX
|
|
||||||
return y, backward
|
|
||||||
model = layerize(forward)
|
|
||||||
model._layers.append(layer)
|
|
||||||
return model
|
|
||||||
|
|
||||||
|
|
||||||
def _divide_array(X, size):
|
def _divide_array(X, size):
|
||||||
parts = []
|
parts = []
|
||||||
index = 0
|
index = 0
|
||||||
|
@ -508,11 +462,13 @@ def preprocess_doc(docs, drop=0.):
|
||||||
vals = ops.allocate(keys.shape[0]) + 1
|
vals = ops.allocate(keys.shape[0]) + 1
|
||||||
return (keys, vals, lengths), None
|
return (keys, vals, lengths), None
|
||||||
|
|
||||||
|
|
||||||
def getitem(i):
|
def getitem(i):
|
||||||
def getitem_fwd(X, drop=0.):
|
def getitem_fwd(X, drop=0.):
|
||||||
return X[i], None
|
return X[i], None
|
||||||
return layerize(getitem_fwd)
|
return layerize(getitem_fwd)
|
||||||
|
|
||||||
|
|
||||||
def build_tagger_model(nr_class, **cfg):
|
def build_tagger_model(nr_class, **cfg):
|
||||||
embed_size = util.env_opt('embed_size', 7000)
|
embed_size = util.env_opt('embed_size', 7000)
|
||||||
if 'token_vector_width' in cfg:
|
if 'token_vector_width' in cfg:
|
||||||
|
@ -552,29 +508,6 @@ def SpacyVectors(docs, drop=0.):
|
||||||
return batch, None
|
return batch, None
|
||||||
|
|
||||||
|
|
||||||
def foreach(layer, drop_factor=1.0):
|
|
||||||
'''Map a layer across elements in a list'''
|
|
||||||
def foreach_fwd(Xs, drop=0.):
|
|
||||||
drop *= drop_factor
|
|
||||||
ys = []
|
|
||||||
backprops = []
|
|
||||||
for X in Xs:
|
|
||||||
y, bp_y = layer.begin_update(X, drop=drop)
|
|
||||||
ys.append(y)
|
|
||||||
backprops.append(bp_y)
|
|
||||||
def foreach_bwd(d_ys, sgd=None):
|
|
||||||
d_Xs = []
|
|
||||||
for d_y, bp_y in zip(d_ys, backprops):
|
|
||||||
if bp_y is not None and bp_y is not None:
|
|
||||||
d_Xs.append(d_y, sgd=sgd)
|
|
||||||
else:
|
|
||||||
d_Xs.append(None)
|
|
||||||
return d_Xs
|
|
||||||
return ys, foreach_bwd
|
|
||||||
model = wrap(foreach_fwd, layer)
|
|
||||||
return model
|
|
||||||
|
|
||||||
|
|
||||||
def build_text_classifier(nr_class, width=64, **cfg):
|
def build_text_classifier(nr_class, width=64, **cfg):
|
||||||
nr_vector = cfg.get('nr_vector', 5000)
|
nr_vector = cfg.get('nr_vector', 5000)
|
||||||
pretrained_dims = cfg.get('pretrained_dims', 0)
|
pretrained_dims = cfg.get('pretrained_dims', 0)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user