【发布时间】:2017-08-09 22:48:38
【问题描述】:
我看到在 tensorflow contrib 库中有一个 Kmeans 集群的实现。但是,我无法进行为 2D 点估计聚类中心的简单操作。
代码:
## Generate synthetic data
N,D = 1000, 2 # number of points and dimenstinality
means = np.array([[0.5, 0.0],
[0, 0],
[-0.5, -0.5],
[-0.8, 0.3]])
covs = np.array([np.diag([0.01, 0.01]),
np.diag([0.01, 0.01]),
np.diag([0.01, 0.01]),
np.diag([0.01, 0.01])])
n_clusters = means.shape[0]
points = []
for i in range(n_clusters):
x = np.random.multivariate_normal(means[i], covs[i], N )
points.append(x)
points = np.concatenate(points)
## construct model
kmeans = tf.contrib.learn.KMeansClustering(num_clusters = n_clusters)
kmeans.fit(points.astype(np.float32))
我收到以下错误:
InvalidArgumentError (see above for traceback): Shape [-1,2] has negative dimensions
[[Node: input = Placeholder[dtype=DT_FLOAT, shape=[?,2], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
我想我做错了什么,但无法从文档中弄清楚。
编辑:
我使用input_fn 解决了它,但它真的很慢(我必须将每个集群中的点数减少到 10 才能看到结果)。为什么会这样,我怎样才能让它更快?
def input_fn():
return tf.constant(points, dtype=tf.float32), None
## construct model
kmeans = tf.contrib.learn.KMeansClustering(num_clusters = n_clusters, relative_tolerance=0.0001)
kmeans.fit(input_fn=input_fn)
centers = kmeans.clusters()
print(centers)
已解决:
似乎应该设置一个相对容差。所以我只改变了一行,它工作正常。
kmeans = tf.contrib.learn.KMeansClustering(num_clusters = n_clusters, relative_tolerance=0.0001)
【问题讨论】:
-
你运行的是什么版本的TF?
标签: python tensorflow