/** * Compute the proportion of completely zero rows to rows with non-zero * values * * @param mat * the matrix * @return the row sparsity */ public static double rowSparsity(Matrix mat) { final double nrows = mat.getNumRows(); double nsparse = 0; for (int r = 0; r < nrows; r++) { if (mat.getRow(r).sum() == 0) { nsparse++; } } return nsparse / nrows; }
for (int wordIndex : sortedImportantWords.indexes) { System.out.println("Word: " + inversewords.get(wordIndex) + " index " + wordIndex); System.out.println(bilinearLearner.getW().getRow(wordIndex));
for (int wordIndex : sortedImportantWords.indexes) { System.out.println("Word: " + inversewords.get(wordIndex) + " index " + wordIndex); System.out.println(bilinearLearner.getW().getRow(wordIndex));
@Override public Matrix init(int rows, int cols) { final Matrix ret = smf.createMatrix(rows, cols); final Vector oneRow = CFMatrixUtils.plusInplace(smf.createMatrix(1, cols), 1).getRow(0); for (int i = 0; i < rows; i++) { if (this.random.nextDouble() > sparcity) { ret.setRow(i, oneRow.clone()); } } return ret; }
public Set<IndexedTermSimilarityRelation> relationsFrom( final IndexedTerm source) { final int sourceIndex = source.getIndex(); final LinkedHashSet<IndexedTermSimilarityRelation> result = new LinkedHashSet<IndexedTermSimilarityRelation>(); // Walk the rows of the matrix to get the relation. for (VectorEntry entry : this.similarities.getRow(sourceIndex)) { final double similarity = entry.getValue(); if (similarity != 0.0) { final IndexedTerm target = this.termIndex.getIndexedTerm( entry.getIndex()); result.add(new IndexedTermSimilarityRelation(source, target, similarity)); } // else - We ignore zero similarities. } return result; }
public Set<IndexedTermSimilarityRelation> relationsFrom( final IndexedTerm source) { final int sourceIndex = source.getIndex(); final LinkedHashSet<IndexedTermSimilarityRelation> result = new LinkedHashSet<IndexedTermSimilarityRelation>(); // Walk the rows of the matrix to get the relation. for (VectorEntry entry : this.similarities.getRow(sourceIndex)) { final double similarity = entry.getValue(); if (similarity != 0.0) { final IndexedTerm target = this.termIndex.getIndexedTerm( entry.getIndex()); result.add(new IndexedTermSimilarityRelation(source, target, similarity)); } // else - We ignore zero similarities. } return result; }
public Set<IndexedTermSimilarityRelation> relationsFrom( final IndexedTerm source) { final int sourceIndex = source.getIndex(); final LinkedHashSet<IndexedTermSimilarityRelation> result = new LinkedHashSet<IndexedTermSimilarityRelation>(); // Walk the rows of the matrix to get the relation. for (VectorEntry entry : this.similarities.getRow(sourceIndex)) { final double similarity = entry.getValue(); if (similarity != 0.0) { final IndexedTerm target = this.termIndex.getIndexedTerm( entry.getIndex()); result.add(new IndexedTermSimilarityRelation(source, target, similarity)); } // else - We ignore zero similarities. } return result; }
@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
/** * 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; }
/** * Compute the sparsity * * @param mat * the matrix * @return the sparsity */ public static double sparsity(Matrix mat) { double count = 0; final double size = mat.getNumRows() * mat.getNumColumns(); if (mat instanceof SparseRowMatrix) { for (int i = 0; i < mat.getNumRows(); i++) { final SparseVector row = (SparseVector) mat.getRow(i); count += row.getNumElementsUsed(); } } else if (mat instanceof SparseColumnMatrix) { for (int i = 0; i < mat.getNumColumns(); i++) { final SparseVector col = (SparseVector) mat.getColumn(i); count += col.getNumElementsUsed(); } } else { for (final MatrixEntry matrixEntry : mat) { if (matrixEntry.getValue() != 0) count++; } } return (size - count) / size; }
this.dataSize); final Vector factorRow = factors.getRow(k); for (int i = 0; i < this.dataSize; i++)
for (final VectorEntry entry : this.factors.getRow(k))
for (final VectorEntry entry : this.factors.getRow(k))
for (final VectorEntry entry : this.factors.getRow(k))
final double thisEntryValue = thisEntry.getValue(); for (final VectorEntry otherEntry : other.getRow(thisEntry.getColumnIndex()))
final double thisEntryValue = thisEntry.getValue(); for (final VectorEntry otherEntry : other.getRow(thisEntry.getColumnIndex()))
final double thisEntryValue = thisEntry.getValue(); for (final VectorEntry otherEntry : other.getRow(thisEntry.getColumnIndex()))