buffer named "tensor_name"
Return type:
      Module
-[ Examples ]-



import torch
import torch.nn as nn
import torch.nn.utils.parametrize as P
class Symmetric(nn.Module):
    def forward(self, X):
        return X.triu() + X.triu(1).T  # Return a symmetric matrix
def right_inverse(self, A):
    return A.triu()

m = nn.Linear(5, 5)
P.register_parametrization(m, "weight", Symmetric())
print(torch.allclose(m.weight, m.weight.T))  # m.weight is now symmetric
   True
A = torch.rand(5, 5)
A = A + A.T   # A is now symmetric
m.weight = A  # Initialize the weight to be the symmetric matrix A
print(torch.allclose(m.weight, A))
   True
class RankOne(nn.Module):
    def forward(self, x, y):
        # Form a rank 1 matrix multiplying two vectors
        return x.unsqueeze(-1) @ y.unsqueeze(-2)
def right_inverse(self, Z):



