Hooked
Hooked

Reputation: 88198

Reduce an array of matrices in tensorflow

Functions like tf.reduce_mean and tf.reduce_prod perform element wise operations to reduce a tensor along an axis. I have a tensor R with shape (1000, 3, 3), a list of 3x3 matrices. What I'd like to do is matrix multiply them so I remain with a single 3x3 matrix. If this was numpy I could use

np.linalg.multi_dot(R)

How can I do this in tensorflow?

Upvotes: 2

Views: 636

Answers (1)

GeertH
GeertH

Reputation: 1768

You can use tf.scan: tf.scan(lambda a, b: tf.matmul(a, b), R)[-1]

import tensorflow as tf
import numpy as np

R = np.random.rand(10, 3, 3)
R_reduced = np.linalg.multi_dot(R)

R_reduced_t = tf.scan(lambda a, b: tf.matmul(a, b), R)[-1]

with tf.Session() as sess:
  R_reduced_val = sess.run(R_reduced_t)
  diff = R_reduced_val - R_reduced
  print(diff)

This prints:

[[ -3.55271368e-15   0.00000000e+00   0.00000000e+00]
 [  1.77635684e-15   0.00000000e+00   3.55271368e-15]
 [ -1.77635684e-15   3.55271368e-15   0.00000000e+00]]

Upvotes: 5

Related Questions