def alternating_from_data(cluster_num, x_train, y_train, alpha_v, epochs=100, index_init=None): n, tmax = np.shape(x_train) assert (np.shape(x_train) == np.shape(y_train)) D0 = np.matmul(x_train, x_train.T) / tmax D1 = np.matmul(x_train, y_train.T) / tmax def _func1(v, ind): result = 0 _z = get_index_matrix(cluster_num, ind) for j in range(cluster_num): result += np.dot(v[j, :], np.dot(D1, _z[:, j])) return result def _func0(ind): result = 0 _z = get_index_matrix(cluster_num, ind) for j in range(cluster_num): result += np.dot(_z[:, j], np.dot(D0, _z[:, j])) return result if index_init is None: res = kmeans_greedy.kmeans_greedy(_func0, cluster_num, n, iter_limit=100) ind_start = res.index else: ind_start = index_init for e in range(epochs): if e == 0: ind_est = ind_start nochange = False else: #print("K={}: epochs {}/{}".format(cluster_num, e, epochs)) res = kmeans_greedy.kmeans_greedy(lambda ind: _func1(v_est, ind), cluster_num, n, iter_limit=50) ind_est = res.index nochange = res.nochange z_est = get_index_matrix(cluster_num, ind_est) v_est, loss = v_step_from_data(x_train, y_train, alpha_v, z_est) if nochange: break theta_est = np.matmul(z_est, v_est) return _ResultInstance(theta_est, z_est, v_est, ind_est, loss)
def alternating(cluster_num, D0, D1, alpha_v, epochs=10, index_init=None): n, _ = np.shape(D0) assert (np.shape(D0) == (n, n)) assert (np.shape(D1) == (n, n)) if index_init is None: mat = np.matmul(random_basis(n, cluster_num).T, D1) res = kmeans_greedy.kmeans_greedy( lambda ind: np.linalg.norm( np.matmul(mat, get_index_matrix(cluster_num, ind)), ord='nuc'), cluster_num, n) ind_start = res.index else: ind_start = index_init for e in range(epochs): if e == 0: ind_est = ind_start z_est = get_index_matrix(cluster_num, ind_start) else: z_est, ind_est, _ = z_step(cluster_num, D1, v_est, ind_old=ind_est) v_est, loss = v_step(D0, D1, alpha_v, z_est) theta_est = np.matmul(z_est, v_est) #print("loss : {}".format(loss)) return _ResultInstance(theta_est, z_est, v_est, ind_est, loss)
def z_step(cluster_num, D1, v, ind_old=None): k, n = v.shape assert (np.shape(D1) == (n, n)) assert (k <= n) mat = np.matmul(v, D1) #check this stupid function!!! <- apparently works... res = kmeans_greedy.kmeans_greedy(lambda ind: np.matrix.trace( np.matmul(mat, get_index_matrix(cluster_num, ind))), cluster_num, n, init_index=ind_old) ind_mat = get_index_matrix(cluster_num, res.index) return ind_mat, res.index, .0
def direct_from_data(cluster_num, x_train, y_train, alpha_v, index_init=None): n, tmax = np.shape(x_train) assert (np.shape(x_train) == np.shape(y_train)) func = lambda ind: -v_step_from_data(x_train, y_train, alpha_v, get_index_matrix(cluster_num, ind))[ 1] # loss res = kmeans_greedy.kmeans_greedy(func, cluster_num, n, init_index=index_init) z_est = get_index_matrix(cluster_num, res.index) v_est, loss = v_step(x_train, y_train, alpha_v, z_est) theta_est = np.matmul(z_est, v_est) # print("loss : {}".format(loss)) return _ResultInstance(theta_est, z_est, v_est, res.index, loss)
def direct(cluster_num, D0, D1, alpha_v, index_init=None): n, _ = np.shape(D0) assert (np.shape(D0) == (n, n)) assert (np.shape(D1) == (n, n)) func = lambda ind: -v_step(D0, D1, alpha_v, get_index_matrix(cluster_num, ind))[1] #loss res = kmeans_greedy.kmeans_greedy(func, cluster_num, n, init_index=index_init) z_est = get_index_matrix(cluster_num, res.index) v_est, loss = v_step(D0, D1, alpha_v, z_est) theta_est = np.matmul(z_est, v_est) #print("loss : {}".format(loss)) return _ResultInstance(theta_est, z_est, v_est, res.index, loss)