Anano - use tenordot to calculate the point product of two tensors

I want to use tensordot to calculate the point product of a certain dim of two tensors. How:

A is a tensor whose shape is (3, 4, 5) B is a tensor whose shape is (3, 5)

I want to use a dot. The third dim and B the second dim, and get the output, dims (3, 4)

As below:

for i in range(3):
    C[i] = dot(A[i], B[i])

How to do it by tensordot?

+4
source share
2 answers

Well, do you want this in numpy or in Theano? In the case where, as you state, you want to compress the 3 A axis against the 2 axis of B, both are simple:

import numpy as np

a = np.arange(3 * 4 * 5).reshape(3, 4, 5).astype('float32')
b = np.arange(3 * 5).reshape(3, 5).astype('float32')

result = a.dot(b.T)

in Theano it is written as

import theano.tensor as T

A = T.ftensor3()
B = T.fmatrix()

out = A.dot(B.T)

out.eval({A: a, B: b})

(3, 4, 3). , (3, 4), numpy einsum, ,

einsum_out = np.einsum('ijk, ik -> ij', a, b)

Theano einsum. , :

out = (a * b[:, np.newaxis]).sum(2)

Theano

out = (A * B.dimshuffle(0, 'x', 1)).sum(2)
out.eval({A: a, B: b})
+4

einsum, , , tensordot. :

c = np.einsum('ijk,ik->ij', a, b)

, . ( ), ( ->).

  • a 3, 4, 5, ijk
  • b 3, 5 (ik)
  • , c 3, 4 (ij)

, ? .

  • , "" ->, , . , dot.
  • 3, 4, k
  • , c ij
  • , b ik.

:

import numpy as np

a = np.random.random((3, 4, 5))
b = np.random.random((3, 5))

# Looping through things
c1 = []
for i in range(3):
    c1.append(a[i].dot(b[i]))
c1 = np.array(c1)

# Using einsum instead
c2 = np.einsum('ijk,ik->ij', a, b)

assert np.allclose(c1, c2)

tensordot. , . (, - tensordot , !)

+2

Source: https://habr.com/ru/post/1620351/


All Articles