espnet.nets.chainer_backend.deterministic_embed_id.embed_id
Less than 1 minute
espnet.nets.chainer_backend.deterministic_embed_id.embed_id
espnet.nets.chainer_backend.deterministic_embed_id.embed_id(x, W, ignore_label=None)
Efficient linear function for one-hot input.
This function implements so called word embeddings. It takes two arguments: a set of IDs (words) x
in $B$ dimensional integer vector, and a set of all ID (word) embeddings W
in $V \times d$ float32 matrix. It outputs $B \times d$ matrix whose i
-th column is the x[i]
-th column of W
. This function is only differentiable on the input W
.
- Parameters:
- x (chainer.Variable | np.ndarray) – Batch vectors of IDs. Each element must be signed integer.
- W (chainer.Variable | np.ndarray) – Distributed representation of each ID (a.k.a. word embeddings).
- ignore_label (int) – If ignore_label is an int value, i-th column of return value is filled with 0.
- Returns: Embedded variable.
- Return type: chainer.Variable
EmbedID
Examples
>>> x = np.array([2, 1]).astype('i')
>>> x
array([2, 1], dtype=int32)
>>> W = np.array([[0, 0, 0],
... [1, 1, 1],
... [2, 2, 2]]).astype('f')
>>> W
array([[ 0., 0., 0.],
[ 1., 1., 1.],
[ 2., 2., 2.]], dtype=float32)
>>> F.embed_id(x, W).data
array([[ 2., 2., 2.],
[ 1., 1., 1.]], dtype=float32)
>>> F.embed_id(x, W, ignore_label=1).data
array([[ 2., 2., 2.],
[ 0., 0., 0.]], dtype=float32)