Tensorflow 用非标量的值填充张量

Tensorflow 用非标量的值填充张量,tensorflow,Tensorflow,我正在尝试使用tensorflow将点列表移动到原点。从数学上来说,最好的方法是找到点列表的质心,然后用该质心减去点列表 问题:点列表中包含的行数在运行时之前是未知的 迄今为止的代码: import tensorflow as tf example_point_list = tf.constant([[3., 3.], [.2, .2], [.1, .1]]) // but with any number of points centroid = tf.reduce_mean(example

我正在尝试使用tensorflow将点列表移动到原点。从数学上来说,最好的方法是找到点列表的质心,然后用该质心减去点列表

问题:点列表中包含的行数在运行时之前是未知的

迄今为止的代码:

import tensorflow as tf

example_point_list = tf.constant([[3., 3.], [.2, .2], [.1, .1]]) // but with any number of points

centroid = tf.reduce_mean(example_point_list, 0)

// subtract???
origin_point_list = tf.sub(example_point_list, centroid)
问题是减法是一个元素一个元素地工作的,所以我必须创建一个质心张量,它的行数与点列表的行数相同,但是没有方法可以做到这一点

(用数学术语来说)

感谢您的帮助

因为,您不需要平铺行。事实上,不平铺它们并直接从矩阵中减去向量更有效。在你的情况下,看起来是这样的

tf.reset\u default\u graph()
示例_points=np.array([[1,1],[2,2],[3,3]],dtype=np.float32)
示例点列表=tf.placeholder(tf.float32)
质心=tf.减少平均值(示例点列表,0)
结果=示例\u点\u列表-质心
sess=tf.InteractiveSession()
run(结果,feed_dict={example_point_list:example_points})
结果

数组([-1.,-1.]),
[ 0.,  0.],
[1,1.],dtype=32)
如果确实希望显式平铺质心向量,可以使用
shape
操作符来实现,该操作符可以在运行时获得形状

tf.reset\u default\u graph()
示例_point_list0=np.array([[1,1],[2,2],[3,3]],dtype=np.float32)
示例点列表=tf.placeholder(tf.float32)
#从数组中获取示例数:[3]
num_examples=tf.slice(tf.shape(示例_点),[0],[1])
#将[3]重塑为3
num_examples_flat=tf.重塑(num_examples,())
质心=tf.减少平均值(示例点列表,0)
#将质心向量[2,2]重塑为矩阵[[2,2]]
质心矩阵=tf.重塑(质心,[1,-1])
#将3组装成要平铺的尺寸向量:[3,1]
tile\u shape=tf.pack([num\u示例\u flat,1])
#将[[2,2]]平铺到[[2,2]、[2,2]、[2,2]]
质心_平铺=tf.平铺(质心_矩阵,平铺形状)
sess=tf.InteractiveSession()
run(质心平铺,提要dict={example\u point\u list:example\u point\u list0})
结果

数组([[2,2.],
[ 2.,  2.],
[2,2.]],dtype=32)
A = [[1, 1],
[2, 2]
[3, 3]]

B = avg(A) // [2, 2]

// step I need to do but do not know how to do it
B -> B1 // [[2, 2], [2, 2], [2, 2]]

Result = A - B1