Skip to content

ContrastiveLoss with dynamic batchsize #5070

Closed

Description

@a-parida12

Is your feature request related to a problem? Please describe.
When we instantiate an instance of type class ContrastiveLoss we need to pass in the parameter for batch_size. This assumes that the batch size remains constant all through the training(which is not always the case).

Describe the solution you'd like
As we know and have fixed in the docs that the forward accepts inputs of shape B[F], why not pick up the Batch size dynamically in the forward. Is there a reason why it is not designed in that fashion?

I would be happy to put a pull request if it is okay.

Activity

  1. finalelement commented on Sep 2, 2022

    @finalelement
    Collaborator

    @a-parida12 Please go ahead and send a PR, I feel we should make the dynamic part optional so that we don't break existing code for other users and keep the default dynamic_batch_size=False

    We need insights from @wyli if it's okay for the optional idea :)

  2. a-parida12 commented on Sep 3, 2022

    @a-parida12
    ContributorAuthor

    Thanks, @finalelement! I thought the majority of the losses from PyTorch don't require that the batch size be known and fixed so extending that to this too. But you are right we need to be backward compatible too.

    How about I raise a "warning" when the batch_size parameter is set to let users know it is unnecessary ?

    Also will wait for @wyli 's input and then begin the PR.

  3. a-parida12 commented on Sep 3, 2022

    @a-parida12
    ContributorAuthor

    Also tagging this discussion for the completeness of my motivation Project-MONAI/tutorials#624 (comment)

  4. wyli commented on Sep 6, 2022

    @wyli
    Contributor

    yes please go ahead, would be great to automatically infer the batch size whenever possible.

  5. added 2 commits that reference this issue on Sep 6, 2022
    91a58ed
    b61f3b6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions