-
Notifications
You must be signed in to change notification settings - Fork 2k
Broadcasting dimension seems counter-intuitive #927
Comments
Hi @AlexJCross, Thanks for raising this issue! This is actually something that I always wanted to double-check with another first time user of the elementwise operations. I've been exposed for those methods for so long, that I kind of lost my intuition of what a first time user would expect them to mean. I understand that the "dimension" parameter in the elementwise operations is not intuitive, but I am not sure it is counter-intuitive either. If I didn't know how it worked already, I think it also would not be immediate to me why it would have worked with The current behavior of those methods is based on the following behavior in Python: In [3]: matrix = np.zeros((100, 3))
In [4]: vec = np.array([1,2,3])
In [5]: result = np.add(matrix, np.expand_dims(vec, axis=0))
In [6]: result = np.add(matrix, np.expand_dims(vec, axis=1))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-e3923ebe2c5e> in <module>()
----> 1 result = np.add(matrix, np.expand_dims(vec, axis=1))
ValueError: operands could not be broadcast together with shapes (100,3) (3,1) However, I see that the documentation is certainly not helping, since the documentation for the "dimension" parameter is plain wrong in some cases. The real documentation should have been something along the lines of (let me know if you have ideas to improve it):
Please let me know how did you interpret the dimension parameter at a first look, and if you have any ideas to improve this. Thanks a lot! |
Hey @cesarsouza, Thanks very much for the response. I agree that the issue is not clear cut!
My (initial) interpretation was that However, thinking about it this morning, I've decided my thinking was a bit woolly and doesn't readily generalise to higher dimensions. If I were to Agree the documentation not being wrong would help (although I only went to read it once my code bombed out). To my mind, the clearest thing to do is to be more explicit that `double[,] broadcasted1 = matrix.Add(vec, expandDimension: 0); // preferred` Numpy does exactly that and it makes it a lot clearer I think: `double[,] broadcasted1 = matrix.Add(vec, isRowVector: true);` Regarding the proposed documentation...
I'm sorry but I'm afraid I don't understand this. Are you proposing changing the current behaviour? If currently set to 0, the vector b must have the same dimensions as columns of A right? Apologies if I have completely misunderstood! Best, |
Hi @AlexJCross,
I am sorry. As you see, this was confusing even for me and I switched the axes in that proposal! I guess that's a sign that my brain keeps telling me that the inverted approach is better (#902), and with two independent sources also reporting that I guess your suggestion for the boolean parameter is indeed the clearest (thanks!), and the fact that it uses a different method signature is a big plus because it would allow us to deprecate the previous methods instead of changing their behavior abruptly. Then maybe what we could consider is to add a new What do you think? Thanks a lot for the feedback on this! Regards, |
- Updates GH-927: Broadcasting dimension seems counter-intuitive
- Adding new overloads for element-wise operations that accept a VectorType enumeration instead of a raw integer; - Marking the previous methods as obsolete.
Added in 3.8.0. |
Hi @cesarsouza ,
Is my intuition failing me here?
Certainly took me by surprise. It's not a massive issue if you get an
IndexOutOfRangeException
but it's a lot harder to spot on square matrices.Thanks,
A
ps - Perhaps my intuition agrees with yours (#902)?
The text was updated successfully, but these errors were encountered: