用不是标量的值填充张量

Fill a tensor with a value that is not a scalar

我正在尝试使用 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)

问题是 subtract 在一个元素一个元素的基础上工作,所以我必须创建一个与点列表具有相同行数的质心张量,但没有方法可以做到这一点。

(用数学术语来说)

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

感谢任何帮助!

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

tf.reset_default_graph()
example_points = np.array([[1, 1], [2, 2], [3, 3]], dtype=np.float32)
example_point_list = tf.placeholder(tf.float32)
centroid = tf.reduce_mean(example_point_list, 0)
result = example_point_list - centroid
sess = tf.InteractiveSession()

sess.run(result, feed_dict={example_point_list: example_points})

结果

array([[-1., -1.],
       [ 0.,  0.],
       [ 1.,  1.]], dtype=float32)

如果你真的想显式地平铺质心向量,你可以使用 shape 运算符来实现,它可以在运行时获得形状

tf.reset_default_graph()
example_point_list0 = np.array([[1, 1], [2, 2], [3, 3]], dtype=np.float32)
example_point_list = tf.placeholder(tf.float32)

# get number of examples from the array: [3]
num_examples = tf.slice(tf.shape(example_points), [0], [1])

# reshape [3] into 3
num_examples_flat = tf.reshape(num_examples, ())

centroid = tf.reduce_mean(example_point_list, 0)

# reshape centroid vector [2, 2] into matrix [[2, 2]]
centroid_matrix = tf.reshape(centroid, [1, -1])

# assemble 3 into vector of dimensions to tile: [3, 1]
tile_shape = tf.pack([num_examples_flat, 1])

# tile [[2, 2]] into [[2, 2], [2, 2], [2, 2]]
centroid_tiled = tf.tile(centroid_matrix, tile_shape)

sess = tf.InteractiveSession()
sess.run(centroid_tiled, feed_dict={example_point_list: example_point_list0})

结果

array([[ 2.,  2.],
       [ 2.,  2.],
       [ 2.,  2.]], dtype=float32)