Compare two different shapes of tensors

Hi,
I have two Tensors.One is [16],other [1]. I want to compare that whether both of them have the same value and return a bool result of shape[16].
How to do that?
thank you for your time and consideration!

a = torch.Tensor([1,2,3,4,5])
b = torch.Tensor([4])

print(a.numpy() == np.tile(b.item(), a.size()[0]))
# [False False False  True False]

Is this an answer for your question :slight_smile:. It benefits from numpy, but i don’t know full pytorch solution if there is.

1 Like

you can use torch. eq ( input , other , out=None ) → Tensor .But torch.eq() only returns torch.ByteTensor with (0/1)s not bool vaule. There is another function torch. equal ( tensor1 , tensor2 ) → bool, but it doesn’t do broadcasting, only return a single bool value with shape 1.

a = torch.tensor([1, 2, 3])
b = torch.tensor([1])
c = torch.eq(a, b)
# c is a tensor([1, 0, 0], dtype=torch.uint8)
d = c.numpy().astype('bool') 
# d is a array([ True, False, False], dtype=bool)

In pytorch, there are 100+ Tensor operations including transposing, indexing, slicing, mathematical operations, linear algebra, random numbers, etc., are described here.

1 Like

Thank you for your reply :grin:

thank you ! this is what I want