view

Topology functions reorganizing shape while it hold total elements a.k.a. reshape. At most one dimension of the new shape can be -1. In this case, the value is inferred from the size of the tensor and the remaining dimensions.

view
(
T
size_t sdim
size_t tdim
alias Storage
)
(
Variable!(T, sdim, Storage) x
,
ptrdiff_t[tdim] shape...
)

Examples

1 import numir;
2 import mir.ndslice;
3 import grain.testing;
4 
5 auto hx = uniform!float(6, 4).slice.variable(true);
6 auto hgy = uniform!float(2, 3, 4).slice.variable;
7 auto ugy = UntypedVariable(hgy);
8 auto hy = hx.view([2, 3, -1]);
9 assert(hy.sliced == numir.view(hx.sliced, [2, 3, -1]));
10 hy.backward(&ugy);
11 assert(hx.gradSliced == numir.view(hgy.sliced, [6, 4]));
12 // gradCheckChain!(x => x.view([2, 3, -1]))(hx, hgy, 1e-3, 5e-2, 5e-2);

Meta