3

Say I have a shape (3, 5, 3) tensor like so:

  x = [[[ 4.,  6.,  6.],
    [ 0.,  0.,  3.],
    [ 6.,  6.,  5.],
    [ 4.,  1.,  8.],
    [ 3.,  6.,  7.]],

   [[ 4.,  0.,  5.],
    [ 4.,  7.,  2.],
    [ 4.,  5.,  3.],
    [ 4.,  2.,  1.],
    [ 3.,  4.,  4.]],

   [[ 0.,  3.,  4.],
    [ 6.,  7.,  5.],
    [ 1.,  2.,  2.],
    [ 3.,  8.,  3.],
    [ 8.,  5.,  7.]]]

And a shape (3, 3, 4) tensor like so:

y = [[[ 3.,  2.,  5.,  4.],
    [ 8.,  7.,  1.,  8.],
    [ 4.,  0.,  5.,  3.]],

   [[ 8.,  7.,  7.,  3.],
    [ 5.,  4.,  0.,  1.],
    [ 6.,  5.,  4.,  4.]],

   [[ 7.,  0.,  1.,  2.],
    [ 7.,  5.,  0.,  6.],
    [ 7.,  5.,  4.,  1.]]]

How would do a matrix multiplication so that the resulting matrix is of shape (3, 5, 4)

Whereby the first element of the matrix is given by the matrix multiplication of

[[ 4.,  6.,  6.],
[ 0.,  0.,  3.],
[ 6.,  6.,  5.],
[ 4.,  1.,  8.],
[ 3.,  6.,  7.]]

and

[[ 3.,  2.,  5.,  4.] 
 [ 8.,  7.,  1.,  8.]
 [ 4.,  0.,  5.,  3.]]

I've tried using tf.tensordot like:

z = tf.tensorflow(x, y, axes = [[2],[1]])

which I believe is multiply the 3rd axis of x with the 2nd axis of y but it gives me a tensor of shape (3, 5, 3, 4). Any ideas?

YellowPillow
  • 4,100
  • 6
  • 31
  • 57

1 Answers1

3

Silly me after reading tf.matmul docs it seems like since the inner dimensions match I can just do tf.matmul(x,y) and it gives me the answer

YellowPillow
  • 4,100
  • 6
  • 31
  • 57