<div dir="ltr">You may find np.einsum() more intuitive than np.dot() for aligning axes -- it's certainly more explicit.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Apr 19, 2019 at 3:59 PM C W <<a href="mailto:tmrsg11@gmail.com">tmrsg11@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr">Thanks, you are right. I overlooked it's for addition.<div><br></div><div>The original problem was that I have matrix X (RBG image, 3 layers), and vector y. </div><div><br></div><div>I wanted to do np(X, y.T).</div><div><div>>>> X.shape   # 100 of 28 x 28 matrix</div><div>(100, 28, 28)</div><div>>>> y.shape   # Just one 28 x 28 matrix </div><div>(1, 28, 28)</div></div><div><br></div><div>But, np.dot() gives me four axis shown below,</div><div><div>>>> z = np.dot(X, y.T)</div><div>>>> z.shape</div><div>(100, 28, 28, 1)</div></div><div><br></div><div>The fourth axis is unexpected. Should y.shape be (28, 28), not (1, 28, 28)?</div><div><br></div><div>Thanks again!</div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Apr 19, 2019 at 6:39 PM Andras Deak <<a href="mailto:deak.andris@gmail.com" target="_blank">deak.andris@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Sat, Apr 20, 2019 at 12:24 AM C W <<a href="mailto:tmrsg11@gmail.com" target="_blank">tmrsg11@gmail.com</a>> wrote:<br>
><br>
> Am I miss reading something? Thank you in advance!<br>
<br>
Hey,<br>
<br>
You are missing that the broadcasting rules typically apply to<br>
arithmetic operations and methods that are specified explicitly to<br>
broadcast. There is no mention of broadcasting in the docs of np.dot<br>
[1], and its behaviour is a bit more complicated.<br>
Specifically for multidimensional arrays (which you have), the doc says<br>
<br>
If a is an N-D array and b is an M-D array (where M>=2), it is a sum<br>
product over the last axis of a and the second-to-last axis of b:<br>
dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])<br>
<br>
So your (3,4,5) @ (3,5) would want to collapse the 4-length axis of<br>
`a` with the 3-length axis of `b`; this won't work. If you want<br>
elementwise multiplication according to the broadcasting rules, just<br>
use `a * b`:<br>
<br>
>>> a = np.arange(3*4*5).reshape(3,4,5)<br>
... b = np.arange(4*5).reshape(4,5)<br>
... (a * b).shape<br>
(3, 4, 5)<br>
<br>
<br>
[1]: <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html" rel="noreferrer" target="_blank">https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html</a><br>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org" target="_blank">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div>