Update orthogonal.py

This commit is contained in:
Nicola Demo
2024-08-26 11:26:31 +02:00
parent a1e041c1f0
commit 68f5521476

View File

@@ -32,15 +32,17 @@ class OrthogonalBlock(torch.nn.Module):
# check dim is less than all the other dimensions # check dim is less than all the other dimensions
if X.shape[self.dim] > min(X.shape): if X.shape[self.dim] > min(X.shape):
raise Warning( raise Warning(
"The dimension where to orthogonalize is greater\ "The dimension where to orthogonalize is greater"
than the other dimensions" " than the other dimensions"
) )
result = torch.zeros_like(X) result = torch.zeros_like(X)
# normalize first basis # normalize first basis
X_0 = torch.select(X, self.dim, 0) X_0 = torch.select(X, self.dim, 0)
result_0 = torch.select(result, self.dim, 0) result_0 = torch.select(result, self.dim, 0)
result_0 += X_0 / torch.norm(X_0) result_0 += X_0 / torch.norm(X_0)
# iterate over the rest of the basis with Gram-Schmidt # iterate over the rest of the basis with Gram-Schmidt
for i in range(1, X.shape[self.dim]): for i in range(1, X.shape[self.dim]):
v = torch.select(X, self.dim, i) v = torch.select(X, self.dim, i)
@@ -52,4 +54,5 @@ class OrthogonalBlock(torch.nn.Module):
) * torch.select(result, self.dim, j) ) * torch.select(result, self.dim, j)
result_i = torch.select(result, self.dim, i) result_i = torch.select(result, self.dim, i)
result_i += v / torch.norm(v) result_i += v / torch.norm(v)
return result return result