Eduardo Barrera
Eduardo Barrera

Reputation: 576

How do I extract indices of non-equivalent entries between two tensors?

I have a tensor with N predictions of N objects' classes, and I have another tensor with the real N target objects' classes. I would like to pull out the tensor indices where my classifier predictions are wrong.

Consider the two following tensors defined as:

import torch
predictions = torch.tensor([ [0], [1], [1], [0], [0], [1] ])
target      = torch.tensor([ [0], [0], [1], [1], [0], [1] ])

I want to find some function where I can pass these two vectors and have returned a list like index_diff = [1, 3]. Does this function exist? My current thoughts are to cast both of these vectors to numpy arrays and then loop through N times and compare each entry at each index, but this seemed a little circuitous to me. Is there an alternative?

Upvotes: 1

Views: 110

Answers (1)

jodag
jodag

Reputation: 22184

Something like

index_diff = (predictions.flatten() != target.flatten()).nonzero().flatten()

should work.

Upvotes: 2

Related Questions