Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary Classification Accuracy calculation error #99

Closed
akozlu opened this issue Oct 18, 2018 · 0 comments
Closed

Binary Classification Accuracy calculation error #99

akozlu opened this issue Oct 18, 2018 · 0 comments

Comments

@akozlu
Copy link
Contributor

akozlu commented Oct 18, 2018

anaconda3/lib/python3.6/site-packages/torchsample/modules/module_trainer.py", line 277, in fit
    metrics_logs = self.metric_container(output_batch, target_batch)
  File "/Users/aysekozlu/anaconda3/lib/python3.6/site-packages/torchsample/metrics.py", line 31, in __call__
    metric) 
anaconda3/lib/python3.6/site-packages/torchsample/modules/module_trainer.py", line 669, in calculate_loss
    return loss_fn(output_batch, target_batch)
anaconda3/lib/python3.6/site-packages/torchsample/metrics.py", line 86, in __call__
    self.correct_count += y_pred_round.eq(y_true).float().sum().data[0]
RuntimeError: The size of tensor a (2) must match the size of tensor b (64) at non-singleton dimension 1`

I think this happens when y_pred has more than one classes (i.e. the neural net output has more than 1 class.) This might be solved easily by finding index of element with highest probability
y_pred = np.argmax(y_pred, axis=1) # get position of max probability
I overwrote the class in the following way and it works, just letting you know.

class Binary_Classification(torchsample.metrics.BinaryAccuracy):
    def __call__(self, y_pred, y_true):
        # print('buraya_girdim')
        y_pred = y_pred.data.numpy()  # Turn it into np arrays
        y_true = y_true.data.numpy()
        y_pred = np.argmax(y_pred, axis=1)  # get position of max probability

        y_pred = y_pred.reshape(len(y_pred), 1)
        y_true = y_true.reshape(len(y_true), 1)
        self.correct_count += np.count_nonzero(y_pred == y_true)
        self.total_count += len(y_pred)
        accuracy = 100. * float(self.correct_count) / float(self.total_count)
        return accuracy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants