/** toy example */ public static void test2() { int N = 500; double[][] m1 = new double[N][N]; double[][] m2 = new double[N][N]; double[][] m3 = new double[N][N]; // init Random rand = new Random(); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { m1[i][j] = 10 * (rand.nextDouble() - 0.2); m2[i][j] = 20 * (rand.nextDouble() - 0.8); } // inverse System.out.println("Start"); Matrix mat1 = new Matrix(m1); Matrix mat2 = mat1.inverse(); Matrix mat3 = mat1.times(mat2); double[][] m4 = mat3.getArray(); /* for (int i = 0; i < m4.length; i++) { int ss = 10; for (int j = 0; j < ss; j++) { System.out.printf("%f ", m4[i][j]); } System.out.print("\n"); } */ System.out.println("Done"); /* // matrix * System.out.println("Start"); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { double cell = 0; for (int k = 0; k < N; k++) cell += m1[i][k] * m2[k][j]; // System.out.printf("%f ", cell); m3[i][j] = cell; } System.out.println("Done"); */ }
public static void test1(String seed) { /* read, init data & parameters */ for (int t = t0; t < T; t++) { // String fileDir = "../../data/graph/" + Integer.toString(t) + ".csv"; // original // co-voting dataset // String fileDir = "./data/" + Integer.toString(t) + ".csv"; // artificial toy dataset String fileDir = "../../data_sm/nips_17/out/" + seed + "/" + Integer.toString(t) + ".train.csv"; // nips dataset (smaller) Map<Integer, Double> freq = FileParser.readCSVDict(fileDir); double[][] G = new double[n][n]; double[][] A = new double[n][n]; double[][] mu = new double[n][K]; double[][] mu_hat = new double[n][K]; double[][] mu_prime = new double[n][K]; double[][] mu_hat_prime = new double[n][K]; double[][] h = new double[n][K]; double[][] h_hat = new double[n][K]; FileParser.readCSVGraph(fileDir, freq, G, A); for (int i = 0; i < n; i++) for (int k = 0; k < K; k++) { mu[i][k] = scale_0 * (rand.nextDouble() - 0.5); mu_hat[i][k] = scale_0 * (rand.nextDouble() - 0.5); mu_prime[i][k] = mu[i][k]; mu_hat_prime[i][k] = mu_hat[i][k]; h[i][k] = scale * (rand.nextDouble() - 0.5); h_hat[i][k] = scale * (rand.nextDouble() - 0.5); } GS.add(G); AS.add(A); mu_s.add(mu); mu_hat_s.add(mu_hat); mu_prime_s.add(mu_prime); mu_hat_prime_s.add(mu_hat_prime); h_s.add(h); h_prime_s.add(h); h_hat_s.add(h_hat); h_hat_prime_s.add(h); /* for test */ delta_s.add(delta); delta_prime_s.add(delta); // TODO previous: 0.1 v_s.add(0.1); v_hat_s.add(0.1); v_prime_s.add(0.1); v_hat_prime_s.add(0.1); System.out.println("done! t = " + t); } for (int t = t0; t < T; t++) { for (int s = t0; s < T; s++) { grad_mu_s.add(new double[n][K]); grad_mu_hat_s.add(new double[n][K]); grad_mu_prime_s.add(new double[n][K]); grad_mu_hat_prime_s.add(new double[n][K]); } grad_h_hat_s.add(new double[n][K]); grad_h_hat_prime_s.add(new double[n][K]); } /* end initialization */ /* outer for-loop */ double old_obj_1 = -1, old_obj_2 = -1; for (int iter = 0; iter < MAX_ITER; iter++) { // Scanner sc = new Scanner(System.in); int gu; gu = sc.nextInt(); System.out.println("====== iter = " + iter + " ======"); /** intrinsic feature * */ forward1(true, iter); backward1(true); compute_gradient1(iter); double new_obj_1 = 0; /* gradient descent: inner for-loop here */ int inner_iter_1 = 0; while (inner_iter_1 < INNER_ITER) { /* update variational parameters \hat{h} using gradient descent */ for (int t = 0; t < T - t0; t++) { double[][] h_hat_t = h_hat_s.get(t); double[][] grad_h_hat_t = grad_h_hat_s.get(t); for (int i = 0; i < n; i++) for (int k = 0; k < K; k++) { h_hat_t[i][k] += lr_1 * grad_h_hat_t[i][k]; } h_hat_s.set(t, h_hat_t); } /* update \hat{\mu} and \hat{V}, since both are function of \hat{h} */ forward1(false, iter); backward1(false); double obj1 = compute_objective1(); if (inner_iter_1 % 10 == 0) System.out.println("(1) iter = " + inner_iter_1 + ", obj 1 = " + obj1); if (inner_iter_1 != 0 && obj1 < new_obj_1) { lr_1 *= 0.8; break; } new_obj_1 = obj1; inner_iter_1 += 1; } if (inner_iter_1 == INNER_ITER) lr_1 *= 2; /* sample */ for (int t = 0; t < T - t0; t++) { double[][] samples = Operations.sample_multivariate_normal(mu_hat_s.get(t), v_hat_s.get(t), N_SAMPLES); double[][] h_t = new double[n][K]; for (int i = 0; i < n; i++) for (int k = 0; k < K; k++) { h_t[i][k] = samples[i][k]; } h_s.set(t, h_t); } /** impression feature * */ forward2(true); backward2(true); compute_gradient2(iter); double new_obj_2 = 0; /* gradient descent: inner for-loop here */ int inner_iter_2 = 0; while (inner_iter_2 < INNER_ITER) { /* update \hat{h}' using gradient descent */ for (int t = 0; t < T - t0; t++) { double[][] h_hat_prime_t = h_hat_prime_s.get(t); double[][] grad_h_hat_prime_t = grad_h_hat_prime_s.get(t); for (int i = 0; i < n; i++) for (int k = 0; k < K; k++) { h_hat_prime_t[i][k] += lr_2 * grad_h_hat_prime_t[i][k]; } h_hat_prime_s.set(t, h_hat_prime_t); } /* update \hat{\mu}' and \hat{V}', since both are function of \hat{h}' */ forward2(false); backward2(false); double obj2 = compute_objective2(); if (inner_iter_2 % 10 == 0) System.out.println("(2) iter = " + inner_iter_2 + ", obj 2 = " + obj2); if (inner_iter_2 != 0 && obj2 < new_obj_2) { lr_2 *= 0.8; break; } new_obj_2 = obj2; inner_iter_2 += 1; } if (inner_iter_2 == INNER_ITER) lr_2 *= 2; /* sample */ for (int t = 0; t < T - t0; t++) { double[][] samples = Operations.sample_multivariate_normal( mu_hat_prime_s.get(t), v_hat_prime_s.get(t), N_SAMPLES); double[][] h_prime_t = new double[n][K]; for (int i = 0; i < n; i++) for (int k = 0; k < K; k++) { h_prime_t[i][k] = samples[i][k]; } h_prime_s.set(t, h_prime_t); } /** output * */ for (int t = 0; t < T - t0; t++) { double[][] h_t = h_s.get(t); double[][] h_prime_t = h_prime_s.get(t); /* output filename: * ./res/<seed>_<sigma>/h_<time>_<iter>.txt */ FileParser.output( h_t, "./res/" + seed + "_" + delta_str + "/h_" + (t + t0) + "_" + iter + ".txt"); FileParser.output( h_prime_t, "./res/" + seed + "_" + delta_str + "/h_p_" + (t + t0) + "_" + iter + ".txt"); } /* check convergence */ double diff_1 = -(new_obj_1 - old_obj_1) / old_obj_1; double diff_2 = -(new_obj_2 - old_obj_2) / old_obj_2; if (iter != 0 && diff_1 < 1e-6 && diff_2 < 1e-6) { System.out.println("diff_1 = " + diff_1); System.out.println("diff_2 = " + diff_2); break; } old_obj_1 = new_obj_1; old_obj_2 = new_obj_2; } }