Use torch.eq() only for some value in Pytorch
Is there a way to use torch.eq() or a similar function to compute element-based equality but only for some elements?
Let's say I need to know how many 1s are equal in the two tensors but I don't care about other numbers.

Any idea how to do this?

Let's say we have 2 tensors A and B filled with random elements and eventually some 1s somewhere. The tensor C is the result of want you aim for: 

A = torch.rand((2, 3, 3))
B = torch.rand((2, 3, 3))

# fill A and B with some 1s
...

C = (A == 1) * (B == 1)


Using the following tensors we get:

(A) [[[ 0.6151,  1.0000,  0.6515],
         [ 0.3337,  0.4262,  0.0731],
         [ 0.4571,  0.2380,  1.0000]],

        [[ 1.0000,  0.1114,  0.8183],
         [ 0.9178,  1.0000,  1.0000],
         [ 0.8180,  0.8112,  0.2972]]]

(B) [[[ 0.4305,  1.0000,  0.5378],
         [ 0.4171,  0.4365,  0.2805],
         [ 0.1076,  0.1259,  0.9695]],

        [[ 1.0000,  0.0911,  1.0000],
         [ 0.6757,  0.5095,  0.4499],
         [ 0.5787,  1.0000,  1.0000]]]

(C) [[[ 0,  1,  0],
         [ 0,  0,  0],
         [ 0,  0,  0]],

        [[ 1,  0,  0],
         [ 0,  0,  0],
         [ 0,  0,  0]]]

