Torch accuracy and sklearn accuracy is v different #12311
Answered
by
talhaanwarch
talhaanwarch
asked this question in
Lightning Trainer API: Trainer, LightningModule, LightningDataModule
-
This is the code def test_step(self,batch,batch_idx):
image,label=batch
pred = self(image)
loss=self.criterion(pred.flatten(),label.float()) #calculate loss
acc=self.metrics(pred.flatten(),label)#calculate accuracy
pred=torch.sigmoid(pred)
return {'loss':loss,'acc':acc,'label':label,'pred':pred}
def test_epoch_end(self, outputs):
loss=torch.stack([x["loss"] for x in outputs]).mean().detach().cpu().numpy().round(2)
acc=torch.stack([x["acc"] for x in outputs]).mean().detach().cpu().numpy().round(2)
label=torch.cat([x["label"] for x in outputs]).detach().cpu().numpy().ravel()
pred=torch.cat([x["pred"] for x in outputs]).detach().cpu().numpy().ravel()
pred=pred.astype(int)
print('torch acc',acc)
print(classification_report(label,pred))
print('sklearn',accuracy_score(label,pred)) There is difference of 10-15% between accuracies obtained by torchmetrics and sklearn |
Beta Was this translation helpful? Give feedback.
Answered by
talhaanwarch
Apr 2, 2022
Replies: 2 comments
-
Hi @talhaanwarch, could you keep the description as it was and share how you've fixed it? Someone in the community might have the same issue and this thread could be useful for them :) |
Beta Was this translation helpful? Give feedback.
0 replies
-
This is the solution
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
akihironitta
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the solution