@Override public Matrix init(int rows, int cols) { final Matrix ret = smf.createMatrix(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (this.random.nextDouble() > sparcity) ret.setElement(i, j, 1d); } } return ret; }
private Matrix constructYMatrix(Map<String, Double> y) { final Matrix mat = SparseMatrixFactoryMTJ.INSTANCE.createMatrix(1, values.size()); for (final Entry<String, Double> ent : y.entrySet()) { mat.setElement(0, values.get(ent.getKey()), ent.getValue()); } return mat; }
private Matrix constructXMatrix(Map<String, Map<String, Double>> x) { final Matrix mat = SparseMatrixFactoryMTJ.INSTANCE.createMatrix(vocabulary.size(), users.size()); for (final Entry<String, Map<String, Double>> userwords : x.entrySet()) { final int userindex = this.users.get(userwords.getKey()); for (final Entry<String, Double> ent : userwords.getValue().entrySet()) { mat.setElement(vocabulary.get(ent.getKey()), userindex, ent.getValue()); } } return mat; }
@Override public Matrix init(int rows, int cols) { final SparseMatrix rand = (SparseMatrix) smf.createUniformRandom(rows, cols, min, max, random); final Matrix ret = smf.createMatrix(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (this.random.nextDouble() > sparcity) ret.setElement(i, j, rand.getElement(i, j)); } } return ret; }
/** * Bring each element to the power d * * @param degree * @param d * @return the input */ public static <T extends Matrix> T powInplace(T degree, double d) { for (final MatrixEntry ent : degree) { degree.setElement(ent.getRowIndex(), ent.getColumnIndex(), Math.pow(ent.getValue(), d)); } return degree; }
private Matrix prepareMatrix(double[] y) { final Matrix Y = DMF.createMatrix(1, y.length + 1); Y.setElement(0, 0, 1); Y.setSubMatrix(0, 1, DMF.copyArray(new double[][] { y })); return Y.transpose(); }
private Matrix prepareMatrix(double[] y) { final Matrix Y = DMF.createMatrix(1, y.length + 1); Y.setElement(0, 0, 1); Y.setSubMatrix(0, 1, DMF.copyArray(new double[][] { y })); return Y.transpose(); }
/** * @param A * @param col */ public static void minusEqualsCol(Matrix A, Vector col) { for (int i = 0; i < A.getNumRows(); i++) { for (int j = 0; j < A.getNumColumns(); j++) { A.setElement(i, j, A.getElement(i, j) - col.getElement(i)); } } }
/** * @param A * @param row */ public static void minusEqualsRow(Matrix A, Vector row) { for (int i = 0; i < A.getNumRows(); i++) { for (int j = 0; j < A.getNumColumns(); j++) { A.setElement(i, j, A.getElement(i, j) - row.getElement(j)); } } }
@Override public Matrix apply(Matrix in) { final Matrix out = DMF.copyMatrix(in); for (int i = 0; i < in.getNumRows(); i++) { for (int j = 0; j < in.getNumColumns(); j++) { out.setElement(i, j, g.apply(in.getElement(i, j))); } } return out; }
@Override public Matrix apply(Matrix in) { final Matrix out = DMF.copyMatrix(in); for (int i = 0; i < in.getNumRows(); i++) { for (int j = 0; j < in.getNumColumns(); j++) { out.setElement(i, j, gPrime.apply(in.getElement(i, j))); } } return out; }
@Override public Matrix apply(Matrix in) { final Matrix out = DMF.copyMatrix(in); for (int i = 0; i < in.getNumRows(); i++) { for (int j = 0; j < in.getNumColumns(); j++) { out.setElement(i, j, g.apply(in.getElement(i, j))); } } return out; }
@Override public Matrix apply(Matrix in) { final Matrix out = DMF.copyMatrix(in); for (int i = 0; i < in.getNumRows(); i++) { for (int j = 0; j < in.getNumColumns(); j++) { out.setElement(i, j, gPrime.apply(in.getElement(i, j))); } } return out; }
private Matrix prepareMatrix(Vector y) { final Matrix Y = DMF.createMatrix(1, y.getDimensionality() + 1); Y.setElement(0, 0, 1); Y.setSubMatrix(0, 1, DMF.copyRowVectors(y)); return Y.transpose(); }
private Matrix prepareMatrix(Vector y) { final Matrix Y = DMF.createMatrix(1, y.getDimensionality() + 1); Y.setElement(0, 0, 1); Y.setSubMatrix(0, 1, DMF.copyRowVectors(y)); return Y.transpose(); }
@Override public Matrix gradient(Matrix W) { final Matrix resid = X.times(W).minus(Y); if (this.bias != null) resid.plusEquals(this.bias); for (int r = 0; r < Y.getNumRows(); r++) { final double yc = Y.getElement(r, 0); if (Double.isNaN(yc)) { resid.setElement(r, 0, 0); } } return X.transpose().times(resid); }
@Override public Matrix init(int rows, int cols) { int mrows = mat.getNumRows(); int mcols = mat.getNumColumns(); if(mrows == rows && mcols == cols) return mat; if(rows == cols && mcols == rows){ Matrix retMat = SparseMatrixFactoryMTJ.getSparseDefault().createMatrix(rows, rows); for (int i = 0; i < mcols; i++) { retMat.setElement(i, i, mat.getElement(0, i)); } return retMat; } return mat; }
public Matrix differentiate( Vector input) { int M = input.getDimensionality(); Matrix dydx = MatrixFactory.getDefault().createMatrix(M, M); for (int i = 0; i < M; i++) { dydx.setElement(i, i, this.getScalarFunction().differentiate(input.getElement(i))); } return dydx; }
public Matrix differentiate( Vector input) { int M = input.getDimensionality(); Matrix dydx = MatrixFactory.getDefault().createMatrix(M, M); for (int i = 0; i < M; i++) { dydx.setElement(i, i, this.getScalarFunction().differentiate(input.getElement(i))); } return dydx; }
public Matrix differentiate( Vector input) { int M = input.getDimensionality(); Matrix dydx = MatrixFactory.getDefault().createMatrix(M, M); for (int i = 0; i < M; i++) { dydx.setElement(i, i, this.getScalarFunction().differentiate(input.getElement(i))); } return dydx; }