Clarifying once and for all the confusion over NumPy’s dot product

Am I the only one who periodically gets confused when dealing with dimensions in NumPy? Today, while reading a Gradio’s documentation page, I came across the following code snippet:

Hey, hey, hey! Why does the dot product of an image (W, H, 3) with a filter (3, 3) is legal? I asked ChatGPT to explain it to me, but it started giving me wrong answers (like saying this doesn’t work) or ignoring my question and started answering something else instead. So, there was no other solution than using my brain (plus reading the documentation, sigh).

If you are also a little confuse by the code above, continue reading.

If a.shape = (I, J, C) and b.shape = (K, C, L), then dot(a, b)[i, j, k, l] = sum(a[i, j, :] * b[k, :, l]). Notice that the last dimension of “a” is equal to the second-to-last dimension of “b”.

Or, in code:

I, J, K, L, C = 10, 20, 30, 40, 50 a = np.random.random((I, J, C)) b = np.random.random((K, C, L)) c = a.dot(b) i, j, k, l = 3, 2, 4, 5 print(c[i, j, k, l]) print(sum(a[i, j, :] * b[k, :, l]))

Output (same result):

13.125012901284713 13.125012901284713

To determine the shape of a dot product beforehand, follow these steps:

Step 1: Consider two arrays, “a” and “b,” with their respective shapes.

# Example shapes for arrays a and b a_shape = (4, 3, 2) b_shape = (3, 2, 5) # Create random arrays with the specified shapes a = np.random.random(a_shape) b =…