/** * Computes the relevance for each term in U (universe of entities) to the * terms in the seedset. * * @param seedSet S a subset of U, this are the indices where to find the * items in the similarity matrix. * @return a vector of length of the universe of entities. Which index * encapsulates the relevance described in the paper as * S_rel(TERM_AT_INDEX_i,S) */ private DenseDoubleVector computeRelevanceScore(int[] seedSet) { final int termsLength = termNodes.length; final DenseDoubleVector relevanceScores = new DenseDoubleVector(termsLength); final double constantLoss = 1.0d / seedSet.length; for (int i = 0; i < termsLength; i++) { double sum = 0.0d; for (int j : seedSet) { DoubleVector columnVectorI = weightMatrix.getColumnVector(i); DoubleVector columnVectorJ = weightMatrix.getColumnVector(j); double similarity = 0.0d; if (columnVectorI != null && columnVectorJ != null) { similarity = similarityMeasurer.measureSimilarity(columnVectorI, columnVectorJ); } sum += similarity; } relevanceScores.set(i, constantLoss * sum); } return relevanceScores; }
stateSequence[index] = chooseState(optionalRandom.get(), transitionProbabilities .getColumnVector(stateSequence[index + 1])); } else { stateSequence[index] = transitionProbabilities.getColumnVector( stateSequence[index + 1]).maxIndex();
/** * Creates a new matrix with the given vector into the first column and the * other matrix to the other columns. This is usually used in machine learning * algorithms that add a bias on the zero-index column. * * @param first the new first column. * @param otherMatrix the other matrix to set on from the second column. */ public SparseDoubleRowMatrix(DenseDoubleVector first, DoubleMatrix otherMatrix) { this(otherMatrix.getRowCount(), otherMatrix.getColumnCount() + 1); setColumnVector(0, first); for (int i = 1; i < numColumns; i++) { setColumnVector(i, otherMatrix.getColumnVector(i - 1)); } }
/** * Creates a new matrix with the given vector into the first column and the * other matrix to the other columns. This is usually used in machine learning * algorithms that add a bias on the zero-index column. * * @param first the new first column. * @param otherMatrix the other matrix to set on from the second column. */ public DenseDoubleMatrix(DenseDoubleVector first, DoubleMatrix otherMatrix) { this(otherMatrix.getRowCount(), otherMatrix.getColumnCount() + 1); // copy the first column System.arraycopy(first.toArray(), 0, matrix, 0, first.getDimension()); int offset = first.getDimension(); for (int col : otherMatrix.columnIndices()) { double[] clv = otherMatrix.getColumnVector(col).toArray(); System.arraycopy(clv, 0, matrix, offset, clv.length); offset += clv.length; } }
@Override public DoubleMatrix divide(DoubleMatrix other) { SparseDoubleRowMatrix m = new SparseDoubleRowMatrix(other); for (int row : this.matrix.keys()) { Iterator<DoubleVectorElement> iterateNonZero = matrix.get(row) .iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement e = iterateNonZero.next(); m.set(row, e.getIndex(), get(row, e.getIndex()) / other.get(row, e.getIndex())); } } for (int col : other.columnIndices()) { Iterator<DoubleVectorElement> iterateNonZero = other.getColumnVector(col) .iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement e = iterateNonZero.next(); m.set(e.getIndex(), col, get(e.getIndex(), col) / other.get(e.getIndex(), col)); } } return m; }
public static void calculateGradients(DoubleMatrix[] thetas, DoubleMatrix[] thetaGradients, DoubleMatrix[] ax, DoubleMatrix[] deltaX, final int m, NetworkConfiguration conf) { // calculate the gradients of the weights for (int i = 0; i < thetaGradients.length; i++) { DoubleMatrix gradDXA = multiply(deltaX[i + 1], ax[i], true, false, conf); if (m != 1) { thetaGradients[i] = gradDXA.divide(m); } else { thetaGradients[i] = gradDXA; } if (conf.lambda != 0d) { thetaGradients[i] = thetaGradients[i].add((thetas[i] .multiply(conf.lambda / m))); // subtract the regularized bias DoubleVector regBias = thetas[i] .slice(0, thetas[i].getRowCount(), 0, 1).multiply(conf.lambda / m) .getColumnVector(0); thetaGradients[i].setColumnVector(0, regBias); } } }
/** * @return the normalized matrix (0 mean and stddev of 1) as well as the mean * and the stddev. */ public static Tuple3<DoubleMatrix, DoubleVector, DoubleVector> meanNormalizeColumns( DoubleMatrix x) { DenseDoubleMatrix toReturn = new DenseDoubleMatrix(x.getRowCount(), x.getColumnCount()); final int length = x.getColumnCount(); DoubleVector meanVector = new DenseDoubleVector(length); DoubleVector stddevVector = new DenseDoubleVector(length); for (int col = 0; col < length; col++) { DoubleVector column = x.getColumnVector(col); double mean = column.sum() / column.getLength(); meanVector.set(col, mean); double var = column.subtract(mean).pow(2).sum() / column.getLength(); stddevVector.set(col, Math.sqrt(var)); } for (int col = 0; col < length; col++) { DoubleVector column = x.getColumnVector(col) .subtract(meanVector.get(col)).divide(stddevVector.get(col)); toReturn.setColumn(col, column.toArray()); } return new Tuple3<>(toReturn, meanVector, stddevVector); }
@Override public DoubleMatrix gradient(DoubleMatrix matrix) { DoubleMatrix newInstance = newInstance(matrix); if (matrix.isSparse()) { // if we have a sparse matrix, it is more efficient to loop over the // sparse column vectors int[] columnIndices = matrix.columnIndices(); for (int col : columnIndices) { newInstance.setColumnVector(col, gradient(matrix.getColumnVector(col))); } } else { // on dense matrices we can be faster by directly looping over the items for (int i = 0; i < matrix.getRowCount(); i++) { for (int j = 0; j < matrix.getColumnCount(); j++) { newInstance.set(i, j, gradient(matrix.get(i, j))); } } } return newInstance; }
DoubleVector bias = thetaGradient.getColumnVector(0); thetaGradient = thetaGradient.subtract(thetaGradient.multiply(lambda / data.getRowCount()));