@Override public DoubleMatrix slice(int rowOffset, int rowMax, int colOffset, int colMax) { DoubleMatrix m = new SparseDoubleRowMatrix(rowMax - rowOffset, colMax - colOffset); for (int col : columnIndices()) { DoubleVector columnVector = getColumnVector(col); columnVector = columnVector.slice(rowOffset, rowMax); m.setColumnVector(col - colOffset, columnVector); } return m; }
@Override public DoubleMatrix divide(DoubleVector vec) { DoubleMatrix cop = new DenseDoubleMatrix(this.getRowCount(), this.getColumnCount()); for (int i = 0; i < this.getColumnCount(); i++) { cop.setColumnVector(i, getColumnVector(i).divide(vec)); } return cop; }
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); } } }
@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; }
theta, false, false)); positiveHiddenProbs.setColumnVector(0, DenseDoubleVector.ones(positiveHiddenProbs.getRowCount())); DoubleMatrix positiveAssociations = multiply(data, positiveHiddenProbs, negativeData.setColumnVector(0, DenseDoubleVector.ones(negativeData.getRowCount())); DoubleMatrix negativeHiddenProbs = activationFunction.apply(multiply( negativeData, theta, false, false)); negativeHiddenProbs.setColumnVector(0, DenseDoubleVector.ones(negativeHiddenProbs.getRowCount())); DoubleMatrix negativeAssociations = multiply(negativeData, thetaGradient = thetaGradient.subtract(thetaGradient.multiply(lambda / data.getRowCount())); thetaGradient.setColumnVector(0, bias);