Is there an elegant way to build a
Torch.Tensor like this from a given set of values?
Here is an 3x3 example, but in my application I would have a matrix of any odd-size.
A function call
gen_matrix([a, b, c, d, e, f]) should generate
EDIT: As of now, I implemented the following solution. A more elegant way is desirable without a
for loop. Using plain
torch operations is desirable.
def weights_to_symmetric(weights, N): assert(weights.ndim == 3) tensor = torch.zeros((*weights.shape[:2], N, N)) idx = 0 for diag in range(N): size = N - diag w = weights[:, :, idx:idx+size] tensor += torch.diag_embed(w, offset=diag) if diag > 0: tensor += torch.diag_embed(w, offset=-diag) idx += size return tensor