Sum

sum to scalar. mode is similar to mir.math.sum

struct Sum (
string mode = "fast"
T
size_t dim
) {
uint[dim] shape;
}

Examples

1 import mir.ndslice;
2 import mir.math;
3 auto x = [1f, 2f, 3f, 4f].sliced(2, 2).variable;
4 Sum!("fast", float, 2) func;
5 auto y = func.forward(x);
6 assert(y == 10f.variable);
7 assert(func.backward(1.2f.variable) == [1.2f, 1.2f, 1.2f, 1.2f].sliced(2, 2).variable);
8 
9 version (grain_cuda) {
10     auto cx = x.to!DeviceStorage;
11     auto cy = func.forward(cx).to!HostStorage;
12     assert(cy == 10f.variable);
13     auto cgx = func.backward(1.2f.variable.to!DeviceStorage).to!HostStorage;
14     assert(cgx.sliced == [1.2f, 1.2f, 1.2f, 1.2f].sliced(2, 2));
15 }

Meta