[ad_1]
Am I the one one who periodically will get confused when coping with dimensions in NumPy? At the moment, whereas studying a Gradio’s documentation page, I got here throughout the next code snippet:
sepia_filter = np.array([
[0.393, 0.769, 0.189],
[0.349, 0.686, 0.168],
[0.272, 0.534, 0.131],
])
# input_img form (H, W, 3)
# sepia_filter form (3, 3)
sepia_img = input_img.dot(sepia_filter.T) # <- why that is authorized??
sepia_img /= sepia_img.max()
Hey, hey, hey! Why does the dot product of a picture (W, H, 3) with a filter (3, 3) is authorized? I requested ChatGPT to clarify it to me, but it surely began giving me unsuitable solutions (like saying this doesn’t work) or ignoring my query and began answering one thing else as an alternative. So, there was no different answer than utilizing my mind (plus studying the documentation, sigh).
In case you are additionally a bit of confuse by the code above, proceed studying.
From the NumPy dot product documentation (with minor modifications):
If a.form = (I, J, C) and b.form = (Ok, C, L), then dot(a, b)[i, j, k, l] = sum(a[i, j, :] * b[k, :, l]). Discover that the final dimension of “a” is the same as the second-to-last dimension of “b”.
Or, in code:
I, J, Ok, L, C = 10, 20, 30, 40, 50
a = np.random.random((I, J, C))
b = np.random.random((Ok, C, L))
c = a.dot(b)
i, j, okay, l = 3, 2, 4, 5
print(c[i, j, k, l])
print(sum(a[i, j, :] * b[k, :, l]))
Output (identical outcome):
13.125012901284713
13.125012901284713
To find out the form of a dot product beforehand, observe these steps:
Step 1: Think about two arrays, “a” and “b,” with their respective shapes.
# Instance shapes for arrays a and b
a_shape = (4, 3, 2)
b_shape = (3, 2, 5)
# Create random arrays with the required shapes
a = np.random.random(a_shape)
b =…
[ad_2]
Source link