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
Solution 1:
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]]
Post a Comment for "Reduce An Array Of Matrices In Tensorflow"