Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何一次对多个维度执行reduce_op?_Python_Tensorflow - Fatal编程技术网

Python 如何一次对多个维度执行reduce_op?

Python 如何一次对多个维度执行reduce_op?,python,tensorflow,Python,Tensorflow,假设我有一个N阶张量列表,表示为N+1阶张量。例如,将100个10x20矩阵列为具有形状(100,10,20)的秩3张量。我需要对每个矩阵执行相同的操作:对每个矩阵的所有元素求和,第I个矩阵值的平均值和中值。 是否可以沿轴0执行类似于tf.math.reduce\u sum,tf.math.reduce\u mean,tf.contrib.distributions.percentile,但沿轴0一次计算每个元素的整个矩阵 例如: matricesList = tf.constant([[[1,

假设我有一个N阶张量列表,表示为N+1阶张量。例如,将100个10x20矩阵列为具有形状(100,10,20)的秩3张量。我需要对每个矩阵执行相同的操作:对每个矩阵的所有元素求和,第I个矩阵值的平均值和中值。 是否可以沿轴0执行类似于
tf.math.reduce\u sum
tf.math.reduce\u mean
tf.contrib.distributions.percentile
,但沿轴0一次计算每个元素的整个矩阵

例如:

matricesList = tf.constant([[[1,1],[1,1]],
                           [[2,2],[2,2]]])

op = sum_matrices_along_axis(matrixList)

使用预期的
op=[4,8]

可以将多个维度传递给缩减操作的
参数:

import tensorflow as tf

matricesList = tf.constant([[[1, 1], [1, 1]],
                            [[2, 2], [2, 2]]])
matricesSum = tf.reduce_sum(matricesList, axis=[1, 2])
with tf.Session() as sess:
    print(sess.run(matricesSum))
    # [4 8]
即使您事先不知道尺寸的数量,也可以减少“除第一个尺寸外的所有尺寸”:

import tensorflow as tf

# The number of dimensions of tensorsList is unspecified
tensorsList = tf.placeholder(tf.int32)
# Dimensions from one to the last one
reduceDims = tf.range(1, tf.rank(tensorsList))
tensorsSum = tf.reduce_sum(tensorsList, axis=reduceDims)
with tf.Session() as sess:
    matrices = [[[1, 1], [1, 1]],
                [[2, 2], [2, 2]]]
    print(sess.run(tensorsSum, feed_dict={tensorsList: matrices}))
    # [4 8]

非常感谢,我不知道可以通过多轴,但最重要的是,我完全没有想到我可以简单地展平数组中N+1秩张量的元素,并计算展平张量矩阵上的所有运算,因为我不介意它们的维数。@LolAsdOmgWtfAfk是的,没错,你也可以做
tf.reduce_sum(tf.reformate(tensorsList)[tf.shape(tensorsList)[0],-1]),axis=1)
。对不起,N秩张量*我将尝试最快的选项,谢谢:)