Embedding

Emebedding ID into vector. TODO: support N-dim input. support sparse weight matrix

struct Embedding (
T
) {
Variable!(int, 1, HostStorage) hx;
uint[2] wshape;
Variable!(int, 1, DeviceStorage) dx;
}

Examples

1 import numir;
2 
3 Embedding!float embed;
4 auto w = [[1.0f, 2.0f], [3.0f, 4.0f]].nparray.variable;
5 auto x = [0, 1, 0].variable;
6 auto y = embed.forward(w, x);
7 assert(y.sliced == [[1,2],[3,4],[1,2]]);
8 
9 auto gy = [[1f, 2f], [-1f, -2f], [1f, 0f]].nparray.variable;
10 auto gw = embed.backward(gy)[0];
11 assert(gw.sliced == [[2f, 2f], [-1f, -2f]]);
12 
13 version (grain_cuda) {
14     Embedding!float dembed;
15     auto dy = dembed.forward(w.to!DeviceStorage, x.to!DeviceStorage);
16     assert(dy.to!HostStorage.sliced == y.sliced);
17     auto dgw = dembed.backward(gy.to!DeviceStorage)[0];
18     assert(dgw.to!HostStorage.sliced == gw.sliced);
19 }

Meta