From a18659630788e40eb7aa3c729160b6fb4da07a09 Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Fri, 22 Sep 2017 09:37:03 -0500 Subject: [PATCH] Add 'reapply' combinator, for iterated CNN --- spacy/_ml.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/spacy/_ml.py b/spacy/_ml.py index c139623c1..91640c14a 100644 --- a/spacy/_ml.py +++ b/spacy/_ml.py @@ -271,6 +271,28 @@ def Tok2Vec(width, embed_size, pretrained_dims=0, **kwargs): return tok2vec +def reapply(layer, n_times): + def reapply_fwd(X, drop=0.): + backprops = [] + for i in range(n_times): + Y, backprop = layer.begin_update(X, drop=drop) + X = Y + backprops.append(backprop) + def reapply_bwd(dY, sgd=None): + dX = None + for backprop in reversed(backprops): + dY = backprop(dY, sgd=sgd) + if dX is None: + dX = dY + else: + dX += dY + return dX + return Y, reapply_bwd + return wrap(reapply_fwd, layer) + + + + def asarray(ops, dtype): def forward(X, drop=0.): return ops.asarray(X, dtype=dtype), None