Skip to content
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

Builtin cosine_similarity #6

Closed
rwightman opened this issue Aug 4, 2020 · 4 comments · Fixed by #7
Closed

Builtin cosine_similarity #6

rwightman opened this issue Aug 4, 2020 · 4 comments · Fixed by #7

Comments

@rwightman
Copy link

Hello, curious if there is any reason why the projection function uses self defined cosine sim vs something like below

    def _projection(self, p, grad, perturb, delta, wd_ratio, eps):
        wd = 1
        expand_size = [-1] + [1] * (len(p.shape) - 1)
        for view_func in [self._channel_view, self._layer_view]:
            g_view = view_func(grad)
            p_view = view_func(p.data)

            cosine_sim = F.cosine_similarity(g_view, p_view, dim=1, eps=eps).abs_()

            if cosine_sim.max() < delta / math.sqrt(p_view.size(1)):
                p_n = p.data / p_view.norm(dim=1).add_(eps).view(expand_size)
                perturb -= p_n * view_func(p_n * perturb).sum(dim=1).view(expand_size)
                wd = wd_ratio

                return perturb, wd

        return perturb, wd
@rwightman
Copy link
Author

Also, curious why your cosine similarity has an absolute value, I scanned the paper but it wasn't immediately obvious...

@bhheo
Copy link
Collaborator

bhheo commented Aug 5, 2020

Hi

There is no special reason to use self defined cosine sim
I just couldn't think of PyTorch cosine similarity.
I will replace it in the next code release.

I missed the absolute value operation in the paper and it is a mistake.
In if statement of eq (11), we want to check whether the cosine similarity is close to zero or not.
So, it should be
image
I will change it in the next ArXiv version

Thanks for the valuable advices.

@bhheo
Copy link
Collaborator

bhheo commented Aug 27, 2020

Cosine similarity is changed to built-in operation in #7.
Thanks for your contribution.

@SanghyukChun
Copy link
Collaborator

@rwightman
The fixed version is just released as adamp==0.3.0(https://github.com/clovaai/AdamP/releases/tag/v0.3.0).
Thanks for your contribution again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants