The new labels look like: [0, 1, 1, 0, 0, 1, 0]. The original loss function: torch.nn.CrossEntropyLoss() The calculation segment: pred = model(images.to(device)) loss = loss_function(pred, labels.to(device)) (How to use torch.nn.BCEWithLogitsLoss to replace that?

I have gotten some answers from GPTs and Google without executable details.

1

There are 1 answers

0
Karl On BEST ANSWER

If you have your labels in that format already, you can just swap the loss function.

import torch
import torch.nn as nn

loss_fn = nn. BCEWithLogitsLoss()

logits = torch.randn(3)
labels = torch.tensor([1, 0, 1]).float()

loss = loss_fn(logits, labels)