Tensor multiplication in Tensorflow

11,096

Try

tf.tensordot(A_tf, B_tf,axes = [[1], [0]])

For example:

x=tf.tensordot(A_tf, B_tf,axes = [[1], [0]])
x.get_shape()
TensorShape([Dimension(5), Dimension(2), Dimension(3)])

Here is tensordot documentation, and here is the relevant github repository.

Share:
11,096
Nipun Batra
Author by

Nipun Batra

Nipun Batra is an Assistant Professor in Computer Science at IIT Gandhinagar. He previously completed his postdoc from University of Virginia. He completed his PhD. from IIIT Delhi where he was a TCS PhD fellow. His group broadly works on machine learning/AI/sensors/IoT for computational sustainability problems like smart buildings and air quality. His work has been awarded several awards, including, the best PhD presentation at ACM Sensys, best demo at ACM Buildsys, and a best video nominee at ACM KDD.

Updated on June 11, 2022

Comments

  • Nipun Batra
    Nipun Batra almost 2 years

    I am trying to carry out tensor multiplication in NumPy/Tensorflow.

    I have 3 tensors- A (M X h), B (h X N X s), C (s X T).

    I believe that A X B X C should produce a tensor D (M X N X T).

    Here's the code (using both numpy and tensorflow).

    M = 5
    N = 2
    T = 3
    h = 2
    s = 3
    A_np = np.random.randn(M, h)
    C_np = np.random.randn(s, T)
    B_np = np.random.randn(h, N, s)
    
    A_tf = tf.Variable(A_np)
    C_tf = tf.Variable(C_np)
    B_tf = tf.Variable(B_np)
    
    # Tensorflow
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        print sess.run(A_tf)
        p = tf.matmul(A_tf, B_tf)
        sess.run(p)
    

    This returns the following error:

    ValueError: Shape must be rank 2 but is rank 3 for 'MatMul_2' (op: 'MatMul') with input shapes: [5,2], [2,2,3].
    

    If we try the multiplication only with numpy matrices, we get the following errors:

    np.multiply(A_np, B_np)
    
    ValueError: operands could not be broadcast together with shapes (5,2) (2,2,3)
    

    However, we can use np.tensordot as follows:

    np.tensordot(np.tensordot(A_np, B_np, axes=1), C_np, axes=1)
    

    Is there an equivalent operation in TensorFlow?

    Answer

    In numpy, we would do as follows:

    ABC_np = np.tensordot(np.tensordot(A_np, B_np, axes=1), C_np, axes=1)
    

    In tensorflow, we would do as follows:

    AB_tf = tf.tensordot(A_tf, B_tf,axes = [[1], [0]])
    AB_tf_C_tf = tf.tensordot(AB_tf, C_tf, axes=[[2], [0]])
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        ABC_tf = sess.run(AB_tf_C_tf)
    

    np.allclose(ABC_np, ABC_tf) return True.