Change default logit scale in contrastive loss with temperature from parameter to float #510
      
        
          +1
        
        
          −1
        
        
          
        
      
    
  
  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.
  
    
  
    
GitHub actions for #506 show failures in
test_contrastive_loss_with_temperature.pyeven though the changes in that PR do not touch any contrastive loss components. Running e.g.python -m pytest -v tests/modules/losses/test_contrastive_loss_with_temperature.py, even on that PR, there are no failures. But when we run the full test suite, two of the test cases intest_contrastive_loss_with_temperature.pyfail.This is because of how we define the default value of
logit_scaleinContrastiveLossWithTemperature. We set the default to annn.Parameter, which is initialized the first time the class gets imported. But then this parameter is already defined outside of the test class and so we lose isolation of our test cases.The fix is to use a float as the default instead. Since this gets cast to an
nn.Parameteron init anyways, there will be no difference from the user's perspective. But this way we isolate the parameter to an instance of the class instead of creating a global parameter on import.Tested on top of #506. Before the change:
After the change: