-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmeans.py
22 lines (16 loc) · 985 Bytes
/
kmeans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import tensorflow as tf
class kmeans():
def __init__(self, opts):
self.hidden_dim = opts['encoder_hidden_units'][-1]
self.training_samples_num = opts['training_samples_num']
self.cluster_num = opts['cluster_num']
self.F = tf.Variable(tf.random_normal(shape=(self.training_samples_num, self.cluster_num),mean=0,stddev=0.1), name='kmeans_F', dtype=tf.float32)
def kmeans_optimalize(self, h):
self.H = tf.transpose(h, [1,0], name='kmeans_H') # shape: hidden_dim, training_samples_num, m*N
loss_kmeans = tf.subtract(tf.trace(tf.matmul(self.H, self.H, transpose_a=True)),
tf.trace(tf.matmul(tf.matmul(tf.matmul(self.F, self.H, transpose_a=True, transpose_b=True), self.H), self.F)),
name='l_kmeans')
return loss_kmeans
def update_f(self, new_value):
self.F = tf.assign(self.F, new_value)
return self.F #tensor