List of usage examples for java.lang Double NEGATIVE_INFINITY
double NEGATIVE_INFINITY
To view the source code for java.lang Double NEGATIVE_INFINITY.
Click Source Link
From source file:ivory.ltr.GreedyLearn.java
public void train(String featFile, String modelOutputFile, int numModels, String metricClassName, boolean pruneCorrelated, double correlationThreshold, boolean logFeatures, boolean productFeatures, boolean quotientFeatures, int numThreads) throws IOException, InterruptedException, ExecutionException, ConfigurationException, InstantiationException, IllegalAccessException, ClassNotFoundException { // read training instances Instances trainInstances = new Instances(featFile); // get feature map (mapping from feature names to feature number) Map<String, Integer> featureMap = trainInstances.getFeatureMap(); // construct initial model Model initialModel = new Model(); // initialize feature pools Map<Model, ArrayList<Feature>> featurePool = new HashMap<Model, ArrayList<Feature>>(); featurePool.put(initialModel, new ArrayList<Feature>()); // add simple features to feature pools for (String featureName : featureMap.keySet()) { featurePool.get(initialModel).add(new SimpleFeature(featureMap.get(featureName), featureName)); }//w ww.j a v a 2 s . c om // eliminate document-independent features List<Feature> constantFeatures = new ArrayList<Feature>(); for (int i = 0; i < featurePool.size(); i++) { Feature f = featurePool.get(initialModel).get(i); if (trainInstances.featureIsConstant(f)) { System.err.println("Feature " + f.getName() + " is constant -- removing from feature pool!"); constantFeatures.add(f); } } featurePool.get(initialModel).removeAll(constantFeatures); // initialize score tables Map<Model, ScoreTable> scoreTable = new HashMap<Model, ScoreTable>(); scoreTable.put(initialModel, new ScoreTable(trainInstances)); // initialize model queue List<Model> models = new ArrayList<Model>(); models.add(initialModel); // set up threading ExecutorService threadPool = Executors.newFixedThreadPool(numThreads); Map<Model, ArrayList<ArrayList<Feature>>> featureBatches = new HashMap<Model, ArrayList<ArrayList<Feature>>>(); featureBatches.put(initialModel, new ArrayList<ArrayList<Feature>>()); for (int i = 0; i < numThreads; i++) { featureBatches.get(initialModel).add(new ArrayList<Feature>()); } for (int i = 0; i < featurePool.get(initialModel).size(); i++) { featureBatches.get(initialModel).get(i % numThreads).add(featurePool.get(initialModel).get(i)); } // greedily add features double curMetric = 0.0; double prevMetric = Double.NEGATIVE_INFINITY; int iter = 1; while (curMetric - prevMetric > TOLERANCE) { Map<ModelFeaturePair, AlphaMeasurePair> modelFeaturePairMeasures = new HashMap<ModelFeaturePair, AlphaMeasurePair>(); // update models for (Model model : models) { List<Future<Map<Feature, AlphaMeasurePair>>> futures = new ArrayList<Future<Map<Feature, AlphaMeasurePair>>>(); for (int i = 0; i < numThreads; i++) { // construct measure Measure metric = (Measure) Class.forName(metricClassName).newInstance(); // line searcher LineSearch search = new LineSearch(model, featureBatches.get(model).get(i), scoreTable.get(model), metric); Future<Map<Feature, AlphaMeasurePair>> future = threadPool.submit(search); futures.add(future); } for (int i = 0; i < numThreads; i++) { Map<Feature, AlphaMeasurePair> featAlphaMeasureMap = futures.get(i).get(); for (Feature f : featAlphaMeasureMap.keySet()) { AlphaMeasurePair featAlphaMeasure = featAlphaMeasureMap.get(f); modelFeaturePairMeasures.put(new ModelFeaturePair(model, f), featAlphaMeasure); } } } // sort model-feature pairs List<ModelFeaturePair> modelFeaturePairs = new ArrayList<ModelFeaturePair>( modelFeaturePairMeasures.keySet()); Collections.sort(modelFeaturePairs, new ModelFeatureComparator(modelFeaturePairMeasures)); // preserve current list of models List<Model> oldModels = new ArrayList<Model>(models); // add best model feature pairs to pool models = new ArrayList<Model>(); //Lidan: here consider top-K features, rather than just the best one for (int i = 0; i < numModels; i++) { Model model = modelFeaturePairs.get(i).model; Feature feature = modelFeaturePairs.get(i).feature; String bestFeatureName = feature.getName(); AlphaMeasurePair bestAlphaMeasure = modelFeaturePairMeasures.get(modelFeaturePairs.get(i)); System.err.println("Model = " + model); System.err.println("Best feature: " + bestFeatureName); System.err.println("Best alpha: " + bestAlphaMeasure.alpha); System.err.println("Best measure: " + bestAlphaMeasure.measure); Model newModel = new Model(model); models.add(newModel); ArrayList<ArrayList<Feature>> newFeatureBatch = new ArrayList<ArrayList<Feature>>(); for (ArrayList<Feature> fb : featureBatches.get(model)) { newFeatureBatch.add(new ArrayList<Feature>(fb)); } featureBatches.put(newModel, newFeatureBatch); featurePool.put(newModel, new ArrayList<Feature>(featurePool.get(model))); // add auxiliary features (for atomic features only) if (featureMap.containsKey(bestFeatureName)) { int bestFeatureIndex = featureMap.get(bestFeatureName); // add log features, if requested if (logFeatures) { Feature logFeature = new LogFeature(bestFeatureIndex, "log(" + bestFeatureName + ")"); featureBatches.get(newModel).get(bestFeatureIndex % numThreads).add(logFeature); featurePool.get(newModel).add(logFeature); } // add product features, if requested if (productFeatures) { for (String featureNameB : featureMap.keySet()) { int indexB = featureMap.get(featureNameB); Feature prodFeature = new ProductFeature(bestFeatureIndex, indexB, bestFeatureName + "*" + featureNameB); featureBatches.get(newModel).get(indexB % numThreads).add(prodFeature); featurePool.get(newModel).add(prodFeature); } } // add quotient features, if requested if (quotientFeatures) { for (String featureNameB : featureMap.keySet()) { int indexB = featureMap.get(featureNameB); Feature divFeature = new QuotientFeature(bestFeatureIndex, indexB, bestFeatureName + "/" + featureNameB); featureBatches.get(newModel).get(indexB % numThreads).add(divFeature); featurePool.get(newModel).add(divFeature); } } } // prune highly correlated features if (pruneCorrelated) { if (!newModel.containsFeature(feature)) { List<Feature> correlatedFeatures = new ArrayList<Feature>(); for (Feature f : featurePool.get(newModel)) { if (f == feature) { continue; } double correl = trainInstances.getCorrelation(f, feature); if (correl > correlationThreshold) { System.err.println("Pruning highly correlated feature: " + f.getName()); correlatedFeatures.add(f); } } for (ArrayList<Feature> batch : featureBatches.get(newModel)) { batch.removeAll(correlatedFeatures); } featurePool.get(newModel).removeAll(correlatedFeatures); } } // update score table if (iter == 0) { scoreTable.put(newModel, scoreTable.get(model).translate(feature, 1.0, 1.0)); newModel.addFeature(feature, 1.0); } else { scoreTable.put(newModel, scoreTable.get(model).translate(feature, bestAlphaMeasure.alpha, 1.0 / (1.0 + bestAlphaMeasure.alpha))); newModel.addFeature(feature, bestAlphaMeasure.alpha); } } for (Model model : oldModels) { featurePool.remove(model); featureBatches.remove(model); scoreTable.remove(model); } // update metrics prevMetric = curMetric; curMetric = modelFeaturePairMeasures.get(modelFeaturePairs.get(0)).measure; iter++; } // serialize model System.out.println("Final Model: " + models.get(0)); models.get(0).write(modelOutputFile); threadPool.shutdown(); }
From source file:edu.cmu.sphinx.speakerid.SpeakerIdentification.java
/** * @param start/*from ww w. j ava 2 s .c om*/ * The starting frame * @param length * The length of the interval, as numbers of frames * @param features * The matrix build with feature vectors as rows * @return Returns the changing point in the input represented by features * */ private int getPoint(int start, int length, int step, Array2DRowRealMatrix features) { double max = Double.NEGATIVE_INFINITY; int ncols = features.getColumnDimension(), point = 0; Array2DRowRealMatrix sub = (Array2DRowRealMatrix) features.getSubMatrix(start, start + length - 1, 0, ncols - 1); double bicValue = getBICValue(sub); for (int i = Segment.FEATURES_SIZE + 1; i < length - Segment.FEATURES_SIZE; i += step) { double aux = getLikelihoodRatio(bicValue, i, sub); if (aux > max) { max = aux; point = i; } } if (max < 0) point = Integer.MIN_VALUE; return point + start; }
From source file:com.clust4j.algo.RadiusNeighbors.java
@Override protected RadiusNeighbors fit() { synchronized (fitLock) { if (null != res) return this; final LogTimer timer = new LogTimer(); Neighborhood initRes = new Neighborhood(tree.queryRadius(fit_X, radius, false)); info("queried " + this.alg + " for radius neighbors in " + timer.toString()); double[][] dists = initRes.getDistances(); int[][] indices = initRes.getIndices(); int[] tmp_ind_neigh, ind_neighbor; double[] tmp_dists, dist_row; for (int ind = 0; ind < indices.length; ind++) { ind_neighbor = indices[ind]; dist_row = dists[ind];/* w w w .j a v a 2 s .c o m*/ // Keep track for summary double v, sum = 0, minDist = Double.POSITIVE_INFINITY, maxDist = Double.NEGATIVE_INFINITY; int b_count = 0; boolean b_val; boolean[] mask = new boolean[ind_neighbor.length]; for (int j = 0; j < ind_neighbor.length; j++) { b_val = ind_neighbor[j] != ind; mask[j] = b_val; v = dist_row[j]; if (b_val) { sum += v; minDist = FastMath.min(minDist, v); maxDist = FastMath.max(maxDist, v); b_count++; } } tmp_ind_neigh = new int[b_count]; tmp_dists = new double[b_count]; for (int j = 0, k = 0; j < mask.length; j++) { if (mask[j]) { tmp_ind_neigh[k] = ind_neighbor[j]; tmp_dists[k] = dist_row[j]; k++; } } indices[ind] = tmp_ind_neigh; dists[ind] = tmp_dists; fitSummary.add(new Object[] { ind, b_count, minDist, (double) sum / (double) b_count, maxDist, timer.wallTime() }); } res = new Neighborhood(dists, indices); sayBye(timer); return this; } }
From source file:de.unidue.langtech.teaching.rp.detector.LanguageDetectorWeb1T.java
@Override public void process(JCas jcas) throws AnalysisEngineProcessException { List<String> words = JCasUtil.toText(JCasUtil.select(jcas, Token.class)); if (words.size() < 1) { return;// ww w.j ava2 s . com } List<String> ngrams = new ArrayList<String>(); for (String ngram : new NGramStringIterable(words, 1, 1)) { ngram = ngram.replaceAll("#", "").replaceAll("RT", ""); if (ngram.length() > 1 && !(ngram.startsWith("@")) && !(ngram.startsWith("http"))) { ngrams.add(ngram); } } try { Map<String, Double> langProbs; String maxLanguage = "x-unspecified"; langProbs = getLanguageProbabilities(ngrams); int zeroCounter = 0; //counter used to check if all probs are zero double maxLogProb = Double.NEGATIVE_INFINITY; for (String lang : langProbs.keySet()) { double prob = langProbs.get(lang); if (prob > maxLogProb) { maxLogProb = prob; maxLanguage = lang; } System.out.println(lang + " - " + prob); if (prob == 0.0) { zeroCounter++; } } if (zeroCounter == langProbs.size()) { maxLanguage = "x-unspecified"; } // //Uncomment if getCertainty is used. // Map<String, Double> langProbabilities = getCertainty(langProbs); // // //http://www.avajava.com/tutorials/lessons/how-do-i-use-numberformat-to-format-a-percent.html // NumberFormat defaultFormat = NumberFormat.getPercentInstance(); // defaultFormat.setMinimumFractionDigits(2); // // System.out.println("Possible languages listed below: "); // // for (Entry<String, Double> langEntry : langProbabilities.entrySet()) { // // System.out.println("Language : " + langEntry.getKey() + " Certainty : " + defaultFormat.format(langEntry.getValue())); // } // // System.out.println("Classified " + langProbabilities.size() + " possible languages"); jcas.setDocumentLanguage(maxLanguage); } catch (Exception e) { throw new AnalysisEngineProcessException(e); } }
From source file:ffx.crystal.ReflectionList.java
/** * <p>/* w w w . java2 s.c o m*/ * Constructor for ReflectionList.</p> * * @param crystal a {@link ffx.crystal.Crystal} object. * @param resolution a {@link ffx.crystal.Resolution} object. * @param properties a * {@link org.apache.commons.configuration.CompositeConfiguration} object. */ public ReflectionList(Crystal crystal, Resolution resolution, CompositeConfiguration properties) { this.crystal = crystal; spaceGroup = crystal.spaceGroup; crystalSystem = spaceGroup.crystalSystem; laueSystem = spaceGroup.laueSystem; this.resolution = resolution; int hmax = (int) (this.crystal.a / this.resolution.resolutionLimit()); int kmax = (int) (this.crystal.b / this.resolution.resolutionLimit()); int lmax = (int) (this.crystal.c / this.resolution.resolutionLimit()); minres = Double.POSITIVE_INFINITY; maxres = Double.NEGATIVE_INFINITY; int n = 0; HKL hkl = new HKL(); for (int h = -hmax; h <= hmax; h++) { hkl.h(h); for (int k = -kmax; k <= kmax; k++) { hkl.k(k); for (int l = -lmax; l <= lmax; l++) { hkl.l(l); double res = Crystal.invressq(this.crystal, hkl); getepsilon(hkl); if (SpaceGroup.checkLaueRestrictions(laueSystem, h, k, l) && resolution.inInverseResSqRange(res) && !HKL.sys_abs(hkl)) { minres = min(res, minres); maxres = max(res, maxres); String s = ("" + h + "_" + k + "_" + l).intern(); hklmap.put(s, new HKL(hkl.h(), hkl.k(), hkl.l(), hkl.epsilon(), hkl.allowed)); n++; } } } } n = 0; for (Map.Entry ei : hklmap.entrySet()) { Object key = ei.getKey(); HKL ih = (HKL) ei.getValue(); ih.index(n); hkllist.add(ih); n++; } /* * set up the resolution bins * first build a histogram */ for (HKL ih : hkllist) { double r = (Crystal.invressq(this.crystal, ih) - minres) / (maxres - minres); int i = (int) (min(r, 0.999) * 1000.0); hist[i + 1] += 1.0; } // convert to cumulative histogram for (int i = 1; i < hist.length; i++) { hist[i] += hist[i - 1]; } for (int i = 0; i < hist.length; i++) { hist[i] /= hist[hist.length - 1]; } // assign each reflection to a bin in the range (0-nbins) setResolutionBins(properties); }
From source file:MPC.ModelLP.java
public ModelLP(int D, double time, int N, double Delta, double Umax, List<Point2D[]> obstacles, Point2D... points) throws IloException { this.D = D;//from w w w . j a v a 2 s .co m this.N = N; this.Delta = Delta; double delta = this.Delta / (N * obstacles.size()); this.erfInverse = erfInv(1 - 2 * delta) / 10; sigmaMatrix(0.03, 0); // Create the enviroment cplex = new IloCplex(); // Define the control variables u = new IloNumVar[N - 1][D]; for (int n = 0; n < N - 1; n++) { for (int d = 0; d < D; d++) { u[n][d] = cplex.numVar(-Umax, +Umax, "u(" + n + "," + d + ")"); } } // Define the state variables x = new IloNumVar[N][2 * D]; for (int n = 0; n < N; n++) { for (int d = 0; d < 2 * D; d++) { x[n][d] = cplex.numVar(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, "x(" + n + "," + d + ")"); } } pen = cplex.numVar(0, Double.POSITIVE_INFINITY, "penality"); IloNumExpr obj = null; for (int n = 0; n < N - 1; n++) { for (int d = 0; d < D; d++) { if (obj == null) { obj = cplex.prod(u[n][d], u[n][d]); } else { obj = cplex.sum(obj, cplex.prod(u[n][d], u[n][d])); } } } obj = cplex.sum(obj, cplex.prod(1000, pen)); cplex.addMinimize(obj); // Dynamic equations: x(n+1) = A x(n) + B u(n) final double dt = time / N; final double A[][] = A(D, dt); final double B[][] = B(D, dt); for (int n = 0; n < N - 1; n++) { for (int d = 0; d < 2 * D; d++) { IloNumExpr expr = null; //A x(n) for (int j = 0; j < 2 * D; j++) { if (expr == null) { expr = cplex.prod(A[d][j], x[n][j]); } else { expr = cplex.sum(expr, cplex.prod(A[d][j], x[n][j])); } } //B u(n) for (int j = 0; j < D; j++) { expr = cplex.sum(expr, cplex.prod(B[d][j], u[n][j])); } cplex.addEq(x[n + 1][d], expr, "dy(" + n + "," + d + ")"); } } //spatial restrictions: H x(n) <= b Hyperplane h[] = Hyperplane.hyperplansFrom(D > 2, points); for (int n = 0; n < N; n++) { for (int i = 0; i < h.length; i++) { //h(i) x(n) <= b IloNumExpr expr = h[i].scalProd(cplex, x[n]); cplex.addLe(expr, cplex.sum(h[i].b, pen), "sr(" + n + "," + i + ")"); } } z = new IloNumVar[obstacles.size()][N][]; // obstacles constraints for (int r = 0; r < obstacles.size(); ++r) { Hyperplane obstacle[] = Hyperplane.hyperplansFrom(D > 2, obstacles.get(r)); for (int n = 0; n < N; n++) { IloNumExpr z_sum = null; z[r][n] = new IloNumVar[obstacle.length]; for (int i = 0; i < obstacle.length; ++i) { //h(i) x(n) >= b - M(1-z(n,r)) z[r][n][i] = cplex.boolVar("z(" + r + "," + n + "," + i + ")"); if (z_sum == null) { z_sum = z[r][n][i]; } else { z_sum = cplex.sum(z_sum, z[r][n][i]); } IloNumExpr expr = obstacle[i].scalProd(cplex, x[n]); IloNumExpr m = cplex.prod(10000, cplex.sum(-1, z[r][n][i])); double c = c_in(n, zeroFill(obstacle[i].a)); cplex.addGe(expr, cplex.sum(obstacle[i].b + c, m), "obc(" + r + "," + n + "," + i + ")"); } cplex.addGe(z_sum, 1, "sum(" + r + "," + n + ")"); } } // Corner cutting constraints for (int r = 0; r < z.length; ++r) { for (int n = 1; n < z[r].length - 1; n++) { IloNumExpr p_sum = null; for (int i = 0; i < z[r][n].length; ++i) { IloNumVar p = cplex.numVar(0, Double.POSITIVE_INFINITY, "p(" + r + "," + n + "," + i + ")"); // p(r,n,i) >= z(r,n,i) + p(r,n-1,i) - 1 IloNumExpr expr; expr = cplex.sum(z[r][n][i], z[r][n - 1][i]); expr = cplex.sum(expr, -1); cplex.addGe(p, expr, "corner1(" + r + "," + n + "," + i + ")"); // p(r,n,i) <= z(r,n,i) cplex.addLe(p, z[r][n][i], "corner2(" + r + "," + n + "," + i + ")"); // p(r,n,i) <= z(r,n-1,i) cplex.addLe(p, z[r][n - 1][i], "corner3(" + r + "," + n + "," + i + ")"); // sum_i p(r,n,i) >= 1 \forall r,n if (p_sum == null) { p_sum = p; } else { p_sum = cplex.sum(p_sum, p); } } cplex.addGe(p_sum, 1, "sump(" + r + "," + n + ")"); } } cplex.exportModel("./model.lp"); }
From source file:com.rapidminer.gui.plotter.RangeablePlotterAdapter.java
@Override public List<ParameterType> getAdditionalParameterKeys(InputPort inputPort) { List<ParameterType> types = super.getAdditionalParameterKeys(inputPort); boolean inputDeliversAttributes = false; if (inputPort != null) { MetaData metaData = inputPort.getMetaData(); if (metaData != null && (metaData instanceof ExampleSetMetaData || metaData instanceof ModelMetaData)) { inputDeliversAttributes = true; }//from w w w. j a va2s. c o m } if (inputDeliversAttributes) { types.add(new ParameterTypeList(PARAMETER_PREFIX_RANGE_LIST, "Defines the ranges for the given attribute", new ParameterTypeAttribute(PARAMETER_DIMENSION_NAME, "This is the name of the dimension, the range should be applied onto.", inputPort), new ParameterTypeTupel(PARAMETER_PREFIX_RANGE, "Defines the range of the corresponding axis.", new ParameterTypeDouble(PARAMETER_PREFIX_RANGE_MIN, "Defines the lower bound of the axis.", Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY), new ParameterTypeDouble(PARAMETER_PREFIX_RANGE_MAX, "Defines the upper bound of the axis.", Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)))); } else { types.add(new ParameterTypeList(PARAMETER_PREFIX_RANGE_LIST, "Defines the ranges for the given attribute", new ParameterTypeString(PARAMETER_DIMENSION_NAME, "This is the name of the dimension, the range should be applied onto."), new ParameterTypeTupel(PARAMETER_PREFIX_RANGE, "Defines the range of the corresponding axis.", new ParameterTypeDouble(PARAMETER_PREFIX_RANGE_MIN, "Defines the lower bound of the axis.", Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY), new ParameterTypeDouble(PARAMETER_PREFIX_RANGE_MAX, "Defines the upper bound of the axis.", Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)))); } return types; }
From source file:io.coala.log.CoalaLog4jLogger.java
/** * @return//from w ww .j a v a 2s. com */ protected String getTimePrefix() { if (this.clock == null) { // System.err.println("2. clock was null for " + getName()); return EMPTY_PREFIX_FORMAT; } final Instant<?> time = this.clock.getTime(); if (time == null) { // System.err.println("2. time was null for " + getName()); return EMPTY_PREFIX_FORMAT; } if (time.getValue() instanceof Double) { final double t = time.doubleValue(); if (t == Double.NaN) { // System.err.println("2. time is NaN for " + getName()); return String.format(NAN_TIME_PREFIX_FORMAT, time.getUnit().toString()); } if (t == Double.NEGATIVE_INFINITY) { // System.err.println("2. time is pos inf for " + getName()); return String.format(NEG_INF_TIME_PREFIX_FORMAT, time.getUnit().toString()); } if (t == Double.NEGATIVE_INFINITY) { // System.err.println("2. time is neg inf for " + getName()); return String.format(POS_INF_TIME_PREFIX_FORMAT, time.getUnit().toString()); } } // System.err.println("2. time is " + time + " for " + getName()); try { if (time instanceof SimTime) return String.format(SIMTIME_PREFIX_FORMAT, time.doubleValue(), time.getUnit().toString(), SIMTIME_DATE_FORMAT.format(((SimTime) time).getIsoTime())); return String.format(TIME_PREFIX_FORMAT, time.doubleValue(), time.getUnit().toString()); } catch (final Throwable t) { t.printStackTrace(); return time.toString(); } }
From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.core.indicator.RIndicator.java
/** * Computes the expected utility for the given population. * /* w ww .ja v a2 s.c om*/ * @param population the population * @return the expected utility */ public double expectedUtility(NondominatedPopulation population) { double sum = 0.0; for (int i = 0; i < weights.length; i++) { double max = Double.NEGATIVE_INFINITY; for (Solution solution : population) { max = Math.max(max, utilityFunction.computeUtility(solution, weights[i])); } sum += max; } return sum / weights.length; }
From source file:Tsne.java
/** * Convert data to probability/*from w ww.ja v a2s . com*/ * co-occurrences (aka calculating the kernel) * @param d the data to convert * @param u the perplexity of the model * @return the probabilities of co-occurrence */ public INDArray computeGaussianPerplexity(final INDArray d, double u) { int n = d.rows(); final INDArray p = zeros(n, n); final INDArray beta = ones(n, 1); final double logU = Math.log(u); log.info("Calculating probabilities of data similarities.."); for (int i = 0; i < n; i++) { if (i % 500 == 0 && i > 0) log.info("Handled " + i + " records"); double betaMin = Double.NEGATIVE_INFINITY; double betaMax = Double.POSITIVE_INFINITY; NDArrayIndex[] range = new NDArrayIndex[] { NDArrayIndex.concat(NDArrayIndex.interval(0, i), NDArrayIndex.interval(i + 1, d.columns())) }; INDArray row = d.slice(i).get(range); Pair<INDArray, INDArray> pair = hBeta(row, beta.getDouble(i)); INDArray hDiff = pair.getFirst().sub(logU); int tries = 0; //while hdiff > tolerance while (BooleanIndexing.and(abs(hDiff), Conditions.greaterThan(tolerance)) && tries < 50) { //if hdiff > 0 if (BooleanIndexing.and(hDiff, Conditions.greaterThan(0))) { if (Double.isInfinite(betaMax)) beta.putScalar(i, beta.getDouble(i) * 2.0); else beta.putScalar(i, (beta.getDouble(i) + betaMax) / 2.0); betaMin = beta.getDouble(i); } else { if (Double.isInfinite(betaMin)) beta.putScalar(i, beta.getDouble(i) / 2.0); else beta.putScalar(i, (beta.getDouble(i) + betaMin) / 2.0); betaMax = beta.getDouble(i); } pair = hBeta(row, beta.getDouble(i)); hDiff = pair.getFirst().subi(logU); tries++; } p.slice(i).put(range, pair.getSecond()); } //dont need data in memory after log.info("Mean value of sigma " + sqrt(beta.rdiv(1)).mean(Integer.MAX_VALUE)); BooleanIndexing.applyWhere(p, Conditions.isNan(), new Value(realMin)); //set 0 along the diagonal INDArray permute = p.transpose(); INDArray pOut = p.add(permute); pOut.divi(pOut.sum(Integer.MAX_VALUE)); BooleanIndexing.applyWhere(pOut, Conditions.lessThan(Nd4j.EPS_THRESHOLD), new Value(Nd4j.EPS_THRESHOLD)); //ensure no nans return pOut; }