Update: Problem solved by changing the code as follows:
if isinstance(per_class_accuracy[i], torch.Tensor):
    per_class_accuracy[i] = per_class_accuracy[i].cpu().numpy()

and
if isinstance(best_acc, torch.Tensor):
    best_acc = best_acc.cpu().numpy()

and
if isinstance(best_acc, torch.Tensor):
    class_acc_dict['accuracy'] = best_acc.cpu().numpy()
else:
    class_acc_dict['accuracy'] = best_acc

It still seems to me that this is a bug, since the problem occurred on only one dataset.