Reputation: 1
I need to get the outer product of each row of a tensor separately. The code goes like:
input1=K.placeholder(shape=(None, 12)) prod=K.map_fn(someMethod,input1)
the someMethod needs to do the following:
*def someMethod(x):*
## get the outerproduct row-wise of input1 #
outer=x*x.T
## subtract the identity matrix from the outer product #
diff=outer-np.eye(12)
## and return the trace of the difference matrix #
trace=np.trace(diff)
return trace
I expect the trace value to be a scalar but the prod to be a list of input of the batch size? I am using plaidml as the backend, therefore, would like something working with numpy or keras backend to work, or perhaps tensorflow.
Upvotes: 0
Views: 1271
Reputation: 311
The following example performs the requested operation on a tensor x
of shape [None, None]
(variable batch size and variable vector dimensions). It has been tested in Tensorflow 1.13.1 and 2.0RC (tf.placeholder
must be removed for 2.0). Comments assume input shape is [None, 12]
for explanatory purposes.
import tensorflow as tf
x = tf.placeholder(tf.float32, shape=[None, None]) # Remove for TF 2.0
# Explicitly perform batch-wise outer product using Einstein summation notation
outer = tf.einsum('bi, bj -> bij', x, x)
outer.shape
# TensorShape([Dimension(None), Dimension(12), Dimension(12)])
diff = outer - tf.eye(tf.shape(x)[1])
trace = tf.linalg.trace(diff)
trace.shape
# TensorShape([Dimension(None)])
As you can see, mapping a helper function over the batch dimension of the input is not required with Tensorflow. You can learn more about tf.einsum
here.
Upvotes: 0
Reputation: 967
Hello and welcome to Stack Overflow.
For row-wise outer-product of matrix A use the following:
outer_product = np.matmul(A[:,:,np.newaxis], A[:,np.newaxis,:])
Upvotes: 1