mirror of https://github.com/explosion/spaCy.git
31 lines
1.2 KiB
Plaintext
31 lines
1.2 KiB
Plaintext
|
//- 💫 DOCS > USAGE > VECTORS & SIMILARITY > GPU
|
||
|
|
||
|
p
|
||
|
| If you're using a GPU, it's much more efficient to keep the word vectors
|
||
|
| on the device. You can do that by setting the
|
||
|
| #[+api("vectors#attributes") #[code Vectors.data]] attribute to a
|
||
|
| #[code cupy.ndarray] object if you're using spaCy
|
||
|
| or #[+a("https://chainer.org") Chainer], or a
|
||
|
| #[code torch.Tensor] object if you're using
|
||
|
| #[+a("http://pytorch.org") PyTorch]. The #[code data] object just needs
|
||
|
| to support #[code __iter__] and #[code __getitem__], so if you're using
|
||
|
| another library such as #[+a("https://www.tensorflow.org") TensorFlow],
|
||
|
| you could also create a wrapper for your vectors data.
|
||
|
|
||
|
+code("spaCy, Thinc or Chainer").
|
||
|
import cupy.cuda
|
||
|
from spacy.vectors import Vectors
|
||
|
|
||
|
vector_table = numpy.zeros((3, 300), dtype='f')
|
||
|
vectors = Vectors([u'dog', u'cat', u'orange'], vector_table)
|
||
|
with cupy.cuda.Device(0):
|
||
|
vectors.data = cupy.asarray(vectors.data)
|
||
|
|
||
|
+code("PyTorch").
|
||
|
import torch
|
||
|
from spacy.vectors import Vectors
|
||
|
|
||
|
vector_table = numpy.zeros((3, 300), dtype='f')
|
||
|
vectors = Vectors([u'dog', u'cat', u'orange'], vector_table)
|
||
|
vectors.data = torch.Tensor(vectors.data).cuda(0)
|