-
Notifications
You must be signed in to change notification settings - Fork 0
some of the investigation types #6
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
Open
gabar92
wants to merge
6
commits into
linear_predictor
Choose a base branch
from
similarity_based_cross_entropy
base: linear_predictor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f0ac81
some of the investigation types
gabar92 a2a52cb
changing infonce loss
gabar92 7b21875
renaming runname
gabar92 7937868
extend info_nce based loss to scheduled topk cossim
gabar92 4ac733c
adding epoch to loss
gabar92 3ab18d9
removing true positive from nominator
gabar92 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 hidden or 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 |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| from scale_grad import ScaleGrad | ||
| from mp_utils import AllGather | ||
| import copy | ||
|
|
||
| __all__ = ['simclr_loss'] | ||
|
|
||
|
|
@@ -24,7 +25,9 @@ def __init__(self, tau: float = 0.07, grad_scaling=False): | |
| self.tau = tau | ||
| self.grad_scaling = grad_scaling | ||
|
|
||
| def forward(self, z1: torch.Tensor, z2: torch.Tensor) -> torch.Tensor: | ||
| def forward(self, z1: torch.Tensor, z2: torch.Tensor, epoch) -> torch.Tensor: | ||
|
|
||
| run_type = ["label_smoothing", "top_k_based_on_cos_sim", "scheduled_cos_sim", "scheduled_top_k_cos_sim"][3] | ||
|
|
||
| # Collect from all gpu | ||
| z1 = AllGather.apply(z1) | ||
|
|
@@ -41,16 +44,89 @@ def forward(self, z1: torch.Tensor, z2: torch.Tensor) -> torch.Tensor: | |
| ones = torch.ones(n // 2).to(z.device) | ||
| labels = ones.diagflat(n // 2) + ones.diagflat(-n // 2) | ||
|
|
||
| if run_type == "label_smoothing": | ||
| labels = labels.clamp(min=0.01) | ||
| labels = F.normalize(labels, dim=1) | ||
|
|
||
| # Note: The following code might require a large amount of memory | ||
| # in case of large batch size | ||
| sim_m = z @ z.T | ||
| F.normalize(z, dim=1, p=2) | ||
| if run_type == "top_k_based_on_cos_sim": | ||
| # pairwise similarities between embeddings | ||
| cos_sim_m = F.softmax(z @ z.T, dim=1) | ||
|
Owner
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. miért van softmax? |
||
| # masking for ruling-out pairs from the same siamese branch | ||
| branch_mask_zeros = torch.zeros((int(cos_sim_m.shape[0]/2), int(cos_sim_m.shape[1]/2))) | ||
| branch_mask_ones = torch.ones((int(cos_sim_m.shape[0]/2), int(cos_sim_m.shape[1]/2))) | ||
| branch_mask = torch.cat( | ||
| [torch.cat([branch_mask_zeros, branch_mask_ones], dim=1), | ||
| torch.cat([branch_mask_ones, branch_mask_zeros], dim=1)], | ||
| dim=0 | ||
| ).to(cos_sim_m.device) | ||
| cos_sim_m = torch.mul(branch_mask, cos_sim_m) | ||
| # selecting top-k similarities | ||
| top_k, top_k_indices = torch.topk(cos_sim_m, k=10, dim=1) | ||
| top_sim = torch.zeros_like(cos_sim_m).scatter(dim=1, index=top_k_indices, src=torch.full_like(top_k, 0.75)) | ||
| labels = torch.max(labels, top_sim) | ||
| elif run_type == "scheduled_cos_sim": | ||
| # pairwise similarities between embeddings | ||
| cos_sim_m = F.softmax(z @ z.T, dim=1) | ||
| # masking for ruling-out pairs from the same siamese branch | ||
| branch_mask_zeros = torch.zeros((int(cos_sim_m.shape[0]/2), int(cos_sim_m.shape[1]/2))) | ||
| branch_mask_ones = torch.ones((int(cos_sim_m.shape[0]/2), int(cos_sim_m.shape[1]/2))) | ||
| branch_mask = torch.cat( | ||
| [torch.cat([branch_mask_zeros, branch_mask_ones], dim=1), | ||
| torch.cat([branch_mask_ones, branch_mask_zeros], dim=1)], | ||
| dim=0 | ||
| ).to(cos_sim_m.device) | ||
| cos_sim_m = torch.mul(branch_mask, cos_sim_m) | ||
| # scheduling | ||
| schedule_start_epoch = 30 | ||
| schedule_length_epoch = 20 | ||
| if epoch >= schedule_start_epoch: | ||
| schedule = torch.linspace(start=0, end=1, steps=schedule_length_epoch) | ||
| index_based_on_epoch = min(epoch - schedule_start_epoch, schedule_length_epoch - 1) | ||
| labels = (1 - schedule[index_based_on_epoch]) * labels + schedule[index_based_on_epoch] * cos_sim_m | ||
| labels = F.softmax(labels, dim=1) | ||
| elif run_type == "scheduled_top_k_cos_sim": | ||
| # hyper-parameters | ||
| num_of_top_k = 10 # 10 | ||
| schedule_start_epoch = 30 # 30 | ||
| schedule_length_epoch = 20 | ||
| # pairwise similarities between embeddings | ||
| cos_sim_m = F.softmax(z @ z.T, dim=1) | ||
| # masking for ruling-out pairs from the same siamese branch | ||
| branch_mask_zeros = torch.zeros((int(cos_sim_m.shape[0] / 2), int(cos_sim_m.shape[1] / 2))) | ||
| branch_mask_ones = torch.ones((int(cos_sim_m.shape[0] / 2), int(cos_sim_m.shape[1] / 2))) | ||
| branch_mask = torch.cat( | ||
| [torch.cat([branch_mask_zeros, branch_mask_ones], dim=1), | ||
| torch.cat([branch_mask_ones, branch_mask_zeros], dim=1)], | ||
| dim=0 | ||
| ).to(cos_sim_m.device) | ||
| cos_sim_m = torch.mul(branch_mask, cos_sim_m) | ||
| # selecting top-k similarities | ||
| top_k, top_k_indices = torch.topk(cos_sim_m, k=num_of_top_k, dim=1) | ||
| top_sim = torch.zeros_like(cos_sim_m).scatter(dim=1, index=top_k_indices, src=torch.full_like(top_k, 0.75)) | ||
| labels_based_on_similarity = torch.max(labels, top_sim) | ||
| # scheduling | ||
| if epoch >= schedule_start_epoch: | ||
| schedule = torch.linspace(start=0, end=1, steps=schedule_length_epoch) | ||
| index_based_on_epoch = min(epoch - schedule_start_epoch, schedule_length_epoch - 1) | ||
| labels = (1 - schedule[index_based_on_epoch]) * labels + schedule[index_based_on_epoch] * labels_based_on_similarity | ||
| labels = F.normalize(labels, dim=1) | ||
|
|
||
| # This is a bit of cheat. Instead of removing cells from | ||
| # the matrix where i==j, instead we set it to a very small value | ||
| sim_m = sim_m.fill_diagonal_(-10) / self.tau | ||
|
|
||
| # Get probability distribution | ||
| sim_m = torch.nn.functional.log_softmax(sim_m, dim=1) | ||
| sim_true_positive = sim_m.clone() | ||
| sim_auxiliary_positive = sim_m.clone() | ||
| true_positive_mask = torch.roll(torch.eye(sim_m.shape[0]), int(sim_m.shape[0]/2), dims=0).bool().cuda() | ||
| sim_auxiliary_positive.masked_fill_(true_positive_mask, (-10 / self.tau)) | ||
| sim_true_positive = torch.nn.functional.log_softmax(sim_true_positive, dim=1) | ||
| sim_auxiliary_positive = torch.nn.functional.log_softmax(sim_auxiliary_positive, dim=1) | ||
| sim_m = torch.where(true_positive_mask.cuda(), sim_true_positive, sim_auxiliary_positive) | ||
|
|
||
| # Choose values on which we calculate the loss | ||
| loss = -torch.sum(sim_m * labels) / n | ||
|
|
||
This file contains hidden or 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
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.
Ez szerintem nem inplace operátor, így nem csinál semmit
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.
Huh, eddig igy futtattok a simclr-t?
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.
Oh semmi, teljesen masik repo. Oke :D