PatrickHua / SimSiam

A pytorch implementation for paper 'Exploring Simple Siamese Representation Learning'
MIT License
814 stars 135 forks source link

Implementation of Stopgrad #22

Closed GZYZG closed 3 years ago

GZYZG commented 3 years ago

May I a ask you a question, how did you implment stop grad? I viewed the code, but I didn't find it. Thank you!

PatrickHua commented 3 years ago
def D(p, z, version='simplified'): # negative cosine similarity
    if version == 'original':
        z = z.detach() # stop gradient
        p = F.normalize(p, dim=1) # l2-normalize 
        z = F.normalize(z, dim=1) # l2-normalize 
        return -(p*z).sum(dim=1).mean()

Stop grad is implemented using the detach operation

GZYZG commented 3 years ago

OHHHH, I see, Thank you very much!