🎨 Format Python code with psf/black
This commit is contained in:
@@ -9,6 +9,7 @@ class OrthogonalBlock(torch.nn.Module):
|
|||||||
The module takes a tensor of size [N, M] and returns a tensor of
|
The module takes a tensor of size [N, M] and returns a tensor of
|
||||||
size [N, M] where the columns are orthonormal.
|
size [N, M] where the columns are orthonormal.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, dim=-1):
|
def __init__(self, dim=-1):
|
||||||
"""
|
"""
|
||||||
Initialize the OrthogonalBlock module.
|
Initialize the OrthogonalBlock module.
|
||||||
@@ -30,8 +31,10 @@ 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("The dimension where to orthogonalize is greater\
|
raise Warning(
|
||||||
than the other dimensions")
|
"The dimension where to orthogonalize is greater\
|
||||||
|
than the other dimensions"
|
||||||
|
)
|
||||||
|
|
||||||
result = torch.zeros_like(X)
|
result = torch.zeros_like(X)
|
||||||
# normalize first basis
|
# normalize first basis
|
||||||
@@ -42,9 +45,11 @@ class OrthogonalBlock(torch.nn.Module):
|
|||||||
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)
|
||||||
for j in range(i):
|
for j in range(i):
|
||||||
v -= torch.sum(v * torch.select(result, self.dim, j),
|
v -= torch.sum(
|
||||||
dim=self.dim, keepdim=True) * torch.select(
|
v * torch.select(result, self.dim, j),
|
||||||
result, self.dim, j)
|
dim=self.dim,
|
||||||
|
keepdim=True,
|
||||||
|
) * 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
|
||||||
Reference in New Issue
Block a user