Embeddings

word2vec_embedding

Embedding class

class mindnlp.modules.embeddings.Fasttext(init_embed, requires_grad: bool = True, dropout=0.0)[source]

Bases: TokenEmbedding

Embedding layer.

Parameters:
  • init_embed (Tensor) – Passing into Tensor,use these values to initialize Embedding directly.

  • requires_grad (bool) – Whether this parameter needs to be gradient to update. Default: True.

  • dropout (float) – Dropout of the output of Embedding. Default: 0.5.

Examples

>>> init_embed = Tensor(np.zeros((4, 4)).astype(np.float32))
>>> fasttext_embed = Fasttext(init_embed)
>>> ids = Tensor([1, 2, 3])
>>> output = fasttext_embed(ids)
construct(ids)[source]
Parameters:

ids (Tensor) – Ids to query.

Returns:

  • Tensor, returns the Embedding query results.

classmethod from_pretrained(name='1M', dims=300, root='/home/docs/checkouts/readthedocs.org/user_builds/mindnlpdoc/checkouts/latest/docs/.mindnlp', special_first=True, **kwargs)[source]

Creates Embedding instance from given pre-trained word vector.

Parameters:
  • name (str) – The name of the pretrained vector. Default: “1M”.

  • dims (int) – The dimension of the pretrained vector. Default: 300.

  • root (str) – Default storage directory. Default: DEFAULT_ROOT.

  • special_first (bool) – Indicates whether special participles from special_tokens will be added to the top of the dictionary. If True, add special_tokens to the beginning of the dictionary, otherwise add them to the end. Default: True.

  • kwargs (dict) –

    • requires_grad (bool): Whether this parameter needs to be gradient to update.

    • dropout (float): Dropout of the output of Embedding.

Returns:

  • Fasttext, Returns an embedding instance generated through a pretrained word vector.

classmethod load(foldername=None, root='/home/docs/checkouts/readthedocs.org/user_builds/mindnlpdoc/checkouts/latest/docs/.mindnlp', load_npy=False, npy_path=None)[source]

Load embedding from the specified location.

Parameters:
  • foldername (str) – Name of the folder to load. Default: None.

  • root (Path) – Path of the embedding folder. Default: DEFAULT_ROOT.

  • load_npy (Bool) – Whether to initialize the embedding as a npy file. Npy_path are valid when load_npy is True. Default: False.

  • npy_path (Path) – Location of the npy file. Default: None.

Returns:

None

save(foldername, root='/home/docs/checkouts/readthedocs.org/user_builds/mindnlpdoc/checkouts/latest/docs/.mindnlp')[source]

Save the embedding to the specified location.

Parameters:
  • foldername (str) – Name of the folder to store.

  • root (Path) – Path of the embedding folder. Default: DEFAULT_ROOT.

Returns:

None

class mindnlp.modules.embeddings.Glove(init_embed, requires_grad: bool = True, dropout=0.0)[source]

Bases: TokenEmbedding

Embedding layer.

Parameters:
  • init_embed (Tensor) – Passing into Tensor,use these values to initialize Embedding directly.

  • requires_grad (bool) – Whether this parameter needs to be gradient to update. Default: True.

  • dropout (float) – Dropout of the output of Embedding. Default: 0.5.

Examples

>>> init_embed = Tensor(np.zeros((4, 4)).astype(np.float32))
>>> glove_embed = Glove(init_embed)
>>> ids = Tensor([1, 2, 3])
>>> output = glove_embed(ids)
construct(ids)[source]
Parameters:

ids (Tensor) – Ids to query.

Returns:

  • Tensor, returns the Embedding query results.

classmethod from_pretrained(name='6B', dims=300, root='/home/docs/checkouts/readthedocs.org/user_builds/mindnlpdoc/checkouts/latest/docs/.mindnlp', special_first=True, **kwargs)[source]

Creates Embedding instance from given pre-trained word vector.

Parameters:
  • name (str) – The name of the pretrained vector. Default: ‘6B’.

  • dims (int) – The dimension of the pretrained vector. Default: 300.

  • root (str) – Default storage directory. Default: DEFAULT_ROOT.

  • special_first (bool) – Indicates whether special participles from special_tokens will be added to the top of the dictionary. If True, add special_tokens to the beginning of the dictionary, otherwise add them to the end. Default: True.

  • kwargs (dict) –

    • requires_grad (bool): Whether this parameter needs to be gradient to update.

    • dropout (float): Dropout of the output of Embedding.

Returns:

  • Glove, Returns an embedding instance generated through a pretrained word vector.

classmethod load(foldername=None, root='/home/docs/checkouts/readthedocs.org/user_builds/mindnlpdoc/checkouts/latest/docs/.mindnlp', load_npy=False, npy_path=None)[source]

Load embedding from the specified location.

Parameters:
  • foldername (str) – Name of the folder to load. Default: None.

  • root (Path) – Path of the embedding folder. Default: DEFAULT_ROOT.

  • load_npy (Bool) – Whether to initialize the embedding as a npy file. Npy_path are valid when load_npy is True. Default: False.

  • npy_path (Path) – Location of the npy file. Default: None.

Returns:

None

save(foldername, root='/home/docs/checkouts/readthedocs.org/user_builds/mindnlpdoc/checkouts/latest/docs/.mindnlp')[source]

Save the embedding to the specified location.

Parameters:
  • foldername (str) – Name of the folder to store.

  • root (Path) – Path of the embedding folder. Default: DEFAULT_ROOT.

Returns:

None