def MoG_validation(K): MoG_valid = mog.MoG("data2D.npy") _, X_data, mu, _, sigma_2, log_pi, pi_np = MoG_valid.cluster( K, D, B, 1.0 / 3.0) # _, X_data, mu, _, sigma_2, log_pi, pi_np = MoG_valid.cluster(K, D, B) loss_valid = MoG_valid.cal_loss(MoG_valid.validation.astype(np.float32), mu, D, log_pi, sigma_2) min_idx = MoG_valid.cal_min_idx(X_data, mu, np.sqrt(sigma_2), pi_np, D) data = tf.ones(shape=[ B, ]) division = tf.unsorted_segment_sum(data, min_idx, K, name=None) data_valid = tf.ones(shape=[ (B - (1 - 1 / 3) * B), ]) min_idx_valid = MoG_valid.cal_min_idx( MoG_valid.validation.astype(np.float32), mu, np.sqrt(sigma_2), pi_np, D) division_valid = tf.unsorted_segment_sum(data_valid, min_idx_valid, K, name=None) with tf.Session(): print 'loss_validation:', loss_valid.eval() print 'Total Proportion:', division.eval() / 10000 plot.plot_cluster(min_idx.eval(), X_data, mu, K) plot.plot_valid_cluster(min_idx_valid.eval(), MoG_valid.validation, mu, K)
def k_comparison(K): D = 2 B = 10000 KM = km.k_mean("data2D.npy") _, segment_ids, X_data, mu= KM.cluster(K, D, B) data = tf.ones(shape = [B,]) division = tf.unsorted_segment_sum(data, segment_ids, K, name=None) with tf.Session(): print "K =",K,":",division.eval()/10000 plot.plot_cluster(segment_ids, X_data, mu, K)
def MoG_validation(K): MoG_valid = mog.MoG("data2D.npy") _, X_data, mu, _, sigma_2, log_pi, pi_np = MoG_valid.cluster(K, D, B, 1.0/3.0) loss_valid = MoG_valid.cal_loss(MoG_valid.validation.astype(np.float32), mu, D, log_pi, sigma_2) min_idx = MoG_valid.cal_min_idx(X_data, mu, np.sqrt(sigma_2), pi_np, D) data = tf.ones(shape = [B,]) division = tf.unsorted_segment_sum(data, min_idx, K, name=None) with tf.Session(): print 'loss_validation:', loss_valid.eval() print 'Total Proportion:', division.eval()/10000 plot.plot_cluster(min_idx.eval(), X_data, mu, K)
else: loss_prv = loss_train if (epoch % 100 == 0): print 'epoch', epoch print 'loss', loss_train print 'K =', K print 'loss_training:', loss_train print 'centroid:', mu_final print 'pi_k:', tf.exp(pi_log).eval() print 'sigma_square:', sigma_square min_idx = self.cal_min_idx(self.train, mu_final, np.sqrt(sigma_square), tf.exp(pi_log), D).eval() return res_loss, X_data, mu_final, min_idx, sigma_square, pi_log, tf.exp( pi_log).eval() # When K = 3, compute the loss function for the whole data K = 3 B = 10000 D = 2 mog = MoG("data2D.npy") res_loss, X_plot, mu_plot, min_idx, _, _, pi_1 = mog.cluster(K, D, B) plot.plot_loss(res_loss) plot.plot_cluster(min_idx, X_plot, mu_plot, K)
min_idx = [] record, loss_prv = 0, 0 for epoch in range(training_epochs): loss_np, min_idx, mu, _ = sess.run([loss, cluster, Y, train_op], feed_dict={X: X_train}) if record == 200: break elif loss_prv == loss_np: record += 1 else: loss_prv = loss_np res_loss.append(loss_np) ''' if(epoch % 200 == 0): print epoch, ':' ,loss_np # print Y.eval() ''' return res_loss, min_idx, X_data, mu K = 3 # Define 3 clusters D = 2 #len(mean) # numbers of element per each dataset B = 10000 km = k_mean("data2D.npy") # Required argument: numbers of clusters, dimensions of points, numbers of points res_loss, min_idx, X_data, mu= km.cluster(K, D, B, 1.0/3.0) print mu plot.plot_loss(res_loss) plot.plot_cluster(min_idx, X_data, mu, K)
elif loss_prv == loss_train: record += 1 else: loss_prv = loss_train if epoch % 100 == 0: print "epoch", epoch print "loss", loss_train print "K =", K print "loss_training:", loss_train print "centroid:", mu_final print "pi_k:", tf.exp(pi_log).eval() print "sigma_square:", sigma_square min_idx = self.cal_min_idx(self.train, mu_final, np.sqrt(sigma_square), tf.exp(pi_log), D).eval() return res_loss, X_data, mu_final, min_idx, sigma_square, pi_log, tf.exp(pi_log).eval() # When K = 3, compute the loss function for the whole data K = 3 B = 10000 D = 2 mog = MoG("data2D.npy") res_loss, X_plot, mu_plot, min_idx, _, _, pi_1 = mog.cluster(K, D, B) plot.plot_loss(res_loss) plot.plot_cluster(min_idx, X_plot, mu_plot, K)
record, loss_prv = 0, 0 for epoch in range(training_epochs): loss_np, min_idx, mu, _ = sess.run([loss, cluster, Y, train_op], feed_dict={X: X_train}) if record == 200: break elif loss_prv == loss_np: record += 1 else: loss_prv = loss_np res_loss.append(loss_np) ''' if(epoch % 200 == 0): print epoch, ':' ,loss_np # print Y.eval() ''' return res_loss, min_idx, X_data, mu K = 3 # Define 3 clusters D = 2 #len(mean) # numbers of element per each dataset B = 10000 km = k_mean("data2D.npy") # Required argument: numbers of clusters, dimensions of points, numbers of points res_loss, min_idx, X_data, mu = km.cluster(K, D, B, 1.0 / 3.0) print mu plot.plot_loss(res_loss) plot.plot_cluster(min_idx, X_data, mu, K)