Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outer product in tensorflow

Tags:

tensorflow

In tensorflow, there are nice functions for entrywise and matrix multiplication, but after looking through the docs, I cannot find any internal function for taking an outer product of two tensors, i.e., making a bigger tensor by all possible products of elements of smaller tensors (like numpy.outer):

v_{i,j} = x_i*h_j

or

M_{ij,kl} = A_{ij}*B_{kl}

Does tensorflow have such a function?

like image 478
co9olguy Avatar asked Nov 22 '15 17:11

co9olguy


People also ask

How do you calculate outer product?

If the two vectors have dimensions n and m, then their outer product is an n × m matrix.

What is axes in TF Tensordot?

Tensordot (also known as tensor contraction) sums the product of elements from a and b over the indices specified by axes . This operation corresponds to numpy. tensordot(a, b, axes) . Example 1: When a and b are matrices (order 2), the case axes=1 is equivalent to matrix multiplication.

What is Tensorflow Matmul?

Multiplies matrix a by matrix b , producing a * b . The inputs must, following any transpositions, be tensors of rank >= 2 where the inner 2 dimensions specify valid matrix multiplication arguments, and any further outer dimensions match.

What is a double dot product?

A double dot product is the two tensor's contraction according to the first tensor's last two values and the second tensor's first two values. It contains two definitions.


1 Answers

Yes, you can do this by taking advantage of the broadcast semantics of tensorflow. Size the first out to size 1xN of itself, and the second to size Mx1 of itself, and you'll get a broadcast to MxN of all of the results when you multiply them.

(You can play around with the same thing in numpy to see how it behaves in a simpler context, btw:

a = np.array([1, 2, 3, 4, 5]).reshape([5,1])
b = np.array([6, 7, 8, 9, 10]).reshape([1,5])
a*b

How exactly you do it in tensorflow depends a bit on which axes you want to use and what semantics you want for the resulting multiply, but the general idea applies.

like image 105
dga Avatar answered Sep 22 '22 20:09

dga