From f65e36925d5c797faa5a48bf1d3820a376c77b26 Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Tue, 20 Aug 2019 15:08:59 +0200 Subject: [PATCH] Fix absolute imports and avoid importing from cli --- spacy/_ml.py | 15 +++++++++++++++ spacy/cli/pretrain.py | 19 ++----------------- spacy/pipeline/pipes.pyx | 6 ++---- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/spacy/_ml.py b/spacy/_ml.py index 0411c4bd4..660d20c46 100644 --- a/spacy/_ml.py +++ b/spacy/_ml.py @@ -808,3 +808,18 @@ def _replace_word(word, random_words, mask="[MASK]"): return random_words.next() else: return word + + +def get_cossim_loss(yh, y): + # Add a small constant to avoid 0 vectors + yh = yh + 1e-8 + y = y + 1e-8 + # https://math.stackexchange.com/questions/1923613/partial-derivative-of-cosine-similarity + xp = get_array_module(yh) + norm_yh = xp.linalg.norm(yh, axis=1, keepdims=True) + norm_y = xp.linalg.norm(y, axis=1, keepdims=True) + mul_norms = norm_yh * norm_y + cosine = (yh * y).sum(axis=1, keepdims=True) / mul_norms + d_yh = (y / mul_norms) - (cosine * (yh / norm_yh ** 2)) + loss = xp.abs(cosine - 1).sum() + return loss, -d_yh diff --git a/spacy/cli/pretrain.py b/spacy/cli/pretrain.py index 99ea5dadc..e78cdc9b8 100644 --- a/spacy/cli/pretrain.py +++ b/spacy/cli/pretrain.py @@ -10,7 +10,7 @@ from collections import Counter from pathlib import Path from thinc.v2v import Affine, Maxout from thinc.misc import LayerNorm as LN -from thinc.neural.util import prefer_gpu, get_array_module +from thinc.neural.util import prefer_gpu from wasabi import Printer import srsly @@ -18,7 +18,7 @@ from ..errors import Errors from ..tokens import Doc from ..attrs import ID, HEAD from .._ml import Tok2Vec, flatten, chain, create_default_optimizer -from .._ml import masked_language_model +from .._ml import masked_language_model, get_cossim_loss from .. import util from .train import _load_pretrained_tok2vec @@ -307,21 +307,6 @@ def get_vectors_loss(ops, docs, prediction, objective="L2"): return loss, d_target -def get_cossim_loss(yh, y): - # Add a small constant to avoid 0 vectors - yh = yh + 1e-8 - y = y + 1e-8 - # https://math.stackexchange.com/questions/1923613/partial-derivative-of-cosine-similarity - xp = get_array_module(yh) - norm_yh = xp.linalg.norm(yh, axis=1, keepdims=True) - norm_y = xp.linalg.norm(y, axis=1, keepdims=True) - mul_norms = norm_yh * norm_y - cosine = (yh * y).sum(axis=1, keepdims=True) / mul_norms - d_yh = (y / mul_norms) - (cosine * (yh / norm_yh ** 2)) - loss = xp.abs(cosine - 1).sum() - return loss, -d_yh - - def create_pretraining_model(nlp, tok2vec): """Define a network for the pretraining. We simply add an output layer onto the tok2vec input model. The tok2vec input model needs to be a model that diff --git a/spacy/pipeline/pipes.pyx b/spacy/pipeline/pipes.pyx index db45e0faa..8b15ebd38 100644 --- a/spacy/pipeline/pipes.pyx +++ b/spacy/pipeline/pipes.pyx @@ -13,9 +13,6 @@ from thinc.misc import LayerNorm from thinc.neural.util import to_categorical from thinc.neural.util import get_array_module -from spacy.kb import KnowledgeBase - -from spacy.cli.pretrain import get_cossim_loss from .functions import merge_subtokens from ..tokens.doc cimport Doc from ..syntax.nn_parser cimport Parser @@ -27,7 +24,8 @@ from ..vocab cimport Vocab from ..syntax import nonproj from ..attrs import POS, ID from ..parts_of_speech import X -from .._ml import Tok2Vec, build_tagger_model, cosine +from ..kb import KnowledgeBase +from .._ml import Tok2Vec, build_tagger_model, cosine, get_cossim_loss from .._ml import build_text_classifier, build_simple_cnn_text_classifier from .._ml import build_bow_text_classifier, build_nel_encoder from .._ml import link_vectors_to_models, zero_init, flatten