I'm training a neural network for a multi-label classification problem. I have two rows A and B of size BxL (B = batch size, L = number of labels) where A are the actual labels of the minibatch and B are the predictions maded by my model, somethin like that:
A = array([[0., 1., 0.],
[0., 1., 1.],
[0., 1., 0.],
[0., 0., 0.]])
B = array([[1., 1., 0.],
[0., 1., 1.],
[0., 0., 0.],
[0., 1., 0.]])
And I want to count how many samples are classified correctly (that means, how many rows are equals in A and B)
I would like to know if there is a way to do it with tensor/numpy parallel functions...
something like
sum(torch.eq(A,B, axis=0)) # that doesn't exists
You can use numpy
with all
:
np.sum((A == B).all(1))
#1
This works by searching whether all values in each row match.
>>> A == B
array([[False, True, True],
[ True, True, True],
[ True, False, True],
[ True, False, True]])
Gives you where the elements match, then all(axis=1)
returns a boolean of rows where all values are True
:
>>> (A == B).all(1)
array([False, True, False, False])
Showing that the row at index 1
is an exact match between the two arrays.
Then, summing over the boolean array gives you the count of such rows.