Tensorflow K-均值示例(tf.expand_dims)

Tensorflow K-均值示例(tf.expand_dims),tensorflow,k-means,Tensorflow,K Means,在Tensorflow的Kmeans示例代码中 使用函数“tf.expand_dims”(在张量形状中插入一个1的尺寸)时,点_展开,质心_展开 在计算tf之前,减少_和 为什么在第二个参数中有不同的索引(0,1) import numpy as np import tensorflow as tf points_n = 200 clusters_n = 3 iteration_n = 100 points = tf.constant(np.random.uniform(0, 10, (poin

在Tensorflow的Kmeans示例代码中

使用函数“tf.expand_dims”(在张量形状中插入一个1的尺寸)时,点_展开,质心_展开 在计算tf之前,减少_和

为什么在第二个参数中有不同的索引(0,1)

import numpy as np
import tensorflow as tf
points_n = 200
clusters_n = 3
iteration_n = 100
points = tf.constant(np.random.uniform(0, 10, (points_n, 2)))
centroids = tf.Variable(tf.slice(tf.random_shuffle(points), [0, 0],[clusters_n, -1]))
points_expanded = tf.expand_dims(points, 0)
centroids_expanded = tf.expand_dims(centroids, 1)
distances = tf.reduce_sum(tf.square(tf.subtract(points_expanded, centroids_expanded)), 2)
assignments = tf.argmin(distances, 0)
means = []
for c in range(clusters_n):
    means.append(tf.reduce_mean(tf.gather(points,tf.reshape(tf.where(tf.equal(assignments, c)), [1, -1])), reduction_indices=[1]))
new_centroids = tf.concat(means,0)
update_centroids = tf.assign(centroids, new_centroids)
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    for step in range(iteration_n):
    [_, centroid_values, points_values, assignment_values] = sess.run([update_centroids, centroids, points, assignments])
    print("centroids" + "\n", centroid_values)
plt.scatter(points_values[:, 0], points_values[:, 1], c=assignment_values, s=50, alpha=0.5)
plt.plot(centroid_values[:, 0], centroid_values[:, 1], 'kx', markersize=15)
plt.show()

这样做是为了从每个点减去每个质心。首先,确保你理解广播的概念() 从tf.subtract()链接的。然后,您只需绘制
扩展_点
质心
、和
扩展_质心
的形状,并了解哪些值在哪里“广播”。一旦你这样做了,你会看到广播允许你精确地计算你想要的——从每个质心减去每个点

作为一个健全的检查,因为有200个点,3个质心,每个都是2D,我们应该有200*3*2的差异。这正是我们得到的:

In [53]: points
Out[53]: <tf.Tensor 'Const:0' shape=(200, 2) dtype=float64>

In [54]: points_expanded
Out[54]: <tf.Tensor 'ExpandDims_4:0' shape=(1, 200, 2) dtype=float64>

In [55]: centroids
Out[55]: <tf.Variable 'Variable:0' shape=(3, 2) dtype=float64_ref>

In [56]: centroids_expanded
Out[56]: <tf.Tensor 'ExpandDims_5:0' shape=(3, 1, 2) dtype=float64>

In [57]: tf.subtract(points_expanded, centroids_expanded)
Out[57]: <tf.Tensor 'Sub_5:0' shape=(3, 200, 2) dtype=float64>
[53]中的
:点
出[53]:
在[54]中:扩展点
出[54]:
[55]中:质心
出[55]:
[56]中:质心_展开
出[56]:
[57]中:tf.减法(点扩展,质心扩展)
出[57]:

如果在绘制形状时遇到问题,可以考虑将带有尺寸
(1200,2)
扩展点
广播到尺寸
(3200,2)
,就像沿第一个尺寸复制200x2矩阵3次一样。
质心中的3x2矩阵(形状(3,1,2))在第二维度上被复制了200次。

谢谢你的解释