List of usage examples for org.apache.commons.math3.linear RealVector getDistance
public double getDistance(RealVector v) throws DimensionMismatchException
From source file:org.lambda3.indra.core.impl.EuclideanClient.java
@Override protected double sim(RealVector r1, RealVector r2, boolean sparse) { return r1.getDistance(r2); }
From source file:RBFNN.MainFrame.java
public static double Dmax(ArrayList<instance> centers) {//calculate the largest //distance of the centers int len = centers.size(); double maxG = -1; double maxL = -1; for (int i = 0; i < len; i++) { RealVector ci = MatrixUtils.createRealVector(centers.get(i).getnumFeature()); double dis = 0; for (int j = 0; j < len; j++) { RealVector cj = MatrixUtils.createRealVector(centers.get(j).getnumFeature()); dis = ci.getDistance(cj); if (dis > maxL) maxL = dis;// w ww . ja va 2 s .co m } if (maxL > maxG) maxG = maxL; } return maxG; }
From source file:shapeCompare.ProcrustesAnalysis.java
private float computeResidual(RealMatrix matrixPointsModel, RealMatrix rotatedmatrixPointsCandidate) { float distance; double sumSquareDeltaDistances = 0.0; for (int i = 0; i < matrixPointsModel.getColumnDimension(); i++) { RealVector vectorFromModel = matrixPointsModel.getColumnVector(i); RealVector vectorFromCandidate = rotatedmatrixPointsCandidate.getColumnVector(i); distance = (float) vectorFromModel.getDistance(vectorFromCandidate); if (algoParameters != null) { float correctedDistance = computeCorrectedDistance(distance); sumSquareDeltaDistances += (correctedDistance * correctedDistance); continue; }/*from w w w . jav a 2 s . c o m*/ sumSquareDeltaDistances += distance * distance; } float rmsd = (float) Math.sqrt(sumSquareDeltaDistances / matrixPointsModel.getColumnDimension()); return rmsd; }
From source file:syncleus.gremlann.model.Autoencoder.java
public double train(RealVector v, double lr, double corruption_level) { double[] x = Graphs.doubles(v); if ((tilde_x == null) || (tilde_x.length != getInputCount())) { tilde_x = new double[getInputCount()]; L_vbias = new double[getInputCount()]; L_hbias = new double[getOutputCount()]; }//from w ww . java 2 s.c om if (corruption_level > 0) { get_corrupted_input(x, tilde_x, 1 - corruption_level); } else { tilde_x = x; } encode(tilde_x, getOutputs(), true, false); get_reconstructed_input(getOutputs(), getInputs()); // inputBias for (int i = 0; i < getInputCount(); i++) { L_vbias[i] = x[i] - Neuron.signal(inputs.get(i)); inputBias[i] += lr * L_vbias[i]; } // outputBias for (int i = 0; i < getOutputCount(); i++) { L_hbias[i] = 0; for (int j = 0; j < getInputCount(); j++) { L_hbias[i] += weight(j, i) * L_vbias[j]; } double yi = outputSignal(i); L_hbias[i] *= yi * (1 - yi); outputBias[i] += lr * L_hbias[i]; } // W for (int i = 0; i < getOutputCount(); i++) { for (int j = 0; j < getInputCount(); j++) { weight(j, i, weight(j, i) + lr * (L_hbias[i] * tilde_x[j] + L_vbias[j] * Neuron.signal(outputs.get(i)))); } } return v.getDistance(signals(getInputs())); }
From source file:syncleus.gremlann.SomBrainTest.java
@Test public void testColor() { final int TEST_ITERATIONS = 100; final int TRAIN_ITERATIONS = 10000; final double DRIFT_FACTOR = 400.0; final int OUTPUT_WIDTH = 10; final int OUTPUT_HEIGHT = 10; final int OUTPUT_DIMENSIONS = 2; final double LEARNING_RATE = 0.1; final int INPUT_DIMENSIONS = 3; //initialize brain with 3d input and 2d output SOM brain = new SOM(graph.addVertex(), new SomExponentialDecay(TRAIN_ITERATIONS, LEARNING_RATE), OUTPUT_DIMENSIONS, INPUT_DIMENSIONS); //create the output latice for (double x = 0; x < OUTPUT_WIDTH; x++) { for (double y = 0; y < OUTPUT_HEIGHT; y++) { brain.addOutput(x, y);/*from w w w . j av a 2 s . c om*/ } } //run through RANDOM training data for (int iteration = 0; iteration < TRAIN_ITERATIONS; iteration++) { brain.input(random.nextDouble(), random.nextDouble(), random.nextDouble()); brain.getBestMatchingUnit(true); } //some static varibles for the blocksize final double blockSize = 0.0025; final double maxOffset = 1.0 - blockSize; //test the maximum distance of close colors in the color space double farthestDistanceClose = 0.0; String closeOutText = ""; for (int iteration = 0; iteration < TEST_ITERATIONS; iteration++) { final StringBuilder outText = new StringBuilder(64); //find a mutual offset in the color space (leaving room for the //block) final double redOffset = random.nextDouble() * maxOffset; final double greenOffset = random.nextDouble() * maxOffset; final double blueOffset = random.nextDouble() * maxOffset; outText.append("close color offsets... red: ").append(redOffset).append(", green: ").append(greenOffset) .append(", blue: ").append(blueOffset).append('\n'); //get the location of a color within the block brain.input(redOffset + (random.nextDouble() * blockSize), greenOffset + (random.nextDouble() * blockSize), blueOffset + (random.nextDouble() * blockSize)); double[] iRandom = brain.inputSignals().toArray(); outText.append("close color1... red:").append(iRandom[0]).append(", green: ").append(iRandom[1]) .append(", blue").append(iRandom[2]).append('\n'); final RealVector color1 = brain.getBestMatchingUnit(true); //get the location of the other color within the block brain.input(redOffset + (random.nextDouble() * blockSize), greenOffset + (random.nextDouble() * blockSize), blueOffset + (random.nextDouble() * blockSize)); double[] jRandom = brain.inputSignals().toArray(); outText.append("close color2... red:").append(jRandom[0]).append(", green: ").append(jRandom[1]) .append(", blue").append(jRandom[2]).append('\n'); final RealVector color2 = brain.getBestMatchingUnit(true); //calculate the distance between these two points outText.append("close color1 point: ").append(color1).append('\n'); outText.append("close color2 point: ").append(color2).append('\n'); final double distance = color1.getDistance(color2); outText.append("close color distance: ").append(distance).append('\n'); //store the distance if its greater than the current max if (farthestDistanceClose < distance) { farthestDistanceClose = distance; closeOutText = outText.toString(); } } //test the maximum distance of far colors in the color space final double maxDrift = maxOffset / DRIFT_FACTOR; double closestDistanceFar = Double.POSITIVE_INFINITY; String farOutText = ""; for (int iteration = 0; iteration < TEST_ITERATIONS; iteration++) { final StringBuilder outText = new StringBuilder(64); //get the location of a color within the block final boolean isRed1Positive = random.nextBoolean(); final boolean isGreen1Positive = random.nextBoolean(); final boolean isBlue1Positive = random.nextBoolean(); brain.input((isRed1Positive ? random.nextDouble() * maxDrift : 1.0 - (random.nextDouble() * maxDrift)), (isGreen1Positive ? random.nextDouble() * maxDrift : 1.0 - (random.nextDouble() * maxDrift)), (isBlue1Positive ? random.nextDouble() * maxDrift : 1.0 - (random.nextDouble() * maxDrift))); outText.append("far color1... red:").append(brain.inputSignal(0)).append(", green: ") .append(brain.inputSignal(1)).append(", blue").append(brain.inputSignal(2)).append('\n'); final RealVector color1 = brain.getBestMatchingUnit(true); //get the location of the other color within the block brain.input((isRed1Positive ? 1.0 - (random.nextDouble() * maxDrift) : random.nextDouble() * maxDrift), (isGreen1Positive ? 1.0 - (random.nextDouble() * maxDrift) : random.nextDouble() * maxDrift), (isBlue1Positive ? 1.0 - (random.nextDouble() * maxDrift) : random.nextDouble() * maxDrift)); outText.append("far color2... red:").append(brain.inputSignal(0)).append(", green: ") .append(brain.inputSignal(1)).append(", blue").append(brain.inputSignal(2)).append('\n'); final RealVector color2 = brain.getBestMatchingUnit(true); //calculate the distance between these two points outText.append("far color1 point: ").append(color1).append('\n'); outText.append("far color2 point: ").append(color2).append('\n'); final double distance = color1.getDistance(color2); outText.append("far color distance: ").append(distance).append('\n'); //store the distance if its greater than the current max if (closestDistanceFar > distance) { closestDistanceFar = distance; farOutText = outText.toString(); } //System.out.println(outText); } //check that the farthest close is closer than the farthest far, //essentially make sure similar colors are always close and //dissimilar colors are always far away. Assert.assertTrue( "colors did not map properly: far: " + closestDistanceFar + " -> close: " + farthestDistanceClose + '\n' + closeOutText + '\n' + farOutText + '\n', closestDistanceFar > farthestDistanceClose); }