microsoft / DeBERTa

The implementation of DeBERTa
MIT License
1.91k stars 216 forks source link

Index tensor must have the same number of dimensions as self tensor #81

Closed Bushra178 closed 2 years ago

Bushra178 commented 2 years ago

I am trying to train my model but i am getting "Index tensor must have the same number of dimensions as self tensor" error. The error occurs in line targets = zeros.scatter_(1, targets.unsqueeze(1).data.cpu(), 1). I am stuck on this for days. if anyone has any suggestion...

    def __init__(self, num_classes, eps=0.1, use_gpu=True, label_smooth=True):
    super(CrossEntropyLoss, self).__init__()
    self.num_classes = num_classes
    self.eps = eps if label_smooth else 0
    self.use_gpu = use_gpu
    self.logsoftmax = nn.LogSoftmax(dim=1)

def forward(self, inputs, targets):
    log_probs = self.logsoftmax(inputs)
    zeros = torch.zeros(log_probs.size())
    targets = zeros.scatter_(1, targets.unsqueeze(1).data.cpu(), 1)
    print(targets)
    if self.use_gpu:
        targets = targets.cuda()
    targets = (1 - self.eps) * targets + self.eps / self.num_classes
    return (-targets * log_probs).mean(0).sum()
xgk commented 2 years ago

Please check the document on pytorch website for the usage of scatter.