Embedding

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

Members

Functions

backward
auto backward(Variable!(T, 2, DeviceStorage) gy)
Undocumented in source. Be warned that the author may not have intended to support it.
backward
auto backward(Variable!(T, 2, HostStorage) gy)
Undocumented in source. Be warned that the author may not have intended to support it.
forward
auto forward(Variable!(T, 2, HostStorage) weight, Variable!(int, 1, HostStorage) ids)
Undocumented in source. Be warned that the author may not have intended to support it.
forward
auto forward(Variable!(T, 2, DeviceStorage) weight, Variable!(int, 1, DeviceStorage) ids)
Undocumented in source. Be warned that the author may not have intended to support it.

Mixins

__anonymous
mixin FunctionCommon
Undocumented in source.

Variables

dx
Variable!(int, 1, DeviceStorage) dx;
Undocumented in source.
hx
Variable!(int, 1, HostStorage) hx;
Undocumented in source.
wshape
uint[2] wshape;
Undocumented in source.

Mixed In Members

From mixin FunctionCommon

this(this)
this(this)
Undocumented in source.
DeviceRets
alias DeviceRets = Tuple!(Parameters!backward)
Undocumented in source.
DeviceArgs
alias DeviceArgs = Tuple!(Parameters!forward)
Undocumented in source.
__anonymous
mixin TypeChecker!(forward, backward)
Undocumented in source.
_mixin_dargs
DeviceArgs _mixin_dargs;
Undocumented in source.
HostRets
alias HostRets = Tuple!(Parameters!backward)
Undocumented in source.
HostArgs
alias HostArgs = Tuple!(Parameters!forward)
Undocumented in source.
__anonymous
mixin TypeChecker!(forward, backward)
Undocumented in source.
_mixin_hargs
HostArgs _mixin_hargs;
Undocumented in source.
applyForward
auto applyForward(Args args)

store grain.autograd.BackProp object in returned variables from forward function

applyBackward
void applyBackward(UntypedVariable[] ugradOutputs)

type-erased version of backward function used in grain.autograd.BackProp object

Examples

import numir;

Embedding!float embed;
auto w = [[1.0f, 2.0f], [3.0f, 4.0f]].nparray.variable;
auto x = [0, 1, 0].variable;
auto y = embed.forward(w, x);
assert(y.sliced == [[1,2],[3,4],[1,2]]);

auto gy = [[1f, 2f], [-1f, -2f], [1f, 0f]].nparray.variable;
auto gw = embed.backward(gy)[0];
assert(gw.sliced == [[2f, 2f], [-1f, -2f]]);

version (grain_cuda) {
    Embedding!float dembed;
    auto dy = dembed.forward(w.to!DeviceStorage, x.to!DeviceStorage);
    assert(dy.to!HostStorage.sliced == y.sliced);
    auto dgw = dembed.backward(gy.to!DeviceStorage)[0];
    assert(dgw.to!HostStorage.sliced == gw.sliced);
}

Meta