megengine.module.Embedding.from_pretrained

classmethod Embedding.from_pretrained(embeddings, freeze=True, padding_idx=None, max_norm=None, norm_type=None)[源代码]

从给定的2维FloatTensor创建词向量实例。

参数
  • embeddings (Parameter) – tensor contained weight for the embedding.

  • freeze (Optional[bool]) – if True, the weight does not get updated during the learning process. Default: True.

  • padding_idx (Optional[int]) – should be set to None, not support Now.

  • max_norm (Optional[float]) – should be set to None, not support Now.

  • norm_type (Optional[float]) – should be set to None, not support Now.

实际案例

import numpy as np
import megengine as mge
import megengine.module as M
weight = mge.tensor(np.array([(1.2,2.3,3.4,4.5,5.6)], dtype=np.float32))
data = mge.tensor(np.array([(0,0)], dtype=np.int32))

embedding = M.Embedding.from_pretrained(weight, freeze=False)
output = embedding(data)
print(output.numpy())

输出:

[[[1.2 2.3 3.4 4.5 5.6]
  [1.2 2.3 3.4 4.5 5.6]]]