Passing the weights to CrossEntropyLoss correctly - PyTorch ...
discuss.pytorch.org › t › passing-the-weights-toMar 10, 2018 · I create the loss function in the init and pass the weights to the loss: weights = [0.5, 1.0, 1.0, 1.0, 0.3, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] class_weights = torch.FloatTensor(weights).cuda() self.criterion = nn.CrossEntropyLoss(weight=class_weights) Then in the update step, I pass the labels of my current batch to the...
CrossEntropyLoss — PyTorch 1.10.1 documentation
pytorch.org › torchweight (Tensor, optional) – a manual rescaling weight given to each class. If given, has to be a Tensor of size C. size_average (bool, optional) – Deprecated (see reduction). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there are multiple elements per sample.