I am trying to understand the following rule of numpy.dot():
"When a is ND array, b is MD array(where M>=2). The dot product is defindes as the sum product over the last axis of a and the second-to-last axis of b"
What I want to understand is, how the calculation looks in detail for a specific example:
a = np.array([[[2,3,4],[5,6,7],[1,2,3]],[[1,3,4],[7,1,2],[6,2,1]]])
print(a)
[[[2 3 4]
[5 6 7]
[1 2 3]]
[[1 3 4]
[7 1 2]
[6 2 1]]]
b = np.array([[1 , 2, 3],[4, 5 ,6],[7, 8, 9]])
print (b)
b = [[1 2 3]
[4 5 6]
[7 8 9]]
np.dot(a,b) = [[[ 42 51 60]
[ 78 96 114]
[ 30 36 42]]
[[ 41 49 57]
[ 25 35 45]
[ 21 30 39]]]
I couldn't seem to figure out how to get the first value. I understood the other rules of the numpy.dot() definition, but not this last one.
From the documentation ofnumpy.dot
:
If a is an ND array and b is an MD array (where M>=2), it is a sum product over the last axis of a and the second-to-last axis of b:
dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
In your example, a
has shape (2,3,3) and the axis are (0,1,2). So the last axis of a
is 2. b
has shape (3,3) and axis are (0,1). The meaning of second to last axis is penultimate axis. Since b has just 2 axis, the penultimate axis is 0.
Data along last axis of a: [2,3, 4] Data along penultimate axis of b: [1, 4,7]
sum product = sum([2*1,3*4,4*7])
= 42.
Same logic can be applied for all values of the output.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.