38 const c10::optional<at::Generator> generator)
40 if (tensor.ndimension() < 2)
41 mooseError(
"Only tensors with 2 or more dimensions are supported for orthogonal "
47 torch::NoGradGuard no_grad;
49 const auto rows = tensor.size(0);
50 const auto cols = tensor.numel() / rows;
51 auto flattened = torch::empty({rows, cols}, tensor.options());
52 at::normal_out(flattened, 0.0, 1.0, {rows, cols}, generator);
55 flattened = flattened.transpose(0, 1);
57 auto qr = at::linalg_qr(flattened,
"reduced");
58 auto q = std::get<0>(qr);
59 const auto phases = torch::diag(std::get<1>(qr), 0).sign();
63 q = q.transpose(0, 1);
65 tensor.view_as(q).copy_(q);
void orthogonalInitializeTensor(torch::Tensor &tensor, Real gain=1.0, c10::optional< at::Generator > generator=c10::nullopt)
Fill a tensor with a (semi) orthogonal matrix using the provided generator.