From 140d53649d0db63d2e6f5c017131ce0b658adc78 Mon Sep 17 00:00:00 2001 From: Adriane Boyd Date: Fri, 31 Mar 2023 13:41:41 +0200 Subject: [PATCH] Convert values to numpy for label smoothing tests (#12472) --- spacy/tests/pipeline/test_morphologizer.py | 7 +++++-- spacy/tests/pipeline/test_tagger.py | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/spacy/tests/pipeline/test_morphologizer.py b/spacy/tests/pipeline/test_morphologizer.py index 8ce74ccfa..74c571ccf 100644 --- a/spacy/tests/pipeline/test_morphologizer.py +++ b/spacy/tests/pipeline/test_morphologizer.py @@ -1,6 +1,8 @@ import pytest from numpy.testing import assert_equal, assert_almost_equal +from thinc.api import get_current_ops + from spacy import util from spacy.training import Example from spacy.lang.en import English @@ -52,8 +54,9 @@ def test_label_smoothing(): tag_scores, bp_tag_scores = morph_ls.model.begin_update( [eg.predicted for eg in train_examples] ) - no_ls_grads = morph_no_ls.get_loss(train_examples, tag_scores)[1][0] - ls_grads = morph_ls.get_loss(train_examples, tag_scores)[1][0] + ops = get_current_ops() + no_ls_grads = ops.to_numpy(morph_no_ls.get_loss(train_examples, tag_scores)[1][0]) + ls_grads = ops.to_numpy(morph_ls.get_loss(train_examples, tag_scores)[1][0]) assert_almost_equal(ls_grads / no_ls_grads, 0.94285715) diff --git a/spacy/tests/pipeline/test_tagger.py b/spacy/tests/pipeline/test_tagger.py index 0cc25a64b..746f32ee3 100644 --- a/spacy/tests/pipeline/test_tagger.py +++ b/spacy/tests/pipeline/test_tagger.py @@ -6,7 +6,7 @@ from spacy import util from spacy.training import Example from spacy.lang.en import English from spacy.language import Language -from thinc.api import compounding +from thinc.api import compounding, get_current_ops from ..util import make_tempdir @@ -85,8 +85,9 @@ def test_label_smoothing(): tag_scores, bp_tag_scores = tagger_ls.model.begin_update( [eg.predicted for eg in train_examples] ) - no_ls_grads = tagger_no_ls.get_loss(train_examples, tag_scores)[1][0] - ls_grads = tagger_ls.get_loss(train_examples, tag_scores)[1][0] + ops = get_current_ops() + no_ls_grads = ops.to_numpy(tagger_no_ls.get_loss(train_examples, tag_scores)[1][0]) + ls_grads = ops.to_numpy(tagger_ls.get_loss(train_examples, tag_scores)[1][0]) assert_almost_equal(ls_grads / no_ls_grads, 0.925)