NegativeLogLikelihood

struct NegativeLogLikelihood (
F
I = long
) {
bool sizeAverage;
int ignoreIndex;
Variable!(I, 1, HostStorage) _htargetId;
F _normalize;
int _nClass;
Variable!(I, 1, DeviceStorage) _dtargetId;
}

Members

Mixins

__anonymous
mixin FunctionCommon

Compute negative log-likelihood: -logP(y=t)

Examples

test nll simple case, gradcheck and cpu/cuda equality

1 /++ equivalent torch v0.4 code
2  x = torch.FloatTensor([[0.2, 0.4, 0.4], [0.1,0.5,0.4]])
3  x.requires_grad = True
4  t = torch.LongTensor([1, 0])
5  l = torch.nn.functional.nll_loss(x, t)
6  print(l)       # tensor(-0.2500)
7  l.backward()
8  print(x.grad)  # tensor([[0.0, -0.5, 0.0], [-0.5, 0.0, 0.0]])
9  +/
10 import std.typecons;
11 import grain.testing;
12 NegativeLogLikelihood!(float, int) func;
13 auto hx = [[0.2f, 0.4f, 0.4f], [0.1f, 0.5f, 0.4f], [0.1f, 0.5f, 0.4f]].variable;
14 auto ht = [1, 0, func.ignoreIndex].variable;
15 auto hl = func.forward(hx, ht);
16 assert(func._normalize == 0.5);
17 assert(hl.sliced == [-(0.4f + 0.1f + 0.0f) / 2]);
18 auto hgx = func.backward(1.0f.variable);
19 assert(hgx[0].sliced == [[0.0, -0.5, 0.0], [-0.5, 0.0, 0.0], [0.0, 0.0, 0.0]]);
20 assert(!hgx[1].defined);
21 gradCheck(func, tuple(hx, ht), 1.0f.variable);
22 
23 version (grain_cuda) {
24     auto dx = hx.to!DeviceStorage;
25     auto dt = ht.to!DeviceStorage;
26     auto dl = func.forward(dx, dt);
27     assert(func._normalize == 0.5);
28     assert(dl.to!HostStorage.sliced == [-(0.4f + 0.1f + 0.0f) / 2]);
29     auto dgx = func.backward(1.0f.variable.to!DeviceStorage);
30     assert(dgx[0].to!HostStorage.sliced == [[0.0, -0.5, 0.0], [-0.5, 0.0, 0.0], [0.0, 0.0, 0.0]]);
31     assert(!dgx[1].defined);
32 }

Meta