关于这个链接,(链接)我尝试使用tf.contrib.factorization.KMeansClustering来进行集群。以下简单代码工作正常:
import numpy as np
import tensorflow as tf
# ---- Create Data Sample -----
k = 5
n = 100
variables = 5
points = np.random.uniform(0, 1000, [n, variables])
# ---- Clustering -----
input_fn=lambda: tf.train.limit_epochs(tf.convert_to_tensor(points, dtype=tf.float32), num_epochs=1)
kmeans=tf.contrib.factorization.KMeansClustering(num_clusters=6)
kmeans.train(input_fn=input_fn)
centers = kmeans.cluster_centers()
# ---- Print out -----
cluster_indices = list(kmeans.predict_cluster_index(input_fn))
for i, point in enumerate(points):
cluster_index = cluster_indices[i]
print ('point:', point, 'is in cluster', cluster_index, 'centered at', centers[cluster_index])我的问题是,为什么这个"input_fn“代码会起作用?如果我将代码更改为这个,它将运行到一个无限循环。为什么??
input_fn=lambda:tf.convert_to_tensor(points, dtype=tf.float32)从文档(在这里)看,train()似乎期待着input_fn的参数,它只是一个A 'tf.data.Dataset‘对象,就像张量(X)一样。那么,为什么我必须对lambda: tf.train.limit_epochs()做所有这些棘手的事情呢?
任何熟悉tensorflow估值器基本原理的人能帮助解释吗?非常感谢!
发布于 2018-03-23 20:06:19
我的问题是,为什么这个"input_fn“代码会起作用?如果我将代码更改为这个,它将运行到一个无限循环。为什么??
文档指出,input_fn被反复调用,直到它返回一个tf.errors.OutOfRangeError为止。用tf.train.limit_epochs装饰张量可以确保最终产生错误,这向KMeans发出了停止训练的信号。
https://stackoverflow.com/questions/49418325
复制相似问题