In the following code c = np.multiply (a, b.conj()) d = np.abs (np.sum (c, axis=0)/rows) d2 = np.abs (np.tensordot (a, b.conj(), ((0,),(0,)))/rows) print a.shape, b.shape, d.shape, d2.shape The 1st compute steps, where I do multiply and then sum give the result I want. I wanted to try to combine these 2 steps using tensordot, but it's not doing what I want. The print statement outputs this: (1004, 13) (1004, 13) (13,) (13, 13) The correct output should be (13,), but the tensordot output is (13,13). It's supposed to take 2 matrixes, each (1004, 13) and do element-wise multiply, then sum over axis 0.
Hi Neal: The tensordot part: np.tensordot (a, b.conj(), ((0,),(0,)) is returning a (13, 13) array whose [i, j]-th entry is sum( a[k, i] * b.conj()[k, j] for k in xrange(1004) ). -Brad The print statement outputs this:
(1004, 13) (1004, 13) (13,) (13, 13)
The correct output should be (13,), but the tensordot output is (13,13).
It's supposed to take 2 matrixes, each (1004, 13) and do element-wise multiply, then sum over axis 0.
Bradley M. Froehle wrote:
Hi Neal:
The tensordot part: np.tensordot (a, b.conj(), ((0,),(0,))
is returning a (13, 13) array whose [i, j]-th entry is sum( a[k, i] * b.conj()[k, j] for k in xrange(1004) ).
-Brad
The print statement outputs this:
(1004, 13) (1004, 13) (13,) (13, 13)
The correct output should be (13,), but the tensordot output is (13,13).
It's supposed to take 2 matrixes, each (1004, 13) and do element-wise multiply, then sum over axis 0.
Can I use tensordot to do what I want?
It's supposed to take 2 matrixes, each (1004, 13) and do element-wise multiply, then sum over axis 0.
Can I use tensordot to do what I want?
No. In your case I'd just do (a*b.conj()).sum(0). (Assuming that a and b are arrays, not matrices). It is most helpful to think of tensordot as a generalization on matrix multiplication where the axes argument gives the axes of the first and second arrays which should be summed over. a = np.random.rand(4,5,6,7) b = np.random.rand(8,7,5,2) c = np.tensordot(a, b, axes=((1, 3), (2, 1))) # contract over dimensions with size 5 and 7 assert c.shape == (4, 6, 8, 2) # the resulting shape is the shape given by a.shape + b.shape, which contracted dimensions removed. -Brad
participants (2)
-
Bradley M. Froehle
-
Neal Becker