功能类似于tf.reduce_mean https://www.tensorflow.org/api_docs/python/tf/reduce_mean and tf.reduce_prod https://www.tensorflow.org/api_docs/python/tf/reduce_prod执行元素明智的操作以减少沿轴的张量。我有一个张量R
有形状(1000, 3, 3)
,3x3 矩阵的列表。我想做的是matrix将它们相乘,这样我就保留了一个 3x3 矩阵。如果这是 numpy 我可以使用
np.linalg.multi_dot(R)
我怎样才能在张量流中做到这一点?
您可以使用tf.scan https://www.tensorflow.org/api_docs/python/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)
这打印:
[[ -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]]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)