/** * Extract the diagonal elements as a vector * * @param mat * the matrix to extract from * @return the diagonal */ public static Vector diag(Matrix mat) { Vector ret; if (mat.getNumColumns() > mat.getNumRows()) { ret = mat.getRow(0); } else { ret = mat.getColumn(0); } final int rowcol = ret.getDimensionality(); for (int rc = 0; rc < rowcol; rc++) { ret.setElement(rc, mat.getElement(rc, rc)); } return ret; }
/** * @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)); } } }
@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); }
private Matrix repmat(Matrix dL1, int nRows, int nCols) { final Matrix out = DMF.createMatrix(nRows * dL1.getNumRows(), nCols * dL1.getNumColumns()); for (int i = 0; i < nRows; i++) { for (int j = 0; j < nCols; j++) { out.setSubMatrix(i * dL1.getNumRows(), j * dL1.getNumColumns(), dL1); } } return out; }
public void setValue( Matrix value) { final int dim = this.conditionalDistribution.getInputDimensionality(); if( (value.getNumRows() != dim) || (value.getNumColumns() != (dim+1) ) ) { throw new IllegalArgumentException( "Expected (dim x dim+1) Matrix" ); } Vector mean = value.getColumn(0); Matrix covariance = value.getSubMatrix(0, dim-1, 1,dim); this.conditionalDistribution.setMean(mean); this.conditionalDistribution.setCovariance(covariance); }
public double logEvaluate( Matrix input) { final int d = input.getNumRows(); Vector mean = input.getColumn(0); Matrix C = input.getSubMatrix(0,d-1,1,d); C.scaleEquals(1.0/this.covarianceDivisor); double lpg = this.gaussian.getProbabilityFunction().logEvaluate(mean); double lpiw = this.inverseWishart.getProbabilityFunction().logEvaluate(C); return lpg + lpiw; }
if (this.getNumColumns() != other.getNumRows()) + other.getNumRows() + "x" + other.getNumColumns()); this.getNumRows(), other.getNumColumns()); final double thisEntryValue = thisEntry.getValue(); for (final VectorEntry otherEntry : other.getRow(thisEntry.getColumnIndex())) result.increment(rowIndex, otherEntry.getIndex(), thisEntryValue * otherEntry.getValue());
@Override public double eval(Matrix W) { Matrix resid = null; if(W == null){ resid = X.clone(); } else { resid = CFMatrixUtils.fastdot(X,W); } Matrix vnobias = resid.clone(); if(this.bias!=null) { resid.plusEquals(this.bias); } Matrix v = resid.clone(); resid.minusEquals(Y); double retval = 0; for (int t = 0; t < resid.getNumColumns(); t++) { double loss = resid.getElement(t, t); retval += loss * loss; logger.debug( String.format( "yr=%d,y=%3.2f,v=%3.2f,v(no bias)=%2.5f,error=%2.5f,serror=%2.5f", t, Y.getElement(t, t), v.getElement(t, t), vnobias.getElement(t,t), loss, loss*loss ) ); } return retval; }
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(); }
@Override public Matrix gradient(Matrix W) { Matrix ret = W.clone(); if(CFMatrixUtils.containsInfinity(X)){ throw new RuntimeException(); } if(CFMatrixUtils.containsInfinity(W)){ throw new RuntimeException(); } Matrix resid = CFMatrixUtils.fastdot(X,W); if(CFMatrixUtils.containsInfinity(resid)){ CFMatrixUtils.fastdot(X,W); throw new RuntimeException(); } if(this.bias!=null) { resid.plusEquals(this.bias); } CFMatrixUtils.fastminusEquals(resid, Y); if(CFMatrixUtils.containsInfinity(resid)){ throw new RuntimeException(); } for (int t = 0; t < resid.getNumColumns(); t++) { Vector xcol = this.X.getRow(t).scale(resid.getElement(t, t)).clone(); CFMatrixUtils.fastsetcol(ret,t, xcol); } return ret; } @Override
@Override public boolean checkSameDimensions( Matrix otherMatrix ) { return (this.getNumRows() == otherMatrix.getNumRows()) && (this.getNumColumns() == otherMatrix.getNumColumns()); }
public Matrix predict(double[] x) { final Matrix X = prepareMatrix(x); final Matrix hiddenTimes = weightsL1.transpose().times(X); final Matrix hiddenVal = prepareMatrix(gMat.apply(hiddenTimes).getColumn(0)); final Matrix finalTimes = weightsL2.transpose().times(hiddenVal); final Matrix finalVal = gMat.apply(finalTimes); return finalVal; }
@Override public Matrix predict(Matrix x) { final Matrix mult = this.u.transpose().times(x.transpose()).times(this.w); if(this.biasMode)mult.plusEquals(this.bias); final Vector ydiag = CFMatrixUtils.diag(mult); final Matrix createIdentity = SparseMatrixFactoryMTJ.INSTANCE.createIdentity(1, ydiag.getDimensionality()); createIdentity.setRow(0, ydiag); return createIdentity; } }
public Matrix getValue() { final int dim = this.conditionalDistribution.getInputDimensionality(); Matrix parameter = MatrixFactory.getDefault().createMatrix(dim, dim+1); parameter.setColumn(0, this.conditionalDistribution.getMean() ); parameter.setSubMatrix(0,1, this.conditionalDistribution.getCovariance() ); return parameter; }
@Override public double getAdjustedAlpha( int i, int j) { return this.adjustedAlphas.getElement(i, j); }
public boolean test_backtrack(Matrix W, Matrix grad, Matrix prox, double eta){ Matrix tmp = prox.clone(); tmp.minusEquals(W); Vector tmpvec = tmp.getColumn(0); return ( eval(prox) <= eval(W) + grad.getColumn(0).dotProduct(tmpvec) + 0.5*eta*tmpvec.norm2()); } }
@Override public Vector evaluate( final Vector input ) { return this.discriminant.times( input ); }
/** * Clones the input matrix to prevent any later edits to the input from * changing the results of iterative multiplications. * * @param m The matrix to multiply with */ public MatrixVectorMultiplier(Matrix m) { this.m = m.clone(); }
/** * Gets the i-th orthogonal term vector that makes up the basis for * the transform. * * @param i * An index. Must be between 0 (inclusive) and rank (exclusive). * @return * The i-th orthogonal term vector. */ public Vector getTermVector( final int i) { return this.termBasis.getColumn(i); }