/** * @param x normal feature matrix, column 0 should contain the bias. * @param y normal outcome matrix, for multiple classes use the one-hot * encoding. This matrix should be transposed. * @param lambda l1 reg parameter. */ public LogisticRegressionCostFunction(DoubleMatrix x, DoubleMatrix y, double lambda) { this.x = x; this.lambda = lambda; this.m = x.getRowCount(); this.xTransposed = this.x.transpose(); this.y = y; }
/** * Creates a reconstruction of the input using the given hidden activations. * (That, what is returned by {@link #predict(DoubleVector)}). * * @param hiddenActivations the activations of the predict method. * @return the reconstructed input vector. */ public DoubleVector reconstructInput(DoubleVector hiddenActivations) { DoubleVector lastOutput = hiddenActivations; for (int i = weights.length - 1; i >= 0; i--) { lastOutput = computeHiddenActivations(lastOutput, weights[i].transpose()); } // slice the hidden bias away return lastOutput.slice(1, lastOutput.getDimension()); }
/** * Constructs the similarity aggregation by seed tokens to expand and a given * bipartite graph. The bipartite graph is represented as a three tuple, which * consists of the vertices (called (candidate-) terms or entities) on the * first item, the context vertices on the second item and the edges between * those is a NxM matrix, where n is the entity tokens count and m is the * number of the context vertices. Alpha is the constant weighting factor used * throughout the paper (usually 0.5). The distance measurer to be used must * be also defined. */ public IterativeSimilarityAggregation(String[] seedTokens, Tuple<String[], DoubleMatrix> bipartiteGraph, double alpha, DistanceMeasurer distance) { this.seedTokens = seedTokens; this.termNodes = bipartiteGraph.getFirst(); // make sure we transpose to have a better distance lookup this.weightMatrix = bipartiteGraph.getSecond().transpose(); this.alpha = alpha; this.similarityMeasurer = new SimilarityMeasurer(distance); init(); }
@Override public void train(DoubleVector[] features, DoubleVector[] outcome) { Preconditions.checkArgument(features.length == outcome.length, "Features and Outcomes need to match in length!"); DoubleMatrix x = null; DoubleMatrix y = null; // add the bias if (features[0].isSparse()) { x = new SparseDoubleRowMatrix(DenseDoubleVector.ones(features.length), new SparseDoubleRowMatrix(features)); } else { x = new DenseDoubleMatrix(DenseDoubleVector.ones(features.length), new DenseDoubleMatrix(features)); } if (outcome[0].isSparse()) { y = new SparseDoubleRowMatrix(outcome); } else { y = new DenseDoubleMatrix(outcome); } // transpose y to get a faster lookup in the cost function y = y.transpose(); LogisticRegressionCostFunction cnf = new LogisticRegressionCostFunction(x, y, lambda); // random init theta theta = new DenseDoubleVector(x.getColumnCount() * y.getRowCount()); for (int i = 0; i < theta.getDimension(); i++) { theta.set(i, (random.nextDouble() * 2) - 1d); } theta = minimizer.minimize(cnf, theta, numIterations, verbose); }