spaCy/spacy/vectors.pyx

475 lines
17 KiB
Cython
Raw Normal View History

cimport numpy as np
from cython.operator cimport dereference as deref
from libcpp.set cimport set as cppset
import functools
2017-06-05 13:32:08 +03:00
import numpy
import srsly
2020-02-18 17:38:18 +03:00
from thinc.api import get_array_module, get_current_ops
2017-06-05 13:32:08 +03:00
from .strings cimport StringStore
from .strings import get_string_id
from .errors import Errors
2017-10-27 20:45:19 +03:00
from . import util
2017-06-05 13:32:08 +03:00
2018-03-11 00:53:42 +03:00
def unpickle_vectors(bytes_data):
return Vectors().from_bytes(bytes_data)
2017-10-31 20:25:08 +03:00
2020-07-12 15:03:23 +03:00
class GlobalRegistry:
"""Global store of vectors, to avoid repeatedly loading the data."""
data = {}
@classmethod
def register(cls, name, data):
cls.data[name] = data
return functools.partial(cls.get, name)
@classmethod
def get(cls, name):
return cls.data[name]
2017-06-05 13:32:08 +03:00
cdef class Vectors:
2017-10-27 20:45:19 +03:00
"""Store, save and load word vectors.
2017-10-02 01:05:54 +03:00
2017-10-01 23:10:33 +03:00
Vectors data is kept in the vectors.data attribute, which should be an
2017-10-27 20:45:19 +03:00
instance of numpy.ndarray (for CPU vectors) or cupy.ndarray
(for GPU vectors). `vectors.key2row` is a dictionary mapping word hashes to
rows in the vectors.data table.
2017-11-01 01:23:34 +03:00
2017-10-31 20:25:08 +03:00
Multiple keys can be mapped to the same vector, and not all of the rows in
the table need to be assigned - so len(list(vectors.keys())) may be
2017-10-31 20:25:08 +03:00
greater or smaller than vectors.shape[0].
DOCS: https://spacy.io/api/vectors
2017-10-27 20:45:19 +03:00
"""
cdef public object name
2017-06-05 13:32:08 +03:00
cdef public object data
2017-08-19 05:33:03 +03:00
cdef public object key2row
cdef cppset[int] _unset
2017-06-05 13:32:08 +03:00
def __init__(self, *, shape=None, data=None, keys=None, name=None):
2017-10-31 20:25:08 +03:00
"""Create a new vector store.
2017-11-01 01:23:34 +03:00
2017-10-31 20:25:08 +03:00
shape (tuple): Size of the table, as (# entries, # columns)
2017-10-27 20:45:19 +03:00
data (numpy.ndarray): The vector data.
2017-11-01 01:23:34 +03:00
keys (iterable): A sequence of keys, aligned with the data.
2020-05-24 18:20:58 +03:00
name (str): A name to identify the vectors table.
DOCS: https://spacy.io/api/vectors#init
2017-10-27 20:45:19 +03:00
"""
self.name = name
2017-10-31 20:25:08 +03:00
if data is None:
if shape is None:
shape = (0,0)
data = numpy.zeros(shape, dtype="f")
2017-10-31 20:25:08 +03:00
self.data = data
self.key2row = {}
2017-10-31 20:25:08 +03:00
if self.data is not None:
self._unset = cppset[int]({i for i in range(self.data.shape[0])})
2017-06-05 13:32:08 +03:00
else:
self._unset = cppset[int]()
2017-10-31 20:25:08 +03:00
if keys is not None:
for i, key in enumerate(keys):
self.add(key, row=i)
2017-11-01 01:23:34 +03:00
2017-10-31 20:25:08 +03:00
@property
def shape(self):
"""Get `(rows, dims)` tuples of number of rows and number of dimensions
in the vector table.
RETURNS (tuple): A `(rows, dims)` pair.
DOCS: https://spacy.io/api/vectors#shape
2017-10-31 20:25:08 +03:00
"""
return self.data.shape
@property
def size(self):
"""The vector size i,e. rows * dims.
RETURNS (int): The vector size.
DOCS: https://spacy.io/api/vectors#size
"""
2017-10-31 20:25:08 +03:00
return self.data.shape[0] * self.data.shape[1]
@property
def is_full(self):
"""Whether the vectors table is full.
RETURNS (bool): `True` if no slots are available for new keys.
DOCS: https://spacy.io/api/vectors#is_full
"""
return self._unset.size() == 0
2017-06-05 13:32:08 +03:00
2017-10-31 21:30:52 +03:00
@property
def n_keys(self):
"""Get the number of keys in the table. Note that this is the number
of all keys, not just unique vectors.
RETURNS (int): The number of keys in the table.
DOCS: https://spacy.io/api/vectors#n_keys
"""
2017-10-31 21:30:52 +03:00
return len(self.key2row)
2017-06-05 13:32:08 +03:00
def __reduce__(self):
2018-03-11 00:53:42 +03:00
return (unpickle_vectors, (self.to_bytes(),))
2017-06-05 13:32:08 +03:00
def __getitem__(self, key):
2017-10-31 20:25:08 +03:00
"""Get a vector by key. If the key is not found, a KeyError is raised.
2017-10-01 23:10:33 +03:00
2017-10-31 20:25:08 +03:00
key (int): The key to get the vector for.
RETURNS (ndarray): The vector for the key.
DOCS: https://spacy.io/api/vectors#getitem
2017-10-27 20:45:19 +03:00
"""
2017-08-19 05:33:03 +03:00
i = self.key2row[key]
2017-06-05 13:32:08 +03:00
if i is None:
raise KeyError(Errors.E058.format(key=key))
2017-06-05 13:32:08 +03:00
else:
return self.data[i]
def __setitem__(self, key, vector):
2017-10-31 20:25:08 +03:00
"""Set a vector for the given key.
2017-10-27 20:45:19 +03:00
2017-10-31 20:25:08 +03:00
key (int): The key to set the vector for.
2017-11-01 01:23:34 +03:00
vector (ndarray): The vector to set.
DOCS: https://spacy.io/api/vectors#setitem
2017-10-27 20:45:19 +03:00
"""
2017-08-19 05:33:03 +03:00
i = self.key2row[key]
2017-06-05 13:32:08 +03:00
self.data[i] = vector
if self._unset.count(i):
self._unset.erase(self._unset.find(i))
2017-06-05 13:32:08 +03:00
def __iter__(self):
2017-11-01 01:23:34 +03:00
"""Iterate over the keys in the table.
2017-10-27 20:45:19 +03:00
2017-11-01 01:23:34 +03:00
YIELDS (int): A key in the table.
DOCS: https://spacy.io/api/vectors#iter
2017-10-27 20:45:19 +03:00
"""
2017-10-31 20:25:08 +03:00
yield from self.key2row
2017-06-05 13:32:08 +03:00
def __len__(self):
2017-10-31 20:25:08 +03:00
"""Return the number of vectors in the table.
2017-10-27 20:45:19 +03:00
RETURNS (int): The number of vectors in the data.
DOCS: https://spacy.io/api/vectors#len
2017-10-27 20:45:19 +03:00
"""
2017-10-31 20:25:08 +03:00
return self.data.shape[0]
2017-08-19 20:52:25 +03:00
def __contains__(self, key):
2017-10-31 20:25:08 +03:00
"""Check whether a key has been mapped to a vector entry in the table.
2017-10-27 20:45:19 +03:00
2017-10-31 20:25:08 +03:00
key (int): The key to check.
2017-10-27 20:45:19 +03:00
RETURNS (bool): Whether the key has a vector entry.
DOCS: https://spacy.io/api/vectors#contains
2017-10-27 20:45:19 +03:00
"""
2017-08-19 20:52:25 +03:00
return key in self.key2row
2017-10-31 20:25:08 +03:00
def resize(self, shape, inplace=False):
2017-11-01 01:23:34 +03:00
"""Resize the underlying vectors array. If inplace=True, the memory
2017-10-31 20:25:08 +03:00
is reallocated. This may cause other references to the data to become
invalid, so only use inplace=True if you're sure that's what you want.
If the number of vectors is reduced, keys mapped to rows that have been
deleted are removed. These removed items are returned as a list of
2017-11-01 01:23:34 +03:00
`(key, row)` tuples.
shape (tuple): A `(rows, dims)` tuple.
inplace (bool): Reallocate the memory.
RETURNS (list): The removed items as a list of `(key, row)` tuples.
DOCS: https://spacy.io/api/vectors#resize
2017-11-01 01:23:34 +03:00
"""
xp = get_array_module(self.data)
2017-10-31 20:25:08 +03:00
if inplace:
if shape[1] != self.data.shape[1]:
raise ValueError(Errors.E193.format(new_dim=shape[1], curr_dim=self.data.shape[1]))
if xp == numpy:
self.data.resize(shape, refcheck=False)
else:
raise ValueError(Errors.E192)
2017-10-31 20:25:08 +03:00
else:
resized_array = xp.zeros(shape, dtype=self.data.dtype)
copy_shape = (min(shape[0], self.data.shape[0]), min(shape[1], self.data.shape[1]))
resized_array[:copy_shape[0], :copy_shape[1]] = self.data[:copy_shape[0], :copy_shape[1]]
self.data = resized_array
self._sync_unset()
2017-10-31 20:25:08 +03:00
removed_items = []
for key, row in list(self.key2row.items()):
2017-10-31 20:25:08 +03:00
if row >= shape[0]:
self.key2row.pop(key)
removed_items.append((key, row))
return removed_items
2017-11-01 01:23:34 +03:00
2017-10-31 20:25:08 +03:00
def keys(self):
"""RETURNS (iterable): A sequence of keys in the table."""
2017-11-01 01:23:34 +03:00
return self.key2row.keys()
2017-10-31 20:25:08 +03:00
def values(self):
2017-11-01 01:23:34 +03:00
"""Iterate over vectors that have been assigned to at least one key.
2017-10-31 20:25:08 +03:00
Note that some vectors may be unassigned, so the number of vectors
2017-11-01 01:23:34 +03:00
returned may be less than the length of the vectors table.
YIELDS (ndarray): A vector in the table.
DOCS: https://spacy.io/api/vectors#values
2017-11-01 01:23:34 +03:00
"""
2017-10-31 20:25:08 +03:00
for row, vector in enumerate(range(self.data.shape[0])):
if not self._unset.count(row):
2017-10-31 20:25:08 +03:00
yield vector
def items(self):
"""Iterate over `(key, vector)` pairs.
YIELDS (tuple): A key/vector pair.
DOCS: https://spacy.io/api/vectors#items
2017-10-31 20:25:08 +03:00
"""
for key, row in self.key2row.items():
yield key, self.data[row]
2017-11-01 02:34:55 +03:00
def find(self, *, key=None, keys=None, row=None, rows=None):
2017-11-01 02:42:39 +03:00
"""Look up one or more keys by row, or vice versa.
2017-11-01 02:34:55 +03:00
2020-05-24 18:20:58 +03:00
key (str / int): Find the row that the given key points to.
2017-11-01 02:34:55 +03:00
Returns int, -1 if missing.
2017-11-01 02:42:39 +03:00
keys (iterable): Find rows that the keys point to.
2017-11-01 02:34:55 +03:00
Returns ndarray.
2019-03-16 19:10:57 +03:00
row (int): Find the first key that points to the row.
2017-11-01 02:34:55 +03:00
Returns int.
2017-11-01 02:42:39 +03:00
rows (iterable): Find the keys that point to the rows.
2017-11-01 02:34:55 +03:00
Returns ndarray.
2017-11-01 02:42:39 +03:00
RETURNS: The requested key, keys, row or rows.
"""
2017-11-01 02:34:55 +03:00
if sum(arg is None for arg in (key, keys, row, rows)) != 3:
bad_kwargs = {"key": key, "keys": keys, "row": row, "rows": rows}
raise ValueError(Errors.E059.format(kwargs=bad_kwargs))
2017-10-31 20:25:08 +03:00
xp = get_array_module(self.data)
2017-11-01 02:34:55 +03:00
if key is not None:
key = get_string_id(key)
2017-11-01 02:34:55 +03:00
return self.key2row.get(key, -1)
elif keys is not None:
keys = [get_string_id(key) for key in keys]
2017-11-01 02:34:55 +03:00
rows = [self.key2row.get(key, -1.) for key in keys]
return xp.asarray(rows, dtype="i")
2017-11-01 02:34:55 +03:00
else:
row2key = {row: key for key, row in self.key2row.items()}
2017-11-01 02:34:55 +03:00
if row is not None:
return row2key[row]
2017-11-01 02:34:55 +03:00
else:
results = [row2key[row] for row in rows]
return xp.asarray(results, dtype="uint64")
2017-10-31 20:25:08 +03:00
def add(self, key, *, vector=None, row=None):
"""Add a key to the table. Keys can be mapped to an existing vector
by setting `row`, or a new vector can be added.
2017-10-27 20:45:19 +03:00
key (int): The key to add.
vector (ndarray / None): A vector to add for the key.
row (int / None): The row number of a vector to map the key to.
RETURNS (int): The row the vector was added to.
DOCS: https://spacy.io/api/vectors#add
2017-10-27 20:45:19 +03:00
"""
# use int for all keys and rows in key2row for more efficient access
# and serialization
key = int(get_string_id(key))
if row is not None:
row = int(row)
2017-10-31 04:00:26 +03:00
if row is None and key in self.key2row:
row = self.key2row[key]
elif row is None:
2017-10-31 20:25:08 +03:00
if self.is_full:
raise ValueError(Errors.E060.format(rows=self.data.shape[0],
cols=self.data.shape[1]))
row = deref(self._unset.begin())
if row < self.data.shape[0]:
self.key2row[key] = row
else:
raise ValueError(Errors.E197.format(row=row, key=key))
2017-08-19 20:52:25 +03:00
if vector is not None:
self.data[row] = vector
if self._unset.count(row):
self._unset.erase(self._unset.find(row))
return row
2017-11-01 01:23:34 +03:00
def most_similar(self, queries, *, batch_size=1024, n=1, sort=True):
"""For each of the given vectors, find the n most similar entries
2017-10-31 20:25:08 +03:00
to it, by cosine.
2017-11-01 01:23:34 +03:00
Queries are by vector. Results are returned as a `(keys, best_rows,
scores)` tuple. If `queries` is large, the calculations are performed in
chunks, to avoid consuming too much memory. You can set the `batch_size`
to control the size/space trade-off during the calculations.
queries (ndarray): An array with one or more vectors.
batch_size (int): The batch size to use.
n (int): The number of entries to return for each query.
sort (bool): Whether to sort the n entries returned by score.
RETURNS (tuple): The most similar entries as a `(keys, best_rows, scores)`
tuple.
"""
filled = sorted(list({row for row in self.key2row.values()}))
if len(filled) < n:
raise ValueError(Errors.E198.format(n=n, n_rows=len(filled)))
2017-10-31 20:25:08 +03:00
xp = get_array_module(self.data)
2017-11-01 01:23:34 +03:00
norms = xp.linalg.norm(self.data[filled], axis=1, keepdims=True)
norms[norms == 0] = 1
vectors = self.data[filled] / norms
2017-11-01 01:23:34 +03:00
best_rows = xp.zeros((queries.shape[0], n), dtype='i')
scores = xp.zeros((queries.shape[0], n), dtype='f')
2017-10-31 20:25:08 +03:00
# Work in batches, to avoid memory problems.
for i in range(0, queries.shape[0], batch_size):
batch = queries[i : i+batch_size]
batch_norms = xp.linalg.norm(batch, axis=1, keepdims=True)
batch_norms[batch_norms == 0] = 1
batch /= batch_norms
2017-10-31 20:25:08 +03:00
# batch e.g. (1024, 300)
# vectors e.g. (10000, 300)
# sims e.g. (1024, 10000)
sims = xp.dot(batch, vectors.T)
best_rows[i:i+batch_size] = xp.argpartition(sims, -n, axis=1)[:,-n:]
scores[i:i+batch_size] = xp.partition(sims, -n, axis=1)[:,-n:]
if sort and n >= 2:
sorted_index = xp.arange(scores.shape[0])[:,None][i:i+batch_size],xp.argsort(scores[i:i+batch_size], axis=1)[:,::-1]
scores[i:i+batch_size] = scores[sorted_index]
best_rows[i:i+batch_size] = best_rows[sorted_index]
for i, j in numpy.ndindex(best_rows.shape):
best_rows[i, j] = filled[best_rows[i, j]]
# Round values really close to 1 or -1
scores = xp.around(scores, decimals=4, out=scores)
# Account for numerical error we want to return in range -1, 1
scores = xp.clip(scores, a_min=-1, a_max=1, out=scores)
2017-11-01 04:06:58 +03:00
row2key = {row: key for key, row in self.key2row.items()}
keys = xp.asarray(
[[row2key[row] for row in best_rows[i] if row in row2key]
for i in range(len(queries)) ], dtype="uint64")
return (keys, best_rows, scores)
2017-06-05 13:32:08 +03:00
def to_disk(self, path, **kwargs):
2017-10-27 20:45:19 +03:00
"""Save the current state to a directory.
2020-05-24 18:20:58 +03:00
path (str / Path): A path to a directory, which will be created if
it doesn't exists.
DOCS: https://spacy.io/api/vectors#to_disk
2017-10-27 20:45:19 +03:00
"""
xp = get_array_module(self.data)
if xp is numpy:
save_array = lambda arr, file_: xp.save(file_, arr, allow_pickle=False)
else:
save_array = lambda arr, file_: xp.save(file_, arr)
def save_vectors(path):
# the source of numpy.save indicates that the file object is closed after use.
# but it seems that somehow this does not happen, as ResourceWarnings are raised here.
# in order to not rely on this, wrap in context manager.
with path.open("wb") as _file:
save_array(self.data, _file)
serializers = {
2020-06-20 16:52:00 +03:00
"vectors": lambda p: save_vectors(p),
"key2row": lambda p: srsly.write_msgpack(p, self.key2row)
}
return util.to_disk(path, serializers, [])
2017-08-18 21:45:48 +03:00
def from_disk(self, path, **kwargs):
2017-10-27 20:45:19 +03:00
"""Loads state from a directory. Modifies the object in place and
returns it.
2020-05-24 18:20:58 +03:00
path (str / Path): Directory path, string or Path-like object.
2017-10-27 20:45:19 +03:00
RETURNS (Vectors): The modified object.
DOCS: https://spacy.io/api/vectors#from_disk
2017-10-27 20:45:19 +03:00
"""
2017-10-31 21:58:35 +03:00
def load_key2row(path):
2017-08-19 23:07:00 +03:00
if path.exists():
self.key2row = srsly.read_msgpack(path)
2017-10-31 21:58:35 +03:00
for key, row in self.key2row.items():
if self._unset.count(row):
self._unset.erase(self._unset.find(row))
2017-10-31 21:58:35 +03:00
def load_keys(path):
if path.exists():
keys = numpy.load(str(path))
for i, key in enumerate(keys):
self.add(key, row=i)
2017-08-19 19:42:11 +03:00
def load_vectors(path):
Update spaCy for thinc 8.0.0 (#4920) * Add load_from_config function * Add train_from_config script * Merge configs and expose via spacy.config * Fix script * Suggest create_evaluation_callback * Hard-code for NER * Fix errors * Register command * Add TODO * Update train-from-config todos * Fix imports * Allow delayed setting of parser model nr_class * Get train-from-config working * Tidy up and fix scores and printing * Hide traceback if cancelled * Fix weighted score formatting * Fix score formatting * Make output_path optional * Add Tok2Vec component * Tidy up and add tok2vec_tensors * Add option to copy docs in nlp.update * Copy docs in nlp.update * Adjust nlp.update() for set_annotations * Don't shuffle pipes in nlp.update, decruft * Support set_annotations arg in component update * Support set_annotations in parser update * Add get_gradients method * Add get_gradients to parser * Update errors.py * Fix problems caused by merge * Add _link_components method in nlp * Add concept of 'listeners' and ControlledModel * Support optional attributes arg in ControlledModel * Try having tok2vec component in pipeline * Fix tok2vec component * Fix config * Fix tok2vec * Update for Example * Update for Example * Update config * Add eg2doc util * Update and add schemas/types * Update schemas * Fix nlp.update * Fix tagger * Remove hacks from train-from-config * Remove hard-coded config str * Calculate loss in tok2vec component * Tidy up and use function signatures instead of models * Support union types for registry models * Minor cleaning in Language.update * Make ControlledModel specifically Tok2VecListener * Fix train_from_config * Fix tok2vec * Tidy up * Add function for bilstm tok2vec * Fix type * Fix syntax * Fix pytorch optimizer * Add example configs * Update for thinc describe changes * Update for Thinc changes * Update for dropout/sgd changes * Update for dropout/sgd changes * Unhack gradient update * Work on refactoring _ml * Remove _ml.py module * WIP upgrade cli scripts for thinc * Move some _ml stuff to util * Import link_vectors from util * Update train_from_config * Import from util * Import from util * Temporarily add ml.component_models module * Move ml methods * Move typedefs * Update load vectors * Update gitignore * Move imports * Add PrecomputableAffine * Fix imports * Fix imports * Fix imports * Fix missing imports * Update CLI scripts * Update spacy.language * Add stubs for building the models * Update model definition * Update create_default_optimizer * Fix import * Fix comment * Update imports in tests * Update imports in spacy.cli * Fix import * fix obsolete thinc imports * update srsly pin * from thinc to ml_datasets for example data such as imdb * update ml_datasets pin * using STATE.vectors * small fix * fix Sentencizer.pipe * black formatting * rename Affine to Linear as in thinc * set validate explicitely to True * rename with_square_sequences to with_list2padded * rename with_flatten to with_list2array * chaining layernorm * small fixes * revert Optimizer import * build_nel_encoder with new thinc style * fixes using model's get and set methods * Tok2Vec in component models, various fixes * fix up legacy tok2vec code * add model initialize calls * add in build_tagger_model * small fixes * setting model dims * fixes for ParserModel * various small fixes * initialize thinc Models * fixes * consistent naming of window_size * fixes, removing set_dropout * work around Iterable issue * remove legacy tok2vec * util fix * fix forward function of tok2vec listener * more fixes * trying to fix PrecomputableAffine (not succesful yet) * alloc instead of allocate * add morphologizer * rename residual * rename fixes * Fix predict function * Update parser and parser model * fixing few more tests * Fix precomputable affine * Update component model * Update parser model * Move backprop padding to own function, for test * Update test * Fix p. affine * Update NEL * build_bow_text_classifier and extract_ngrams * Fix parser init * Fix test add label * add build_simple_cnn_text_classifier * Fix parser init * Set gpu off by default in example * Fix tok2vec listener * Fix parser model * Small fixes * small fix for PyTorchLSTM parameters * revert my_compounding hack (iterable fixed now) * fix biLSTM * Fix uniqued * PyTorchRNNWrapper fix * small fixes * use helper function to calculate cosine loss * small fixes for build_simple_cnn_text_classifier * putting dropout default at 0.0 to ensure the layer gets built * using thinc util's set_dropout_rate * moving layer normalization inside of maxout definition to optimize dropout * temp debugging in NEL * fixed NEL model by using init defaults ! * fixing after set_dropout_rate refactor * proper fix * fix test_update_doc after refactoring optimizers in thinc * Add CharacterEmbed layer * Construct tagger Model * Add missing import * Remove unused stuff * Work on textcat * fix test (again :)) after optimizer refactor * fixes to allow reading Tagger from_disk without overwriting dimensions * don't build the tok2vec prematuraly * fix CharachterEmbed init * CharacterEmbed fixes * Fix CharacterEmbed architecture * fix imports * renames from latest thinc update * one more rename * add initialize calls where appropriate * fix parser initialization * Update Thinc version * Fix errors, auto-format and tidy up imports * Fix validation * fix if bias is cupy array * revert for now * ensure it's a numpy array before running bp in ParserStepModel * no reason to call require_gpu twice * use CupyOps.to_numpy instead of cupy directly * fix initialize of ParserModel * remove unnecessary import * fixes for CosineDistance * fix device renaming * use refactored loss functions (Thinc PR 251) * overfitting test for tagger * experimental settings for the tagger: avoid zero-init and subword normalization * clean up tagger overfitting test * use previous default value for nP * remove toy config * bringing layernorm back (had a bug - fixed in thinc) * revert setting nP explicitly * remove setting default in constructor * restore values as they used to be * add overfitting test for NER * add overfitting test for dep parser * add overfitting test for textcat * fixing init for linear (previously affine) * larger eps window for textcat * ensure doc is not None * Require newer thinc * Make float check vaguer * Slop the textcat overfit test more * Fix textcat test * Fix exclusive classes for textcat * fix after renaming of alloc methods * fixing renames and mandatory arguments (staticvectors WIP) * upgrade to thinc==8.0.0.dev3 * refer to vocab.vectors directly instead of its name * rename alpha to learn_rate * adding hashembed and staticvectors dropout * upgrade to thinc 8.0.0.dev4 * add name back to avoid warning W020 * thinc dev4 * update srsly * using thinc 8.0.0a0 ! Co-authored-by: Matthew Honnibal <honnibal+gh@gmail.com> Co-authored-by: Ines Montani <ines@ines.io>
2020-01-29 19:06:46 +03:00
ops = get_current_ops()
2017-08-19 23:07:00 +03:00
if path.exists():
Update spaCy for thinc 8.0.0 (#4920) * Add load_from_config function * Add train_from_config script * Merge configs and expose via spacy.config * Fix script * Suggest create_evaluation_callback * Hard-code for NER * Fix errors * Register command * Add TODO * Update train-from-config todos * Fix imports * Allow delayed setting of parser model nr_class * Get train-from-config working * Tidy up and fix scores and printing * Hide traceback if cancelled * Fix weighted score formatting * Fix score formatting * Make output_path optional * Add Tok2Vec component * Tidy up and add tok2vec_tensors * Add option to copy docs in nlp.update * Copy docs in nlp.update * Adjust nlp.update() for set_annotations * Don't shuffle pipes in nlp.update, decruft * Support set_annotations arg in component update * Support set_annotations in parser update * Add get_gradients method * Add get_gradients to parser * Update errors.py * Fix problems caused by merge * Add _link_components method in nlp * Add concept of 'listeners' and ControlledModel * Support optional attributes arg in ControlledModel * Try having tok2vec component in pipeline * Fix tok2vec component * Fix config * Fix tok2vec * Update for Example * Update for Example * Update config * Add eg2doc util * Update and add schemas/types * Update schemas * Fix nlp.update * Fix tagger * Remove hacks from train-from-config * Remove hard-coded config str * Calculate loss in tok2vec component * Tidy up and use function signatures instead of models * Support union types for registry models * Minor cleaning in Language.update * Make ControlledModel specifically Tok2VecListener * Fix train_from_config * Fix tok2vec * Tidy up * Add function for bilstm tok2vec * Fix type * Fix syntax * Fix pytorch optimizer * Add example configs * Update for thinc describe changes * Update for Thinc changes * Update for dropout/sgd changes * Update for dropout/sgd changes * Unhack gradient update * Work on refactoring _ml * Remove _ml.py module * WIP upgrade cli scripts for thinc * Move some _ml stuff to util * Import link_vectors from util * Update train_from_config * Import from util * Import from util * Temporarily add ml.component_models module * Move ml methods * Move typedefs * Update load vectors * Update gitignore * Move imports * Add PrecomputableAffine * Fix imports * Fix imports * Fix imports * Fix missing imports * Update CLI scripts * Update spacy.language * Add stubs for building the models * Update model definition * Update create_default_optimizer * Fix import * Fix comment * Update imports in tests * Update imports in spacy.cli * Fix import * fix obsolete thinc imports * update srsly pin * from thinc to ml_datasets for example data such as imdb * update ml_datasets pin * using STATE.vectors * small fix * fix Sentencizer.pipe * black formatting * rename Affine to Linear as in thinc * set validate explicitely to True * rename with_square_sequences to with_list2padded * rename with_flatten to with_list2array * chaining layernorm * small fixes * revert Optimizer import * build_nel_encoder with new thinc style * fixes using model's get and set methods * Tok2Vec in component models, various fixes * fix up legacy tok2vec code * add model initialize calls * add in build_tagger_model * small fixes * setting model dims * fixes for ParserModel * various small fixes * initialize thinc Models * fixes * consistent naming of window_size * fixes, removing set_dropout * work around Iterable issue * remove legacy tok2vec * util fix * fix forward function of tok2vec listener * more fixes * trying to fix PrecomputableAffine (not succesful yet) * alloc instead of allocate * add morphologizer * rename residual * rename fixes * Fix predict function * Update parser and parser model * fixing few more tests * Fix precomputable affine * Update component model * Update parser model * Move backprop padding to own function, for test * Update test * Fix p. affine * Update NEL * build_bow_text_classifier and extract_ngrams * Fix parser init * Fix test add label * add build_simple_cnn_text_classifier * Fix parser init * Set gpu off by default in example * Fix tok2vec listener * Fix parser model * Small fixes * small fix for PyTorchLSTM parameters * revert my_compounding hack (iterable fixed now) * fix biLSTM * Fix uniqued * PyTorchRNNWrapper fix * small fixes * use helper function to calculate cosine loss * small fixes for build_simple_cnn_text_classifier * putting dropout default at 0.0 to ensure the layer gets built * using thinc util's set_dropout_rate * moving layer normalization inside of maxout definition to optimize dropout * temp debugging in NEL * fixed NEL model by using init defaults ! * fixing after set_dropout_rate refactor * proper fix * fix test_update_doc after refactoring optimizers in thinc * Add CharacterEmbed layer * Construct tagger Model * Add missing import * Remove unused stuff * Work on textcat * fix test (again :)) after optimizer refactor * fixes to allow reading Tagger from_disk without overwriting dimensions * don't build the tok2vec prematuraly * fix CharachterEmbed init * CharacterEmbed fixes * Fix CharacterEmbed architecture * fix imports * renames from latest thinc update * one more rename * add initialize calls where appropriate * fix parser initialization * Update Thinc version * Fix errors, auto-format and tidy up imports * Fix validation * fix if bias is cupy array * revert for now * ensure it's a numpy array before running bp in ParserStepModel * no reason to call require_gpu twice * use CupyOps.to_numpy instead of cupy directly * fix initialize of ParserModel * remove unnecessary import * fixes for CosineDistance * fix device renaming * use refactored loss functions (Thinc PR 251) * overfitting test for tagger * experimental settings for the tagger: avoid zero-init and subword normalization * clean up tagger overfitting test * use previous default value for nP * remove toy config * bringing layernorm back (had a bug - fixed in thinc) * revert setting nP explicitly * remove setting default in constructor * restore values as they used to be * add overfitting test for NER * add overfitting test for dep parser * add overfitting test for textcat * fixing init for linear (previously affine) * larger eps window for textcat * ensure doc is not None * Require newer thinc * Make float check vaguer * Slop the textcat overfit test more * Fix textcat test * Fix exclusive classes for textcat * fix after renaming of alloc methods * fixing renames and mandatory arguments (staticvectors WIP) * upgrade to thinc==8.0.0.dev3 * refer to vocab.vectors directly instead of its name * rename alpha to learn_rate * adding hashembed and staticvectors dropout * upgrade to thinc 8.0.0.dev4 * add name back to avoid warning W020 * thinc dev4 * update srsly * using thinc 8.0.0a0 ! Co-authored-by: Matthew Honnibal <honnibal+gh@gmail.com> Co-authored-by: Ines Montani <ines@ines.io>
2020-01-29 19:06:46 +03:00
self.data = ops.xp.load(str(path))
2017-08-18 21:45:48 +03:00
serializers = {
"vectors": load_vectors,
2020-06-20 16:52:00 +03:00
"keys": load_keys,
"key2row": load_key2row,
}
2017-08-18 21:45:48 +03:00
util.from_disk(path, serializers, [])
self._sync_unset()
2017-08-19 19:42:11 +03:00
return self
2017-06-05 13:32:08 +03:00
def to_bytes(self, **kwargs):
2017-10-27 20:45:19 +03:00
"""Serialize the current state to a binary string.
exclude (list): String names of serialization fields to exclude.
2017-10-27 20:45:19 +03:00
RETURNS (bytes): The serialized form of the `Vectors` object.
DOCS: https://spacy.io/api/vectors#to_bytes
2017-10-27 20:45:19 +03:00
"""
2017-06-05 13:32:08 +03:00
def serialize_weights():
if hasattr(self.data, "to_bytes"):
2017-08-18 21:45:48 +03:00
return self.data.to_bytes()
2017-06-05 13:32:08 +03:00
else:
return srsly.msgpack_dumps(self.data)
serializers = {
"key2row": lambda: srsly.msgpack_dumps(self.key2row),
"vectors": serialize_weights
}
return util.to_bytes(serializers, [])
2017-06-05 13:32:08 +03:00
def from_bytes(self, data, **kwargs):
2017-10-27 20:45:19 +03:00
"""Load state from a binary string.
data (bytes): The data to load from.
exclude (list): String names of serialization fields to exclude.
2017-10-27 20:45:19 +03:00
RETURNS (Vectors): The `Vectors` object.
DOCS: https://spacy.io/api/vectors#from_bytes
2017-10-27 20:45:19 +03:00
"""
2017-06-05 13:32:08 +03:00
def deserialize_weights(b):
if hasattr(self.data, "from_bytes"):
2017-08-18 21:45:48 +03:00
self.data.from_bytes()
2017-06-05 13:32:08 +03:00
else:
self.data = srsly.msgpack_loads(b)
2017-06-05 13:32:08 +03:00
deserializers = {
"key2row": lambda b: self.key2row.update(srsly.msgpack_loads(b)),
"vectors": deserialize_weights
}
util.from_bytes(data, deserializers, [])
self._sync_unset()
2017-08-19 19:42:11 +03:00
return self
def _sync_unset(self):
filled = {row for row in self.key2row.values()}
self._unset = cppset[int]({row for row in range(self.data.shape[0]) if row not in filled})