0

I'm finding the best cluster set in my data by getting a result which has the lowest average distance from many k means trials on Tensorflow.

But my code doesn't update initial centroids in each trial so all results are same.

Here's my code1 - tensor_kmeans.py

import numpy as np
import pandas as pd
import random
import tensorflow as tf
from tensorflow.contrib.factorization import KMeans
from sklearn import metrics
import imp
import pickle

# load as DataFrame
pkl = 'fasttext_words_k.pkl'
with open(pkl, 'rb') as f:
    unique_words_in_fasttext = pickle.load(f).T

vector =[]
for i in range(len(unique_words_in_fasttext)):
    vector.append(list(unique_words_in_fasttext.iloc[i,:]))
vector = [np.array(f) for f in vector ]


# Import data
full_data_x = vector


# Parameters
num_steps = 100 # Total steps to train
batch_size = 1024 # The number of samples per batch
n_clusters = 1300 # The number of clusters
num_classes = 100 # The 10 digits
num_rows = 13074
num_features = 300 # Each image is 28x28 pixels


### tensor kmeans ###

# Input images
X = tf.placeholder(tf.float32, shape=[None , num_features])
# Labels (for assigning a label to a centroid and testing)
# Y = tf.placeholder(tf.float32, shape=[None, num_classes])


# K-Means Parameters
kmeans = KMeans(inputs=X, num_clusters=n_clusters, distance_metric='cosine',
                use_mini_batch=True, initial_clusters="random")


# Build KMeans graph
training_graph = kmeans.training_graph()

if len(training_graph) > 6: # Tensorflow 1.4+
    (all_scores, cluster_idx, scores, cluster_centers_initialized,
     cluster_centers_var, init_op, train_op) = training_graph
else:
    (all_scores, cluster_idx, scores, cluster_centers_initialized,
     init_op, train_op) = training_graph

cluster_idx = cluster_idx[0] # fix for cluster_idx being a tuple
avg_distance = tf.reduce_mean(scores)

# Initialize the variables (i.e. assign their default value)
init_vars = tf.global_variables_initializer()

# Start TensorFlow session
sess = tf.Session()

# Run the initializer
sess.run(init_vars, feed_dict={X: full_data_x})
sess.run(init_op, feed_dict={X: full_data_x})

# Training
for i in range(1, num_steps + 1):
    _, d, idx = sess.run([train_op, avg_distance, cluster_idx],
                         feed_dict={X: full_data_x})
    if i % 10 == 0 or i == 1:
        print("Step %i, Avg Distance: %f" % (i, d))


labels = list(range(num_rows))
# Assign a label to each centroid
# Count total number of labels per centroid, using the label of each training
# sample to their closest centroid (given by 'idx')
counts = np.zeros(shape=(n_clusters, num_classes))
for i in range(len(idx)):
    counts[idx[i]] += labels[i]

# Assign the most frequent label to the centroid
labels_map = [np.argmax(c) for c in counts]
labels_map = tf.convert_to_tensor(labels_map)


# Evaluation ops
# Lookup: centroid_id -> label
cluster_label = tf.nn.embedding_lookup(labels_map, cluster_idx)


# assign variables
cluster_list_k = idx

and here's a code outside the code1.

k_li=[]
rotation = 50

best_labels = []
best_k = -1
for i in range(rotation):
    import tensor_kmeans    
    k_li.append(tensor_kmeans.k)
    if len(k_li) > 0:
        for i in range(len(k_li)):
            if k_li[i] > best_k:
                best_labels = tensor_kmeans.cluster_list_k
                best_k = k_li[i]
    tensor_kmeans = imp.reload(tensor_kmeans)

Where can I find the problem? I'm waiting your answer, thank you.

aL_eX
  • 1,453
  • 2
  • 15
  • 30
cornandme
  • 47
  • 2
  • 7

1 Answers1

1

Each time you call KMeans() you should use a new random_seed, i.e.

kmeans = KMeans(inputs=X, num_clusters=n_clusters, distance_metric='cosine',
                use_mini_batch=True, initial_clusters="random", random_seed=SOME_NEW_VALUE)

Otherwise the function KMeans() will assume random_seed=0, so that the results are reproducible (i.e. the results are always the same).

A simple way to resolve your issue would be to make a function out of code1 - tensor_kmeans.py, then calling this function with a new random_seed (as input parameter) for each trial.

vreyespue
  • 731
  • 9
  • 17