MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / __init__

Method __init__

imperative/python/megengine/module/embedding.py:39–73  ·  view source on GitHub ↗
(
        self,
        num_embeddings: int,
        embedding_dim: int,
        padding_idx: Optional[int] = None,
        max_norm: Optional[float] = None,
        norm_type: Optional[float] = None,
        initial_weight: Parameter = None,
        freeze: bool = False,
        **kwargs
    )

Source from the content-addressed store, hash-verified

37 """
38
39 def __init__(
40 self,
41 num_embeddings: int,
42 embedding_dim: int,
43 padding_idx: Optional[int] = None,
44 max_norm: Optional[float] = None,
45 norm_type: Optional[float] = None,
46 initial_weight: Parameter = None,
47 freeze: bool = False,
48 **kwargs
49 ):
50 super().__init__(**kwargs)
51 if padding_idx is not None:
52 raise ValueError("Not support padding index now.")
53 if max_norm is not None or norm_type is not None:
54 raise ValueError("Not support weight normalize now.")
55 self.padding_idx = padding_idx
56 self.max_norm = max_norm
57 self.norm_type = norm_type
58 self.num_embeddings = num_embeddings
59 self.embedding_dim = embedding_dim
60 self.freeze = freeze
61 if initial_weight is None:
62 self.weight = Parameter(
63 np.random.uniform(
64 size=(self.num_embeddings, self.embedding_dim)
65 ).astype(np.float32)
66 )
67 self.reset_parameters()
68 else:
69 if initial_weight.numpy().shape != (num_embeddings, embedding_dim):
70 raise ValueError(
71 "The weight shape should match num_embeddings and embedding_dim"
72 )
73 self.weight = Parameter(initial_weight.numpy())
74
75 def reset_parameters(self) -> None:
76 init.normal_(self.weight)

Callers

nothing calls this directly

Calls 5

reset_parametersMethod · 0.95
ParameterClass · 0.85
uniformMethod · 0.80
astypeMethod · 0.45
numpyMethod · 0.45

Tested by

no test coverage detected