private void costantiniUnwrap() throws LPException { final int ny = wrappedPhase.rows - 1; // start from Zero! final int nx = wrappedPhase.columns - 1; // start from Zero! if (wrappedPhase.isVector()) throw new IllegalArgumentException("Input must be 2D array"); if (wrappedPhase.rows < 2 || wrappedPhase.columns < 2) throw new IllegalArgumentException("Size of input must be larger than 2"); // Default weight DoubleMatrix w1 = DoubleMatrix.ones(ny + 1, 1); w1.put(0, 0.5); w1.put(w1.length - 1, 0.5); DoubleMatrix w2 = DoubleMatrix.ones(1, nx + 1); w2.put(0, 0.5); w2.put(w2.length - 1, 0.5); DoubleMatrix weight = w1.mmul(w2); DoubleMatrix i, j, I_J, IP1_J, I_JP1; DoubleMatrix Psi1, Psi2; DoubleMatrix[] ROWS; // Compute partial derivative Psi1, eqt (1,3) i = intRangeDoubleMatrix(0, ny - 1); j = intRangeDoubleMatrix(0, nx); ROWS = grid2D(i, j); I_J = JblasUtils.sub2ind(wrappedPhase.rows, ROWS[0], ROWS[1]); IP1_J = JblasUtils.sub2ind(wrappedPhase.rows, ROWS[0].add(1), ROWS[1]); Psi1 = JblasUtils.getMatrixFromIdx(wrappedPhase, IP1_J) .sub(JblasUtils.getMatrixFromIdx(wrappedPhase, I_J)); Psi1 = UnwrapUtils.wrapDoubleMatrix(Psi1); // Compute partial derivative Psi2, eqt (2,4) i = intRangeDoubleMatrix(0, ny); j = intRangeDoubleMatrix(0, nx - 1); ROWS = grid2D(i, j); I_J = JblasUtils.sub2ind(wrappedPhase.rows, ROWS[0], ROWS[1]); I_JP1 = JblasUtils.sub2ind(wrappedPhase.rows, ROWS[0], ROWS[1].add(1)); Psi2 = JblasUtils.getMatrixFromIdx(wrappedPhase, I_JP1) .sub(JblasUtils.getMatrixFromIdx(wrappedPhase, I_J)); Psi2 = UnwrapUtils.wrapDoubleMatrix(Psi2); // Compute beq DoubleMatrix beq = DoubleMatrix.zeros(ny, nx); i = intRangeDoubleMatrix(0, ny - 1); j = intRangeDoubleMatrix(0, nx - 1); ROWS = grid2D(i, j); I_J = JblasUtils.sub2ind(Psi1.rows, ROWS[0], ROWS[1]); I_JP1 = JblasUtils.sub2ind(Psi1.rows, ROWS[0], ROWS[1].add(1)); beq.addi(JblasUtils.getMatrixFromIdx(Psi1, I_JP1).sub(JblasUtils.getMatrixFromIdx(Psi1, I_J))); I_J = JblasUtils.sub2ind(Psi2.rows, ROWS[0], ROWS[1]); I_JP1 = JblasUtils.sub2ind(Psi2.rows, ROWS[0].add(1), ROWS[1]); beq.subi(JblasUtils.getMatrixFromIdx(Psi2, I_JP1).sub(JblasUtils.getMatrixFromIdx(Psi2, I_J))); beq.muli(-1 / (2 * Constants._PI)); for (int k = 0; k < beq.length; k++) { beq.put(k, Math.round(beq.get(k))); } beq.reshape(beq.length, 1); logger.debug("Constraint matrix"); i = intRangeDoubleMatrix(0, ny - 1); j = intRangeDoubleMatrix(0, nx - 1); ROWS = grid2D(i, j); DoubleMatrix ROW_I_J = JblasUtils.sub2ind(i.length, ROWS[0], ROWS[1]); int nS0 = nx * ny; // Use by S1p, S1m DoubleMatrix[] COLS; COLS = grid2D(i, j); DoubleMatrix COL_IJ_1 = JblasUtils.sub2ind(i.length, COLS[0], COLS[1]); COLS = grid2D(i, j.add(1)); DoubleMatrix COL_I_JP1 = JblasUtils.sub2ind(i.length, COLS[0], COLS[1]); int nS1 = (nx + 1) * (ny); // SOAPBinding.Use by S2p, S2m COLS = grid2D(i, j); DoubleMatrix COL_IJ_2 = JblasUtils.sub2ind(i.length + 1, COLS[0], COLS[1]); COLS = grid2D(i.add(1), j); DoubleMatrix COL_IP1_J = JblasUtils.sub2ind(i.length + 1, COLS[0], COLS[1]); int nS2 = nx * (ny + 1); // Equality constraint matrix (Aeq) /* S1p = + sparse(ROW_I_J, COL_I_JP1,1,nS0,nS1) ... - sparse(ROW_I_J, COL_IJ_1,1,nS0,nS1); S1m = -S1p; S2p = - sparse(ROW_I_J, COL_IP1_J,1,nS0,nS2) ... + sparse(ROW_I_J, COL_IJ_2,1,nS0,nS2); S2m = -S2p; */ // ToDo: Aeq matrix should be sparse from it's initialization, look into JblasMatrix factory for // howto // ...otherwise even a data set of eg 40x40 pixels will exhaust heap: // ... dimension of Aeq (equality constraints) matrix for 30x30 input is 1521x6240 matrix // ... dimension of Aeq ( ) matrix for 50x50 input is 2401x9800 // ... dimension of Aeq ( ) matrix for 512x512 input is 261121x1046528 DoubleMatrix S1p = JblasUtils.setUpMatrixFromIdx(nS0, nS1, ROW_I_J, COL_I_JP1) .sub(JblasUtils.setUpMatrixFromIdx(nS0, nS1, ROW_I_J, COL_IJ_1)); DoubleMatrix S1m = S1p.neg(); DoubleMatrix S2p = JblasUtils.setUpMatrixFromIdx(nS0, nS2, ROW_I_J, COL_IP1_J) .neg() .add(JblasUtils.setUpMatrixFromIdx(nS0, nS2, ROW_I_J, COL_IJ_2)); DoubleMatrix S2m = S2p.neg(); DoubleMatrix Aeq = concatHorizontally(concatHorizontally(S1p, S1m), concatHorizontally(S2p, S2m)); final int nObs = Aeq.columns; final int nUnkn = Aeq.rows; DoubleMatrix c1 = JblasUtils.getMatrixFromRange(0, ny, 0, weight.columns, weight); DoubleMatrix c2 = JblasUtils.getMatrixFromRange(0, weight.rows, 0, nx, weight); c1.reshape(c1.length, 1); c2.reshape(c2.length, 1); DoubleMatrix cost = DoubleMatrix.concatVertically( DoubleMatrix.concatVertically(c1, c1), DoubleMatrix.concatVertically(c2, c2)); logger.debug("Minimum network flow resolution"); StopWatch clockLP = new StopWatch(); LinearProgram lp = new LinearProgram(cost.data); lp.setMinProblem(true); boolean[] integerBool = new boolean[nObs]; double[] lowerBound = new double[nObs]; double[] upperBound = new double[nObs]; for (int k = 0; k < nUnkn; k++) { lp.addConstraint(new LinearEqualsConstraint(Aeq.getRow(k).toArray(), beq.get(k), "cost")); } for (int k = 0; k < nObs; k++) { integerBool[k] = true; lowerBound[k] = 0; upperBound[k] = 99999; } // setup bounds and integer nature lp.setIsinteger(integerBool); lp.setUpperbound(upperBound); lp.setLowerbound(lowerBound); LinearProgramSolver solver = SolverFactory.newDefault(); // double[] solution; // solution = solver.solve(lp); DoubleMatrix solution = new DoubleMatrix(solver.solve(lp)); clockLP.stop(); logger.debug("Total GLPK time: {} [sec]", (double) (clockLP.getElapsedTime()) / 1000); // Displatch the LP solution int offset; int[] idx1p = JblasUtils.intRangeIntArray(0, nS1 - 1); DoubleMatrix x1p = solution.get(idx1p); x1p.reshape(ny, nx + 1); offset = idx1p[nS1 - 1] + 1; int[] idx1m = JblasUtils.intRangeIntArray(offset, offset + nS1 - 1); DoubleMatrix x1m = solution.get(idx1m); x1m.reshape(ny, nx + 1); offset = idx1m[idx1m.length - 1] + 1; int[] idx2p = JblasUtils.intRangeIntArray(offset, offset + nS2 - 1); DoubleMatrix x2p = solution.get(idx2p); x2p.reshape(ny + 1, nx); offset = idx2p[idx2p.length - 1] + 1; int[] idx2m = JblasUtils.intRangeIntArray(offset, offset + nS2 - 1); DoubleMatrix x2m = solution.get(idx2m); x2m.reshape(ny + 1, nx); // Compute the derivative jumps, eqt (20,21) DoubleMatrix k1 = x1p.sub(x1m); DoubleMatrix k2 = x2p.sub(x2m); // (?) Round to integer solution if (roundK == true) { for (int idx = 0; idx < k1.length; idx++) { k1.put(idx, FastMath.floor(k1.get(idx))); } for (int idx = 0; idx < k2.length; idx++) { k2.put(idx, FastMath.floor(k2.get(idx))); } } // Sum the jumps with the wrapped partial derivatives, eqt (10,11) k1.reshape(ny, nx + 1); k2.reshape(ny + 1, nx); k1.addi(Psi1.div(Constants._TWO_PI)); k2.addi(Psi2.div(Constants._TWO_PI)); // Integrate the partial derivatives, eqt (6) // cumsum() method in JblasTester -> see cumsum_demo() in JblasTester.cumsum_demo() DoubleMatrix k2_temp = DoubleMatrix.concatHorizontally(DoubleMatrix.zeros(1), k2.getRow(0)); k2_temp = JblasUtils.cumsum(k2_temp, 1); DoubleMatrix k = DoubleMatrix.concatVertically(k2_temp, k1); k = JblasUtils.cumsum(k, 1); // Unwrap - final solution unwrappedPhase = k.mul(Constants._TWO_PI); }
// align input so it can be used in training protected DoubleMatrix preProcessInput(DoubleMatrix input) { if (concatBiases) return DoubleMatrix.concatHorizontally(input, DoubleMatrix.ones(input.rows, 1)); return input; }
public static double[] polyFit2D( final DoubleMatrix x, final DoubleMatrix y, final DoubleMatrix z, final int degree) throws IllegalArgumentException { logger.setLevel(Level.INFO); if (x.length != y.length || !x.isVector() || !y.isVector()) { logger.error("polyfit: require same size vectors."); throw new IllegalArgumentException("polyfit: require same size vectors."); } final int numOfObs = x.length; final int numOfUnkn = numberOfCoefficients(degree) + 1; DoubleMatrix A = new DoubleMatrix(numOfObs, numOfUnkn); // designmatrix DoubleMatrix mul; /** Set up design-matrix */ for (int p = 0; p <= degree; p++) { for (int q = 0; q <= p; q++) { mul = pow(y, (p - q)).mul(pow(x, q)); if (q == 0 && p == 0) { A = mul; } else { A = DoubleMatrix.concatHorizontally(A, mul); } } } // Fit polynomial logger.debug("Solving lin. system of equations with Cholesky."); DoubleMatrix N = A.transpose().mmul(A); DoubleMatrix rhs = A.transpose().mmul(z); // solution seems to be OK up to 10^-09! rhs = Solve.solveSymmetric(N, rhs); DoubleMatrix Qx_hat = Solve.solveSymmetric(N, DoubleMatrix.eye(N.getRows())); double maxDeviation = (N.mmul(Qx_hat).sub(DoubleMatrix.eye(Qx_hat.rows))).normmax(); logger.debug("polyfit orbit: max(abs(N*inv(N)-I)) = " + maxDeviation); // ___ report max error... (seems sometimes this can be extremely large) ___ if (maxDeviation > 1e-6) { logger.warn("polyfit orbit: max(abs(N*inv(N)-I)) = {}", maxDeviation); logger.warn("polyfit orbit interpolation unstable!"); } // work out residuals DoubleMatrix y_hat = A.mmul(rhs); DoubleMatrix e_hat = y.sub(y_hat); if (e_hat.normmax() > 0.02) { logger.warn( "WARNING: Max. polyFit2D approximation error at datapoints (x,y,or z?): {}", e_hat.normmax()); } else { logger.info( "Max. polyFit2D approximation error at datapoints (x,y,or z?): {}", e_hat.normmax()); } if (logger.isDebugEnabled()) { logger.debug("REPORTING POLYFIT LEAST SQUARES ERRORS"); logger.debug(" time \t\t\t y \t\t\t yhat \t\t\t ehat"); for (int i = 0; i < numOfObs; i++) { logger.debug( " (" + x.get(i) + "," + y.get(i) + ") :" + "\t" + y.get(i) + "\t" + y_hat.get(i) + "\t" + e_hat.get(i)); } } return rhs.toArray(); }
// TODO: DOUBLECHECK EVERYTHING @Override public void map(Text key, jBLASArrayWritable input, GibbsSampling.Context context) throws IOException, InterruptedException { /* *******************************************************************/ /* initialize all memory we're going to use during the process */ long start_time = System.nanoTime(); ArrayList<DoubleMatrix> data = input.getData(); label = data.get(4); v_data = data.get(5); // check to see if we are in the first layer or there are layers beneath us we must sample // from if (data.size() > 6) { int prelayer = (data.size() - 6) / 3; DoubleMatrix[] preWeights = new DoubleMatrix[prelayer], preHbias = new DoubleMatrix[prelayer], preVbias = new DoubleMatrix[prelayer]; for (int i = 0; i < prelayer; i++) { preWeights[i] = data.get(6 + i * 3); preHbias[i] = data.get(7 + i * 3); preVbias[i] = data.get(8 + i * 3); } DoubleMatrix vnew = null; for (int i = 0; i < prelayer; i++) { weights = preWeights[i]; vbias = preVbias[i]; hbias = preHbias[i]; vnew = sample_h_from_v(i == 0 ? v_data : vnew); } v_data = vnew; } weights = data.get(0); hbias = data.get(1); hiddenChain = data.get(2); vbias = data.get(3); // check if we need to attach labels to the observed variables if (vbias.columns != v_data.columns) { DoubleMatrix labels = DoubleMatrix.zeros(1, classCount); int labelNum = (new Double(label.get(0))).intValue(); labels.put(labelNum, 1.0); v_data = DoubleMatrix.concatHorizontally(v_data, labels); } w1 = DoubleMatrix.zeros(weights.rows, weights.columns); hb1 = DoubleMatrix.zeros(hbias.rows, hbias.columns); vb1 = DoubleMatrix.zeros(vbias.rows, vbias.columns); /* ********************************************************************/ // sample hidden state to get positive phase // if empty, use it as the start of the chain // or use persistent hidden state from pCD DoubleMatrix phaseSample = sample_h_from_v(v_data); h1_data = new DoubleMatrix(); v1_data = new DoubleMatrix(); if (hiddenChain == null) { data.set(2, new DoubleMatrix(hbias.rows, hbias.columns)); hiddenChain = data.get(2); hiddenChain.copy(phaseSample); h1_data.copy(phaseSample); } else { h1_data.copy(hiddenChain); } // run Gibbs chain for k steps for (int j = 0; j < gibbsSteps; j++) { v1_data.copy(sample_v_from_h(h1_data)); h1_data.copy(sample_h_from_v(v1_data)); } DoubleMatrix hprob = propup(v1_data); weight_contribution(hiddenChain, v_data, hprob, v1_data); hiddenChain.copy(h1_data); data.get(0).copy(w1); data.get(1).copy(hb1); data.get(2).copy(hiddenChain); data.get(3).copy(vb1); jBLASArrayWritable outputmatrix = new jBLASArrayWritable(data); context.write(key, outputmatrix); log.info("Job completed in: " + (System.nanoTime() - start_time) / (1E6) + " ms"); }