-
-
Notifications
You must be signed in to change notification settings - Fork 387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Readme update2 #1142
Merged
Merged
Readme update2 #1142
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f174050
readme
Scitator 5578a42
batch overfit fix for reproducibility
Scitator eb8d3f3
batch overfit fix for reproducibility
Scitator 00fe6f5
Merge branch 'master' of github.com:catalyst-team/catalyst into data-…
Scitator c71de89
readme
Scitator 74c111f
readme update
Scitator 6680dca
readme update
Scitator 8985dc2
extra Config/Hydra API fixes
Scitator adf7aa3
extra Config/Hydra API fixes
Scitator c6ec48e
codestyle
Scitator File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from typing import Union | ||
|
||
import torch | ||
from torch import nn | ||
|
||
|
||
class FactorizedLinear(nn.Module): | ||
"""Factorized wrapper for ``nn.Linear`` | ||
|
||
Args: | ||
nn_linear: torch ``nn.Linear`` module | ||
dim_ratio: dimension ration to use after weights SVD | ||
""" | ||
|
||
def __init__(self, nn_linear: nn.Linear, dim_ratio: Union[int, float] = 1.0): | ||
super().__init__() | ||
self.bias = nn.parameter.Parameter(nn_linear.bias.data, requires_grad=True) | ||
u, vh = self._spectral_init(nn_linear.weight.data, dim_ratio=dim_ratio) | ||
# print(f"Doing SVD of tensor {or_linear.weight.shape}, U: {u.shape}, Vh: {vh.shape}") | ||
self.u = nn.parameter.Parameter(u, requires_grad=True) | ||
self.vh = nn.parameter.Parameter(vh, requires_grad=True) | ||
self.dim_ratio = dim_ratio | ||
self.in_features = u.size(0) | ||
self.out_features = vh.size(1) | ||
|
||
@staticmethod | ||
def _spectral_init(m, dim_ratio: Union[int, float] = 1): | ||
u, s, vh = torch.linalg.svd(m, full_matrices=False) | ||
u = u @ torch.diag(torch.sqrt(s)) | ||
vh = torch.diag(torch.sqrt(s)) @ vh | ||
if dim_ratio < 1: | ||
dims = int(u.size(1) * dim_ratio) | ||
u = u[:, :dims] | ||
vh = vh[:dims, :] | ||
# s_share = s[:dims].sum() / s.sum() * 100 | ||
# print(f"SVD eigenvalue share {s_share:.2f}%") | ||
return u, vh | ||
|
||
def extra_repr(self) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [pep8] reported by reviewdog 🐶 |
||
"""Extra representation log.""" | ||
return ( | ||
f"in_features={self.in_features}, " | ||
f"out_features={self.out_features}, " | ||
f"bias=True, dim_ratio={self.dim_ratio}" | ||
) | ||
|
||
def forward(self, x: torch.Tensor): | ||
"""Forward call.""" | ||
return x @ (self.u @ self.vh).transpose(0, 1) + self.bias | ||
|
||
|
||
__all__ = ["FactorizedLinear"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[pep8] reported by reviewdog 🐶
D101 Missing docstring in public class