2019-10-31 14:01:15 +00:00
|
|
|
import pytest
|
|
|
|
|
2020-02-27 17:42:27 +00:00
|
|
|
from spacy.ml.models.tok2vec import build_Tok2Vec_model
|
2019-10-31 14:01:15 +00:00
|
|
|
from spacy.vocab import Vocab
|
|
|
|
from spacy.tokens import Doc
|
|
|
|
|
2020-03-29 17:40:36 +00:00
|
|
|
from .util import get_batch
|
2019-10-31 14:01:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
# This fails in Thinc v7.3.1. Need to push patch
|
|
|
|
@pytest.mark.xfail
|
|
|
|
def test_empty_doc():
|
|
|
|
width = 128
|
|
|
|
embed_size = 2000
|
|
|
|
vocab = Vocab()
|
|
|
|
doc = Doc(vocab, words=[])
|
2020-02-27 17:42:27 +00:00
|
|
|
# TODO: fix tok2vec arguments
|
2020-06-03 09:50:16 +00:00
|
|
|
tok2vec = build_Tok2Vec_model(width, embed_size, dropout=None)
|
2019-10-31 14:01:15 +00:00
|
|
|
vectors, backprop = tok2vec.begin_update([doc])
|
|
|
|
assert len(vectors) == 1
|
|
|
|
assert vectors[0].shape == (0, width)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"batch_size,width,embed_size", [[1, 128, 2000], [2, 128, 2000], [3, 8, 63]]
|
|
|
|
)
|
|
|
|
def test_tok2vec_batch_sizes(batch_size, width, embed_size):
|
|
|
|
batch = get_batch(batch_size)
|
2020-02-27 17:42:27 +00:00
|
|
|
tok2vec = build_Tok2Vec_model(
|
|
|
|
width,
|
|
|
|
embed_size,
|
|
|
|
pretrained_vectors=None,
|
|
|
|
conv_depth=4,
|
|
|
|
bilstm_depth=0,
|
|
|
|
window_size=1,
|
|
|
|
maxout_pieces=3,
|
|
|
|
subword_features=True,
|
|
|
|
char_embed=False,
|
|
|
|
nM=64,
|
|
|
|
nC=8,
|
2020-06-03 09:50:16 +00:00
|
|
|
dropout=None,
|
2020-02-27 17:42:27 +00:00
|
|
|
)
|
2020-01-29 16:06:46 +00:00
|
|
|
tok2vec.initialize()
|
2019-10-31 14:01:15 +00:00
|
|
|
vectors, backprop = tok2vec.begin_update(batch)
|
|
|
|
assert len(vectors) == len(batch)
|
|
|
|
for doc_vec, doc in zip(vectors, batch):
|
|
|
|
assert doc_vec.shape == (len(doc), width)
|
|
|
|
|
|
|
|
|
2020-02-27 17:42:27 +00:00
|
|
|
# fmt: off
|
2019-10-31 14:01:15 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"tok2vec_config",
|
|
|
|
[
|
2020-06-03 09:50:16 +00:00
|
|
|
{"width": 8, "embed_size": 100, "char_embed": False, "nM": 64, "nC": 8, "pretrained_vectors": None, "window_size": 1, "conv_depth": 2, "bilstm_depth": 0, "maxout_pieces": 3, "subword_features": True, "dropout": None},
|
|
|
|
{"width": 8, "embed_size": 100, "char_embed": True, "nM": 64, "nC": 8, "pretrained_vectors": None, "window_size": 1, "conv_depth": 2, "bilstm_depth": 0, "maxout_pieces": 3, "subword_features": True, "dropout": None},
|
|
|
|
{"width": 8, "embed_size": 100, "char_embed": False, "nM": 64, "nC": 8, "pretrained_vectors": None, "window_size": 1, "conv_depth": 6, "bilstm_depth": 0, "maxout_pieces": 3, "subword_features": True, "dropout": None},
|
|
|
|
{"width": 8, "embed_size": 100, "char_embed": False, "nM": 64, "nC": 8, "pretrained_vectors": None, "window_size": 1, "conv_depth": 6, "bilstm_depth": 0, "maxout_pieces": 3, "subword_features": True, "dropout": None},
|
|
|
|
{"width": 8, "embed_size": 100, "char_embed": False, "nM": 64, "nC": 8, "pretrained_vectors": None, "window_size": 1, "conv_depth": 2, "bilstm_depth": 0, "maxout_pieces": 3, "subword_features": False, "dropout": None},
|
|
|
|
{"width": 8, "embed_size": 100, "char_embed": False, "nM": 64, "nC": 8, "pretrained_vectors": None, "window_size": 3, "conv_depth": 2, "bilstm_depth": 0, "maxout_pieces": 3, "subword_features": False, "dropout": None},
|
|
|
|
{"width": 8, "embed_size": 100, "char_embed": True, "nM": 81, "nC": 8, "pretrained_vectors": None, "window_size": 3, "conv_depth": 2, "bilstm_depth": 0, "maxout_pieces": 3, "subword_features": False, "dropout": None},
|
|
|
|
{"width": 8, "embed_size": 100, "char_embed": True, "nM": 81, "nC": 9, "pretrained_vectors": None, "window_size": 3, "conv_depth": 2, "bilstm_depth": 0, "maxout_pieces": 3, "subword_features": False, "dropout": None},
|
2019-10-31 14:01:15 +00:00
|
|
|
],
|
|
|
|
)
|
2020-02-27 17:42:27 +00:00
|
|
|
# fmt: on
|
2019-10-31 14:01:15 +00:00
|
|
|
def test_tok2vec_configs(tok2vec_config):
|
|
|
|
docs = get_batch(3)
|
2020-02-27 17:42:27 +00:00
|
|
|
tok2vec = build_Tok2Vec_model(**tok2vec_config)
|
2020-03-29 17:40:36 +00:00
|
|
|
tok2vec.initialize(docs)
|
2019-10-31 14:01:15 +00:00
|
|
|
vectors, backprop = tok2vec.begin_update(docs)
|
|
|
|
assert len(vectors) == len(docs)
|
|
|
|
assert vectors[0].shape == (len(docs[0]), tok2vec_config["width"])
|
|
|
|
backprop(vectors)
|