He size of tensor a (16) must match the size of tensor b (53) at non-singleton dimension 1

Hi, trying to calculate Accuracy over the whole data.

The error is because output of the model is 53 and target tensor size is 16(the batch size)

This is the code:

for step,batch in enumerate(val_loader):
    ids = batch['input_ids'].to(device, dtype = torch.long)
    mask = batch['attention_mask'].to(device, dtype = torch.long)
    token_type_ids = batch['token_type_ids'].to(device, dtype = torch.long)
    targets = batch['labels'].to(device)

    with torch.no_grad():
      logits=model(ids,mask,token_type_ids)
    loss=loss_f(logits,targets)
    val_loss.append(loss.item())
    preds=torch.argmax(logits,dim=1).flatten()
    **val_acc_=(preds==targets).cpu().numpy().mean()*100** # this is the error
    val_accuracy.append(val_acc_.item())
  val_loss=np.mean(val_loss)
  val_acc=np.mean(val_accuracy)
  
  return val_loss,val_acc

How do i solve that? or is there any other way to calculate accuracy?

Based on this statement it seems your model changes the batch size of the activations, so check the forward method for view or reshape operations and make sure they are not flattening the batch dimension but use something like x = x.view(x.size(0), -1).