Skip to content

Enhance ContrastiveLoss to avoid warning #3872

@yiheng-wang-nv

Description

@yiheng-wang-nv

Call ContrastiveLoss will see a warning message:

To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).

Simple code to reproduce this issue:

from monai.losses import ContrastiveLoss
import torch

inp = torch.randn([2, 10])
target = torch.randn([2, 10])
loss = ContrastiveLoss(batch_size=2)
loss(inp, target)

Metadata

Metadata

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