From d0b041aff422e6c785c901bd13ca8b5869262371 Mon Sep 17 00:00:00 2001 From: Paul O'Leary McCann Date: Thu, 8 Jul 2021 16:08:36 +0900 Subject: [PATCH] Switch to using Thinc tuplify The tuplify code here was added to Thinc proper and that's been released, so no need to have it here any more. --- spacy/ml/models/coref.py | 58 ++-------------------------------------- 1 file changed, 2 insertions(+), 56 deletions(-) diff --git a/spacy/ml/models/coref.py b/spacy/ml/models/coref.py index 31643d248..719750ecb 100644 --- a/spacy/ml/models/coref.py +++ b/spacy/ml/models/coref.py @@ -1,7 +1,8 @@ from dataclasses import dataclass import warnings -from thinc.api import Model, Linear, Relu, Dropout, chain, noop, Embed, add +from thinc.api import Model, Linear, Relu, Dropout +from thinc.api import chain, noop, Embed, add, tuplify from thinc.types import Floats2d, Floats1d, Ints2d, Ragged from typing import List, Callable, Tuple, Any from ...tokens import Doc @@ -56,61 +57,6 @@ def build_coref( return model -# TODO replace this with thinc version once PR is in -def tuplify(layer1: Model, layer2: Model, *layers) -> Model: - layers = (layer1, layer2) + layers - names = [layer.name for layer in layers] - return Model( - "tuple(" + ", ".join(names) + ")", - tuplify_forward, - init=tuplify_init, - layers=layers, - ) - - -# TODO replace this with thinc version once PR is in -def tuplify_forward(model, X, is_train): - Ys = [] - backprops = [] - for layer in model.layers: - Y, backprop = layer(X, is_train) - Ys.append(Y) - backprops.append(backprop) - - def backprop_tuplify(dYs): - dXs = [bp(dY) for bp, dY in zip(backprops, dYs)] - dX = dXs[0] - for dx in dXs[1:]: - dX += dx - return dX - - return tuple(Ys), backprop_tuplify - - -# TODO replace this with thinc version once PR is in -def tuplify_init(model, X, Y) -> Model: - if X is None and Y is None: - for layer in model.layers: - layer.initialize() - if model.layers[0].has_dim("nI"): - model.set_dim("nI", model.layers[0].get_dim("nI")) - return model - - # Try to set nO on each layer, where available. - # All layers have the same input, and the output should map directly from the - # given Y, if provided. - for ii, layer in enumerate(model.layers): - if Y is not None and layer.has_dim("nO") is None: - layer.initialize(X=X, Y=Y[ii]) - else: - layer.initialize(X=X) - - if model.layers[0].has_dim("nI"): - model.set_dim("nI", model.layers[0].get_dim("nI")) - # this model can have an input dimension, but can't have an output dimension - return model - - @dataclass class SpanEmbeddings: indices: Ints2d # Array with 2 columns (for start and end index)