how is tensorflow reduce_sum finding the intersection in the below code for dice coefficient? In the below code I am not able to grasp how multiplying y_true and y_pred and putting reduce_sum on it gives the intersection between the two
def dice_coefficient(y_true, y_pred):
    numerator = 2 * tensorflow.reduce_sum(y_true * y_pred)
    denominator = tensorflow.reduce_sum(y_true + y_pred)

In numerator, why are we multiplying the predicted and true labels? and how is reduce sum finding the intersection?
If we take an example:
lets say our true pixels were [5,2,3,7,7] and predicted pixels were [4,2,3,9,7].
So we have 3 intersecting pixels hence numerator must be 2*3=6
denominator=5+5=10, hence dice coeff  = 6/10=0.6
However, the way reduce sum is working is this:
[5,2,3,7,7] * [4,2,3,9,7] = [20, 4, 9, 63, 49]
reduce_sum( [20, 4, 9, 63, 49] ) i.e 20+4+9+63+49 = 145
hence, numerator = 2*145 = 290
[5,2,3,7,7] + [4,2,3,9,7] = [9,4,6,16,14]
Denominator = reduce_sum( [9,4,6,16,14] ) i.e 9+4+6+16+14 = 49
hence, dice coeff  = 290/49 = 5.91
How is reduce_sum() finding the intersection of pixels?
 A: Dice coefficient is used to measure the overlap of two sets. It is defined as $$D(X,Y)=2\frac{|X \cap Y|}{|X|+|Y|}$$.
The only way this expression is a valid implementation of the Dice coefficient is if $X$ and $Y$ are sets, where $|X| + |Y| > 0$.
In terms of tensors, one way to denote set membership is to use binary encoding, so the tensors take on the value of 1 whenever an element is a member of some set, and 0 otherwise. Since you mention pixels, I surmise that you're working on a problem that involves boolean masks for your images. These can be represented as binary tensors, taking the value of 1 when the object of interest is present, and 0 otherwise. If you're working with photos of dogs, you would use 1 to denote all of the pixels where dogs are present, 0 otherwise.
Thus the multiplication of two tensors of binary encodings gives you the intersection, because the only time that you get the value of 1 is 1 * 1; all other products yield zero. Summing all of these 1s tells you the cardinality of the intersection. This is what the numerator expresses in your code.
Taking the sum of the binary tensors gives you the cardinality of the two tensors, hence the denominator.
I think the gap in your understanding is the assumption that the outputs are non-binary; if you supply these types of inputs, then there is not a correspondence to the definition of the Dice coefficient.
