I have a tensor of size (1000, 30, 16, 16). I'm doing experiments on how to normalize it. I'm trying to normalize across cases, and may be frequency axis etc.
The following works:
a = np.random.rand(1000, 30, 16, 16)
a - a.mean(axis=(0, )) #==> it works
a - a.mean(axis=(0, 1)) #==> successful broadcast
a - a.mean(axis=(0, 1, 2)) #==> works well
a - a.mean(axis=(0, 1, 2, 3)) #==> succesful broadcast of scalar mean to all a values
#Those however fail:
a - a.mean(axis=(2, 3))
#OR:
a - a.mean(axis=(0, 2, 3))
I get:
ValueError: operands could not be broadcast together with shapes (1000, 30, 16, 16) (30,)
It seems that it succesfully completes the missing axes in simple cases like (30, 16, 16)
(16, 16)
(16,)
(1,)
But fails when the missing axes are to the right rather than left, eg: (1000, 30) and it cannot broadcast it to (1000, 30, 16, 16).
To be specific with my question, how can I dictate how broadcasting is being done? For instance, I have (30,) and I want to broadcast it to (1000, 30, 16, 16)
It throws an error as it fails to broadcast. I have a hacky solution, which permuting the axes and making (30,) comes last so that broadcasting works, but I'm wondering if there's a way to dictate how broadcasting should be done. And furthermore, why is't this being done automatically?